Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JosephMaxwell/f610edd51b958435466c to your computer and use it in GitHub Desktop.
Save JosephMaxwell/f610edd51b958435466c to your computer and use it in GitHub Desktop.
<?php
/**
* SwiftOtter_Base is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SwiftOtter_Base is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with SwiftOtter_Base. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright: 2015 (c) SwiftOtter Studios
*
* @author Jesse Maxwell
* @copyright Swift Otter Studios, 2/20/15
* @package default
**/
/** @var $installer Mage_Eav_Model_Entity_Setup */
$installer = $this;
$installer->startSetup();
/**
* Loads the blocks associated with CMS pages
* @param Mage_Eav_Model_Entity_Setup $installer
* @return array
*/
function getCmsBlocks (Mage_Eav_Model_Entity_Setup $installer) {
$outputBlocks = array();
$cmsPages = $installer->getConnection()->select();
if (Mage::helper('core')->isModuleEnabled('Clever_Cms')) {
$table = $installer->getTable('cms/page') . '_tree';
} else {
$table = $installer->getTable('cms/page');
}
$cmsPages->from($table, array('content'));
$cmsPages->where('content LIKE ?', '%{{block%');
foreach($installer->getConnection()->fetchCol($cmsPages) as $pageId => $content) {
$outputBlocks = array_merge_recursive(parseForBlocks($content), $outputBlocks);
}
return $outputBlocks;
}
/**
* Loads the blocks associated with products
*
* @param Mage_Eav_Model_Entity_Setup $installer
* @return array
*/
function getProductBlocks (Mage_Eav_Model_Entity_Setup $installer) {
$outputBlocks = array();
$productEntityType = Mage::getModel('eav/entity_type')->loadByCode(Mage_Catalog_Model_Product::ENTITY);
$productAttributes = Mage::getResourceModel('eav/entity_attribute_collection');
$productAttributes->addFieldToFilter('frontend_input', array('eq' => 'textarea'))
->addFieldToFilter('entity_type_id', array('eq' => $productEntityType->getId()));
foreach ($productAttributes as $attribute) {
$usedAttributes = $installer->getConnection()->select();
$usedAttributes->from($installer->getTable($attribute->getBackendTable()), array('value'))
->where('attribute_id = ?', $attribute->getAttributeId())
->where('value LIKE ?', "%{{block%");
foreach ($installer->getConnection()->fetchCol($usedAttributes) as $attributeContent) {
$outputBlocks = array_merge_recursive(parseForBlocks($attributeContent), $outputBlocks);
}
}
return $outputBlocks;
}
/**
* Loads the blocks already in the database
*
* @param Mage_Eav_Model_Entity_Setup $installer
* @return array
*/
function getBlocksAlreadyInDb (Mage_Eav_Model_Entity_Setup $installer) {
$select = $installer->getConnection()->select();
$select->from($installer->getTable('admin/permission_block'), 'block_name');
return $installer->getConnection()->fetchCol($select);
};
/**
* Finds the block types in the content provided
*
* @param $content
* @return array
*/
function parseForBlocks ($content){
$output = array();
preg_match_all("/{{(block|widget).*type=\"(.+)\".*}}/imU", $content, $output);
if (count($output) >= 3) {
return $output[2];
} else {
return array();
}
};
/**
* Final phase of action: adding the block types to the database
*
* @param Mage_Eav_Model_Entity_Setup $installer
* @param $blocks
*/
function addToAllowableBlocks (Mage_Eav_Model_Entity_Setup $installer, $blocks){
try {
$installer->getConnection()->beginTransaction();
$block = Mage::getModel('admin/block');
foreach ($blocks as $blockName) {
$installer->getConnection()->insert($installer->getTable('admin/permission_block'),
array(
'block_name' => $blockName,
'is_allowed' => 1
)
);
}
$installer->getConnection()->commit();
} catch (Exception $ex) {
$installer->getConnection()->rollBack();
}
};
/**
* This is where the functionality happens:
* 1) finding the blocks used by CMS pages and products
* *: loads all products' attributes that have the textarea type
* 2) merges them and ensures only unique values are there
* 3) adds these to the database.
*/
$usedBlocks = array_merge(
getProductBlocks($installer),
getCmsBlocks($installer)
);
$usedBlocks = array_unique($usedBlocks);
$existingBlocks = getBlocksAlreadyInDb($installer);
addToAllowableBlocks($installer, array_diff($usedBlocks, $existingBlocks));
$installer->endSetup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment