| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Web\Common; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use ipl\Html\BaseHtmlElement; |
| 7 |
use ipl\Orm\ResultSet; |
| 8 |
use ipl\Stdlib\BaseFilter; |
| 9 |
use ipl\Web\Widget\EmptyStateBar; |
| 10 |
use ipl\Web\Widget\ItemTable; |
| 11 |
|
| 12 |
/** |
| 13 |
* Base class for item tables |
| 14 |
* |
| 15 |
* @deprecated Use {@see ItemTable} instead |
| 16 |
*/ |
| 17 |
abstract class BaseItemTable extends BaseHtmlElement |
| 18 |
{ |
| 19 |
use BaseFilter; |
| 20 |
|
| 21 |
/** @var string Defines the layout used by this item */ |
| 22 |
public const TABLE_LAYOUT = 'table-layout'; |
| 23 |
|
| 24 |
/** @var array<string, mixed> */ |
| 25 |
protected $baseAttributes = [ |
| 26 |
'class' => 'item-table', |
| 27 |
'data-base-target' => '_next' |
| 28 |
]; |
| 29 |
|
| 30 |
/** @var ResultSet|iterable<object> */ |
| 31 |
protected $data; |
| 32 |
|
| 33 |
protected $tag = 'ul'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Create a new item table |
| 37 |
* |
| 38 |
* @param ResultSet|iterable<object> $data Data source of the table |
| 39 |
*/ |
| 40 |
public function __construct($data) |
| 41 |
{ |
| 42 |
if (! is_iterable($data)) { |
| 43 |
throw new InvalidArgumentException('Data must be an array or an instance of Traversable'); |
| 44 |
} |
| 45 |
|
| 46 |
$this->data = $data; |
| 47 |
|
| 48 |
$this->addAttributes($this->baseAttributes); |
| 49 |
|
| 50 |
$this->init(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Initialize the item table |
| 55 |
* |
| 56 |
* If you want to adjust the item table after construction, override this method. |
| 57 |
*/ |
| 58 |
protected function init(): void |
| 59 |
{ |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get the table layout to use |
| 64 |
* |
| 65 |
* @return string |
| 66 |
*/ |
| 67 |
protected function getLayout(): string |
| 68 |
{ |
| 69 |
return static::TABLE_LAYOUT; |
| 70 |
} |
| 71 |
|
| 72 |
abstract protected function getItemClass(): string; |
| 73 |
|
| 74 |
protected function assemble(): void |
| 75 |
{ |
| 76 |
$this->addAttributes(['class' => $this->getLayout()]); |
| 77 |
|
| 78 |
$itemClass = $this->getItemClass(); |
| 79 |
foreach ($this->data as $data) { |
| 80 |
/** @var BaseTableRowItem $item */ |
| 81 |
$item = new $itemClass($data, $this); |
| 82 |
|
| 83 |
$this->addHtml($item); |
| 84 |
} |
| 85 |
|
| 86 |
if ($this->isEmpty()) { |
| 87 |
$this->setTag('div'); |
| 88 |
$this->addHtml(new EmptyStateBar(t('No items found.'))); |
| 89 |
} |
| 90 |
} |
| 91 |
} |