diff --git a/app/code/community/Rkt/JsCssforSb/Block/Jscss.php b/app/code/community/Rkt/JsCssforSb/Block/Jscss.php index ac62361..ce1a274 100644 --- a/app/code/community/Rkt/JsCssforSb/Block/Jscss.php +++ b/app/code/community/Rkt/JsCssforSb/Block/Jscss.php @@ -26,17 +26,82 @@ class Rkt_JsCssforSb_Block_Jscss extends Mage_Core_Block_Template { /** - * - * Retrieve jscss entity - * - * @param int | $id - * @return Rkt_JsCssforSb_Model_JsCss - * - */ - public function getJscssEntity($id) + * Hold jscss entities which are related to static blocks which are + * involved in the current request. + * + * @var array + */ + protected $_jscss = array(); + + /** + * Initialize template + * + */ + protected function _construct() + { + $this->setTemplate('rkt_jscssforsb/jscss.phtml'); + } + + /** + * Use to set jscss entities + * + * @param array $ids + */ + public function setActiveJsCss($ids) + { + + foreach ($ids as $id) { + $model = Mage::getModel('rkt_jscssforsb/jsCss'); + $this->_jscss[] = $model->load((int)$id); + unset($model); + } + + return $this; + } + + /** + * to get jscss enities + * @return array + */ + public function getActiveJsCss() { + return $this->_jscss; + } + + /** + * Use to get all css related to static blocks + * + * @return array $css an array of stdClass object. + */ + public function getAllStyles() + { + $css = array(); + foreach ($this->_jscss as $entity) { + $cssObject = new \stdClass(); + $cssObject->type = 'text/css'; + $cssObject->content = trim($entity->getJscssCss()); + $css[] = $cssObject; + } - return Mage::getModel('rkt_jscssforsb/jsCss')->load($id); + return $css; } + /** + * Use to get all js related to static blocks + * + * @return array $js an array of stdClass object. + */ + public function getAllScripts() + { + $js = array(); $jscontent = array(); + foreach ($this->_jscss as $entity) { + $jsObject = new \stdClass(); + $jsObject->type = 'text/javascript'; + $jsObject->content = trim($entity->getJscssJs()); + $js[] = $jsObject; + $jscontent[] = $entity->getJscssJs(); + } + + return $js; + } } \ No newline at end of file diff --git a/app/code/community/Rkt/JsCssforSb/Helper/Data.php b/app/code/community/Rkt/JsCssforSb/Helper/Data.php index 40fef8f..0a68bf7 100644 --- a/app/code/community/Rkt/JsCssforSb/Helper/Data.php +++ b/app/code/community/Rkt/JsCssforSb/Helper/Data.php @@ -26,20 +26,17 @@ class Rkt_JsCssforSb_Helper_Data extends Mage_Core_Helper_Abstract { /** - * - * Use to trim unwanted spaces, new lines from passed data - * - * @param string $data - * @return string - * - */ - public function modifyData($data) + * Use to check whether the passed variable is a static block instance + * + * @param mixed $block + * @return boolean + */ + public function isStaticBlock($block) { - if ($data != '') { - $trimed_data = str_replace(array(" ", "\n", "\t", "'"), array("", "", "", '"'), trim($data)); - return preg_replace('/\s+/', '', $trimed_data); + if ($block instanceof Mage_Cms_Block_Block) { + return true; + } else { + return false; } - - return ''; } } diff --git a/app/code/community/Rkt/JsCssforSb/Helper/Page.php b/app/code/community/Rkt/JsCssforSb/Helper/Page.php new file mode 100644 index 0000000..e97d3e7 --- /dev/null +++ b/app/code/community/Rkt/JsCssforSb/Helper/Page.php @@ -0,0 +1,50 @@ +getPageTemplateProcessor(); + $blocks = $processor->filter($cmsContent); + return $blocks; + } +} diff --git a/app/code/community/Rkt/JsCssforSb/Model/JsCss.php b/app/code/community/Rkt/JsCssforSb/Model/JsCss.php index acc5387..97594b5 100644 --- a/app/code/community/Rkt/JsCssforSb/Model/JsCss.php +++ b/app/code/community/Rkt/JsCssforSb/Model/JsCss.php @@ -25,9 +25,24 @@ class Rkt_JsCssforSb_Model_JsCss extends Mage_Core_Model_Abstract { + /** + * constructor + * + * @return void + */ protected function _construct() { $this->_init('rkt_jscssforsb/jsCss'); } + /** + * Use to get jscss entry based on static block id + * + * @param int $id + * @return Rkt_JsCssforSb_Model_JsCss + */ + public function getJsCssByStaticBlockId($id) + { + return $this->load($id, 'block_id'); + } } \ No newline at end of file diff --git a/app/code/community/Rkt/JsCssforSb/Model/Observer.php b/app/code/community/Rkt/JsCssforSb/Model/Observer.php index 7ff4f2c..91f2f9d 100644 --- a/app/code/community/Rkt/JsCssforSb/Model/Observer.php +++ b/app/code/community/Rkt/JsCssforSb/Model/Observer.php @@ -27,12 +27,12 @@ class Rkt_JsCssforSb_Model_Observer { /** - * - * Use to set jsscss fieldsets in cms_block - * - * @param Varien_Event_Observer | $observer - * - */ + * + * Use to set jsscss fieldsets in cms_block + * + * @param Varien_Event_Observer | $observer + * @return Rkt_JsCssforSb_Model_Observer + */ public function addNewFieldsetForCmsBlock(Varien_Event_Observer $observer) { $block = $observer->getEvent()->getBlock(); @@ -42,11 +42,12 @@ public function addNewFieldsetForCmsBlock(Varien_Event_Observer $observer) //get cms_block form $form = $block->getForm(); + $jscssModel = Mage::getModel('rkt_jscssforsb/jsCss'); //get jscss values $js_value = ''; $css_value = ''; $block_id = (int) Mage::registry('cms_block')->getBlockId(); - $cms_block = $this->getJsCssEntity($block_id); + $cms_block = $jscssModel->getJsCssByStaticBlockId($block_id); //get values if entity exist if ($cms_block) { @@ -81,12 +82,12 @@ public function addNewFieldsetForCmsBlock(Varien_Event_Observer $observer) } /** - * - * Use to save js and css for cms_block - * - * @param Varien_Event_Observer | $observer - * - */ + * + * Use to save js and css for cms_block + * + * @param Varien_Event_Observer | $observer + * @return Rkt_JsCssforSb_Model_Observer + */ public function saveJsCss(Varien_Event_Observer $observer) { @@ -95,8 +96,8 @@ public function saveJsCss(Varien_Event_Observer $observer) //retrieve essential datas to store $block_id = (int) $cms_block->getBlockId(); - $js = Mage::helper('rkt_jscssforsb')->modifyData($cms_block->getJscssJs()); - $css = Mage::helper('rkt_jscssforsb')->modifyData($cms_block->getJscssCss()); + $js = $cms_block->getJscssJs(); + $css = $cms_block->getJscssCss(); if ($js != '' || $css != '') { @@ -111,94 +112,96 @@ public function saveJsCss(Varien_Event_Observer $observer) $model = Mage::getModel('rkt_jscssforsb/jsCss'); //saves data if cms block is new - if (!$this->getJsCssEntity($block_id)) { + if (!$model->getJsCssByStaticBlockId($block_id)) { $model->addData($data); $model->save(); } else { //saves data if entry already exist - $exist_block = $this->getJsCssEntity($block_id); + $exist_block = $model->getJsCssByStaticBlockId($block_id); $exist_block->addData($data); $exist_block->save(); } } - + return $this; } /** - * - * Apply css and js to static blocks - * - * @param Varien_Event_Observer | $observer - * - */ - public function applyJsCssToCMSBlocks(Varien_Event_Observer $observer) { - - //set default values to variables + * + * Apply css and js to static blocks + * + * @param Varien_Event_Observer | $observer + * @return Rkt_JsCssforSb_Model_Observer + */ + public function applyJsCssToCMSBlocks(Varien_Event_Observer $observer) + { $flag = 0; $jscss_ids = array(); + $pageHelper = Mage::helper('rkt_jscssforsb/page'); + $jscssHelper = Mage::helper('rkt_jscssforsb'); $layout = $observer->getEvent()->getLayout(); foreach ($layout->getAllBlocks() as $block) { - - if ($block instanceof Mage_Cms_Block_Block) { + + //look static blocks in layouts. If static blocks are + //there, then collect corresponding js css entry. + if ($jscssHelper->isStaticBlock($block)) { + $flag = 1; + $jscss = $this->_getJsCss($block); + if ((int)$jscss->getJscssId() > 0) { + $jscss_ids[] = (int)$jscss->getJscssId(); + } + } + + //There may be static blocks that are included via CMS Page + //content section as block directive or layout directive. + //In that case, find them and collect the js css entry. + if ($block instanceof Mage_Cms_Block_Page) { $flag = 1; - //get cms block id - $block_identifier = $block->getBlockId(); - $block_id = (int)Mage::getModel('cms/block')->getCollection() - ->addFieldToSelect('block_id') - ->addFieldToFilter('identifier', array('eq' => $block_identifier)) - ->load() - ->getFirstItem() - ->getBlockId(); - - - //check for any entry that is correspond for cms block - if ($cms_block = $this->getJsCssEntity($block_id)) { - //store jscss ids - $jscss_ids[] = (int)$cms_block->getJscssId(); - } - + $page = $block->getPage(); + $content = $page->getContent(); + + $cmsStaticBlocks = $pageHelper->findStaticBlocks($content); + foreach ($cmsStaticBlocks as $sb) { + if ($jscssHelper->isStaticBlock($sb)) { + $jscss = $this->_getJsCss($sb); + if ((int)$jscss->getJscssId() > 0) { + $jscss_ids[] = (int)$jscss->getJscssId(); + } + } + } } } - if ($flag == 1) { + //if jscss entity exist for any of the static block that is included + //in the requested page, then include those js and css files into the + //layout. + if ($flag == 1 && count($jscss_ids) > 0) { + $jscssBlock = $layout->createBlock('rkt_jscssforsb/jscss', 'jscss_block'); + $jscssBlock->setActiveJsCss($jscss_ids); + $layout->getBlock('head')->append($jscssBlock); + } - //create a custom block to insert js and css correspond to cms block - $new_block = $layout->createBlock( - 'Rkt_JsCssforSb_Block_Jscss', 'jscss_block', - array( - 'template' => 'rkt_jscssforsb/jscss.phtml', - 'jscss_ids' => Mage::helper('rkt_jscssforsb')->__(implode(",", $jscss_ids)), - ) - ); - $layout->getBlock('content')->append($new_block); - } + return $this; } /** - * - * Use to get jscss entity correspond to cms > block that is editing currently - * - * @param int | $block_id - * @return boolean or Rkt_JsCssforSb_Model_JsCss | false or $item - * - */ - public function getJsCssEntity($block_id) + * Use to get jscss entity corresponding to the static block passed + * + * @param Mage_Cms_Block_Block $block + * @return Rkt_JsCssforSb_Model_JsCss $jscss + */ + protected function _getJsCss(Mage_Cms_Block_Block $block) { + $CMSBlockModel = Mage::getModel('cms/block'); + $jscssModel = Mage::getModel('rkt_jscssforsb/jsCss'); + + /** @var $sb->getBlockId() returns static block identifier */ + $staticBlock = $CMSBlockModel->load($block->getBlockId(), 'identifier'); + $id = (int)$staticBlock->getBlockId(); + $jscss = $jscssModel->getJsCssByStaticBlockId($id); - //loads collection - $collection = Mage::getModel('rkt_jscssforsb/jsCss')->getCollection() - ->addFieldToSelect('*') - ->addFieldToFilter('block_id', array('eq' => $block_id)) - ->load(); - - //ensure an item exist - if (count($collection->getFirstItem()->getData())) { - return $collection->getFirstItem(); - } - - return false; + return $jscss; } } \ No newline at end of file diff --git a/app/code/community/Rkt/JsCssforSb/Model/Template/Filter.php b/app/code/community/Rkt/JsCssforSb/Model/Template/Filter.php new file mode 100644 index 0000000..64c1863 --- /dev/null +++ b/app/code/community/Rkt/JsCssforSb/Model/Template/Filter.php @@ -0,0 +1,185 @@ + $construction) { + /** + * @var $construction = array[3] + * $construction[0] = (string) {{block id="test-sb"}} + * $construction[1] = (string) block + * $construction[2] = (string) id="test-sb" + */ + $filterDirective = $construction[1] . 'Directive'; + $callback = array($this, $filterDirective); + if (!is_callable($callback) + || !$this->isAllowedFilterDirective($filterDirective) + ) { + continue; + } + try { + $callbackOutput = call_user_func($callback, $construction); + } catch (Exception $e) { + throw $e; + } + + if (count($blocks) == 0) { + $blocks = $this->_filterStaticBlocks($callbackOutput); + $staticblocks = $blocks; + } else { + $staticblocks = array_merge( + $blocks, + $this->_filterStaticBlocks($callbackOutput) + ); + } + + } + } + return $staticblocks; + } + + /** + * Retrieve Block html directive + * + * @param array $construction + * @return string + */ + public function blockDirective($construction) + { + $block = ''; + $skipParams = array('type', 'id', 'output'); + $blockParameters = $this->_getIncludeParameters($construction[2]); + $layout = Mage::app()->getLayout(); + + if (isset($blockParameters['type'])) { + $type = $blockParameters['type']; + if ($type == 'cms/block') { + $block = $layout->createBlock($type, null, $blockParameters); + } + + } elseif (isset($blockParameters['id'])) { + $block = $layout->createBlock('cms/block'); + if ($block) { + $block->setBlockId($blockParameters['id']); + } + } + + return $block; + } + + /** + * Retrieve layout html directive + * + * @param array $construction + * @return array + */ + public function layoutDirective($construction) + { + $staticblocks = array(); + $skipParams = array('handle', 'area'); + + $params = $this->_getIncludeParameters($construction[2]); + $layout = Mage::getModel('core/layout'); + /* @var $layout Mage_Core_Model_Layout */ + if (isset($params['area'])) { + $layout->setArea($params['area']); + } + else { + $layout->setArea(Mage::app()->getLayout()->getArea()); + } + + $layout->getUpdate()->addHandle($params['handle']); + $layout->getUpdate()->load(); + + $layout->generateXml(); + $layout->generateBlocks(); + + foreach ($layout->getAllBlocks() as $blockName => $block) { + /* @var $block Mage_Core_Block_Abstract */ + if (Mage::helper('rkt_jscssforsb')->isStaticBlock($block)) { + $staticblocks[] = $block; + } + + } + + return $staticblocks; + } + + /** + * check whether passed directive filter is allowed + * + * @param string $directive + * @return boolean + */ + public function isAllowedFilterDirective($directive) + { + $condition = in_array($directive, $this->_allowedFilterDirectives); + if ($condition) { + return true; + } else { + return false; + } + } + + /** + * Use to filter out items which are instance of cms block + * + * @param mixed $callbackOutput + * @return array $staticblocks + */ + protected function _filterStaticBlocks($callbackOutput) + { $staticblocks = array(); + $jscssHelper = Mage::helper('rkt_jscssforsb'); + if ($jscssHelper->isStaticBlock($callbackOutput)) { + $staticblocks[$callbackOutput->getBlockId()] = $callbackOutput; + } elseif (is_array($callbackOutput)) { + foreach ($callbackOutput as $block) { + if ($jscssHelper->isStaticBlock($block)) { + $staticblocks[$block->getBlockId()] = $block; + } + } + } + + return $staticblocks; + } + +} diff --git a/app/design/frontend/base/default/template/rkt_jscssforsb/jscss.phtml b/app/design/frontend/base/default/template/rkt_jscssforsb/jscss.phtml index a298543..3b5469b 100644 --- a/app/design/frontend/base/default/template/rkt_jscssforsb/jscss.phtml +++ b/app/design/frontend/base/default/template/rkt_jscssforsb/jscss.phtml @@ -25,38 +25,12 @@ * @see Rkt_JsCssforSb_Block_Jscss */ ?> -getJscssIds()); - - foreach ($ids as $key => $value) { - - //get current entity - $jscss = $this->getJscssEntity($value); - - //retrieves required data - $js = Mage::helper('rkt_jscssforsb')->modifyData($jscss->getJscssJs()); - $css = Mage::helper('rkt_jscssforsb')->modifyData($jscss->getJscssCss()); - $block_id = (int)$jscss->getBlockId(); -?> - - - +getAllStyles() as $style) : ?> + + - - - \ No newline at end of file +getAllScripts() as $script) : ?> + + \ No newline at end of file