ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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 mixed $additional_viewcontrol_data,
58 mixed $filter_data,
59 mixed $additional_parameters
60 ): Generator {
61 $records = $this->getRecords($range, $order);
62 foreach ($records as $record) {
63 yield $row_builder->buildDataRow($record['tpl_id'], $record)
64 ->withDisabledAction(
65 'setAsContextDefault',
66 isset($record['is_default']) && $record['is_default']
67 )
68 ->withDisabledAction(
69 'unsetAsContextDefault',
70 !isset($record['is_default']) || !$record['is_default']
71 );
72 }
73 }
74
75 public function initRecords(): void
76 {
77 if ($this->records === null) {
78 $this->records = [];
79 foreach ($this->service->listAllTemplatesAsArray() as $item) {
80 $this->records[] = [
81 'tpl_id' => (string) $item['tpl_id'],
82 'title' => $item['title'],
83 'context' => $this->getContext($item['context'], $item['is_default'] ?? false),
84 'is_default' => $item['is_default'] ?? false,
85 ];
86 }
87 }
88 }
89
90 public function getComponent(): DataTable
91 {
92 $query_params_namespace = ['mail', 'template'];
93 $url_builder = new URLBuilder($this->table_uri);
94 [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
95 $query_params_namespace,
96 'table_action',
97 'tpl_ids'
98 );
99
100 return $this->ui_factory->table()
101 ->data(
102 $this,
103 $this->lng->txt('mail_templates'),
104 $this->getColumns()
105 )
106 ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
107 ->withId(
108 'mail_man_tpl'
109 )
110 ->withOrder(new Order('title', Order::ASC))
111 ->withRange(new Range(0, 100))
112 ->withRequest($this->http_request);
113 }
114
115 public function getContext(string $value, bool $default = false): string
116 {
117 if (isset($this->contexts[$value])) {
118 $is_default_suffix = '';
119 if ($default) {
120 $is_default_suffix = $this->lng->txt('mail_template_default');
121 }
122
123 return implode('', [
124 $this->contexts[$value]->getTitle(),
125 $is_default_suffix,
126 ]);
127 }
128
129 return $this->lng->txt('mail_template_orphaned_context');
130 }
131
132 public function getTotalRowCount(
133 mixed $additional_viewcontrol_data,
134 mixed $filter_data,
135 mixed $additional_parameters
136 ): ?int {
137 $this->initRecords();
138
139 return count((array) $this->records);
140 }
141
145 private function getRecords(Range $range, Order $order): array
146 {
147 $this->initRecords();
148 $records = $this->records;
149
150 if ($order) {
151 $records = $this->orderRecords($records, $order);
152 }
153
154 if ($range) {
155 $records = $this->limitRecords($records, $range);
156 }
157
158 return $records;
159 }
160
164 public function getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token): array
165 {
166 $actions = [];
167 if ($this->contexts !== []) {
168 $actions['edit'] = $this->ui_factory->table()->action()->single(
169 $this->lng->txt('edit'),
170 $url_builder->withParameter($action_parameter_token, 'showEditTemplateForm'),
171 $row_id_token
172 );
173 } else {
174 $actions['view'] = $this->ui_factory->table()->action()->single(
175 $this->lng->txt('view'),
176 $url_builder->withParameter($action_parameter_token, 'showEditTemplateForm'),
177 $row_id_token
178 );
179 }
180
181 if (!$this->read_only) {
182 $actions['delete'] = $this->ui_factory->table()->action()->standard(
183 $this->lng->txt('delete'),
184 $url_builder->withParameter($action_parameter_token, 'confirmDeleteTemplate'),
185 $row_id_token
186 );
187 }
188
189 $actions['unsetAsContextDefault'] = $this->ui_factory->table()->action()->single(
190 $this->lng->txt('mail_template_unset_as_default'),
191 $url_builder->withParameter($action_parameter_token, 'unsetAsContextDefault'),
192 $row_id_token
193 );
194
195 $actions['setAsContextDefault'] = $this->ui_factory->table()->action()->single(
196 $this->lng->txt('mail_template_set_as_default'),
197 $url_builder->withParameter($action_parameter_token, 'setAsContextDefault'),
198 $row_id_token
199 );
200
201 return $actions;
202 }
203
208 private function limitRecords(array $records, Range $range): array
209 {
210 return array_slice($records, $range->getStart(), $range->getLength());
211 }
212
217 private function orderRecords(array $records, Order $order): array
218 {
219 [$order_field, $order_direction] = $order->join(
220 [],
221 fn($ret, $key, $value) => [$key, $value]
222 );
223 usort($records, static fn(array $left, array $right): int => ilStr::strCmp(
224 $left[$order_field] ?? '',
225 $right[$order_field] ?? ''
226 ));
227
228 if ($order_direction === Order::DESC) {
229 $records = array_reverse($records);
230 }
231
232 return $records;
233 }
234
241 private function getColumns(): array
242 {
243 return [
244 'title' => $this->ui_factory->table()->column()
245 ->text($this->lng->txt('title')),
246 'context' => $this->ui_factory->table()->column()
247 ->text($this->lng->txt('mail_template_context'))
248 ];
249 }
250}
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, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e....
getTotalRowCount(mixed $additional_viewcontrol_data, mixed $filter_data, mixed $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:33
$service
Definition: ltiresult.php:36
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31