ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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;
31
33{
34 private readonly ServerRequestInterface $request;
35 private readonly Data\Factory $data_factory;
36 private bool $mailing_allowed = false;
38 private ?array $records = null;
39
40 public function __construct(
41 private readonly ilMailingLists $mailing_lists,
42 private readonly \ilCtrlInterface $ctrl,
43 private readonly ilLanguage $lng,
44 private readonly \ILIAS\UI\Factory $ui_factory,
45 \ILIAS\HTTP\GlobalHttpState $http
46 ) {
47 $this->request = $http->request();
48 $this->data_factory = new Data\Factory();
49 }
50
51 private function isMailingAllowed(): bool
52 {
54 }
55
56 public function setMailingAllowed(bool $mailing_allowed): void
57 {
58 $this->mailing_allowed = $mailing_allowed;
59 }
60
61 public function getComponent(): UI\Component\Table\Data
62 {
63 $columns = $this->getColumns();
64 $actions = $this->getActions();
65
66 return $this->ui_factory
67 ->table()
68 ->data(
69 $this,
70 $this->lng->txt('mail_mailing_lists'),
71 $columns,
72 )
73 ->withOrder(new \ILIAS\Data\Order('title', \ILIAS\Data\Order::ASC))
74 ->withId(self::class)
75 ->withActions($actions)
76 ->withRequest($this->request);
77 }
78
82 private function getColumns(): array
83 {
84 return [
85 'title' => $this->ui_factory
86 ->table()
87 ->column()
88 ->text($this->lng->txt('title'))
89 ->withIsSortable(true),
90 'description' => $this->ui_factory
91 ->table()
92 ->column()
93 ->text($this->lng->txt('description'))
94 ->withIsSortable(true),
95 'members' => $this->ui_factory
96 ->table()
97 ->column()
98 ->number($this->lng->txt('members'))
99 ->withIsSortable(true),
100 ];
101 }
102
106 private function getActions(): array
107 {
108 $query_params_namespace = ['contact', 'mailinglist', 'list'];
109
110 $uri = $this->data_factory->uri(
111 ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(
112 ilMailingListsGUI::class,
113 'handleMailingListActions'
114 )
115 );
116
117 $url_builder = new UI\URLBuilder($uri);
118 [
119 $url_builder,
120 $action_parameter_token_copy,
121 $row_id_token
122 ] = $url_builder->acquireParameters(
123 $query_params_namespace,
124 'action',
125 'ml_ids'
126 );
127
128 $actions = [
129 'confirmDelete' => $this->ui_factory->table()->action()->standard(
130 $this->lng->txt('delete'),
131 $url_builder->withParameter($action_parameter_token_copy, 'confirmDelete'),
132 $row_id_token
133 ),
134 'showForm' => $this->ui_factory->table()->action()->single(
135 $this->lng->txt('edit'),
136 $url_builder->withParameter($action_parameter_token_copy, 'showForm'),
137 $row_id_token
138 ),
139 'showMembersList' => $this->ui_factory->table()->action()->single(
140 $this->lng->txt('members'),
141 $url_builder->withParameter($action_parameter_token_copy, 'showMembersList'),
142 $row_id_token
143 )
144 ];
145
146 if ($this->isMailingAllowed()) {
147 $actions['mailToList'] = $this->ui_factory->table()->action()->standard(
148 $this->lng->txt('send_mail_to'),
149 $url_builder->withParameter($action_parameter_token_copy, 'mailToList'),
150 $row_id_token
151 );
152 }
153
154 return $actions;
155 }
156
157 private function initRecords(): void
158 {
159 if ($this->records === null) {
160 $this->records = [];
161 $counter = 0;
162 $entries = $this->mailing_lists->getAll();
163
164 foreach ($entries as $entry) {
165 if ($entry->getMode() === ilMailingList::MODE_TEMPORARY) {
166 continue;
167 }
168
169 $this->records[$counter]['ml_id'] = $entry->getId();
170 $this->records[$counter]['title'] = $entry->getTitle() . ' [#il_ml_' . $entry->getId() . ']';
171 $this->records[$counter]['description'] = $entry->getDescription() ?? '';
172 $this->records[$counter]['members'] = \count($entry->getAssignedEntries());
173
174 ++$counter;
175 }
176 }
177 }
178
179 public function getRows(
180 UI\Component\Table\DataRowBuilder $row_builder,
181 array $visible_column_ids,
183 Data\Order $order,
184 ?array $filter_data,
185 ?array $additional_parameters
186 ): \Generator {
187 $records = $this->getRecords($range, $order);
188
189 foreach ($records as $record) {
190 $row_id = (string) $record['ml_id'];
191 yield $row_builder->buildDataRow($row_id, $record);
192 }
193 }
194
195 public function getTotalRowCount(
196 ?array $filter_data,
197 ?array $additional_parameters
198 ): ?int {
199 $this->initRecords();
200
201 return \count($this->records);
202 }
203
207 private function sortedRecords(Data\Order $order): array
208 {
209 $records = $this->records;
210 [$order_field, $order_direction] = $order->join([], fn($ret, $key, $value) => [$key, $value]);
211
213 $records,
214 $order_field,
215 strtolower($order_direction),
216 $order_field === 'members'
217 );
218 }
219
223 private function getRecords(Data\Range $range, Data\Order $order): array
224 {
225 $this->initRecords();
226 $records = $this->sortedRecords($order);
227
228 return $this->limitRecords($records, $range);
229 }
230
235 private function limitRecords(array $records, Data\Range $range): array
236 {
237 return \array_slice($records, $range->getStart(), $range->getLength());
238 }
239}
limitRecords(array $records, Data\Range $range)
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)
getRows(UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, Data\Range $range, Data\Order $order, ?array $filter_data, ?array $additional_parameters)
readonly ServerRequestInterface $request
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...
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