/**
  * Rewrite a grid block, to add it some useful/needed methods
  * 
  * @param BL_CustomGrid_Model_Grid $grid
  * @return bool
  */
 protected function _rewriteGridBlock($grid)
 {
     // Get block infos
     list($group, $class, $rewritingClassName) = $this->_getBlockTypeInfos($grid->getBlockType());
     if (!$rewritingClassName && $grid->getRewritingClassName() == '' || $rewritingClassName == $grid->getRewritingClassName()) {
         // Grid model corresponds to current configuration
         if (Mage::helper('customgrid/config')->isExcludedGrid($grid->getBlockType(), $rewritingClassName)) {
             // Do not rewrite if now excluded
             $this->_excludedModels[] = $grid->getId();
         } elseif (!isset($this->_rewritedTypes[$grid->getBlockType()])) {
             // Register our rewriting class (extending previous rewrite if existing)
             $className = 'BL_CustomGrid_Block_Rewrite_' . uc_words($class);
             $extends = $rewritingClassName ? $rewritingClassName : $this->_getBlockClassName($group, $class);
             $this->_registerGridClass($className, $extends);
             if ($rewritingClassName) {
                 $this->_originalRewrites[$grid->getBlockType()] = $rewritingClassName;
             }
             // Register rewrite in config (this will also replace previous rewrite if existing)
             // This doesnt seem to affect Magento config cache in any way
             $rewriteXml = new Varien_Simplexml_Config();
             $rewriteXml->loadString('
             <config>
                 <global>
                     <blocks>
                         <' . $group . '>
                             <rewrite>
                                 <' . $class . '>' . $className . '</' . $class . '>
                             </rewrite>
                         </' . $group . '>
                     </blocks>
                 </global>
             </config>
             ');
             $this->_getConfig()->extend($rewriteXml, true);
             // Remember current type is now rewrited
             $this->_rewritedTypes[$grid->getBlockType()] = true;
         }
         return true;
     } else {
         return false;
     }
 }
 /**
  * Rewrite a grid block, to add it some useful/needed methods
  *
  * @param BL_CustomGrid_Model_Grid $grid
  * @return bool
  */
 protected function _rewriteGridBlock($grid)
 {
     // Get block infos
     list($group, $class, $rewritingClassName) = $this->_getBlockTypeInfos($grid->getBlockType());
     if (!$rewritingClassName && $grid->getRewritingClassName() == '' || $rewritingClassName == $grid->getRewritingClassName()) {
         // Grid model corresponds to current configuration
         if ($this->_getConfigHelper()->isExcludedGrid($grid->getBlockType(), $rewritingClassName)) {
             // Do not rewrite if now excluded
             $this->_excludedModels[] = $grid->getId();
         } elseif (!isset($this->_rewritedTypes[$grid->getBlockType()])) {
             // Generate and register our rewriting class (extending previous rewrite if existing)
             $rewriters = Mage::getSingleton('customgrid/grid_rewriter')->getEnabledRewriters(true);
             $blcgClass = false;
             $originalClass = $rewritingClassName ? $rewritingClassName : $this->_getBlockClassName($group, $class);
             $rewriteErrors = array();
             foreach ($rewriters as $rewriter) {
                 try {
                     $blcgClass = $rewriter->rewriteGrid($originalClass, $grid->getBlockType());
                 } catch (Exception $e) {
                     $blcgClass = false;
                     $rewriteErrors[] = array('exception' => $e, 'rewriter' => $rewriter);
                 }
                 if ($blcgClass) {
                     break;
                 }
             }
             if ($blcgClass) {
                 foreach ($rewriteErrors as $error) {
                     if ($error['rewriter']->getDisplayErrorsIfSuccess()) {
                         $this->_getSession()->addError($error['exception']->getMessage());
                     }
                     if ($error['rewriter']->getLogErrorsIfSuccess()) {
                         Mage::logException($error['exception']);
                     }
                 }
                 if ($rewritingClassName) {
                     $this->_originalRewrites[$grid->getBlockType()] = $rewritingClassName;
                 }
                 // Register rewrite in config (this will also replace previous rewrite if existing)
                 // This doesnt seem to affect Magento config cache in any way
                 $rewriteXml = new Varien_Simplexml_Config();
                 $rewriteXml->loadString('
                 <config>
                     <global>
                         <blocks>
                             <' . $group . '>
                                 <rewrite>
                                     <' . $class . '>' . $blcgClass . '</' . $class . '>
                                 </rewrite>
                             </' . $group . '>
                         </blocks>
                     </global>
                 </config>
                 ');
                 $this->_getConfig()->extend($rewriteXml, true);
                 if ($this->_getConfigHelper()->getForceGridRewrites()) {
                     // Put the rewriting class name in the config cache (should prevent some problems when the config gets overriden afterwards)
                     $this->_getConfig()->getBlockClassName($grid->getBlockType());
                 }
                 // Remember current type is now rewrited
                 $this->_rewritedTypes[$grid->getBlockType()] = true;
             } else {
                 foreach ($rewriteErrors as $error) {
                     if ($error['rewriter']->getDisplayErrors()) {
                         $this->_getSession()->addError($error['exception']->getMessage());
                     }
                     if ($error['rewriter']->getLogErrors()) {
                         Mage::logException($error['exception']);
                     }
                 }
                 // Exclude failed rewrites
                 $this->_excludedModels[] = $grid->getId();
             }
         }
         return true;
     } else {
         return false;
     }
 }