ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilObjSearchLuceneSettingsFormGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
28 
33 {
36  protected ilLanguage $lng;
38  protected Factory $factory;
39  protected Renderer $renderer;
40  protected RefFactory $refinery;
41  protected ilObjUser $user;
42 
44 
45  public function __construct(
46  GlobalHttpState $http,
47  ilCtrlInterface $ctrl,
48  ilLanguage $lng,
49  UIServices $ui,
50  RefFactory $refinery,
51  ilObjUser $user,
53  ) {
54  $this->http = $http;
55  $this->ctrl = $ctrl;
56  $this->lng = $lng;
57  $this->tpl = $ui->mainTemplate();
58  $this->factory = $ui->factory();
59  $this->renderer = $ui->renderer();
60  $this->refinery = $refinery;
61  $this->user = $user;
62  $this->coordinator = $coordinator;
63  }
64 
65  public function executeCommand(): void
66  {
67  $cmd = $this->ctrl->getCmd();
68 
69  switch ($cmd) {
70  case 'readOnly':
71  $this->showForm(true);
72  break;
73 
74  case 'edit':
75  $this->showForm(false);
76  break;
77 
78  case 'permDenied':
79  $this->showPermissionDenied();
80  break;
81 
82  case 'update':
83  $this->update();
84  break;
85 
86  default:
88  'Invalid command for ilObjSearchLuceneSettingsFormGUI: ' . $cmd
89  );
90  }
91  }
92 
93  protected function showForm(
94  bool $read_only,
95  bool $get_from_post = false
96  ): void {
97  $form = $this->initForm($read_only);
98  if ($get_from_post) {
99  $form = $form->withRequest($this->http->request());
100  }
101  $this->tpl->setContent($this->renderer->render($form));
102  }
103 
104  protected function update(): void
105  {
106  $form = $this->initForm(false)
107  ->withRequest($this->http->request());
108 
109  if (!$form->getData()) {
110  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'));
111  $this->showForm(false, true);
112  return;
113  }
114 
115  $settings = $this->getSettings();
116  $data = $form->getData()['section'];
117 
118  $settings->enableLuceneUserSearch((bool) $data['user_search_enabled']);
119  $settings->setFragmentCount((int) $data['fragmentCount']);
120  $settings->setFragmentSize((int) $data['fragmentSize']);
121  $settings->setMaxSubitems((int) $data['maxSubitems']);
122  $settings->enableLuceneMimeFilter(!is_null($data['mime']));
123  if (!is_null($data['mime'])) {
124  $settings->setLuceneMimeFilter((array) $data['mime']);
125  }
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  $this->ctrl->redirect($this, 'edit');
140  } catch (Exception $exception) {
141  $this->tpl->setOnScreenMessage('failure', $exception->getMessage());
142  $this->showForm(false);
143  }
144  }
145 
146  protected function showPermissionDenied(): void
147  {
148  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('permission_denied'), true);
149  $this->ctrl->redirect($this, 'readOnly');
150  }
151 
152  protected function initForm(bool $read_only): StandardForm
153  {
154  $settings = $this->getSettings();
155  $field_factory = $this->factory->input()->field();
156 
157  // User search
158  $user_search = $field_factory->checkbox(
159  $this->lng->txt('search_user_search_form'),
160  $this->lng->txt('search_user_search_info_form')
161  )->withValue($settings->isLuceneUserSearchEnabled());
162 
163  // Item filter
164  $filter = $settings->getLuceneMimeFilter();
165  $checks = [];
166  foreach (ilSearchSettings::getLuceneMimeFilterDefinitions() as $mime => $def) {
167  $checks[$mime] = $field_factory->checkbox(
168  $this->lng->txt($def['trans'])
169  )->withValue(isset($filter[$mime]) && $filter[$mime]);
170  }
171 
172  $item_filter = $field_factory->optionalGroup(
173  $checks,
174  $this->lng->txt('search_mime_filter_form'),
175  $this->lng->txt('search_mime_filter_form_info')
176  );
177  if (!$settings->isLuceneMimeFilterEnabled()) {
178  $item_filter = $item_filter->withValue(null);
179  }
180 
181  // Prefix
182  $prefix = $field_factory->checkbox(
183  $this->lng->txt('lucene_prefix_wildcard'),
184  $this->lng->txt('lucene_prefix_wildcard_info')
185  )->withValue($settings->isPrefixWildcardQueryEnabled());
186 
187  // Number of fragments
188  $frag_count = $field_factory->numeric(
189  $this->lng->txt('lucene_num_fragments'),
190  $this->lng->txt('lucene_num_frag_info')
191  )->withValue($settings->getFragmentCount())
192  ->withRequired(true)
193  ->withAdditionalTransformation(
194  $this->refinery->int()->isLessThanOrEqual(10)
196  $this->refinery->int()->isGreaterThanOrEqual(1)
197  );
198 
199  // Size of fragments
200  $frag_size = $field_factory->numeric(
201  $this->lng->txt('lucene_size_fragments'),
202  $this->lng->txt('lucene_size_frag_info')
203  )->withValue($settings->getFragmentSize())
204  ->withRequired(true)
205  ->withAdditionalTransformation(
206  $this->refinery->int()->isLessThanOrEqual(1000)
208  $this->refinery->int()->isGreaterThanOrEqual(10)
209  );
210 
211  // Number of sub-items
212  $max_sub = $field_factory->numeric(
213  $this->lng->txt('lucene_max_sub'),
214  $this->lng->txt('lucene_max_sub_info')
215  )->withValue($settings->getMaxSubitems())
216  ->withRequired(true)
217  ->withAdditionalTransformation(
218  $this->refinery->int()->isLessThanOrEqual(10)
220  $this->refinery->int()->isGreaterThanOrEqual(1)
221  );
222 
223  // Last Index
224  $timezone = $this->user->getTimeZone();
225  $datetime = new DateTime(
226  '@' . $settings->getLastIndexTime()->get(IL_CAL_UNIX)
227  );
228  $datetime->setTimezone(new DateTimeZone($timezone));
229  $last_index = $field_factory->dateTime(
230  $this->lng->txt('lucene_last_index_time'),
231  $this->lng->txt('lucene_last_index_time_info')
232  )->withRequired(true)
233  ->withUseTime(true)
234  ->withTimezone($timezone);
235  $last_index = $last_index->withValue(
236  $datetime->format($last_index->getFormat()->toString() . ' H:i')
237  );
238 
242  $section = $this->factory->input()->field()->section(
243  [
244  'user_search_enabled' => $user_search,
245  'mime' => $item_filter,
246  'prefix' => $prefix,
247  'fragmentCount' => $frag_count,
248  'fragmentSize' => $frag_size,
249  'maxSubitems' => $max_sub,
250  'last_index' => $last_index
251  ],
252  $this->lng->txt('lucene_settings_title')
253  )->withDisabled($read_only);
254 
255  if ($read_only) {
256  $action = $this->ctrl->getFormAction($this, 'permDenied');
257  } else {
258  $action = $this->ctrl->getFormAction($this, 'update');
259  }
260 
261  return $this->factory->input()->container()->form()->standard(
262  $action,
263  ['section' => $section]
264  );
265  }
266 
267  protected function getSettings(): ilSearchSettings
268  {
270  }
271 }
$datetime
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const IL_CAL_UNIX
Provides fluid interface to RBAC services.
Definition: UIServices.php:23
static http()
Fetches the global http state from ILIAS.
This is how the factory for UI elements looks.
Definition: Factory.php:37
renderer()
Get a renderer for UI components.
Definition: UIServices.php:43
showForm(bool $read_only, bool $get_from_post=false)
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
factory()
Get the factory that crafts UI components.
Definition: UIServices.php:35
__construct(GlobalHttpState $http, ilCtrlInterface $ctrl, ilLanguage $lng, UIServices $ui, RefFactory $refinery, ilObjUser $user, ilObjSearchRpcClientCoordinator $coordinator)
mainTemplate()
Get the ILIAS main template.
Definition: UIServices.php:53
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...