ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilObjSearchSettingsFormGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
26 
31 {
34  protected ilLanguage $lng;
36  protected Factory $factory;
37  protected Renderer $renderer;
38 
40 
41  public function __construct(
42  GlobalHttpState $http,
43  ilCtrlInterface $ctrl,
44  ilLanguage $lng,
45  UIServices $ui,
47  ) {
48  $this->http = $http;
49  $this->ctrl = $ctrl;
50  $this->lng = $lng;
51  $this->tpl = $ui->mainTemplate();
52  $this->factory = $ui->factory();
53  $this->renderer = $ui->renderer();
54  $this->coordinator = $coordinator;
55  }
56 
57  public function executeCommand(): void
58  {
59  $cmd = $this->ctrl->getCmd();
60 
61  switch ($cmd) {
62  case 'readOnly':
63  $this->showForm(true);
64  break;
65 
66  case 'edit':
67  $this->showForm(false);
68  break;
69 
70  case 'permDenied':
71  $this->showPermissionDenied();
72  break;
73 
74  case 'update':
75  $this->update();
76  break;
77 
78  default:
80  'Invalid command for ilObjSearchSettingsFormGUI: ' . $cmd
81  );
82  }
83  }
84 
85  protected function showForm(
86  bool $read_only,
87  bool $get_from_post = false
88  ): void {
89  $form = $this->initForm($read_only);
90  if ($get_from_post) {
91  $form = $form->withRequest($this->http->request());
92  }
93  $this->tpl->setContent($this->renderer->render($form));
94  }
95 
96  protected function update(): void
97  {
98  $form = $this->initForm(false)
99  ->withRequest($this->http->request());
100 
101  if (!$form->getData()) {
102  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'));
103  $this->showForm(false, true);
104  return;
105  }
106 
107  $settings = $this->getSettings();
108 
109  $main_data = $form->getData()['section'];
110  $user_data = $form->getData()['user_section'];
111 
112  $settings->setMaxHits((int) $main_data['max_hits']);
113 
114  switch ((int) $main_data['search_type']) {
116  $settings->enableLucene(false);
117  break;
119  $settings->enableLucene(true);
120  break;
121  }
122  $settings->setDefaultOperator((int) $main_data['operator']);
123  $settings->enableLuceneItemFilter(!is_null($main_data['filter']));
124  if (!is_null($main_data['filter'])) {
125  $settings->setLuceneItemFilter((array) $main_data['filter']);
126  }
127  $settings->setHideAdvancedSearch((bool) $main_data['hide_adv_search']);
128  $settings->enableDateFilter((bool) $main_data['cdate']);
129  $settings->setAutoCompleteLength((int) $user_data['auto_complete_length']);
130  $settings->showInactiveUser((bool) $user_data['inactive_user']);
131  $settings->showLimitedUser((bool) $user_data['limited_user']);
132  $settings->update();
133 
134  // refresh lucene server
135  try {
136  if ($settings->enabledLucene()) {
137  $this->coordinator->refreshLuceneSettings();
138  }
139  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
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  // Max hits
159  $values = [];
160  for ($value = 5; $value <= 50; $value += 5) {
161  $values[$value] = $value;
162  }
163  $hits = $field_factory->select(
164  $this->lng->txt('seas_max_hits'),
165  $values,
166  $this->lng->txt('seas_max_hits_info')
167  )->withValue($settings->getMaxHits())
168  ->withRequired(true);
169 
170  // Search type
171  $type = $field_factory->radio(
172  $this->lng->txt('search_type')
173  )->withOption(
175  $this->lng->txt('search_direct'),
176  $this->lng->txt('search_like_info')
177  )->withOption(
179  $this->lng->txt('search_lucene'),
180  $this->lng->txt('java_server_info')
181  )->withRequired(true);
182 
183  if ($settings->enabledLucene()) {
184  $type = $type->withValue((string) ilSearchSettings::LUCENE_SEARCH);
185  } else {
186  $type = $type->withValue((string) ilSearchSettings::LIKE_SEARCH);
187  }
188 
189  // Default operator
190  $operator = $field_factory->radio(
191  $this->lng->txt('lucene_default_operator'),
192  $this->lng->txt('lucene_default_operator_info')
193  )->withOption(
195  $this->lng->txt('lucene_and')
196  )->withOption(
198  $this->lng->txt('lucene_or')
199  )->withRequired(true)
200  ->withValue((string) $settings->getDefaultOperator());
201 
202  // Item filter
203  $filter = $settings->getLuceneItemFilter();
204  $checks = [];
205  foreach (ilSearchSettings::getLuceneItemFilterDefinitions() as $obj => $def) {
206  $checks[$obj] = $field_factory->checkbox(
207  $this->lng->txt($def['trans'])
208  )->withValue(isset($filter[$obj]) && $filter[$obj]);
209  }
210 
211  $item_filter = $field_factory->optionalGroup(
212  $checks,
213  $this->lng->txt('search_item_filter_form'),
214  $this->lng->txt('search_item_filter_form_info')
215  );
216  if (!$settings->isLuceneItemFilterEnabled()) {
217  $item_filter = $item_filter->withValue(null);
218  }
219 
220  // Filter by date
221  $cdate = $field_factory->checkbox(
222  $this->lng->txt('search_cdate_filter'),
223  $this->lng->txt('search_cdate_filter_info')
224  )->withValue($settings->isDateFilterEnabled());
225 
226  // hide advanced search
227  $hide_adv = $field_factory->checkbox(
228  $this->lng->txt('search_hide_adv_search')
229  )->withValue($settings->getHideAdvancedSearch());
230 
231  // number of auto complete entries
232  $options = [
233  5 => 5,
234  10 => 10,
235  20 => 20,
236  30 => 30
237  ];
238  $val = ($settings->getAutoCompleteLength() > 0)
239  ? $settings->getAutoCompleteLength()
240  : 10;
241  $auto_complete = $field_factory->select(
242  $this->lng->txt('search_auto_complete_length'),
243  $options
244  )->withValue($val);
245 
246  // Show inactive users
247  $inactive_user = $field_factory->checkbox(
248  $this->lng->txt('search_show_inactive_user'),
249  $this->lng->txt('search_show_inactive_user_info')
250  )->withValue($settings->isInactiveUserVisible());
251 
252  // Show limited users
253  $limited_user = $field_factory->checkbox(
254  $this->lng->txt('search_show_limited_user'),
255  $this->lng->txt('search_show_limited_user_info')
256  )->withValue($settings->isLimitedUserVisible());
257 
261  $section = $this->factory->input()->field()->section(
262  [
263  'max_hits' => $hits,
264  'search_type' => $type,
265  'operator' => $operator,
266  'hide_adv_search' => $hide_adv,
267  'filter' => $item_filter,
268  'cdate' => $cdate
269  ],
270  $this->lng->txt('seas_settings')
271  )->withDisabled($read_only);
272 
273  $user_section = $this->factory->input()->field()->section(
274  [
275  'auto_complete_length' => $auto_complete,
276  'inactive_user' => $inactive_user,
277  'limited_user' => $limited_user
278  ],
279  $this->lng->txt('user_search_settings_section')
280  )->withDisabled($read_only);
281 
282  if ($read_only) {
283  $action = $this->ctrl->getFormAction($this, 'permDenied');
284  } else {
285  $action = $this->ctrl->getFormAction($this, 'update');
286  }
287 
288  return $this->factory->input()->container()->form()->standard(
289  $action,
290  [
291  'section' => $section,
292  'user_section' => $user_section,
293  ]
294  );
295  }
296 
297  protected function getSettings(): ilSearchSettings
298  {
300  }
301 }
ilObjSearchRpcClientCoordinator $coordinator
static getLuceneItemFilterDefinitions()
Get lucene item filter definitions.
showForm(bool $read_only, bool $get_from_post=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
__construct(GlobalHttpState $http, ilCtrlInterface $ctrl, ilLanguage $lng, UIServices $ui, ilObjSearchRpcClientCoordinator $coordinator)
factory()
Get the factory that crafts UI components.
Definition: UIServices.php:35
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...