ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMailTemplateTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25use ILIAS\UI\Factory as UIFactory;
26use ILIAS\Data\Factory as DataFactory;
28use Psr\Http\Message\ServerRequestInterface;
29use ILIAS\UI\Component\Table\Data as DataTable;
32
34{
36 protected array $contexts = [];
37
39 private ?array $records = null;
40
41 public function __construct(
42 private readonly ServerRequestInterface $http_request,
43 private readonly ilLanguage $lng,
44 private readonly UIFactory $ui_factory,
45 private readonly Uri $table_uri,
46 private readonly ilMailTemplateService $service,
47 private readonly bool $read_only = false,
48 ) {
50 }
51
52 public function getRows(
53 ILIAS\UI\Component\Table\DataRowBuilder $row_builder,
54 array $visible_column_ids,
56 Order $order,
57 ?array $filter_data,
58 ?array $additional_parameters,
59 ): Generator {
60 $records = $this->getRecords($range, $order);
61 foreach ($records as $record) {
62 yield $row_builder->buildDataRow($record['tpl_id'], $record)
63 ->withDisabledAction(
64 'setAsContextDefault',
65 isset($record['is_default']) && $record['is_default']
66 )
67 ->withDisabledAction(
68 'unsetAsContextDefault',
69 !isset($record['is_default']) || !$record['is_default']
70 );
71 }
72 }
73
74 public function initRecords(): void
75 {
76 if ($this->records === null) {
77 $this->records = [];
78 foreach ($this->service->listAllTemplatesAsArray() as $item) {
79 $this->records[] = [
80 'tpl_id' => (string) $item['tpl_id'],
81 'title' => $item['title'],
82 'context' => $this->getContext($item['context'], $item['is_default'] ?? false),
83 'is_default' => $item['is_default'] ?? false,
84 ];
85 }
86 }
87 }
88
89 public function getComponent(): DataTable
90 {
91 $query_params_namespace = ['mail', 'template'];
92 $url_builder = new URLBuilder($this->table_uri);
93 [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
94 $query_params_namespace,
95 'table_action',
96 'tpl_ids'
97 );
98
99 return $this->ui_factory->table()
100 ->data(
101 $this,
102 $this->lng->txt('mail_templates'),
103 $this->getColumns()
104 )
105 ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
106 ->withId(
107 'mail_man_tpl'
108 )
109 ->withOrder(new Order('title', Order::ASC))
110 ->withRequest($this->http_request);
111 }
112
113 public function getContext(string $value, bool $default = false): string
114 {
115 if (isset($this->contexts[$value])) {
116 $is_default_suffix = '';
117 if ($default) {
118 $is_default_suffix = $this->lng->txt('mail_template_default');
119 }
120
121 return implode('', [
122 $this->contexts[$value]->getTitle(),
123 $is_default_suffix,
124 ]);
125 }
126
127 return $this->lng->txt('mail_template_orphaned_context');
128 }
129
130 public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
131 {
132 $this->initRecords();
133
134 return count((array) $this->records);
135 }
136
140 private function getRecords(Range $range, Order $order): array
141 {
142 $this->initRecords();
143 $records = $this->records;
144
145 if ($order) {
146 $records = $this->orderRecords($records, $order);
147 }
148
149 if ($range) {
150 $records = $this->limitRecords($records, $range);
151 }
152
153 return $records;
154 }
155
159 public function getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token): array
160 {
161 $actions = [];
162 if ($this->contexts !== []) {
163 $actions['edit'] = $this->ui_factory->table()->action()->single(
164 $this->lng->txt('edit'),
165 $url_builder->withParameter($action_parameter_token, 'showEditTemplateForm'),
166 $row_id_token
167 );
168 } else {
169 $actions['view'] = $this->ui_factory->table()->action()->single(
170 $this->lng->txt('view'),
171 $url_builder->withParameter($action_parameter_token, 'showEditTemplateForm'),
172 $row_id_token
173 );
174 }
175
176 if (!$this->read_only) {
177 $actions['delete'] = $this->ui_factory->table()->action()->standard(
178 $this->lng->txt('delete'),
179 $url_builder->withParameter($action_parameter_token, 'confirmDeleteTemplate'),
180 $row_id_token
181 );
182 }
183
184 $actions['unsetAsContextDefault'] = $this->ui_factory->table()->action()->single(
185 $this->lng->txt('mail_template_unset_as_default'),
186 $url_builder->withParameter($action_parameter_token, 'unsetAsContextDefault'),
187 $row_id_token
188 );
189
190 $actions['setAsContextDefault'] = $this->ui_factory->table()->action()->single(
191 $this->lng->txt('mail_template_set_as_default'),
192 $url_builder->withParameter($action_parameter_token, 'setAsContextDefault'),
193 $row_id_token
194 );
195
196 return $actions;
197 }
198
203 private function limitRecords(array $records, Range $range): array
204 {
205 return array_slice($records, $range->getStart(), $range->getLength());
206 }
207
212 private function orderRecords(array $records, Order $order): array
213 {
214 [$order_field, $order_direction] = $order->join(
215 [],
216 fn($ret, $key, $value) => [$key, $value]
217 );
218 usort($records, static fn(array $left, array $right): int => ilStr::strCmp(
219 $left[$order_field] ?? '',
220 $right[$order_field] ?? ''
221 ));
222
223 if ($order_direction === Order::DESC) {
224 $records = array_reverse($records);
225 }
226
227 return $records;
228 }
229
236 private function getColumns(): array
237 {
238 return [
239 'title' => $this->ui_factory->table()->column()
240 ->text($this->lng->txt('title')),
241 'context' => $this->ui_factory->table()->column()
242 ->text($this->lng->txt('mail_template_context'))
243 ];
244 }
245}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
join($init, callable $fn)
Definition: Order.php:75
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter's value if the supplied token is valid.
Definition: URLBuilder.php:166
language handling
static getTemplateContexts(?array $a_id=null)
Returns an array of mail template contexts, the key of each entry matches its id.
getRecords(Range $range, Order $order)
getRows(ILIAS\UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters,)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e....
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
orderRecords(array $records, Order $order)
__construct(private readonly ServerRequestInterface $http_request, private readonly ilLanguage $lng, private readonly UIFactory $ui_factory, private readonly Uri $table_uri, private readonly ilMailTemplateService $service, private readonly bool $read_only=false,)
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
limitRecords(array $records, Range $range)
getContext(string $value, bool $default=false)
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
This describes a Data Table.
Definition: Data.php:31
$service
Definition: ltiresult.php:36
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31