ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
MailingListsTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use ILIAS\Data;
24use ILIAS\UI;
25use Psr\Http\Message\ServerRequestInterface;
26use ilArrayUtil;
29use ilLanguage;
32
34{
35 private readonly ServerRequestInterface $request;
36 private readonly Data\Factory $data_factory;
37 private bool $mailing_allowed = false;
39 private ?array $records = null;
40
41 public function __construct(
42 private readonly ilMailingLists $mailing_lists,
43 private readonly \ilCtrlInterface $ctrl,
44 private readonly ilLanguage $lng,
45 private readonly \ILIAS\UI\Factory $ui_factory,
46 \ILIAS\HTTP\GlobalHttpState $http
47 ) {
48 $this->request = $http->request();
49 $this->data_factory = new Data\Factory();
50 }
51
52 private function isMailingAllowed(): bool
53 {
55 }
56
57 public function setMailingAllowed(bool $mailing_allowed): void
58 {
59 $this->mailing_allowed = $mailing_allowed;
60 }
61
62 public function getComponent(): UI\Component\Table\Data
63 {
64 $columns = $this->getColumns();
65 $actions = $this->getActions();
66
67 return $this->ui_factory
68 ->table()
69 ->data(
70 $this,
71 $this->lng->txt('mail_mailing_lists'),
72 $columns,
73 )
74 ->withOrder(new \ILIAS\Data\Order('title', \ILIAS\Data\Order::ASC))
75 ->withRange(new Range(0, 50))
76 ->withId(str_replace('\\', '', self::class))
77 ->withActions($actions)
78 ->withRequest($this->request);
79 }
80
84 private function getColumns(): array
85 {
86 return [
87 'title' => $this->ui_factory
88 ->table()
89 ->column()
90 ->text($this->lng->txt('title'))
91 ->withIsSortable(true),
92 'description' => $this->ui_factory
93 ->table()
94 ->column()
95 ->text($this->lng->txt('description'))
96 ->withIsSortable(true),
97 'members' => $this->ui_factory
98 ->table()
99 ->column()
100 ->number($this->lng->txt('members'))
101 ->withIsSortable(true),
102 ];
103 }
104
108 private function getActions(): array
109 {
110 $query_params_namespace = ['contact', 'mailinglist', 'list'];
111
112 $uri = $this->data_factory->uri(
113 ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(
114 ilMailingListsGUI::class,
115 'handleMailingListActions'
116 )
117 );
118
119 $url_builder = new UI\URLBuilder($uri);
120 [
121 $url_builder,
122 $action_parameter_token_copy,
123 $row_id_token
124 ] = $url_builder->acquireParameters(
125 $query_params_namespace,
126 'action',
127 'ml_ids'
128 );
129
130 $actions = [
131 'confirmDelete' => $this->ui_factory->table()->action()->standard(
132 $this->lng->txt('delete'),
133 $url_builder->withParameter($action_parameter_token_copy, 'confirmDelete'),
134 $row_id_token
135 ),
136 'showForm' => $this->ui_factory->table()->action()->single(
137 $this->lng->txt('edit'),
138 $url_builder->withParameter($action_parameter_token_copy, 'showForm'),
139 $row_id_token
140 ),
141 'showMembersList' => $this->ui_factory->table()->action()->single(
142 $this->lng->txt('members'),
143 $url_builder->withParameter($action_parameter_token_copy, 'showMembersList'),
144 $row_id_token
145 )
146 ];
147
148 if ($this->isMailingAllowed()) {
149 $actions['mailToList'] = $this->ui_factory->table()->action()->standard(
150 $this->lng->txt('send_mail_to'),
151 $url_builder->withParameter($action_parameter_token_copy, 'mailToList'),
152 $row_id_token
153 );
154 }
155
156 return $actions;
157 }
158
159 private function initRecords(): void
160 {
161 if ($this->records === null) {
162 $this->records = [];
163 $counter = 0;
164 $entries = $this->mailing_lists->getAll();
165
166 foreach ($entries as $entry) {
167 if ($entry->getMode() === ilMailingList::MODE_TEMPORARY) {
168 continue;
169 }
170
171 $this->records[$counter]['ml_id'] = $entry->getId();
172 $this->records[$counter]['title'] = $entry->getTitle() . ' [#il_ml_' . $entry->getId() . ']';
173 $this->records[$counter]['description'] = $entry->getDescription() ?? '';
174 $this->records[$counter]['members'] = \count($entry->getAssignedEntries());
175
176 ++$counter;
177 }
178 }
179 }
180
181 public function getRows(
182 UI\Component\Table\DataRowBuilder $row_builder,
183 array $visible_column_ids,
185 Data\Order $order,
186 mixed $additional_viewcontrol_data,
187 mixed $filter_data,
188 mixed $additional_parameters
189 ): \Generator {
190 $records = $this->getRecords($range, $order);
191
192 foreach ($records as $record) {
193 $row_id = (string) $record['ml_id'];
194 yield $row_builder->buildDataRow($row_id, $record);
195 }
196 }
197
198 public function getTotalRowCount(
199 mixed $additional_viewcontrol_data,
200 mixed $filter_data,
201 mixed $additional_parameters
202 ): ?int {
203 $this->initRecords();
204
205 return \count($this->records);
206 }
207
211 private function sortedRecords(Data\Order $order): array
212 {
213 $records = $this->records;
214 [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
215
217 $records,
218 $order_field,
219 strtolower($order_direction),
220 $order_field === 'members'
221 );
222 }
223
227 private function getRecords(Data\Range $range, Data\Order $order): array
228 {
229 $this->initRecords();
230 $records = $this->sortedRecords($order);
231
232 return $this->limitRecords($records, $range);
233 }
234
239 private function limitRecords(array $records, Data\Range $range): array
240 {
241 return \array_slice($records, $range->getStart(), $range->getLength());
242 }
243}
limitRecords(array $records, Data\Range $range)
getRows(UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, Data\Range $range, Data\Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
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...
getRecords(Data\Range $range, Data\Order $order)
__construct(private readonly ilMailingLists $mailing_lists, private readonly \ilCtrlInterface $ctrl, private readonly ilLanguage $lng, private readonly \ILIAS\UI\Factory $ui_factory, \ILIAS\HTTP\GlobalHttpState $http)
readonly ServerRequestInterface $request
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
Definition: UI.php:24
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stableSortArray(array $array, string $a_array_sortby, string $a_array_sortorder="asc", bool $a_numeric=false)
Sort an aray using a stable sort algorithm, which preveserves the sequence of array elements which ha...
language handling
final const int MODE_TEMPORARY
$http
Definition: deliver.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31
$counter