ListTable.php
155 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Give\Framework\ListTable; |
| 6 | |
| 7 | use Exception; |
| 8 | use Give\Framework\ListTable\Concerns\Columns; |
| 9 | use Give\Framework\ListTable\Exceptions\ColumnIdCollisionException; |
| 10 | use Give\Framework\Models\Model; |
| 11 | use Give\Framework\Support\Contracts\Arrayable; |
| 12 | use Give\Log\Log; |
| 13 | |
| 14 | /** |
| 15 | * @since 2.24.0 |
| 16 | */ |
| 17 | abstract class ListTable implements Arrayable |
| 18 | { |
| 19 | use Columns; |
| 20 | |
| 21 | /** |
| 22 | * @var array |
| 23 | */ |
| 24 | private $items = []; |
| 25 | |
| 26 | /** |
| 27 | * @since 2.24.0 |
| 28 | * |
| 29 | * @throws ColumnIdCollisionException |
| 30 | */ |
| 31 | public function __construct() |
| 32 | { |
| 33 | $this->addColumns(...$this->getDefaultColumns()); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get table ID |
| 38 | * |
| 39 | * @since 2.24.0 |
| 40 | */ |
| 41 | abstract public function id(): string; |
| 42 | |
| 43 | /** |
| 44 | * Define table columns |
| 45 | * |
| 46 | * @since 2.24.0 |
| 47 | * |
| 48 | * @return ModelColumn[] |
| 49 | */ |
| 50 | abstract protected function getDefaultColumns(): array; |
| 51 | |
| 52 | /** |
| 53 | * Define default visible table columns |
| 54 | * |
| 55 | * @since 2.24.0 |
| 56 | * |
| 57 | * @return string[] |
| 58 | */ |
| 59 | abstract protected function getDefaultVisibleColumns(): array; |
| 60 | |
| 61 | /** |
| 62 | * Get table definitions |
| 63 | * |
| 64 | * @since 2.24.0 |
| 65 | * |
| 66 | * @return array |
| 67 | */ |
| 68 | public function toArray(): array |
| 69 | { |
| 70 | return [ |
| 71 | 'id' => $this->id(), |
| 72 | 'columns' => $this->getColumnsArray(), |
| 73 | ]; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Set table items |
| 78 | * |
| 79 | * @since 2.24.0 |
| 80 | * |
| 81 | * @param array $items |
| 82 | * @param string $locale |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | public function items(array $items, string $locale = '') |
| 87 | { |
| 88 | $data = []; |
| 89 | |
| 90 | $columns = $this->getColumns(); |
| 91 | |
| 92 | foreach ($items as $model) { |
| 93 | $row = []; |
| 94 | |
| 95 | foreach ($columns as $column) { |
| 96 | $row[$column::getId()] = $this->safelyGetCellValue($column, $model, $locale);; |
| 97 | } |
| 98 | |
| 99 | $data[] = $row; |
| 100 | } |
| 101 | |
| 102 | $this->items = $data; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @since 2.24.0 |
| 107 | * |
| 108 | * @return array |
| 109 | */ |
| 110 | public function getItems(): array |
| 111 | { |
| 112 | return $this->items; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Safely retrieves the cell value for a column. If an exception is thrown, it will be logged and the cell value |
| 117 | * will be a human-readable error message. This is to prevent fatal errors from breaking the entire table. |
| 118 | * |
| 119 | * @since 2.24.1 |
| 120 | * |
| 121 | * @return mixed |
| 122 | */ |
| 123 | private function safelyGetCellValue(ModelColumn $column, Model $model, string $locale) |
| 124 | { |
| 125 | try { |
| 126 | $cellValue = $column->getCellValue($model, $locale); |
| 127 | } catch (Exception $exception) { |
| 128 | Log::error( |
| 129 | sprintf( |
| 130 | 'Error while rendering column "%s" for table "%s".', |
| 131 | $column::getId(), |
| 132 | $this->id() |
| 133 | ), |
| 134 | [ |
| 135 | 'column' => $column::getId(), |
| 136 | 'table' => $this->id(), |
| 137 | 'model' => $model->toArray(), |
| 138 | 'exception' => $exception->getMessage(), |
| 139 | ] |
| 140 | ); |
| 141 | |
| 142 | $cellValue = __( |
| 143 | sprintf( |
| 144 | 'Something went wrong, more in detail in <a href="%s">logs</a>', |
| 145 | admin_url('edit.php?post_type=give_forms&page=give-tools&tab=logs') |
| 146 | ), |
| 147 | 'give' |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | return $cellValue; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 |