_orderField = $this->getDefaultOrderField(); $this->_availableOrder = $this->getAttributeUsedForSortByArray(); parent::_construct(); } /** * @return $this */ public function disableParamsMemorizing() { $this->_paramsMemorizeAllowed = false; return $this; } /** * @param string $param * @param string $value * @return $this */ protected function _memorizeParam($param, $value) { $session = $this->getSessionModel(); if ($this->_paramsMemorizeAllowed && !$session->getParamsMemorizeDisabled()) { $session->setData($param, $value); } return $this; } /** * @param $collection * @return $this */ public function setCollection(Mage_Catalog_Model_Resource_Product_Collection $collection) { if ($this->getCurrentOrder()) { $collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection()); } parent::setCollection($collection); return $this; } /** * @return string */ public function getOrderVarName() { return $this->_orderVarName; } /** * @return string */ public function getDirectionVarName() { return $this->_directionVarName; } /** * @return null */ public function getCurrentOrder() { $order = $this->_getData('_current_grid_order'); if ($order) { return $order; } $orders = $this->getAvailableOrders(); $defaultOrder = $this->_orderField; if (!isset($orders[$defaultOrder])) { $keys = array_keys($orders); $defaultOrder = $keys[0]; } $order = $this->getRequest()->getParam($this->getOrderVarName()); if ($order && isset($orders[$order])) { if ($order == $defaultOrder) { $this->getSessionModel()->unsSortOrder(); } else { $this->_memorizeParam('sort_order', $order); } } else { $order = $this->getSessionModel()->getSortOrder(); } // validate session value if (!$order || !isset($orders[$order])) { $order = $defaultOrder; } $this->setData('_current_grid_order', $order); return $order; } /** * @return string */ public function getCurrentDirection() { $dir = $this->_getData('_current_grid_direction'); if ($dir) { return $dir; } $directions = array('asc', 'desc'); $dir = strtolower($this->getRequest()->getParam($this->getDirectionVarName())); if ($dir && in_array($dir, $directions)) { if ($dir == $this->_direction) { $this->getSessionModel()->unsSortDirection(); } else { $this->_memorizeParam('sort_direction', $dir); } } else { $dir = $this->getSessionModel()->getSortDirection(); } // validate direction if (!$dir || !in_array($dir, $directions)) { $dir = $this->_direction; } $this->setData('_current_grid_direction', $dir); return $dir; } /** * @param $field * @return $this */ public function setDefaultOrder($field) { if (isset($this->_availableOrder[$field])) { $this->_orderField = $field; } return $this; } /** * @param $dir * @return $this */ public function setDefaultDirection($dir) { if (in_array(strtolower($dir), ['asc', 'desc'])) { $this->_direction = strtolower($dir); } return $this; } /** * @return array */ public function getAvailableOrders() { return $this->_availableOrder; } /** * @param $orders * @return $this */ public function setAvailableOrders($orders) { $this->_availableOrder = $orders; return $this; } /** * @param $order * @param $value * @return $this */ public function addOrderToAvailableOrders($order, $value) { $this->_availableOrder[$order] = $value; return $this; } /** * @param $order * @return $this */ public function removeOrderFromAvailableOrders($order) { if (isset($this->_availableOrder[$order])) { unset($this->_availableOrder[$order]); } return $this; } /** * @param $order * @return bool */ public function isOrderCurrent($order) { return ($order === $this->getCurrentOrder()); } /** * @param $order * @return mixed */ public function getSortUrl($order) { $sortDirection = $this->_direction; if ($this->isOrderCurrent($order)) { $sortDirection = $this->oppositeDirectionOf($this->getCurrentDirection()); } return $this->getOrderUrl($order, $sortDirection); } /** * @param $order * @return string */ private function oppositeDirectionOf($order) { return ($order === 'asc') ? 'desc' : 'asc'; } /** * @param $order * @param $direction * @return mixed */ public function getOrderUrl($order, $direction) { if ($order === null) { $order = $this->getCurrentOrder() ? $this->getCurrentOrder() : $this->_availableOrder[0]; } return $this->getPagerUrl(array( $this->getOrderVarName()=>$order, $this->getDirectionVarName()=>$direction, )); } /** * @param array $params * @return mixed */ public function getPagerUrl($params = []) { $urlParams = []; $urlParams['_current'] = true; $urlParams['_escape'] = true; $urlParams['_use_rewrite'] = true; $urlParams['_query'] = $params; return $this->getUrl('*/*/*', $urlParams); } }