*/ class Fishpig_Bolt_Model_Observer_Adminhtml extends Fishpig_Bolt_Model_Observer_Abstract { /** * Check if using bolt.php or index.php and redirect * * @param Varien_Event_Observer $observer * @return $this */ public function redirectToBoltObserver(Varien_Event_Observer $observer) { if (Mage::app()->getRequest()->getParam('section') !== 'bolt') { return $this; } if (basename($_SERVER['SCRIPT_FILENAME']) === 'bolt.php') { return $this; } $newUri = str_replace(basename($_SERVER['SCRIPT_FILENAME']), 'bolt.php', $_SERVER['REQUEST_URI']); header('Location: ' . $newUri); exit; } /** * After a product has been saved, clear it's fpc files * * @param Varien_Event_Observer $observer * @return $this */ public function catalogProductSaveAfterObserver(Varien_Event_Observer $observer) { if ($this->isBoltDefined()) { $product = $observer->getEvent()->getProduct(); try { Fishpig_Bolt_Cache::setCachePath( Mage::getStoreConfig('bolt/cache_file/path', 0) ); if (($urlsByStore = $this->_getAllProductUrls($product)) !== false) { Mage::helper('bolt')->refreshCache($urlsByStore); } } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); Mage::logException($e); } } return $this; } /** * Automatically clean the CMS page cache * * @param Varien_Event_Observer $observer * @return $this */ public function cmsPageSaveAfterObserver(Varien_Event_Observer $observer) { if ($this->isBoltDefined()) { $page = $observer->getEvent()->getObject(); try { Fishpig_Bolt_Cache::setCachePath( Mage::getStoreConfig('bolt/cache_file/path', 0) ); if (($urlsByStore = $this->_getAllPageUrls($page)) !== false) { Mage::helper('bolt')->refreshCache($urlsByStore); } } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); Mage::logException($e); } } return $this; } /** * Refresh the config file * * @param $observer = null * @return array|false */ public function refreshConfig($observer = null) { return Mage::helper('bolt')->refreshConfig(); } /** * Retrieve all product URL's categorised by store for a specific product * * @param Mage_Catalog_Model_Product $product * @return false|array */ protected function _getAllProductUrls(Mage_Catalog_Model_Product $product) { $resource = Mage::getSingleton('core/resource'); $db = $resource->getConnection('core_read'); $select = $db->select() ->from($resource->getTableName('core/url_rewrite'), '*') ->where('product_id=?', $product->getId()) ->where('request_path <> ?', ''); if ($results = $db->fetchAll($select)) { $urlsByStore = array(); foreach($results as $result) { if (!isset($urlsByStore[$result['store_id']])) { $urlsByStore[$result['store_id']] = array(); } $urlsByStore[$result['store_id']][] = Mage::getUrl('', array('_direct' => $result['request_path'], '_store' => $result['store_id'])); } return $urlsByStore; } return false; } /** * Retrieve all URL's for $page * * @param Mage_Cms_Model_Page $page * @return false|array */ protected function _getAllPageUrls(Mage_Cms_Model_Page $page) { if ($page->getStores()) { $urlsByStore = array(); $stores = $page->getStores(); if (count($stores) === 1 && $stores[0] == 0) { $stores = array_keys(Mage::app()->getStores()); } foreach($stores as $storeId) { $url = Mage::getUrl('', array('_direct' => $page->getIdentifier(), '_store' => $storeId)); $urlsByStore[$storeId] = array( $url, str_replace(basename($_SERVER['SCRIPT_NAME']) . '/', '', $url) ); if ($page->getIdentifier() == Mage::getStoreConfig('web/default/cms_home_page', $storeId)) { $urlsByStore[$storeId][] = rtrim(Mage::getUrl('', array('_store' => $storeId)), '/'); $urlsByStore[$storeId][] = rtrim(str_replace(basename($_SERVER['SCRIPT_NAME']), '', rtrim(Mage::getUrl('', array('_store' => $storeId)), '/')), '/'); } } return $urlsByStore; } return false; } /** * Flush the Bolt cache * * @param Varien_Event_Observer $observer * @return $this */ public function flushCacheObserver(Varien_Event_Observer $observer) { Mage::app()->getCacheInstance()->cleanType('bolt'); return $this; } }