| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\Control; |
| 4 |
|
| 5 |
use ipl\Web\Compat\CompatForm; |
| 6 |
use ipl\Web\Url; |
| 7 |
|
| 8 |
/** |
| 9 |
* Allows to adjust the limit of the number of items to display |
| 10 |
*/ |
| 11 |
class LimitControl extends CompatForm |
| 12 |
{ |
| 13 |
/** @var int Default limit */ |
| 14 |
public const DEFAULT_LIMIT = 25; |
| 15 |
|
| 16 |
/** @var string Default limit param */ |
| 17 |
public const DEFAULT_LIMIT_PARAM = 'limit'; |
| 18 |
|
| 19 |
/** @var int[] Selectable default limits */ |
| 20 |
public static $limits = [ |
| 21 |
'25' => '25', |
| 22 |
'50' => '50', |
| 23 |
'100' => '100', |
| 24 |
'500' => '500' |
| 25 |
]; |
| 26 |
|
| 27 |
/** @var string Name of the URL parameter which stores the limit */ |
| 28 |
protected $limitParam = self::DEFAULT_LIMIT_PARAM; |
| 29 |
|
| 30 |
/** @var int */ |
| 31 |
protected $defaultLimit; |
| 32 |
|
| 33 |
/** @var Url */ |
| 34 |
protected $url; |
| 35 |
|
| 36 |
protected $method = 'GET'; |
| 37 |
|
| 38 |
public function __construct(Url $url) |
| 39 |
{ |
| 40 |
$this->url = $url; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the name of the URL parameter which stores the limit |
| 45 |
* |
| 46 |
* @return string |
| 47 |
*/ |
| 48 |
public function getLimitParam() |
| 49 |
{ |
| 50 |
return $this->limitParam; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Set the name of the URL parameter which stores the limit |
| 55 |
* |
| 56 |
* @param string $limitParam |
| 57 |
* |
| 58 |
* @return $this |
| 59 |
*/ |
| 60 |
public function setLimitParam($limitParam) |
| 61 |
{ |
| 62 |
$this->limitParam = $limitParam; |
| 63 |
|
| 64 |
return $this; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get the default limit |
| 69 |
* |
| 70 |
* @return int |
| 71 |
*/ |
| 72 |
public function getDefaultLimit() |
| 73 |
{ |
| 74 |
return $this->defaultLimit ?: static::DEFAULT_LIMIT; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Set the default limit |
| 79 |
* |
| 80 |
* @param int $limit |
| 81 |
* |
| 82 |
* @return $this |
| 83 |
*/ |
| 84 |
public function setDefaultLimit($limit) |
| 85 |
{ |
| 86 |
$this->defaultLimit = $limit; |
| 87 |
|
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Get the limit |
| 93 |
* |
| 94 |
* @return int |
| 95 |
*/ |
| 96 |
public function getLimit() |
| 97 |
{ |
| 98 |
return $this->url->getParam($this->getLimitParam(), $this->getDefaultLimit()); |
| 99 |
} |
| 100 |
|
| 101 |
protected function assemble() |
| 102 |
{ |
| 103 |
$this->addAttributes(['class' => 'limit-control inline']); |
| 104 |
|
| 105 |
$limits = static::$limits; |
| 106 |
if ($this->defaultLimit && ! isset($limits[$this->defaultLimit])) { |
| 107 |
$limits[$this->defaultLimit] = $this->defaultLimit; |
| 108 |
} |
| 109 |
|
| 110 |
$limit = $this->getLimit(); |
| 111 |
if (! isset($limits[$limit])) { |
| 112 |
$limits[$limit] = $limit; |
| 113 |
} |
| 114 |
|
| 115 |
$this->addElement('select', $this->getLimitParam(), [ |
| 116 |
'class' => 'autosubmit', |
| 117 |
'label' => '#', |
| 118 |
'options' => $limits, |
| 119 |
'title' => t('Change item count per page'), |
| 120 |
'value' => $limit |
| 121 |
]); |
| 122 |
|
| 123 |
if ($this->url->hasParam(PaginationControl::DEFAULT_PAGE_PARAM)) { |
| 124 |
$this->addElement('hidden', PaginationControl::DEFAULT_PAGE_PARAM, [ |
| 125 |
'value' => 1 |
| 126 |
]); |
| 127 |
} |
| 128 |
} |
| 129 |
} |