<?php
namespace PHPFUI\Input;
class MultiSelect extends \PHPFUI\Input\Select
{
private int $gridSize = 12;
private int $numberColumns = 1;
private string $selectAll = '';
public function __construct(string $name, ?string $label = '')
{
parent::__construct($name . '[]', $label);
}
public function select(string | array $selections) : static
{
if (! \is_array($selections))
{
$selections = [$selections];
}
foreach ($this->options as &$values)
{
$values['selected'] = \in_array($values['value'], $selections) ? 'selected' : '';
}
return $this;
}
public function selectAll(string $title = 'Select All') : MultiSelect
{
$this->selectAll = $title;
return $this;
}
public function setColumns(int $numberColumns = 1, int $gridSize = 12) : static
{
$this->gridSize = $gridSize;
$this->numberColumns = $numberColumns;
return $this;
}
protected function getStart() : string
{
if ($this->label)
{
$fieldSet = new \PHPFUI\FieldSet($this->getToolTip($this->label));
}
else
{
$fieldSet = new \PHPFUI\Container();
}
$rowCount = (int)($this->count() / $this->numberColumns) + (int)(($this->count() % $this->numberColumns) > 0);
$selectAllId = '';
if ($this->selectAll)
{
$selectAll = new \PHPFUI\Input\CheckBox('', "<b>{$this->selectAll}</b>", 0);
$selectAllId = $selectAll->getId();
$selectAll->addAttribute('onClick', '$(".' . $selectAllId . '").prop("checked",this.checked)');
$fieldSet->add($selectAll);
}
$gridx = new \PHPFUI\GridX();
$row = 0;
$cell = new \PHPFUI\Cell($this->gridSize / $this->numberColumns);
foreach ($this->options as $option)
{
if (0 == $row)
{
$cell = new \PHPFUI\Cell($this->gridSize / $this->numberColumns);
}
$checkBox = new \PHPFUI\Input\CheckBox($this->name, $option['label'], $option['value']);
if ($selectAllId)
{
$checkBox->addClass($selectAllId);
}
if ($option['selected'])
{
$checkBox->setChecked(true);
}
if ($option['disabled'])
{
$checkBox->setDisabled();
}
$cell->add($checkBox);
if (++$row >= $rowCount)
{
$row = 0;
$gridx->add($cell);
}
}
if (0 != $row)
{
$gridx->add($cell);
}
$fieldSet->add($gridx);
$this->label = '';
return (string)$fieldSet;
}
}