Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Toolbar.php
Go to the documentation of this file.
1 <?php
7 
10 
21 {
27  protected $_collection = null;
28 
34  protected $_availableOrder = null;
35 
41  protected $_availableMode = [];
42 
48  protected $_enableViewSwitcher = true;
49 
55  protected $_isExpanded = true;
56 
62  protected $_orderField = null;
63 
70 
76  protected $_viewMode = null;
77 
81  protected $_paramsMemorizeAllowed = true;
82 
86  protected $_template = 'Magento_Catalog::product/list/toolbar.phtml';
87 
93  protected $_catalogConfig;
94 
100  protected $_catalogSession;
101 
105  protected $_toolbarModel;
106 
111 
115  protected $urlEncoder;
116 
120  protected $_postDataHelper;
121 
132  public function __construct(
133  \Magento\Framework\View\Element\Template\Context $context,
134  \Magento\Catalog\Model\Session $catalogSession,
135  \Magento\Catalog\Model\Config $catalogConfig,
136  ToolbarModel $toolbarModel,
137  \Magento\Framework\Url\EncoderInterface $urlEncoder,
138  ProductList $productListHelper,
139  \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
140  array $data = []
141  ) {
142  $this->_catalogSession = $catalogSession;
143  $this->_catalogConfig = $catalogConfig;
144  $this->_toolbarModel = $toolbarModel;
145  $this->urlEncoder = $urlEncoder;
146  $this->_productListHelper = $productListHelper;
147  $this->_postDataHelper = $postDataHelper;
148  parent::__construct($context, $data);
149  }
150 
156  public function disableParamsMemorizing()
157  {
158  $this->_paramsMemorizeAllowed = false;
159  return $this;
160  }
161 
169  protected function _memorizeParam($param, $value)
170  {
171  if ($this->_paramsMemorizeAllowed && !$this->_catalogSession->getParamsMemorizeDisabled()) {
172  $this->_catalogSession->setData($param, $value);
173  }
174  return $this;
175  }
176 
183  public function setCollection($collection)
184  {
185  $this->_collection = $collection;
186 
187  $this->_collection->setCurPage($this->getCurrentPage());
188 
189  // we need to set pagination only if passed value integer and more that 0
190  $limit = (int)$this->getLimit();
191  if ($limit) {
192  $this->_collection->setPageSize($limit);
193  }
194  if ($this->getCurrentOrder()) {
195  if (($this->getCurrentOrder()) == 'position') {
196  $this->_collection->addAttributeToSort(
197  $this->getCurrentOrder(),
198  $this->getCurrentDirection()
199  );
200  } else {
201  $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
202  }
203  }
204  return $this;
205  }
206 
212  public function getCollection()
213  {
214  return $this->_collection;
215  }
216 
222  public function getCurrentPage()
223  {
224  return $this->_toolbarModel->getCurrentPage();
225  }
226 
232  public function getCurrentOrder()
233  {
234  $order = $this->_getData('_current_grid_order');
235  if ($order) {
236  return $order;
237  }
238 
239  $orders = $this->getAvailableOrders();
240  $defaultOrder = $this->getOrderField();
241 
242  if (!isset($orders[$defaultOrder])) {
243  $keys = array_keys($orders);
244  $defaultOrder = $keys[0];
245  }
246 
247  $order = $this->_toolbarModel->getOrder();
248  if (!$order || !isset($orders[$order])) {
249  $order = $defaultOrder;
250  }
251 
252  if ($order != $defaultOrder) {
253  $this->_memorizeParam('sort_order', $order);
254  }
255 
256  $this->setData('_current_grid_order', $order);
257  return $order;
258  }
259 
265  public function getCurrentDirection()
266  {
267  $dir = $this->_getData('_current_grid_direction');
268  if ($dir) {
269  return $dir;
270  }
271 
272  $directions = ['asc', 'desc'];
273  $dir = strtolower($this->_toolbarModel->getDirection());
274  if (!$dir || !in_array($dir, $directions)) {
275  $dir = $this->_direction;
276  }
277 
278  if ($dir != $this->_direction) {
279  $this->_memorizeParam('sort_direction', $dir);
280  }
281 
282  $this->setData('_current_grid_direction', $dir);
283  return $dir;
284  }
285 
292  public function setDefaultOrder($field)
293  {
294  $this->loadAvailableOrders();
295  if (isset($this->_availableOrder[$field])) {
296  $this->_orderField = $field;
297  }
298  return $this;
299  }
300 
307  public function setDefaultDirection($dir)
308  {
309  if (in_array(strtolower($dir), ['asc', 'desc'])) {
310  $this->_direction = strtolower($dir);
311  }
312  return $this;
313  }
314 
320  public function getAvailableOrders()
321  {
322  $this->loadAvailableOrders();
323  return $this->_availableOrder;
324  }
325 
332  public function setAvailableOrders($orders)
333  {
334  $this->_availableOrder = $orders;
335  return $this;
336  }
337 
346  {
347  $this->loadAvailableOrders();
348  $this->_availableOrder[$order] = $value;
349  return $this;
350  }
351 
359  {
360  $this->loadAvailableOrders();
361  if (isset($this->_availableOrder[$order])) {
362  unset($this->_availableOrder[$order]);
363  }
364  return $this;
365  }
366 
373  public function isOrderCurrent($order)
374  {
375  return $order == $this->getCurrentOrder();
376  }
377 
384  public function getPagerUrl($params = [])
385  {
386  $urlParams = [];
387  $urlParams['_current'] = true;
388  $urlParams['_escape'] = false;
389  $urlParams['_use_rewrite'] = true;
390  $urlParams['_query'] = $params;
391  return $this->getUrl('*/*/*', $urlParams);
392  }
393 
398  public function getPagerEncodedUrl($params = [])
399  {
400  return $this->urlEncoder->encode($this->getPagerUrl($params));
401  }
402 
408  public function getCurrentMode()
409  {
410  $mode = $this->_getData('_current_grid_mode');
411  if ($mode) {
412  return $mode;
413  }
414  $defaultMode = $this->_productListHelper->getDefaultViewMode($this->getModes());
415  $mode = $this->_toolbarModel->getMode();
416  if (!$mode || !isset($this->_availableMode[$mode])) {
417  $mode = $defaultMode;
418  }
419 
420  $this->setData('_current_grid_mode', $mode);
421  return $mode;
422  }
423 
430  public function isModeActive($mode)
431  {
432  return $this->getCurrentMode() == $mode;
433  }
434 
440  public function getModes()
441  {
442  if ($this->_availableMode === []) {
443  $this->_availableMode = $this->_productListHelper->getAvailableViewMode();
444  }
445  return $this->_availableMode;
446  }
447 
454  public function setModes($modes)
455  {
456  $this->getModes();
457  if (!isset($this->_availableMode)) {
458  $this->_availableMode = $modes;
459  }
460  return $this;
461  }
462 
468  public function disableViewSwitcher()
469  {
470  $this->_enableViewSwitcher = false;
471  return $this;
472  }
473 
479  public function enableViewSwitcher()
480  {
481  $this->_enableViewSwitcher = true;
482  return $this;
483  }
484 
490  public function isEnabledViewSwitcher()
491  {
493  }
494 
500  public function disableExpanded()
501  {
502  $this->_isExpanded = false;
503  return $this;
504  }
505 
511  public function enableExpanded()
512  {
513  $this->_isExpanded = true;
514  return $this;
515  }
516 
522  public function isExpanded()
523  {
524  return $this->_isExpanded;
525  }
526 
532  public function getDefaultPerPageValue()
533  {
534  if ($this->getCurrentMode() == 'list' && ($default = $this->getDefaultListPerPage())) {
535  return $default;
536  } elseif ($this->getCurrentMode() == 'grid' && ($default = $this->getDefaultGridPerPage())) {
537  return $default;
538  }
539  return $this->_productListHelper->getDefaultLimitPerPageValue($this->getCurrentMode());
540  }
541 
547  public function getAvailableLimit()
548  {
549  return $this->_productListHelper->getAvailableLimit($this->getCurrentMode());
550  }
551 
557  public function getLimit()
558  {
559  $limit = $this->_getData('_current_limit');
560  if ($limit) {
561  return $limit;
562  }
563 
564  $limits = $this->getAvailableLimit();
565  $defaultLimit = $this->getDefaultPerPageValue();
566  if (!$defaultLimit || !isset($limits[$defaultLimit])) {
567  $keys = array_keys($limits);
568  $defaultLimit = $keys[0];
569  }
570 
571  $limit = $this->_toolbarModel->getLimit();
572  if (!$limit || !isset($limits[$limit])) {
573  $limit = $defaultLimit;
574  }
575 
576  if ($limit != $defaultLimit) {
577  $this->_memorizeParam('limit_page', $limit);
578  }
579 
580  $this->setData('_current_limit', $limit);
581  return $limit;
582  }
583 
588  public function isLimitCurrent($limit)
589  {
590  return $limit == $this->getLimit();
591  }
592 
596  public function getFirstNum()
597  {
598  $collection = $this->getCollection();
599  return $collection->getPageSize() * ($collection->getCurPage() - 1) + 1;
600  }
601 
605  public function getLastNum()
606  {
607  $collection = $this->getCollection();
608  return $collection->getPageSize() * ($collection->getCurPage() - 1) + $collection->count();
609  }
610 
614  public function getTotalNum()
615  {
616  return $this->getCollection()->getSize();
617  }
618 
622  public function isFirstPage()
623  {
624  return $this->getCollection()->getCurPage() == 1;
625  }
626 
630  public function getLastPageNum()
631  {
632  return $this->getCollection()->getLastPageNumber();
633  }
634 
640  public function getPagerHtml()
641  {
642  $pagerBlock = $this->getChildBlock('product_list_toolbar_pager');
643 
644  if ($pagerBlock instanceof \Magento\Framework\DataObject) {
645  /* @var $pagerBlock \Magento\Theme\Block\Html\Pager */
646  $pagerBlock->setAvailableLimit($this->getAvailableLimit());
647 
648  $pagerBlock->setUseContainer(
649  false
650  )->setShowPerPage(
651  false
652  )->setShowAmounts(
653  false
654  )->setFrameLength(
655  $this->_scopeConfig->getValue(
656  'design/pagination/pagination_frame',
658  )
659  )->setJump(
660  $this->_scopeConfig->getValue(
661  'design/pagination/pagination_frame_skip',
663  )
664  )->setLimit(
665  $this->getLimit()
666  )->setCollection(
667  $this->getCollection()
668  );
669 
670  return $pagerBlock->toHtml();
671  }
672 
673  return '';
674  }
675 
682  public function getWidgetOptionsJson(array $customOptions = [])
683  {
684  $defaultMode = $this->_productListHelper->getDefaultViewMode($this->getModes());
685  $options = [
686  'mode' => ToolbarModel::MODE_PARAM_NAME,
687  'direction' => ToolbarModel::DIRECTION_PARAM_NAME,
688  'order' => ToolbarModel::ORDER_PARAM_NAME,
689  'limit' => ToolbarModel::LIMIT_PARAM_NAME,
690  'modeDefault' => $defaultMode,
691  'directionDefault' => $this->_direction ?: ProductList::DEFAULT_SORT_DIRECTION,
692  'orderDefault' => $this->getOrderField(),
693  'limitDefault' => $this->_productListHelper->getDefaultLimitPerPageValue($defaultMode),
694  'url' => $this->getPagerUrl(),
695  ];
696  $options = array_replace_recursive($options, $customOptions);
697  return json_encode(['productListToolbarForm' => $options]);
698  }
699 
705  protected function getOrderField()
706  {
707  if ($this->_orderField === null) {
708  $this->_orderField = $this->_productListHelper->getDefaultSortField();
709  }
710  return $this->_orderField;
711  }
712 
718  private function loadAvailableOrders()
719  {
720  if ($this->_availableOrder === null) {
721  $this->_availableOrder = $this->_catalogConfig->getAttributeUsedForSortByArray();
722  }
723  return $this;
724  }
725 }
__construct(\Magento\Framework\View\Element\Template\Context $context, \Magento\Catalog\Model\Session $catalogSession, \Magento\Catalog\Model\Config $catalogConfig, ToolbarModel $toolbarModel, \Magento\Framework\Url\EncoderInterface $urlEncoder, ProductList $productListHelper, \Magento\Framework\Data\Helper\PostHelper $postDataHelper, array $data=[])
Definition: Toolbar.php:132
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$order
Definition: order.php:55
$value
Definition: gender.phtml:16
getWidgetOptionsJson(array $customOptions=[])
Definition: Toolbar.php:682
$this _collection
Definition: coupons.php:7
Definition: PostHelper.php:15
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
setData($key, $value=null)
Definition: DataObject.php:72
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18