ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilObjSearchLuceneSettingsFormGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25use ILIAS\Refinery\Factory as RefFactory;
27use ILIAS\Search\ObjGUI\Readme\Helper as ServerReadmeHelper;
28
33{
36 protected ilLanguage $lng;
38 protected Factory $factory;
40 protected RefFactory $refinery;
41 protected ilObjUser $user;
42
44 protected ServerReadmeHelper $readme_helper;
45
46 public function __construct(
50 UIServices $ui,
51 RefFactory $refinery,
54 ServerReadmeHelper $readme_helper
55 ) {
56 $this->http = $http;
57 $this->ctrl = $ctrl;
58 $this->lng = $lng;
59 $this->tpl = $ui->mainTemplate();
60 $this->factory = $ui->factory();
61 $this->renderer = $ui->renderer();
62 $this->refinery = $refinery;
63 $this->user = $user;
64 $this->coordinator = $coordinator;
65 $this->readme_helper = $readme_helper;
66 }
67
68 public function executeCommand(): void
69 {
70 $cmd = $this->ctrl->getCmd();
71
72 switch ($cmd) {
73 case 'readOnly':
74 $this->showForm(true);
75 break;
76
77 case 'edit':
78 $this->showForm(false);
79 break;
80
81 case 'permDenied':
82 $this->showPermissionDenied();
83 break;
84
85 case 'update':
86 $this->update();
87 break;
88
89 default:
91 'Invalid command for ilObjSearchLuceneSettingsFormGUI: ' . $cmd
92 );
93 }
94 }
95
96 protected function showForm(
97 bool $read_only,
98 bool $get_from_post = false
99 ): void {
100 $message = $this->readme_helper->getServerInfoMessageBox();
101 $form = $this->initForm($read_only);
102 if ($get_from_post) {
103 $form = $form->withRequest($this->http->request());
104 }
105 $this->tpl->setContent($this->renderer->render([$message, $form]));
106 }
107
108 protected function update(): void
109 {
110 $form = $this->initForm(false)
111 ->withRequest($this->http->request());
112
113 if (!$form->getData()) {
114 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'));
115 $this->showForm(false, true);
116 return;
117 }
118
119 $settings = $this->getSettings();
120 $data = $form->getData()['section'];
121 $text_data = $form->getData()['text_section'];
122 $index_data = $form->getData()['index_section'];
123
124 $settings->enableLuceneUserSearch((bool) $data['user_search_enabled']);
125 $settings->setFragmentCount((int) $text_data['fragmentCount']);
126 $settings->setFragmentSize((int) $text_data['fragmentSize']);
127 $settings->enableLuceneMimeFilter(!is_null($data['mime']));
128 if (!is_null($data['mime'])) {
129 $settings->setLuceneMimeFilter((array) $data['mime']);
130 }
131 $settings->enablePrefixWildcardQuery((bool) $data['prefix']);
132 $settings->setLastIndexTime(new ilDateTime(
133 $index_data['last_index']->getTimestamp(),
135 ));
136 $settings->update();
137
138 // refresh lucene server
139 try {
140 if ($settings->enabledLucene()) {
141 $this->coordinator->refreshLuceneSettings();
142 }
143 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
144 $this->ctrl->redirect($this, 'edit');
145 } catch (Exception $exception) {
146 $this->tpl->setOnScreenMessage('failure', $exception->getMessage());
147 $this->showForm(false);
148 }
149 }
150
151 protected function showPermissionDenied(): void
152 {
153 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
154 $this->ctrl->redirect($this, 'readOnly');
155 }
156
157 protected function initForm(bool $read_only): StandardForm
158 {
159 $settings = $this->getSettings();
160 $field_factory = $this->factory->input()->field();
161
162 // User search
163 $user_search = $field_factory->checkbox(
164 $this->lng->txt('search_user_search_form'),
165 $this->lng->txt('search_user_search_info_form')
166 )->withValue($settings->isLuceneUserSearchEnabled());
167
168 // Item filter
169 $filter = $settings->getLuceneMimeFilter();
170 $checks = [];
171 foreach (ilSearchSettings::getLuceneMimeFilterDefinitions() as $mime => $def) {
172 $checks[$mime] = $field_factory->checkbox(
173 $this->lng->txt($def['trans'])
174 )->withValue(isset($filter[$mime]) && $filter[$mime]);
175 }
176
177 $item_filter = $field_factory->optionalGroup(
178 $checks,
179 $this->lng->txt('search_mime_filter_form'),
180 $this->lng->txt('search_mime_filter_form_info')
181 );
182 if (!$settings->isLuceneMimeFilterEnabled()) {
183 $item_filter = $item_filter->withValue(null);
184 }
185
186 // Prefix
187 $prefix = $field_factory->checkbox(
188 $this->lng->txt('lucene_prefix_wildcard'),
189 $this->lng->txt('lucene_prefix_wildcard_info')
190 )->withValue($settings->isPrefixWildcardQueryEnabled());
191
192 // Number of fragments
193 $frag_count = $field_factory->numeric(
194 $this->lng->txt('lucene_num_fragments'),
195 $this->lng->txt('lucene_num_frag_info')
196 )->withValue($settings->getFragmentCount())
197 ->withRequired(true)
198 ->withAdditionalTransformation(
199 $this->refinery->int()->isLessThanOrEqual(10)
200 )->withAdditionalTransformation(
201 $this->refinery->int()->isGreaterThanOrEqual(1)
202 );
203
204 // Size of fragments
205 $frag_size = $field_factory->numeric(
206 $this->lng->txt('lucene_size_fragments'),
207 $this->lng->txt('lucene_size_frag_info')
208 )->withValue($settings->getFragmentSize())
209 ->withRequired(true)
210 ->withAdditionalTransformation(
211 $this->refinery->int()->isLessThanOrEqual(1000)
212 )->withAdditionalTransformation(
213 $this->refinery->int()->isGreaterThanOrEqual(10)
214 );
215
216 // Last Index
217 $timezone = $this->user->getTimeZone();
218 $datetime = new DateTime(
219 '@' . $settings->getLastIndexTime()->get(IL_CAL_UNIX)
220 );
221 $datetime->setTimezone(new DateTimeZone($timezone));
222 $last_index = $field_factory->dateTime(
223 $this->lng->txt('lucene_last_index_time'),
224 $this->lng->txt('lucene_last_index_time_info')
225 )->withRequired(true)
226 ->withUseTime(true)
227 ->withTimezone($timezone);
228 $last_index = $last_index->withValue(
229 $datetime->format($last_index->getFormat()->toString() . ' H:i')
230 );
231
232 $section = $this->factory->input()->field()->section(
233 [
234 'user_search_enabled' => $user_search,
235 'mime' => $item_filter,
236 'prefix' => $prefix
237 ],
238 $this->lng->txt('lucene_settings_title')
239 )->withDisabled($read_only);
240
241 $text_section = $this->factory->input()->field()->section(
242 [
243 'fragmentCount' => $frag_count,
244 'fragmentSize' => $frag_size
245 ],
246 $this->lng->txt('lucene_settings_text_section')
247 )->withDisabled($read_only);
248
249 $index_section = $this->factory->input()->field()->section(
250 [
251 'last_index' => $last_index
252 ],
253 $this->lng->txt('lucene_settings_index_section')
254 )->withDisabled($read_only);
255
256 if ($read_only) {
257 $action = $this->ctrl->getFormAction($this, 'permDenied');
258 } else {
259 $action = $this->ctrl->getFormAction($this, 'update');
260 }
261
262 return $this->factory->input()->container()->form()->standard(
263 $action,
264 [
265 'section' => $section,
266 'text_section' => $text_section,
267 'index_section' => $index_section
268 ]
269 );
270 }
271
272 protected function getSettings(): ilSearchSettings
273 {
275 }
276}
$datetime
renderer()
factory()
Provides fluid interface to RBAC services.
Definition: UIServices.php:25
renderer()
Get a renderer for UI components.
Definition: UIServices.php:44
mainTemplate()
Get the ILIAS main template.
Definition: UIServices.php:54
factory()
Get the factory that crafts UI components.
Definition: UIServices.php:36
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
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, ServerReadmeHelper $readme_helper)
User class.
static getLuceneMimeFilterDefinitions()
Interface GlobalHttpState.
This describes a standard form.
Definition: Standard.php:30
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...
static http()
Fetches the global http state from ILIAS.