ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
UploadPoliciesTableUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
31 
36 {
37  public const ACTIVE_FILTER_PARAM = "active_filter";
38  protected const ACTIVE_FILTER_ALL = "all";
39  protected const ACTIVE_FILTER_ACTIVE = "active";
40  protected const ACTIVE_FILTER_INACTIVE = "inactive";
41 
45  protected array $components = [];
46 
47  public function __construct(
48  protected UploadPolicyDBRepository $upload_policy_db_repository,
49  protected ilCtrlInterface $ctrl,
50  protected HttpServices $http,
51  protected ilLanguage $language,
52  protected ilGlobalTemplateInterface $main_tpl,
53  protected ilRbacReview $rbac_review,
54  protected RefineryFactory $refinery,
55  protected UIFactory $ui_factory,
56  protected Renderer $ui_renderer,
57  protected bool $write_access
58  ) {
59  $policies = $this->getPresentablePolicies();
60  $actions = $this->buildPolicyActions($policies);
61  $table = $this->ui_factory->table()->presentation(
62  $this->language->txt('upload_policies'),
63  [$this->getViewControl()],
64  function (
65  PresentationRow $row,
66  UploadPolicy $record,
67  UIFactory $ui_factory
68  ) use ($actions): PresentationRow {
69  // Create texts for better data presentation in table
70  $upload_limit_text = $record->getUploadLimitInMB() . " MB";
71  $active_text = ($record->isActive()) ? $this->language->txt('yes') : $this->language->txt('no');
72  $audience_text = $this->getAudienceText($record->getAudienceType(), $record->getAudience());
73  $scope_text = $record->getScopeDefinition();
74  $valid_until_text = $record->getValidUntil()?->format('d.m.Y') ?? $this->language->txt(
75  "policy_no_validity_limitation_set"
76  );
77 
78  // Create row with fields and actions
79  $row = $row
80  ->withHeadline($record->getTitle())
81  ->withImportantFields(
82  [
83  $this->language->txt('policy_upload_limit') => $upload_limit_text,
84  $this->language->txt('policy_audience') => $audience_text,
85  $this->language->txt('active') => $active_text
86  ]
87  )
88  ->withContent(
89  $ui_factory->listing()->descriptive(
90  [
91  $this->language->txt('policy_upload_limit') => $upload_limit_text,
92  $this->language->txt('policy_audience') => $audience_text,
93  $this->language->txt('active') => $active_text,
94  $this->language->txt('policy_scope') => $scope_text,
95  $this->language->txt('policy_valid_until') => $valid_until_text,
96  ]
97  )
98  );
99 
100  if (null !== ($dropdown = $actions[$record->getPolicyId()] ?? null)) {
101  $row = $row->withAction($dropdown);
102  }
103 
104  return $row;
105  }
106  )->withData($policies);
107 
108  $this->components[] = $table;
109  }
110 
114  public function getComponents(): array
115  {
116  return $this->components;
117  }
118 
123  protected function buildPolicyActions(array $policies): array
124  {
125  if(!$this->write_access) {
126  return [];
127  }
128  $dropdowns = [];
129  foreach ($policies as $policy) {
130  // Store policy_id for later use when the table's actions (edit / delete) are used)
131  $this->ctrl->setParameterByClass(
132  ilUploadLimitsOverviewGUI::class,
134  $policy->getPolicyId()
135  );
136 
137  $deletion_modal = $this->getDeletionConfirmationModal($policy);
138  $dropdowns[$policy->getPolicyId()] = $this->ui_factory->dropdown()->standard(
139  [
140  $this->ui_factory->button()->shy(
141  $this->language->txt('edit'),
142  $this->ctrl->getLinkTargetByClass(
143  ilUploadLimitsOverviewGUI::class,
145  )
146  ),
147  $this->ui_factory->button()->shy(
148  $this->language->txt('delete'),
149  $deletion_modal->getShowSignal()
150  )
151  ]
152  );
153 
154  $this->components[] = $deletion_modal;
155  }
156 
157  return $dropdowns;
158  }
159 
160  protected function filterData(string $filter_value, array $data): array
161  {
162  $filtered_data = [];
163  switch ($filter_value) {
164  case self::ACTIVE_FILTER_ACTIVE:
165  $active_value = true;
166  break;
167  case self::ACTIVE_FILTER_INACTIVE:
168  $active_value = false;
169  break;
170  case self::ACTIVE_FILTER_ALL:
171  default:
172  return $data;
173  }
174 
178  foreach ($data as $data_entry) {
179  if ($data_entry->isActive() === $active_value) {
180  $filtered_data[] = $data_entry;
181  }
182  }
183  return $filtered_data;
184  }
185 
186  protected function getAudienceText(int $audience_type, array $audience_data): string
187  {
188  switch ($audience_type) {
190  $audience_text = $this->language->txt('all_global_roles');
191  // add selected roles to audience_text
192  if (!empty($audience_data['global_roles'])) {
193  $roles = $this->rbac_review->getRolesForIDs($audience_data['global_roles'], false);
194  $counter = 0;
195  foreach ($roles as $role) {
196  $counter++;
197  $audience_text .= " \"" . $role['title'] . "\"";
198  if ($counter !== count($roles)) {
199  $audience_text .= ",";
200  }
201  }
202  }
203  break;
205  default:
206  $audience_text = $this->language->txt('all_users');
207  break;
208  }
209 
210  return $audience_text;
211  }
212 
213  protected function getViewControl(): Mode
214  {
215  $target = $this->ctrl->getLinkTargetByClass(
216  ilUploadLimitsOverviewGUI::class,
218  );
219 
220  $active_control_element = self::ACTIVE_FILTER_ALL;
221  if ($this->http->wrapper()->query()->has(self::ACTIVE_FILTER_PARAM)) {
222  $active_control_element = $this->http->wrapper()->query()->retrieve(
223  self::ACTIVE_FILTER_PARAM,
224  $this->refinery->kindlyTo()->string()
225  );
226  }
227 
228  $actions_prefix = $target . "&" . self::ACTIVE_FILTER_PARAM . "=";
229  $actions = [
230  $this->language->txt(self::ACTIVE_FILTER_ALL) => $actions_prefix . self::ACTIVE_FILTER_ALL,
231  $this->language->txt(self::ACTIVE_FILTER_ACTIVE) => $actions_prefix . self::ACTIVE_FILTER_ACTIVE,
232  $this->language->txt(self::ACTIVE_FILTER_INACTIVE) => $actions_prefix . self::ACTIVE_FILTER_INACTIVE
233  ];
234 
235  return $this->ui_factory->viewControl()->mode($actions, 'policy_filter')->withActive(
236  $this->language->txt($active_control_element)
237  );
238  }
239 
241  {
242  $upload_limit_text = $record->getUploadLimitInMB() . " MB";
243  $active_text = ($record->isActive()) ? $this->language->txt('yes') : $this->language->txt('no');
244  $audience_text = $this->getAudienceText($record->getAudienceType(), $record->getAudience());
245  $valid_until_text = $record->getValidUntil()?->format('d.m.Y') ?? $this->language->txt(
246  "policy_no_validity_limitation_set"
247  );
248 
249  $item_text = $this->language->txt('title') . $record->getTitle() . "<br/>"
250  . $this->language->txt('policy_upload_limit') . $upload_limit_text . "<br/>"
251  . $this->language->txt('policy_audience') . $audience_text . "<br/>"
252  . $this->language->txt('active') . $active_text . "<br/>"
253  . $this->language->txt('policy_scope') . $record->getScopeDefinition() . "<br/>"
254  . $this->language->txt('policy_valid_until') . $valid_until_text;
255 
256  $deletion_item = $this->ui_factory->modal()->interruptiveItem()->standard(
257  (string) $record->getPolicyId(),
258  $item_text
259  );
260 
261  return $this->ui_factory->modal()->interruptive(
262  $this->language->txt("delete"),
263  $this->language->txt('policy_confirm_deletion'),
264  $this->ctrl->getLinkTargetByClass(
265  ilUploadLimitsOverviewGUI::class,
267  )
268  )->withAffectedItems(
269  [$deletion_item]
270  )->withActionButtonLabel($this->language->txt('delete'));
271  }
272 
276  protected function getPresentablePolicies(): array
277  {
278  $policy_data = $this->upload_policy_db_repository->getAll();
279 
280  if ($this->http->wrapper()->query()->has(self::ACTIVE_FILTER_PARAM)) {
281  $policy_data = $this->filterData(
282  $this->http->wrapper()->query()->retrieve(
283  self::ACTIVE_FILTER_PARAM,
284  $this->refinery->kindlyTo()->string()
285  ),
286  $policy_data
287  );
288  }
289 
290  return $policy_data;
291  }
292 }
An entity that renders components to a string output.
Definition: Renderer.php:30
const AUDIENCE_TYPE_GLOBAL_ROLE
getDeletionConfirmationModal(UploadPolicy $record)
getAudienceText(int $audience_type, array $audience_data)
static http()
Fetches the global http state from ILIAS.
withHeadline(string $headline)
Get a row like this with the given headline.
$http
Definition: raiseError.php:7
const AUDIENCE_TYPE_ALL_USERS
buildPolicyActions(array $policies)
This describes a Row used in Presentation Table.
This describes a Mode Control.
Definition: Mode.php:28
withAction($action)
Get a row like this with a button or a dropdown for actions in the expanded row.
__construct(protected UploadPolicyDBRepository $upload_policy_db_repository, protected ilCtrlInterface $ctrl, protected HttpServices $http, protected ilLanguage $language, protected ilGlobalTemplateInterface $main_tpl, protected ilRbacReview $rbac_review, protected RefineryFactory $refinery, protected UIFactory $ui_factory, protected Renderer $ui_renderer, protected bool $write_access)
Refinery Factory $refinery