| 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\ItemList; |
| 11 |
|
| 12 |
/** |
| 13 |
* Base class for item lists |
| 14 |
* |
| 15 |
* @deprecated Use {@see ItemList} instead |
| 16 |
*/ |
| 17 |
abstract class BaseItemList extends BaseHtmlElement |
| 18 |
{ |
| 19 |
use BaseFilter; |
| 20 |
|
| 21 |
/** @var string Emitted while assembling the list after adding each list item */ |
| 22 |
public const ON_ITEM_ADD = 'item-added'; |
| 23 |
|
| 24 |
/** @var string Emitted while assembling the list before adding each list item */ |
| 25 |
public const BEFORE_ITEM_ADD = 'before-item-add'; |
| 26 |
|
| 27 |
/** @var array<string, mixed> */ |
| 28 |
protected $baseAttributes = [ |
| 29 |
'class' => ['item-list', 'default-layout'], |
| 30 |
'data-base-target' => '_next', |
| 31 |
'data-pdfexport-page-breaks-at' => '.list-item' |
| 32 |
]; |
| 33 |
|
| 34 |
/** @var ResultSet|iterable<object> */ |
| 35 |
protected $data; |
| 36 |
|
| 37 |
protected $tag = 'ul'; |
| 38 |
|
| 39 |
/** @var ?string Message to show if the list is empty */ |
| 40 |
protected $emptyStateMessage; |
| 41 |
|
| 42 |
/** |
| 43 |
* Create a new item list |
| 44 |
* |
| 45 |
* @param ResultSet|iterable<object> $data Data source of the list |
| 46 |
*/ |
| 47 |
public function __construct($data) |
| 48 |
{ |
| 49 |
if (! is_iterable($data)) { |
| 50 |
throw new InvalidArgumentException('Data must be an array or an instance of Traversable'); |
| 51 |
} |
| 52 |
|
| 53 |
$this->data = $data; |
| 54 |
|
| 55 |
$this->addAttributes($this->baseAttributes); |
| 56 |
|
| 57 |
$this->init(); |
| 58 |
} |
| 59 |
|
| 60 |
abstract protected function getItemClass(): string; |
| 61 |
|
| 62 |
/** |
| 63 |
* Initialize the item list |
| 64 |
* |
| 65 |
* If you want to adjust the item list after construction, override this method. |
| 66 |
*/ |
| 67 |
protected function init(): void |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Create a list item for the given data |
| 73 |
* |
| 74 |
* @param object $data |
| 75 |
* |
| 76 |
* @return BaseListItem|BaseTableRowItem |
| 77 |
*/ |
| 78 |
protected function createListItem(object $data) |
| 79 |
{ |
| 80 |
$className = $this->getItemClass(); |
| 81 |
|
| 82 |
return new $className($data, $this); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Get message to show if the list is empty |
| 87 |
* |
| 88 |
* @return string |
| 89 |
*/ |
| 90 |
public function getEmptyStateMessage(): string |
| 91 |
{ |
| 92 |
if ($this->emptyStateMessage === null) { |
| 93 |
return t('No items found.'); |
| 94 |
} |
| 95 |
|
| 96 |
return $this->emptyStateMessage; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Set message to show if the list is empty |
| 101 |
* |
| 102 |
* @param string $message |
| 103 |
* |
| 104 |
* @return $this |
| 105 |
*/ |
| 106 |
public function setEmptyStateMessage(string $message): self |
| 107 |
{ |
| 108 |
$this->emptyStateMessage = $message; |
| 109 |
|
| 110 |
return $this; |
| 111 |
} |
| 112 |
|
| 113 |
protected function assemble(): void |
| 114 |
{ |
| 115 |
foreach ($this->data as $data) { |
| 116 |
$item = $this->createListItem($data); |
| 117 |
$this->emit(self::BEFORE_ITEM_ADD, [$item, $data]); |
| 118 |
$this->addHtml($item); |
| 119 |
$this->emit(self::ON_ITEM_ADD, [$item, $data]); |
| 120 |
} |
| 121 |
|
| 122 |
if ($this->isEmpty()) { |
| 123 |
$this->setTag('div'); |
| 124 |
$this->addHtml(new EmptyStateBar($this->getEmptyStateMessage())); |
| 125 |
} |
| 126 |
} |
| 127 |
} |