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