<?php
namespace PHPFUI\Input;
class LimitSelect extends \PHPFUI\Input\Select
{
use \PHPFUI\Traits\Page;
private string $limitName;
private array $limits;
private string $pageName;
public function __construct(protected \PHPFUI\Interfaces\Page $page, private int $currentLimit)
{
parent::__construct('l');
$this->addPHPClassName();
$this->setLimits();
$this->setLimitName();
$this->setPageName();
$this->addAttribute('onchange', "LimitSelect{$this->getId()}(this.value)");
$this->page->addJavaScript('function computePage(newLimit,oldLimit,page){return Math.floor(page*oldLimit/newLimit)}');
}
public function getStart() : string
{
$parameters = $this->page->getQueryParameters();
$this->removeAll();
if (! \in_array($this->currentLimit, $this->limits))
{
$this->limits[] = $this->currentLimit;
\sort($this->limits);
}
foreach ($this->limits as $limit)
{
$this->addOption((string)$limit, (string)$limit, $limit == $this->currentLimit);
}
$page = (int)($parameters[$this->pageName] ?? 1);
unset($parameters[$this->pageName], $parameters[$this->limitName]);
$query = \http_build_query($parameters);
$js = "function LimitSelect{$this->getId()}(newLimit){" .
"var p=new URLSearchParams('{$query}');" .
"p.set('{$this->limitName}',newLimit);" .
"p.set('{$this->pageName}',computePage(newLimit,{$this->currentLimit},{$page}));" .
"window.location='{$this->page->getBaseURL()}?'+p.toString()}";
$this->page->addJavaScript($js);
return parent::getStart();
}
public function setLimitName(string $limitName = 'l') : static
{
$this->limitName = $limitName;
return $this;
}
public function setLimits(array $limits = [10, 25, 50, 100]) : static
{
$this->limits = $limits;
return $this;
}
public function setPageName(string $pageName = 'p') : static
{
$this->pageName = $pageName;
return $this;
}
}