ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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
122 $settings->enableLuceneUserSearch((bool) $data['user_search_enabled']);
123 $settings->setFragmentCount((int) $data['fragmentCount']);
124 $settings->setFragmentSize((int) $data['fragmentSize']);
125 $settings->enableLuceneMimeFilter(!is_null($data['mime']));
126 if (!is_null($data['mime'])) {
127 $settings->setLuceneMimeFilter((array) $data['mime']);
128 }
129 $settings->enablePrefixWildcardQuery((bool) $data['prefix']);
130 $settings->setLastIndexTime(new ilDateTime(
131 $data['last_index']->getTimestamp(),
133 ));
134 $settings->update();
135
136 // refresh lucene server
137 try {
138 if ($settings->enabledLucene()) {
139 $this->coordinator->refreshLuceneSettings();
140 }
141 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
142 $this->ctrl->redirect($this, 'edit');
143 } catch (Exception $exception) {
144 $this->tpl->setOnScreenMessage('failure', $exception->getMessage());
145 $this->showForm(false);
146 }
147 }
148
149 protected function showPermissionDenied(): void
150 {
151 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
152 $this->ctrl->redirect($this, 'readOnly');
153 }
154
155 protected function initForm(bool $read_only): StandardForm
156 {
157 $settings = $this->getSettings();
158 $field_factory = $this->factory->input()->field();
159
160 // User search
161 $user_search = $field_factory->checkbox(
162 $this->lng->txt('search_user_search_form'),
163 $this->lng->txt('search_user_search_info_form')
164 )->withValue($settings->isLuceneUserSearchEnabled());
165
166 // Item filter
167 $filter = $settings->getLuceneMimeFilter();
168 $checks = [];
169 foreach (ilSearchSettings::getLuceneMimeFilterDefinitions() as $mime => $def) {
170 $checks[$mime] = $field_factory->checkbox(
171 $this->lng->txt($def['trans'])
172 )->withValue(isset($filter[$mime]) && $filter[$mime]);
173 }
174
175 $item_filter = $field_factory->optionalGroup(
176 $checks,
177 $this->lng->txt('search_mime_filter_form'),
178 $this->lng->txt('search_mime_filter_form_info')
179 );
180 if (!$settings->isLuceneMimeFilterEnabled()) {
181 $item_filter = $item_filter->withValue(null);
182 }
183
184 // Prefix
185 $prefix = $field_factory->checkbox(
186 $this->lng->txt('lucene_prefix_wildcard'),
187 $this->lng->txt('lucene_prefix_wildcard_info')
188 )->withValue($settings->isPrefixWildcardQueryEnabled());
189
190 // Number of fragments
191 $frag_count = $field_factory->numeric(
192 $this->lng->txt('lucene_num_fragments'),
193 $this->lng->txt('lucene_num_frag_info')
194 )->withValue($settings->getFragmentCount())
195 ->withRequired(true)
196 ->withAdditionalTransformation(
197 $this->refinery->int()->isLessThanOrEqual(10)
198 )->withAdditionalTransformation(
199 $this->refinery->int()->isGreaterThanOrEqual(1)
200 );
201
202 // Size of fragments
203 $frag_size = $field_factory->numeric(
204 $this->lng->txt('lucene_size_fragments'),
205 $this->lng->txt('lucene_size_frag_info')
206 )->withValue($settings->getFragmentSize())
207 ->withRequired(true)
208 ->withAdditionalTransformation(
209 $this->refinery->int()->isLessThanOrEqual(1000)
210 )->withAdditionalTransformation(
211 $this->refinery->int()->isGreaterThanOrEqual(10)
212 );
213
214 // Last Index
215 $timezone = $this->user->getTimeZone();
216 $datetime = new DateTime(
217 '@' . $settings->getLastIndexTime()->get(IL_CAL_UNIX)
218 );
219 $datetime->setTimezone(new DateTimeZone($timezone));
220 $last_index = $field_factory->dateTime(
221 $this->lng->txt('lucene_last_index_time'),
222 $this->lng->txt('lucene_last_index_time_info')
223 )->withRequired(true)
224 ->withUseTime(true)
225 ->withTimezone($timezone);
226 $last_index = $last_index->withValue(
227 $datetime->format($last_index->getFormat()->toString() . ' H:i')
228 );
229
233 $section = $this->factory->input()->field()->section(
234 [
235 'user_search_enabled' => $user_search,
236 'mime' => $item_filter,
237 'prefix' => $prefix,
238 'fragmentCount' => $frag_count,
239 'fragmentSize' => $frag_size,
240 'last_index' => $last_index
241 ],
242 $this->lng->txt('lucene_settings_title')
243 )->withDisabled($read_only);
244
245 if ($read_only) {
246 $action = $this->ctrl->getFormAction($this, 'permDenied');
247 } else {
248 $action = $this->ctrl->getFormAction($this, 'update');
249 }
250
251 return $this->factory->input()->container()->form()->standard(
252 $action,
253 ['section' => $section]
254 );
255 }
256
257 protected function getSettings(): ilSearchSettings
258 {
260 }
261}
$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.