ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLuceneSearchGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 include_once './Services/Search/classes/class.ilSearchSettings.php';
25 include_once './Services/Search/classes/class.ilSearchBaseGUI.php';
26 include_once './Services/Search/classes/Lucene/class.ilLuceneAdvancedSearchFields.php';
27 include_once './Services/PersonalDesktop/interfaces/interface.ilDesktopItemHandling.php';
28 include_once './Services/Administration/interfaces/interface.ilAdministrationCommandHandling.php';
29 
45 {
46  protected $ilTabs;
47 
51  public function __construct()
52  {
53  global $ilTabs;
54 
55  $this->tabs_gui = $ilTabs;
58  $this->initUserSearchCache();
59 
60  }
61 
65  public function executeCommand()
66  {
67  global $ilBench, $ilCtrl;
68 
69  $ilBench->start('Lucene','0900_executeCommand');
70  $next_class = $this->ctrl->getNextClass($this);
71  $cmd = $this->ctrl->getCmd();
72 
73  $this->prepareOutput();
74  switch($next_class)
75  {
76  case "ilpropertyformgui":
77  /*$this->initStandardSearchForm(ilSearchBaseGUI::SEARCH_FORM_LUCENE);
78  $ilCtrl->setReturn($this, 'storeRoot');
79  $ilCtrl->forwardCommand($this->form);*/
80  $form = $this->getSearchAreaForm();
81  $ilCtrl->setReturn($this, 'storeRoot');
82  $ilCtrl->forwardCommand($form);
83  break;
84 
85  case 'ilobjectcopygui':
86  include_once './Services/Object/classes/class.ilObjectCopyGUI.php';
87  $cp = new ilObjectCopyGUI($this);
88  $this->ctrl->forwardCommand($cp);
89  break;
90 
91  default:
93  if(!$cmd)
94  {
95  $cmd = "showSavedResults";
96  }
97  $this->handleCommand($cmd);
98  break;
99  }
100  $ilBench->stop('Lucene','0900_executeCommand');
101  return true;
102  }
103 
107  public function prepareOutput()
108  {
110  $this->getTabs();
111  return true;
112 
113  global $ilAccess, $ilSetting;
114  global $ilUser;
115 
116  if($_SESSION['il_cont_admin_panel'])
117  {
118  $GLOBALS["tpl"]->setAdminViewButton(
119  $this->ctrl->getLinkTarget($this, "disableAdministrationPanel"),
120  $this->lng->txt("basic_commands"));
121 
122  $GLOBALS["tpl"]->addAdminPanelCommand("delete",
123  $this->lng->txt("delete_selected_items"));
124 
125  if(!$_SESSION["clipboard"])
126  {
127  $GLOBALS["tpl"]->addAdminPanelCommand("cut",
128  $this->lng->txt("move_selected_items"));
129 
130  $GLOBALS["tpl"]->addAdminPanelCommand("link",
131  $this->lng->txt("link_selected_items"));
132  }
133  else
134  {
135  $GLOBALS["tpl"]->addAdminPanelCommand("paste",
136  $this->lng->txt("paste_clipboard_items"));
137  $GLOBALS["tpl"]->addAdminPanelCommand("clear",
138  $this->lng->txt("clear_clipboard"));
139  }
140  }
141  elseif($ilUser->getId() != ANONYMOUS_USER_ID)
142  {
143  $GLOBALS["tpl"]->setAdminViewButton(
144  $this->ctrl->getLinkTarget($this, "enableAdministrationPanel"),
145  $this->lng->txt("all_commands"));
146  }
147 
148  $this->ctrl->setParameter($this, "type", "");
149  $this->ctrl->setParameter($this, "item_ref_id", "");
150  $GLOBALS["tpl"]->setPageFormAction($this->ctrl->getFormAction($this));
151 
152  }
153 
159  protected function getType()
160  {
161  if(count($this->search_cache))
162  {
164  }
166  }
167 
173  protected function getDetails()
174  {
175  return (array) $this->search_cache->getItemFilter();
176  }
177 
181  protected function remoteSearch()
182  {
183  $_POST['query'] = $_POST['queryString'];
184  $this->search_cache->setRoot((int) $_POST['root_id']);
185  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['queryString']));
186  $this->search_cache->save();
187 
188  $this->search();
189  }
190 
195  protected function showSavedResults()
196  {
197  global $ilUser,$ilBench;
198 
199  if(!strlen($this->search_cache->getQuery()))
200  {
201  $this->showSearchForm();
202  return false;
203  }
204 
205  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
206  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
207  $qp = new ilLuceneQueryParser($this->search_cache->getQuery());
208  $qp->parse();
209  $searcher = ilLuceneSearcher::getInstance($qp);
210  $searcher->search();
211 
212  // Load saved results
213  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
214  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
215  $filter->loadFromDb();
216 
217  // Highlight
218  $searcher->highlight($filter->getResultObjIds());
219 
220  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
221  $presentation = new ilSearchResultPresentation($this);
222  $presentation->setResults($filter->getResultIds());
223 
224  $presentation->setSearcher($searcher);
225 
226  // TODO: other handling required
227  $this->addPager($filter,'max_page');
228 
229  $presentation->setPreviousNext($this->prev_link, $this->next_link);
230 
231  $this->showSearchForm();
232 
233  if($presentation->render())
234  {
235  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
236  }
237  elseif(strlen($this->search_cache->getQuery()))
238  {
239  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'),$qp->getQuery()));
240  }
241  }
242 
247  protected function search()
248  {
249  if(!$this->form->checkInput())
250  {
251  $this->search_cache->deleteCachedEntries();
252  // Reset details
253  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
255  $this->showSearchForm();
256  return false;
257  }
258 
259  unset($_SESSION['max_page']);
260  $this->search_cache->deleteCachedEntries();
261 
262  // Reset details
263  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
265 
266  $this->performSearch();
267  }
268 
272  protected function performSearch()
273  {
274  global $ilUser,$ilBench;
275 
276  unset($_SESSION['vis_references']);
277 
278  $filter_query = '';
279  if($this->search_cache->getItemFilter() and ilSearchSettings::getInstance()->isLuceneItemFilterEnabled())
280  {
281  $filter_settings = ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions();
282  foreach((array) $this->search_cache->getItemFilter() as $obj => $value)
283  {
284  if(!$filter_query)
285  {
286  $filter_query .= '+( ';
287  }
288  else
289  {
290  $filter_query .= 'OR';
291  }
292  $filter_query .= (' '. (string) $filter_settings[$obj]['filter'].' ');
293  }
294  $filter_query .= ') ';
295  }
296  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
297  include_once './Services/Search/classes/Lucene/class.ilLuceneQueryParser.php';
298  $qp = new ilLuceneQueryParser($filter_query.' +('.$this->search_cache->getQuery().')');
299  $qp->parse();
300  $searcher = ilLuceneSearcher::getInstance($qp);
301  $searcher->search();
302 
303  // Filter results
304  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
305  include_once './Services/Search/classes/Lucene/class.ilLucenePathFilter.php';
306  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
307  $filter->addFilter(new ilLucenePathFilter($this->search_cache->getRoot()));
308  $filter->setCandidates($searcher->getResult());
309  $filter->filter();
310 
311  if($filter->getResultObjIds()) {
312  $searcher->highlight($filter->getResultObjIds());
313  }
314 
315  // Show results
316  $this->showSearchForm();
317 
318  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
319  $presentation = new ilSearchResultPresentation($this);
320  $presentation->setResults($filter->getResultIds());
321  $presentation->setSearcher($searcher);
322 
323  // TODO: other handling required
324  $ilBench->start('Lucene','1500_fo');
325  $this->addPager($filter,'max_page');
326  $ilBench->stop('Lucene','1500_fo');
327 
328  $presentation->setPreviousNext($this->prev_link, $this->next_link);
329 
330  if($presentation->render())
331  {
332  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
333  }
334  else
335  {
336  ilUtil::sendInfo(sprintf($this->lng->txt('search_no_match_hint'),$this->search_cache->getQuery()));
337  }
338 
339  if($filter->getResultIds())
340  {
341  #$this->fillAdminPanel();
342  }
343  }
344 
345 
349  protected function storeRoot()
350  {
351  $form = $this->getSearchAreaForm();
352 
353  $this->root_node = $form->getItemByPostVar('area')->getValue();
354  $this->search_cache->setRoot($this->root_node);
355  $this->search_cache->save();
356  $this->search_cache->deleteCachedEntries();
357 
358  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
360 
361  $this->performSearch();
362  }
363 
367  protected function getTabs()
368  {
369  if(ilSearchSettings::getInstance()->getHideAdvancedSearch())
370  {
371  return false;
372  }
373 
374  $this->tabs_gui->addTarget('search',$this->ctrl->getLinkTarget($this));
375  if($this->fields->getActiveFields())
376  {
377  $this->tabs_gui->addTarget('search_advanced',$this->ctrl->getLinkTargetByClass('illuceneAdvancedSearchgui'));
378  }
379 
380  $this->tabs_gui->setTabActive('search');
381 
382  }
383 
390  protected function initUserSearchCache()
391  {
392  global $ilUser;
393 
394  include_once('Services/Search/classes/class.ilUserSearchCache.php');
395  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
396  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_DEFAULT);
397  if((int) $_GET['page_number'])
398  {
399  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
400  }
401  if(isset($_POST['term']))
402  {
403  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['term']));
404  if($_POST['item_filter_enabled'])
405  {
406  $filtered = array();
407  foreach(ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions() as $type => $data)
408  {
409  if($_POST['filter_type'][$type])
410  {
411  $filtered[$type] = 1;
412  }
413  }
414  $this->search_cache->setItemFilter($filtered);
415  }
416  else
417  {
418  // @todo: keep item filter settings
419  $this->search_cache->setItemFilter(array());
420  }
421  }
422  }
423 
429  protected function fillAdminPanel()
430  {
431  global $lng;
432 
433  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
434 
435  // admin panel commands
436  if ((count($this->admin_panel_commands) > 0))
437  {
438  foreach($this->admin_panel_commands as $cmd)
439  {
440  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
441  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
442  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
443  $this->tpl->parseCurrentBlock();
444  }
445 
446  $adm_cmds = true;
447  }
448  if ($adm_cmds)
449  {
450  $this->tpl->setCurrentBlock("lucene_adm_view_components");
451  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("arrow_upright.png"));
452  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $lng->txt("actions"));
453  $this->tpl->parseCurrentBlock();
454  $adm_view_cmp = true;
455  }
456 
457  // admin view button
458  if (is_array($this->admin_view_button))
459  {
460  if (is_array($this->admin_view_button))
461  {
462  $this->tpl->setCurrentBlock("lucene_admin_button");
463  $this->tpl->setVariable("LUCENE_ADMIN_MODE_LINK",
464  $this->admin_view_button["link"]);
465  $this->tpl->setVariable("LUCENE_TXT_ADMIN_MODE",
466  $this->admin_view_button["txt"]);
467  $this->tpl->parseCurrentBlock();
468  }
469  $this->tpl->setCurrentBlock("lucene_admin_view");
470  $this->tpl->parseCurrentBlock();
471  $adm_view = true;
472  }
473 
474  // creation selector
475  if (is_array($this->creation_selector))
476  {
477  $this->tpl->setCurrentBlock("lucene_add_commands");
478  if ($adm_cmds)
479  {
480  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
481  }
482  $this->tpl->setVariable("LUCENE_SELECT_OBJTYPE_REPOS",
483  $this->creation_selector["options"]);
484  $this->tpl->setVariable("LUCENE_BTN_NAME_REPOS",
485  $this->creation_selector["command"]);
486  $this->tpl->setVariable("LUCENE_TXT_ADD_REPOS",
487  $this->creation_selector["txt"]);
488  $this->tpl->parseCurrentBlock();
489  $creation_selector = true;
490  }
491  if ($adm_view || $creation_selector)
492  {
493  $this->tpl->setCurrentBlock("lucene_adm_panel");
494  if ($adm_view_cmp)
495  {
496  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
497  }
498  $this->tpl->parseCurrentBlock();
499  }
500  }
501 
505  protected function addAdminPanelCommand($a_cmd, $a_txt)
506  {
507  $this->admin_panel_commands[] =
508  array("cmd" => $a_cmd, "txt" => $a_txt);
509  }
510 
514  protected function setAdminViewButton($a_link, $a_txt)
515  {
516  $this->admin_view_button =
517  array("link" => $a_link, "txt" => $a_txt);
518  }
519 
520  protected function setPageFormAction($a_action)
521  {
522  $this->page_form_action = $a_action;
523  }
524 
529  protected function showSearchForm()
530  {
531  global $ilCtrl, $lng;
532 
533  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.lucene_search.html','Services/Search');
534 
535  // include js needed
536  include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
538  $this->tpl->addJavascript("./Services/Search/js/Search.js");
539 
540  $this->tpl->setVariable("FORM_ACTION", $ilCtrl->getFormAction($this,'performSearch'));
541  $this->tpl->setVariable("TERM", ilUtil::prepareFormOutput($this->search_cache->getQuery()));
542  $this->tpl->setVariable("TXT_SEARCH", $lng->txt("search"));
543  $this->tpl->setVariable("TXT_OPTIONS", $lng->txt("options"));
544  $this->tpl->setVariable("ARR_IMG", ilUtil::img(ilUtil::getImagePath("mm_down_arrow_dark.png")));
545  $this->tpl->setVariable("TXT_COMBINATION", $lng->txt("search_term_combination"));
546  $this->tpl->setVariable('TXT_COMBINATION_DEFAULT', ilSearchSettings::getInstance()->getDefaultOperator() == ilSearchSettings::OPERATOR_AND ? $lng->txt('search_all_words') : $lng->txt('search_any_word'));
547  $this->tpl->setVariable("TXT_AREA", $lng->txt("search_area"));
548 
549  if (ilSearchSettings::getInstance()->isLuceneItemFilterEnabled())
550  {
551  $this->tpl->setCurrentBlock("type_sel");
552  $this->tpl->setVariable('TXT_TYPE_DEFAULT',$lng->txt("search_off"));
553  $this->tpl->setVariable("ARR_IMGT", ilUtil::img(ilUtil::getImagePath("mm_down_arrow_dark.png")));
554  $this->tpl->setVariable("TXT_FILTER_BY_TYPE", $lng->txt("search_filter_by_type"));
555  $this->tpl->setVariable('FORM',$this->form->getHTML());
556  $this->tpl->parseCurrentBlock();
557  }
558 
559  // search area form
560  $this->tpl->setVariable('SEARCH_AREA_FORM', $this->getSearchAreaForm()->getHTML());
561  $this->tpl->setVariable("TXT_CHANGE", $lng->txt("change"));
562 
563  return true;
564  }
565 }
566 ?>