ILIAS  release_8 Revision v8.24
class.ilObjSearchLuceneSettingsFormGUI.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
24use ILIAS\Refinery\Factory as RefFactory;
27
32{
35 protected ilLanguage $lng;
37 protected Factory $factory;
39 protected RefFactory $refinery;
40 protected ilObjUser $user;
41
43
44 public function __construct(
48 UIServices $ui,
49 RefFactory $refinery,
52 ) {
53 $this->http = $http;
54 $this->ctrl = $ctrl;
55 $this->lng = $lng;
56 $this->tpl = $ui->mainTemplate();
57 $this->factory = $ui->factory();
58 $this->renderer = $ui->renderer();
59 $this->refinery = $refinery;
60 $this->user = $user;
61 $this->coordinator = $coordinator;
62 }
63
64 public function executeCommand(): void
65 {
66 $cmd = $this->ctrl->getCmd();
67
68 switch ($cmd) {
69 case 'readOnly':
70 $this->showForm(true);
71 break;
72
73 case 'edit':
74 $this->showForm(false);
75 break;
76
77 case 'permDenied':
78 $this->showPermissionDenied();
79 break;
80
81 case 'update':
82 $this->update();
83 break;
84
85 default:
87 'Invalid command for ilObjSearchLuceneSettingsFormGUI: ' . $cmd
88 );
89 }
90 }
91
92 protected function showForm(
93 bool $read_only,
94 bool $get_from_post = false
95 ): void {
96 $form = $this->initForm($read_only);
97 if ($get_from_post) {
98 $form = $form->withRequest($this->http->request());
99 }
100 $this->tpl->setContent($this->renderer->render($form));
101 }
102
103 protected function update(): void
104 {
105 $form = $this->initForm(false)
106 ->withRequest($this->http->request());
107
108 if (!$form->getData()) {
109 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'));
110 $this->showForm(false, true);
111 return;
112 }
113
114 $settings = $this->getSettings();
115 $data = $form->getData()['section'];
116
117 $settings->setFragmentCount((int) $data['fragmentCount']);
118 $settings->setFragmentSize((int) $data['fragmentSize']);
119 $settings->setMaxSubitems((int) $data['maxSubitems']);
120 $settings->showRelevance((bool) $data['relevance']);
121 $settings->enableLuceneMimeFilter(!is_null($data['mime']));
122 if (!is_null($data['mime'])) {
123 $settings->setLuceneMimeFilter((array) $data['mime']);
124 }
125 $settings->showSubRelevance((bool) ($data['relevance']['subrelevance'] ?? false));
126 $settings->enablePrefixWildcardQuery((bool) $data['prefix']);
127 $settings->setLastIndexTime(new ilDateTime(
128 $data['last_index']->getTimestamp(),
130 ));
131 $settings->update();
132
133 // refresh lucene server
134 try {
135 if ($settings->enabledLucene()) {
136 $this->coordinator->refreshLuceneSettings();
137 }
138 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
139 ilSession::clear('search_last_class');
140 $this->ctrl->redirect($this, 'edit');
141 } catch (Exception $exception) {
142 $this->tpl->setOnScreenMessage('failure', $exception->getMessage());
143 $this->showForm(false);
144 }
145 }
146
147 protected function showPermissionDenied(): void
148 {
149 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
150 $this->ctrl->redirect($this, 'readOnly');
151 }
152
153 protected function initForm(bool $read_only): StandardForm
154 {
155 $settings = $this->getSettings();
156 $field_factory = $this->factory->input()->field();
157
158 // Item filter
159 $filter = $settings->getLuceneMimeFilter();
160 $checks = [];
161 foreach (ilSearchSettings::getLuceneMimeFilterDefinitions() as $mime => $def) {
162 $checks[$mime] = $field_factory->checkbox(
163 $this->lng->txt($def['trans'])
164 )->withValue(isset($filter[$mime]) && $filter[$mime]);
165 }
166
167 $item_filter = $field_factory->optionalGroup(
168 $checks,
169 $this->lng->txt('search_mime_filter_form'),
170 $this->lng->txt('search_mime_filter_form_info')
171 );
172 if (!$settings->isLuceneMimeFilterEnabled()) {
173 $item_filter = $item_filter->withValue(null);
174 }
175
176 // Prefix
177 $prefix = $field_factory->checkbox(
178 $this->lng->txt('lucene_prefix_wildcard'),
179 $this->lng->txt('lucene_prefix_wildcard_info')
180 )->withValue($settings->isPrefixWildcardQueryEnabled());
181
182 // Number of fragments
183 $frag_count = $field_factory->numeric(
184 $this->lng->txt('lucene_num_fragments'),
185 $this->lng->txt('lucene_num_frag_info')
186 )->withValue($settings->getFragmentCount())
187 ->withRequired(true)
188 ->withAdditionalTransformation(
189 $this->refinery->int()->isLessThanOrEqual(10)
190 )->withAdditionalTransformation(
191 $this->refinery->int()->isGreaterThanOrEqual(1)
192 );
193
194 // Size of fragments
195 $frag_size = $field_factory->numeric(
196 $this->lng->txt('lucene_size_fragments'),
197 $this->lng->txt('lucene_size_frag_info')
198 )->withValue($settings->getFragmentSize())
199 ->withRequired(true)
200 ->withAdditionalTransformation(
201 $this->refinery->int()->isLessThanOrEqual(1000)
202 )->withAdditionalTransformation(
203 $this->refinery->int()->isGreaterThanOrEqual(10)
204 );
205
206 // Number of sub-items
207 $max_sub = $field_factory->numeric(
208 $this->lng->txt('lucene_max_sub'),
209 $this->lng->txt('lucene_max_sub_info')
210 )->withValue($settings->getMaxSubitems())
211 ->withRequired(true)
212 ->withAdditionalTransformation(
213 $this->refinery->int()->isLessThanOrEqual(10)
214 )->withAdditionalTransformation(
215 $this->refinery->int()->isGreaterThanOrEqual(1)
216 );
217
218 // Relevance
219 $subrel = $field_factory->checkbox(
220 $this->lng->txt('lucene_show_sub_relevance')
221 )->withValue($settings->isSubRelevanceVisible());
222
223 $relevance = $field_factory->optionalGroup(
224 ['subrelevance' => $subrel],
225 $this->lng->txt('lucene_relevance'),
226 $this->lng->txt('lucene_show_relevance_info')
227 );
228 if (!$settings->isRelevanceVisible()) {
229 $relevance = $relevance->withValue(null);
230 }
231
232 // Last Index
233 $timezone = $this->user->getTimeZone();
234 $datetime = new DateTime(
235 '@' . $settings->getLastIndexTime()->get(IL_CAL_UNIX)
236 );
237 $datetime->setTimezone(new DateTimeZone($timezone));
238 $last_index = $field_factory->dateTime(
239 $this->lng->txt('lucene_last_index_time'),
240 $this->lng->txt('lucene_last_index_time_info')
241 )->withRequired(true)
242 ->withUseTime(true)
243 ->withTimezone($timezone);
244 $last_index = $last_index->withValue(
245 $datetime->format($last_index->getFormat()->toString() . ' H:i')
246 );
247
251 $section = $this->factory->input()->field()->section(
252 [
253 'mime' => $item_filter,
254 'prefix' => $prefix,
255 'fragmentCount' => $frag_count,
256 'fragmentSize' => $frag_size,
257 'maxSubitems' => $max_sub,
258 'relevance' => $relevance,
259 'last_index' => $last_index
260 ],
261 $this->lng->txt('lucene_settings_title')
262 )->withDisabled($read_only);
263
264 if ($read_only) {
265 $action = $this->ctrl->getFormAction($this, 'permDenied');
266 } else {
267 $action = $this->ctrl->getFormAction($this, 'update');
268 }
269
270 return $this->factory->input()->container()->form()->standard(
271 $action,
272 ['section' => $section]
273 );
274 }
275
276 protected function getSettings(): ilSearchSettings
277 {
279 }
280}
Provides fluid interface to RBAC services.
Definition: UIServices.php:24
renderer()
Get a renderer for UI components.
Definition: UIServices.php:43
mainTemplate()
Get the ILIAS main template.
Definition: UIServices.php:53
factory()
Get the factory that crafts UI components.
Definition: UIServices.php:35
Builds data types.
Definition: Factory.php:21
const IL_CAL_UNIX
@classDescription Date and time handling
language handling
showForm(bool $read_only, bool $get_from_post=false)
__construct(GlobalHttpState $http, ilCtrlInterface $ctrl, ilLanguage $lng, UIServices $ui, RefFactory $refinery, ilObjUser $user, ilObjSearchRpcClientCoordinator $coordinator)
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...
User class.
static getLuceneMimeFilterDefinitions()
static clear(string $a_var)
Interface GlobalHttpState.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
This is how the factory for UI elements looks.
Definition: Factory.php:38
An entity that renders components to a string output.
Definition: Renderer.php:31
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...
static http()
Fetches the global http state from ILIAS.
getSettings()
Get an array of all setting values.
Definition: System.php:287
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200