ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
MultiPropertiesManipulator.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\UI\Component\Button\Standard as StandardButton;
26use ILIAS\UI\Component\Modal\RoundTrip as RoundTripModal;
27use ILIAS\UI\Factory as UIFactory;
28use ILIAS\Refinery\Factory as Refinery;
29use ILIAS\Data\Factory as DataFactory;
30use Psr\Http\Message\ServerRequestInterface;
31
33{
34 public function __construct(
35 private readonly ObjectReferencePropertiesRepository $object_reference_properties_repo,
36 private readonly Aggregator $properties_agregator,
37 private readonly \ilLanguage $language,
38 private readonly \ilCtrlInterface $ctrl,
39 private readonly \ilObjUser $user,
40 private readonly UIFactory $ui_factory,
41 private readonly \ilGlobalTemplateInterface $tpl,
42 private readonly Refinery $refinery
43 ) {
44 }
45
46 public function getAvailabilityPeriodButton(): StandardButton
47 {
48 $on_load_code = function ($id) {
49 return "document.getElementById('$id')"
50 . '.addEventListener("click", '
51 . '(e) => {e.preventDefault();'
52 . 'e.target.setAttribute("name", "cmd[editAvailabilityPeriod]");'
53 . 'e.target.form.requestSubmit(e.target);});';
54 };
55
56 return $this->ui_factory->button()->standard(
57 $this->language->txt('edit_availability_period'),
58 ''
59 )->withAdditionalOnLoadCode($on_load_code);
60 }
61
63 array $ref_ids,
64 \ilObjectGUI $parent_gui
65 ): ?RoundTripModal {
66 if ($ref_ids === []) {
67 $this->tpl->setOnScreenMessage('failure', $this->language->txt('no_objects_selected'));
68 return null;
69 }
70
71 $this->object_reference_properties_repo->preload($ref_ids);
72 $this->properties_agregator->preload($ref_ids);
73
74 $items = $this->getItemsForRefIds($ref_ids);
75
76 $post_url = $this->ctrl->getFormAction($parent_gui, 'saveAvailabilityPeriod');
77
78 return $this->buildModal($post_url, $items, $ref_ids, $this->areAllElementsEqual($ref_ids));
79 }
80
82 \ilObjectGUI $parent_gui,
83 \Closure $check_access,
84 ServerRequestInterface $request
85 ): RoundTripModal|bool {
86 $post_url = $this->ctrl->getFormAction($parent_gui, 'saveAvailabilityPeriod');
87 $availability_period_modal = $this->buildModal($post_url)
88 ->withRequest($request);
89 $data = $availability_period_modal->getData();
90 if ($data === null) {
91 return $availability_period_modal;
92 }
93 $ref_ids = $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->int())->transform(
94 explode(',', $data['affected_items'])
95 );
96
97 if (!$check_access($ref_ids)) {
98 return false;
99 }
100
101 $availability_period_property = $data['enable_availability_period'];
102 $this->saveAvailabilityPeriodPropertyForObjectRefIds($ref_ids, $availability_period_property);
103 return true;
104 }
105
106 private function buildModal(
107 string $post_url,
108 ?array $items = null,
109 ?array $ref_ids = null,
110 bool $all_settings_are_equal = false
111 ): RoundTripModal {
112 $ref_id_for_value = null;
113
114 if ($ref_ids !== null && $all_settings_are_equal) {
115 $ref_id_for_value = $ref_ids[0];
116 }
117 $modal_factory = $this->ui_factory->modal();
118 $content = $items;
119
120 $input_fields = $this->buildForm($ref_id_for_value);
121
122 if ($ref_ids !== null) {
123 $input_fields['affected_items'] = $input_fields['affected_items']->withValue(implode(',', $ref_ids));
124 }
125
126 if ($ref_ids !== null && !$all_settings_are_equal) {
127 $content = [
128 $this->ui_factory->messageBox()->info($this->language->txt('unequal_items_for_availability_period_message')),
129 ] + $items;
130 }
131 return $modal_factory->roundtrip($this->language->txt('edit_availability_period'), $content, $input_fields, $post_url);
132 }
133
138 private function buildForm(?int $ref_id_for_values): array
139 {
140 $data_factory = new DataFactory();
141 $date_format = $this->user->getDateFormat();
142 $environment = [
143 'user_time_zone' => $this->user->getTimeZone(),
144 'user_date_format' => $data_factory->dateFormat()->withTime24($date_format)
145 ];
146
147 $input_fields = [];
148 $input_fields['enable_availability_period'] = $this->object_reference_properties_repo
149 ->getFor($ref_id_for_values)->getPropertyAvailabilityPeriod()->toForm(
150 $this->language,
151 $this->ui_factory->input()->field(),
152 $this->refinery,
153 $environment
154 );
155 $input_fields['affected_items'] = $this->ui_factory->input()->field()->hidden();
156
157 return $input_fields;
158 }
159
161 array $object_reference_ids,
162 AvailabilityPeriod $property
163 ): void {
164 foreach ($object_reference_ids as $object_reference_id) {
165 $this->object_reference_properties_repo->storePropertyAvailabilityPeriod(
166 $property->withObjectReferenceId($object_reference_id)
167 );
168 }
169 }
170
174 private function areAllElementsEqual(array $ref_ids): bool
175 {
176 $previous_element = $this->object_reference_properties_repo->getFor(array_shift($ref_ids))->getPropertyAvailabilityPeriod();
177 foreach ($ref_ids as $ref_id) {
178 $current_element = $this->object_reference_properties_repo->getFor($ref_id)->getPropertyAvailabilityPeriod();
179 if ($current_element->getAvailabilityPeriodEnabled() === false
180 && $previous_element->getAvailabilityPeriodEnabled() === false) {
181 return true;
182 }
183
184 if ($current_element->getAvailabilityPeriodEnabled() !== $previous_element->getAvailabilityPeriodEnabled()
185 || $current_element->getAvailabilityPeriodStart() != $previous_element->getAvailabilityPeriodStart()
186 || $current_element->getAvailabilityPeriodEnd() != $previous_element->getAvailabilityPeriodEnd()
187 || $current_element->getVisibleWhenDisabled() !== $previous_element->getVisibleWhenDisabled()) {
188 return false;
189 }
190 }
191 return true;
192 }
193
198 private function getItemsForRefIds(array $object_reference_ids): array
199 {
200 $items = [];
201 foreach ($object_reference_ids as $object_reference_id) {
202 $object_id = $this->object_reference_properties_repo->getFor(
203 $object_reference_id
204 )->getObjectId();
205 $object_properties = $this->properties_agregator->getFor(
206 $object_id
207 );
208 $title = $object_properties->getPropertyTitleAndDescription()->getTitle();
209 $icon = $this->ui_factory->symbol()->icon()->custom(
210 \ilObject::getIconForReference($object_reference_id, $object_id, ''),
211 $title
212 );
213 $key = 'obj_' . $object_reference_id;
214 $items[$key] = $this->ui_factory->item()->shy($title)->withLeadIcon($icon);
215 }
216 return $items;
217 }
218}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
buildModal(string $post_url, ?array $items=null, ?array $ref_ids=null, bool $all_settings_are_equal=false)
getEditAvailabilityPeriodPropertiesModal(array $ref_ids, \ilObjectGUI $parent_gui)
__construct(private readonly ObjectReferencePropertiesRepository $object_reference_properties_repo, private readonly Aggregator $properties_agregator, private readonly \ilLanguage $language, private readonly \ilCtrlInterface $ctrl, private readonly \ilObjUser $user, private readonly UIFactory $ui_factory, private readonly \ilGlobalTemplateInterface $tpl, private readonly Refinery $refinery)
saveAvailabilityPeriodPropertyForObjectRefIds(array $object_reference_ids, AvailabilityPeriod $property)
saveEditAvailabilityPeriodPropertiesModal(\ilObjectGUI $parent_gui, \Closure $check_access, ServerRequestInterface $request)
language handling
User class.
Class ilObjectGUI Basic methods of all Output classes.
This describes a standard button.
Definition: Standard.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$ref_id
Definition: ltiauth.php:66
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
if(!file_exists('../ilias.ini.php'))