ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilObjSearchSettingsFormGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
25 
30 {
33  protected ilLanguage $lng;
35  protected Factory $factory;
36  protected Renderer $renderer;
37 
39 
40  public function __construct(
41  GlobalHttpState $http,
42  ilCtrlInterface $ctrl,
43  ilLanguage $lng,
44  UIServices $ui,
46  ) {
47  $this->http = $http;
48  $this->ctrl = $ctrl;
49  $this->lng = $lng;
50  $this->tpl = $ui->mainTemplate();
51  $this->factory = $ui->factory();
52  $this->renderer = $ui->renderer();
53  $this->coordinator = $coordinator;
54  }
55 
56  public function executeCommand(): void
57  {
58  $cmd = $this->ctrl->getCmd();
59 
60  switch ($cmd) {
61  case 'readOnly':
62  $this->showForm(true);
63  break;
64 
65  case 'edit':
66  $this->showForm(false);
67  break;
68 
69  case 'permDenied':
70  $this->showPermissionDenied();
71  break;
72 
73  case 'update':
74  $this->update();
75  break;
76 
77  default:
79  'Invalid command for ilObjSearchSettingsFormGUI: ' . $cmd
80  );
81  }
82  }
83 
84  protected function showForm(
85  bool $read_only,
86  bool $get_from_post = false
87  ): void {
88  $form = $this->initForm($read_only);
89  if ($get_from_post) {
90  $form = $form->withRequest($this->http->request());
91  }
92  $this->tpl->setContent($this->renderer->render($form));
93  }
94 
95  protected function update(): void
96  {
97  $form = $this->initForm(false)
98  ->withRequest($this->http->request());
99 
100  if (!$form->getData()) {
101  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'));
102  $this->showForm(false, true);
103  return;
104  }
105 
106  $settings = $this->getSettings();
107  $data = $form->getData()['section'];
108 
109  $settings->setMaxHits((int) $data['max_hits']);
110 
111  switch ((int) $data['search_type']) {
113  $settings->enableLucene(false);
114  break;
116  $settings->enableLucene(true);
117  break;
118  }
119  $settings->setDefaultOperator((int) $data['operator']);
120  $settings->enableLuceneItemFilter(!is_null($data['filter']));
121  if (!is_null($data['filter'])) {
122  $settings->setLuceneItemFilter((array) $data['filter']);
123  }
124  $settings->setHideAdvancedSearch((bool) $data['hide_adv_search']);
125  $settings->setAutoCompleteLength((int) $data['auto_complete_length']);
126  $settings->showInactiveUser((bool) $data['inactive_user']);
127  $settings->showLimitedUser((bool) $data['limited_user']);
128  $settings->enableDateFilter((bool) $data['cdate']);
129  $settings->enableLuceneUserSearch((bool) $data['user_search_enabled']);
130  $settings->update();
131 
132  // refresh lucene server
133  try {
134  if ($settings->enabledLucene()) {
135  $this->coordinator->refreshLuceneSettings();
136  }
137  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
138  ilSession::clear('search_last_class');
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  // Max hits
158  $values = [];
159  for ($value = 5; $value <= 50; $value += 5) {
160  $values[$value] = $value;
161  }
162  $hits = $field_factory->select(
163  $this->lng->txt('seas_max_hits'),
164  $values,
165  $this->lng->txt('seas_max_hits_info')
166  )->withValue($settings->getMaxHits())
167  ->withRequired(true);
168 
169  // Search type
170  $type = $field_factory->radio(
171  $this->lng->txt('search_type')
172  )->withOption(
174  $this->lng->txt('search_direct'),
175  $this->lng->txt('search_like_info')
176  )->withOption(
178  $this->lng->txt('search_lucene'),
179  $this->lng->txt('java_server_info')
180  )->withRequired(true);
181 
182  if ($settings->enabledLucene()) {
183  $type = $type->withValue((string) ilSearchSettings::LUCENE_SEARCH);
184  } else {
185  $type = $type->withValue((string) ilSearchSettings::LIKE_SEARCH);
186  }
187 
188  // Default operator
189  $operator = $field_factory->radio(
190  $this->lng->txt('lucene_default_operator'),
191  $this->lng->txt('lucene_default_operator_info')
192  )->withOption(
194  $this->lng->txt('lucene_and')
195  )->withOption(
197  $this->lng->txt('lucene_or')
198  )->withRequired(true)
199  ->withValue((string) $settings->getDefaultOperator());
200 
201  // User search
202  $user_search = $field_factory->checkbox(
203  $this->lng->txt('search_user_search_form'),
204  $this->lng->txt('search_user_search_info_form')
205  )->withValue($settings->isLuceneUserSearchEnabled());
206 
207  // Item filter
208  $filter = $settings->getLuceneItemFilter();
209  $checks = [];
210  foreach (ilSearchSettings::getLuceneItemFilterDefinitions() as $obj => $def) {
211  $checks[$obj] = $field_factory->checkbox(
212  $this->lng->txt($def['trans'])
213  )->withValue(isset($filter[$obj]) && $filter[$obj]);
214  }
215 
216  $item_filter = $field_factory->optionalGroup(
217  $checks,
218  $this->lng->txt('search_item_filter_form'),
219  $this->lng->txt('search_item_filter_form_info')
220  );
221  if (!$settings->isLuceneItemFilterEnabled()) {
222  $item_filter = $item_filter->withValue(null);
223  }
224 
225  // Filter by date
226  $cdate = $field_factory->checkbox(
227  $this->lng->txt('search_cdate_filter'),
228  $this->lng->txt('search_cdate_filter_info')
229  )->withValue($settings->isDateFilterEnabled());
230 
231  // hide advanced search
232  $hide_adv = $field_factory->checkbox(
233  $this->lng->txt('search_hide_adv_search')
234  )->withValue($settings->getHideAdvancedSearch());
235 
236  // number of auto complete entries
237  $options = [
238  5 => 5,
239  10 => 10,
240  20 => 20,
241  30 => 30
242  ];
243  $val = ($settings->getAutoCompleteLength() > 0)
244  ? $settings->getAutoCompleteLength()
245  : 10;
246  $auto_complete = $field_factory->select(
247  $this->lng->txt('search_auto_complete_length'),
248  $options
249  )->withValue($val);
250 
251  // Show inactive users
252  $inactive_user = $field_factory->checkbox(
253  $this->lng->txt('search_show_inactive_user'),
254  $this->lng->txt('search_show_inactive_user_info')
255  )->withValue($settings->isInactiveUserVisible());
256 
257  // Show limited users
258  $limited_user = $field_factory->checkbox(
259  $this->lng->txt('search_show_limited_user'),
260  $this->lng->txt('search_show_limited_user_info')
261  )->withValue($settings->isLimitedUserVisible());
262 
266  $section = $this->factory->input()->field()->section(
267  [
268  'max_hits' => $hits,
269  'search_type' => $type,
270  'operator' => $operator,
271  'user_search_enabled' => $user_search,
272  'filter' => $item_filter,
273  'cdate' => $cdate,
274  'hide_adv_search' => $hide_adv,
275  'auto_complete_length' => $auto_complete,
276  'inactive_user' => $inactive_user,
277  'limited_user' => $limited_user
278  ],
279  $this->lng->txt('seas_settings')
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  ['section' => $section]
291  );
292  }
293 
294  protected function getSettings(): ilSearchSettings
295  {
297  }
298 }
Interface GlobalHttpState.
ilObjSearchRpcClientCoordinator $coordinator
An entity that renders components to a string output.
Definition: Renderer.php:30
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200
static getLuceneItemFilterDefinitions()
Get lucene item filter definitions.
$type
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.
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
static clear(string $a_var)
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...