ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilLuceneAdvancedSearchGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
36 {
37  protected ilTabsGUI $tabs_gui;
38  protected ilHelpGUI $help;
39 
41 
42  protected ?array $admin_panel_commands;
43  protected ?array $admin_view_button;
44  protected ?array $creation_selector;
45  protected ?string $page_form_action;
46 
47 
51  public function __construct()
52  {
53  global $DIC;
54 
55  $this->tabs_gui = $DIC->tabs();
56  $this->help = $DIC['ilHelp'];
58 
60  $this->initUserSearchCache();
61  }
62 
66  public function executeCommand(): void
67  {
68  $next_class = $this->ctrl->getNextClass($this);
69  $cmd = $this->ctrl->getCmd();
70 
71  $this->prepareOutput();
72  switch ($next_class) {
73  case 'ilobjectcopygui':
74  $this->ctrl->setReturn($this);
75  $cp = new ilObjectCopyGUI($this);
76  $this->ctrl->forwardCommand($cp);
77  break;
78 
79 
80  default:
81  if (!$cmd) {
82  $cmd = "showSavedResults";
83  }
84  $this->handleCommand($cmd);
85  break;
86  }
87  }
88 
89 
93  public function showSavedResults(): void
94  {
95  $qp = new ilLuceneAdvancedQueryParser($this->search_cache->getQuery());
96  $qp->parse();
97  $searcher = ilLuceneSearcher::getInstance($qp);
98  $searcher->search();
99 
100  // Load saved results
101  $filter = ilLuceneSearchResultFilter::getInstance($this->user->getId());
102  $filter->loadFromDb();
103 
104  // Highlight
105  if ($filter->getResultObjIds()) {
106  $searcher->highlight($filter->getResultObjIds());
107  }
108 
109  $this->tpl->addBlockFile('ADM_CONTENT', 'adm_content', 'tpl.lucene_adv_search.html', 'components/ILIAS/Search');
110  $presentation = new ilSearchResultPresentation($this);
111  $presentation->setResults($filter->getResultIds());
112  $presentation->setSearcher($searcher);
113 
114 
115  // TODO: other handling required
116  $this->addPager($filter, 'max_page');
117  $presentation->setPreviousNext($this->prev_link, $this->next_link);
118 
119  if ($presentation->render()) {
120  $this->tpl->setVariable('SEARCH_RESULTS', $presentation->getHTML());
121  } elseif (strlen(trim($qp->getQuery()))) {
122  $this->tpl->setOnScreenMessage('info', $this->lng->txt('search_no_match'));
123  }
124 
125  // and finally add search form
126  $this->initFormSearch();
127  $this->tpl->setVariable('SEARCH_TABLE', $this->form->getHTML());
128 
129  if ($filter->getResultIds()) {
130  $this->fillAdminPanel();
131  }
132  }
133 
137  protected function initFormSearch(): void
138  {
139  $this->form = new ilPropertyFormGUI();
140  $this->form->setFormAction($this->ctrl->getFormAction($this, 'search'));
141  $this->form->setTitle($this->lng->txt('search_advanced'));
142  $this->form->addCommandButton('search', $this->lng->txt('search'));
143  $this->form->addCommandButton('reset', $this->lng->txt('reset'));
144 
145  foreach ($this->fields->getActiveSections() as $definition) {
146  if ($definition['name'] != 'default') {
147  $section = new ilFormSectionHeaderGUI();
148  $section->setTitle($definition['name']);
149  $this->form->addItem($section);
150  }
151 
152  foreach ($definition['fields'] as $field_name) {
153  if (is_object($element = $this->fields->getFormElement($this->search_cache->getQuery(), $field_name, $this->form))) {
154  $this->form->addItem($element);
155  }
156  }
157  }
158  }
159 
163  protected function remoteSearch(): void
164  {
165  $root_id = 0;
166  if ($this->http->wrapper()->post()->has('root_id')) {
167  $root_id = $this->http->wrapper()->post()->retrieve(
168  'root_id',
169  $this->refinery->kindlyTo()->int()
170  );
171  }
172  $queryString = '';
173  if ($this->http->wrapper()->post()->has('queryString')) {
174  $queryString = $this->http->wrapper()->post()->retrieve(
175  'queryString',
176  $this->refinery->kindlyTo()->string()
177  );
178  }
179  $this->search_cache->setRoot($root_id);
180  $this->search_cache->setQuery(['lom_content' => $queryString]);
181  $this->search_cache->save();
182  $this->search();
183  }
184 
185  protected function search(): void
186  {
187  if (!is_array($this->search_cache->getQuery())) {
188  // TOD: handle empty advances search
189  $this->tpl->setOnScreenMessage('info', $this->lng->txt('msg_no_search_string'));
190  $this->showSavedResults();
191  return;
192  }
193  ilSession::clear('max_page');
194  $this->search_cache->deleteCachedEntries();
195 
196  // Reset details
198 
199  $this->performSearch();
200  }
201 
205  protected function reset(): void
206  {
207  $this->search_cache->setQuery(array());
208  $this->search_cache->save();
209  $this->showSavedResults();
210  }
211 
215  protected function performSearch(): void
216  {
217  ilSession::clear('vis_references');
218  $qp = new ilLuceneAdvancedQueryParser($this->search_cache->getQuery());
219  $qp->parse();
220  if (!strlen(trim($qp->getQuery()))) {
221  $this->tpl->setOnScreenMessage('info', $this->lng->txt('msg_no_search_string'));
222  $this->showSavedResults();
223  return;
224  }
225 
226  $searcher = ilLuceneSearcher::getInstance($qp);
227  $searcher->search();
228 
229  // Filter results
230  $filter = ilLuceneSearchResultFilter::getInstance($this->user->getId());
231  $filter->addFilter(new ilLucenePathFilter($this->search_cache->getRoot()));
232  $filter->setCandidates($searcher->getResult());
233  $filter->filter();
234 
235  if ($filter->getResultObjIds()) {
236  $searcher->highlight($filter->getResultObjIds());
237  }
238 
239  // Show results
240  $this->tpl->addBlockFile('ADM_CONTENT', 'adm_content', 'tpl.lucene_adv_search.html', 'components/ILIAS/Search');
241  $presentation = new ilSearchResultPresentation($this);
242  $presentation->setResults($filter->getResultIds());
243  $presentation->setSearcher($searcher);
244 
245  // TODO: other handling required
246  $this->addPager($filter, 'max_page');
247  $presentation->setPreviousNext($this->prev_link, $this->next_link);
248 
249  if ($presentation->render()) {
250  $this->tpl->setVariable('SEARCH_RESULTS', $presentation->getHTML());
251  } else {
252  $this->tpl->setOnScreenMessage('info', $this->lng->txt('search_no_match'));
253  }
254 
255  // and finally add search form
256  $this->initFormSearch();
257  $this->tpl->setVariable('SEARCH_TABLE', $this->form->getHTML());
258 
259  if ($filter->getResultIds()) {
260  $this->fillAdminPanel();
261  }
262  }
263 
267  public function prepareOutput(): void
268  {
269  parent::prepareOutput();
270  $this->getTabs();
271  }
272 
276  protected function getTabs(): void
277  {
278  $this->help->setScreenIdComponent("src_luc");
279 
280  $this->tabs_gui->addTarget('search', $this->ctrl->getLinkTargetByClass('illucenesearchgui'));
281 
282  if (ilSearchSettings::getInstance()->isLuceneUserSearchEnabled()) {
283  $this->tabs_gui->addTarget('search_user', $this->ctrl->getLinkTargetByClass('illuceneusersearchgui'));
284  }
285 
286  if (
287  !ilSearchSettings::getInstance()->getHideAdvancedSearch() and
288  $this->fields->getActiveFields()) {
289  $this->tabs_gui->addTarget('search_advanced', $this->ctrl->getLinkTarget($this));
290  }
291 
292  $this->tabs_gui->setTabActive('search_advanced');
293  }
294 
301  protected function initUserSearchCache(): void
302  {
303  $this->search_cache = ilUserSearchCache::_getInstance($this->user->getId());
304  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_ADVANCED);
305  $page_number = $this->initPageNumberFromQuery();
306  if ($page_number) {
307  $this->search_cache->setResultPageNumber($page_number);
308  }
309  if ($this->http->wrapper()->post()->has('query')) {
310  $this->search_cache->setQuery($this->http->request()->getParsedBody()['query'] ?? []);
311  }
312  }
313 
317  protected function fillAdminPanel(): void
318  {
319  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
320 
321  // admin panel commands
322  if (isset($this->admin_panel_commands) && (count((array) $this->admin_panel_commands) > 0)) {
323  foreach ($this->admin_panel_commands as $cmd) {
324  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
325  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
326  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
327  $this->tpl->parseCurrentBlock();
328  }
329 
330  $adm_cmds = true;
331  }
332  if ($adm_cmds) {
333  $this->tpl->setCurrentBlock("lucene_adm_view_components");
334  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("nav/arrow_upright.svg"));
335  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $this->lng->txt("actions"));
336  $this->tpl->parseCurrentBlock();
337  $adm_view_cmp = true;
338  }
339 
340  // admin view button
341  if (isset($this->admin_view_button) && is_array($this->admin_view_button)) {
342  if (is_array($this->admin_view_button)) {
343  $this->tpl->setCurrentBlock("lucene_admin_button");
344  $this->tpl->setVariable(
345  "LUCENE_ADMIN_MODE_LINK",
346  $this->admin_view_button["link"]
347  );
348  $this->tpl->setVariable(
349  "LUCENE_TXT_ADMIN_MODE",
350  $this->admin_view_button["txt"]
351  );
352  $this->tpl->parseCurrentBlock();
353  }
354  $this->tpl->setCurrentBlock("lucene_admin_view");
355  $this->tpl->parseCurrentBlock();
356  $adm_view = true;
357  }
358 
359  // creation selector
360  if (isset($this->creation_selector) && is_array($this->creation_selector)) {
361  $this->tpl->setCurrentBlock("lucene_add_commands");
362  if ($adm_cmds) {
363  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
364  }
365  $this->tpl->setVariable(
366  "LUCENE_SELECT_OBJTYPE_REPOS",
367  $this->creation_selector["options"]
368  );
369  $this->tpl->setVariable(
370  "LUCENE_BTN_NAME_REPOS",
371  $this->creation_selector["command"]
372  );
373  $this->tpl->setVariable(
374  "LUCENE_TXT_ADD_REPOS",
375  $this->creation_selector["txt"]
376  );
377  $this->tpl->parseCurrentBlock();
378  $creation_selector = true;
379  }
380  if ($adm_view || $creation_selector) {
381  $this->tpl->setCurrentBlock("lucene_adm_panel");
382  if ($adm_view_cmp) {
383  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
384  }
385  $this->tpl->parseCurrentBlock();
386  }
387  }
388 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
GUI class for the workflow of copying objects.
ilPropertyFormGUI $form
static _getInstance(int $a_usr_id)
Help GUI class.
static getInstance(ilLuceneQueryParser $qp)
Get singleton instance.
handleCommand(string $a_cmd)
static resetDetails()
As long as static::resetDetails is not possible this method is final.
prepareOutput()
Add admin panel command.
static http()
Fetches the global http state from ILIAS.
Presentation of search results using object list gui.
global $DIC
Definition: shib_login.php:25
static getImagePath(string $image_name, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
initUserSearchCache()
Init user search cache.
form( $class_path, string $cmd, string $submit_caption="")
addPager($result, string $a_session_key)
__construct(Container $dic, ilPlugin $plugin)
static clear(string $a_var)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilLuceneAdvancedSearchFields $fields