Skip to content

Instantly share code, notes, and snippets.

@gplanchat
Forked from real34/SortableTrait.php
Last active January 26, 2016 17:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gplanchat/8bca0b8699fe408642f0 to your computer and use it in GitHub Desktop.
Save gplanchat/8bca0b8699fe408642f0 to your computer and use it in GitHub Desktop.
Allow to implement sorting in a Magento frontend Block easily.
<?php
trait Foo_Catalog_Block_List_SortableTrait
{
/**
* @var string
*/
protected $_orderVarName = 'order';
/**
* @var string
*/
protected $_directionVarName = 'dir';
/**
* @var string[]
*/
protected $_availableOrder = [];
/**
* @var string|null
*/
protected $_orderField = null;
/**
* @var string
*/
protected $_direction = 'asc';
/**
* @var bool
*/
protected $_paramsMemorizeAllowed = true;
/**
* @return Mage_Catalog_Model_Session
*/
abstract protected function getSessionModel();
/**
* @return string
*/
abstract protected function getDefaultOrderField();
/**
* @return mixed
*/
abstract protected function getAttributeUsedForSortByArray();
/**
* @param string $key
* @return array
*/
abstract protected function _getData($key);
/**
* @return Mage_Core_Controller_Request_Http
*/
abstract public function getRequest();
/**
* @param string $key
* @param mixed $value
* @return $this
*/
abstract public function setData($key, $value);
/**
* @param string $route
* @param array $params
* @return mixed
*/
abstract public function getUrl($route = '', $params = []); // signature of \Mage_Core_Block_Abstract::getUrl()
/**
*
*/
protected function _construct()
{
$this->_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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment