Ejemplo n.º 1
0
 /**
  * Verify validities of filters applied to given grid block,
  * and return safely appliable filters
  * Mostly used for custom columns, which may change of renderers
  * (and those renderers may crash with unexpected kind of filter values)
  * 
  * @param Mage_Adminhtml_Block_Widget_Grid $grid Grid block
  * @param array $filters Applied filters
  * @return array
  */
 public function verifyGridBlockFilters(Mage_Adminhtml_Block_Widget_Grid $grid, $filters)
 {
     // Get previous filters informations from session
     $session = Mage::getSingleton('adminhtml/session');
     // Applied ones
     $sessionFilters = $session->getData(self::SESSION_BASE_KEY_APPLIED_FILTERS . $this->getId());
     $sessionFilters = is_array($sessionFilters) ? $sessionFilters : array();
     // Removed ones
     $removedFilters = $session->getData(self::SESSION_BASE_KEY_REMOVED_FILTERS . $this->getId());
     $removedFilters = is_array($removedFilters) ? $removedFilters : array();
     $foundIds = array();
     $newRemovedIds = array();
     $attributesRenderers = $this->getAvailableAttributesRendererTypes();
     /*
     Verify grid tokens, if request one does not correspond to session one,
     then it is almost sure that we currently come from anywhere but from an effective grid action
     (such as search, sort, export, pagination, ...)
     May be too restrictive, but at the moment, rather be too restrictive than not enough
     */
     if ($grid->getRequest()->has(self::GRID_TOKEN_PARAM_NAME) && $session->hasData(self::SESSION_BASE_KEY_TOKEN . $this->getId())) {
         $requestValue = $grid->getRequest()->getParam(self::GRID_TOKEN_PARAM_NAME, null);
         $sessionValue = $session->getData(self::SESSION_BASE_KEY_TOKEN . $this->getId());
         $isGridAction = $requestValue == $sessionValue;
     } else {
         $isGridAction = false;
     }
     $columns = $this->getColumns(false, true);
     foreach ($filters as $columnId => $data) {
         if (isset($columns[$columnId])) {
             $column = $columns[$columnId];
             if (isset($sessionFilters[$columnId])) {
                 // Previously existing/applied filter
                 $changed = false;
                 if ($sessionFilters[$columnId]['origin'] != $column['origin']) {
                     $changed = true;
                 } elseif ($this->isCollectionColumnOrigin($column['origin'])) {
                     // Check renderer types for collection columns
                     $changed = $sessionFilters[$columnId]['renderer_type'] != $column['renderer_type'];
                 } elseif ($this->isAttributeColumnOrigin($column['origin'])) {
                     // Check corrresponding attributes renderers for attribute columns
                     $oldIndex = $sessionFilters[$columnId]['index'];
                     if (isset($attributesRenderers[$oldIndex])) {
                         $changed = $attributesRenderers[$oldIndex] != $attributesRenderers[$column['index']];
                     } else {
                         $changed = true;
                     }
                 } elseif ($this->isCustomColumnOrigin($column['origin'])) {
                     /*
                     Check index (= corresponding model - although not meant to change),
                     renderer type and custom params for custom columns
                     */
                     $oldIndex = $sessionFilters[$columnId]['index'];
                     $typeModel = Mage::getSingleton('customgrid/grid_type');
                     $rendererTypes = array('old' => $sessionFilters[$columnId]['renderer_type'], 'new' => $column['renderer_type']);
                     $customParams = array('old' => $typeModel->decodeParameters($sessionFilters[$columnId]['custom_params'], true), 'new' => $typeModel->decodeParameters($column['custom_params'], true));
                     if ($oldIndex != $column['index'] || !is_object($column['custom_column']) || $column['custom_column']->shouldInvalidateFilters($this, $column, $customParams, $rendererTypes)) {
                         $changed = true;
                     }
                 }
                 if ($changed) {
                     // Column has significantly changed, unvalidate filter
                     // Remove filter from filters array, to prevent it from being applied
                     unset($filters[$columnId]);
                     // Remove filter from session, to allow new filters to later be set on this column
                     unset($sessionFilters[$columnId]);
                     // Remember which value has been unvalidated, to prevent it to be re-applied from, eg, a page refresh
                     $removedFilters[$columnId] = $data;
                     $newRemovedIds[] = $columnId;
                 }
             } elseif (isset($removedFilters[$columnId]) && !$isGridAction) {
                 // Filter on a column for which another applied filter was previously removed
                 if ($this->compareGridFilterValues($removedFilters[$columnId], $data)) {
                     // Previously removed filter had same value, unvalidate it again
                     unset($filters[$columnId]);
                 }
             } else {
                 // New filter, remember some needed informations in session
                 $sessionFilters[$columnId] = array('index' => $column['index'], 'origin' => $column['origin'], 'renderer_type' => $column['renderer_type'], 'custom_params' => $column['custom_params']);
             }
             $foundIds[] = $columnId;
         } else {
             // Unexisting column : unneeded filter
             unset($filters[$columnId]);
         }
     }
     /**
      * Note : adding new parameters to grid request 
      * will make them be added to, eg, URLs got from next retrievals of current URL
      */
     /*
     Add our token to current request and session
     Use ":" in hash to force Varien_Db_Adapter_Pdo_Mysql::query() using a bind param instead of full request path,
     (as it uses this condition : strpos($sql, ':') !== false),
     when querying core_url_rewrite table, else the query could be too long, 
     making Zend_Db_Statement::_stripQuoted() sometimes crash on one of its call to preg_replace()
     */
     $tokenValue = Mage::helper('core')->uniqHash('blcg:');
     $grid->getRequest()->setParam(self::GRID_TOKEN_PARAM_NAME, $tokenValue);
     $session->setData(self::SESSION_BASE_KEY_TOKEN . $this->getId(), $tokenValue);
     // Remove obsolete filters and save up-to-date filters array to session
     $obsoleteIds = array_diff(array_keys($sessionFilters), $foundIds);
     foreach ($obsoleteIds as $columnId) {
         unset($sessionFilters[$columnId]);
     }
     $session->setData(self::SESSION_BASE_KEY_APPLIED_FILTERS . $this->getId(), $sessionFilters);
     /*
     Remove removed filters once a grid action is done
     The only remaining potential source of "maybe wrong" filters could come from 
     the use of an old URL with obsolete filter(s) in it (eg from browser history),
     but there is no way at the moment to detect them
     (at least I didnt find a simple one with few impacts)
     */
     if ($isGridAction) {
         $session->setData(self::SESSION_BASE_KEY_REMOVED_FILTERS . $this->getId(), array_intersect_key($removedFilters, $newRemovedIds));
     } else {
         $session->setData(self::SESSION_BASE_KEY_REMOVED_FILTERS . $this->getId(), $removedFilters);
     }
     $filterParam = $this->encodeGridFiltersArray($filters);
     if ($grid->blcg_getSaveParametersInSession()) {
         $session->setData($grid->blcg_getSessionParamKey($grid->getVarNameFilter()), $filterParam);
     }
     if ($grid->getRequest()->has($grid->getVarNameFilter())) {
         $grid->getRequest()->setParam($grid->getVarNameFilter(), $filterParam);
     }
     $grid->blcg_setFilterParam($filterParam);
     return $filters;
 }