ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSearchGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Services/Search/classes/class.ilSearchBaseGUI.php';
5 
6 
7 
23 {
24  protected $search_cache = null;
25 
28  var $string;
29  var $type;
30 
31 
36  public function __construct()
37  {
38  global $ilUser, $lng;
39 
40  $lng->loadLanguageModule("search");
41 
42  // put form values into "old" post variables
44  $this->form->checkInput();
45 
46  $new_search = isset($_POST['cmd']['performSearch']) ? true : false;
47 
48  $enabled_types = ilSearchSettings::getInstance()->getEnabledLuceneItemFilterDefinitions();
49  foreach($enabled_types as $type => $pval)
50  {
51  if($_POST['filter_type'][$type] == 1)
52  {
53  $_POST["search"]["details"][$type] = $_POST['filter_type'][$type];
54  }
55  }
56 
57  $_POST["search"]["string"] = $_POST["term"];
58  $_POST["search"]["combination"] = $_POST["combination"];
59  $_POST["search"]["type"] = $_POST["type"];
60  $_SESSION['search_root'] = $_POST["area"];
61 
62  $this->root_node = $_SESSION['search_root'] ? $_SESSION['search_root'] : ROOT_FOLDER_ID;
63  $this->setType($_POST['search']['type'] ? $_POST['search']['type'] : $_SESSION['search']['type']);
64  $this->setCombination($_POST['search']['combination'] ? $_POST['search']['combination'] : $_SESSION['search']['combination']);
65  $this->setString($_POST['search']['string'] ? $_POST['search']['string'] : $_SESSION['search']['string']);
66  #$this->setDetails($_POST['search']['details'] ? $_POST['search']['details'] : $_SESSION['search']['details']);
67  $this->setDetails($new_search ? $_POST['search']['details'] : $_SESSION['search']['details']);
69  }
70 
71 
76  public function executeCommand()
77  {
78  global $rbacsystem, $ilCtrl;
79 
80 
81 
82  $next_class = $this->ctrl->getNextClass($this);
83  $cmd = $this->ctrl->getCmd();
84 
85  switch($next_class)
86  {
87  case "ilpropertyformgui":
88  //$this->initStandardSearchForm(ilSearchBaseGUI::SEARCH_FORM_STANDARD);
89  $form = $this->getSearchAreaForm();
90  $this->prepareOutput();
91  $ilCtrl->setReturn($this, 'storeRoot');
92  return $ilCtrl->forwardCommand($form);
93 
94  case 'ilobjectcopygui':
95  $this->prepareOutput();
96  include_once './Services/Object/classes/class.ilObjectCopyGUI.php';
97  $cp = new ilObjectCopyGUI($this);
98  $this->ctrl->forwardCommand($cp);
99  break;
100 
101  default:
102  $this->initUserSearchCache();
103  if(!$cmd)
104  {
105  $cmd = "showSavedResults";
106  }
107  $this->prepareOutput();
108  $this->handleCommand($cmd);
109  break;
110  }
111  return true;
112  }
113 
118  function setType($a_type)
119  {
120  $_SESSION['search']['type'] = $this->type = $a_type;
121  }
122  function getType()
123  {
124  return $this->type ? $this->type : ilSearchBaseGUI::SEARCH_FAST;
125  }
130  function setCombination($a_combination)
131  {
132  $_SESSION['search']['combination'] = $this->combination = $a_combination;
133  }
134  function getCombination()
135  {
136  return $this->combination ? $this->combination : ilSearchBaseGUI::SEARCH_OR;
137  }
142  function setString($a_str)
143  {
144  $_SESSION['search']['string'] = $this->string = $a_str;
145  }
146  function getString()
147  {
148  return $this->string;
149  }
154  function setDetails($a_details)
155  {
156  $_SESSION['search']['details'] = $this->details = $a_details;
157  }
158  function getDetails()
159  {
160  return $this->details ? $this->details : array();
161  }
162 
163 
164  function getRootNode()
165  {
166  return $this->root_node ? $this->root_node : ROOT_FOLDER_ID;
167  }
168  function setRootNode($a_node_id)
169  {
170  $_SESSION['search_root'] = $this->root_node = $a_node_id;
171  }
172 
173 
174  function remoteSearch()
175  {
176  $this->setString(ilUtil::stripSlashes($_POST['queryString']));
177  $this->setRootNode((int) $_POST['root_id']);
178  $this->performSearch();
179 
180  }
181 
185  protected function storeRoot()
186  {
187  $form = $this->getSearchAreaForm();
188 
189  $this->root_node = $form->getItemByPostVar('area')->getValue();
190  $this->search_cache->setRoot($this->root_node);
191  $this->search_cache->save();
192  $this->search_cache->deleteCachedEntries();
193 
194  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
196 
197  $this->performSearch();
198  }
199 
203  function autoComplete()
204  {
205  $q = $_REQUEST["term"];
206  include_once("./Services/Search/classes/class.ilSearchAutoComplete.php");
207  $list = ilSearchAutoComplete::getList($q);
208  echo $list;
209  exit;
210 
211  }
212 
213  function showSearch()
214  {
215  global $ilLocator, $ilCtrl, $lng;
216 
217  // include js needed
218  include_once("./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php");
220  $this->tpl->addJavascript("./Services/Search/js/Search.js");
221 
222  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.search.html','Services/Search');
223  $this->tpl->setVariable("FORM_ACTION", $ilCtrl->getFormAction($this,'performSearch'));
224  $this->tpl->setVariable("TERM", ilUtil::prepareFormOutput($this->getString()));
225  $this->tpl->setVariable("TXT_SEARCH", $lng->txt("search"));
226  $this->tpl->setVariable("TXT_OPTIONS", $lng->txt("options"));
227  $this->tpl->setVariable("ARR_IMG", ilUtil::img(ilUtil::getImagePath("mm_down_arrow_dark.png")));
228  $this->tpl->setVariable("TXT_COMBINATION", $lng->txt("search_term_combination"));
229  $this->tpl->setVariable('TXT_COMBINATION_DEFAULT', ilSearchSettings::getInstance()->getDefaultOperator() == ilSearchSettings::OPERATOR_AND ? $lng->txt('search_all_words') : $lng->txt('search_any_word'));
230 
231  if (ilSearchSettings::getInstance()->isLuceneItemFilterEnabled())
232  {
233  $this->tpl->setCurrentBlock("type_sel");
234  $this->tpl->setVariable('TXT_TYPE_DEFAULT',$lng->txt("search_fast_info"));
235  $this->tpl->setVariable("TXT_TYPE", $lng->txt("search_type"));
237  $this->tpl->setVariable("ARR_IMGT", ilUtil::img(ilUtil::getImagePath("mm_down_arrow_dark.png")));
238  $this->tpl->setVariable("FORM", $this->form->getHTML());
239  $this->tpl->parseCurrentBlock();
240  }
241 
242  $this->tpl->setVariable("TXT_AREA", $lng->txt("search_area"));
243 
244 
245  // search area form
246  $this->tpl->setVariable('SEARCH_AREA_FORM', $this->getSearchAreaForm()->getHTML());
247 
248  return true;
249  }
250 
251  function showSavedResults()
252  {
253  global $ilUser;
254 
255  // Read old result sets
256  include_once 'Services/Search/classes/class.ilSearchResult.php';
257 
258  $result_obj = new ilSearchResult($ilUser->getId());
259  $result_obj->read();
260  $result_obj->filterResults($this->getRootNode());
261 
262  $this->showSearch();
263 
264  // Show them
265  if(count($result_obj->getResults()))
266  {
267  $this->addPager($result_obj,'max_page');
268 
269  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
271  $presentation->setResults($result_obj->getResultsForPresentation());
272  $presentation->setSubitemIds($result_obj->getSubitemIds());
273  $presentation->setPreviousNext($this->prev_link, $this->next_link);
274  #$presentation->setSearcher($searcher);
275 
276  if($presentation->render())
277  {
278 // $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML());
279  $this->tpl->setVariable('RESULTS_TABLE',$presentation->getHTML(true));
280  }
281  }
282 
283  return true;
284  }
285 
286 
290  function performSearch()
291  {
292  global $ilUser;
293 
294  if(!isset($_GET['page_number']) and $this->search_mode != 'in_results' )
295  {
296  unset($_SESSION['max_page']);
297  $this->search_cache->deleteCachedEntries();
298  }
299 
300  if($this->getType() == ilSearchBaseGUI::SEARCH_DETAILS and !$this->getDetails())
301  {
302  ilUtil::sendInfo($this->lng->txt('search_choose_object_type'));
303  $this->showSearch();
304  return false;
305  }
306 
307  // Step 1: parse query string
308  if(!is_object($query_parser =& $this->__parseQueryString()))
309  {
310  ilUtil::sendInfo($query_parser);
311  $this->showSearch();
312 
313  return false;
314  }
315  // Step 2: perform object search. Get an ObjectSearch object via factory. Depends on fulltext or like search type.
316  $result =& $this->__searchObjects($query_parser);
317 
318  // Step 3: perform meta keyword search. Get an MetaDataSearch object.
319  $result_meta =& $this->__searchMeta($query_parser,'keyword');
320  $result->mergeEntries($result_meta);
321 
322  $result_meta =& $this->__searchMeta($query_parser,'contribute');
323  $result->mergeEntries($result_meta);
324 
325  $result_meta =& $this->__searchMeta($query_parser,'title');
326  $result->mergeEntries($result_meta);
327 
328  $result_meta =& $this->__searchMeta($query_parser,'description');
329  $result->mergeEntries($result_meta);
330 
331  // Perform details search in object specific tables
333  {
334  $result = $this->__performDetailsSearch($query_parser,$result);
335  }
336  // Step 5: Search in results
337  if($this->search_mode == 'in_results')
338  {
339  include_once 'Services/Search/classes/class.ilSearchResult.php';
340 
341  $old_result_obj = new ilSearchResult($ilUser->getId());
342  $old_result_obj->read();
343 
344  $result->diffEntriesFromResult($old_result_obj);
345  }
346 
347 
348  // Step 4: merge and validate results
349  $result->filter($this->getRootNode(),$query_parser->getCombination() == 'and');
350  $result->save();
351  $this->showSearch();
352 
353  if(!count($result->getResults()))
354  {
355  ilUtil::sendInfo($this->lng->txt('search_no_match'));
356  }
357 
358  if($result->isLimitReached())
359  {
360  #$message = sprintf($this->lng->txt('search_limit_reached'),$this->settings->getMaxHits());
361  #ilUtil::sendInfo($message);
362  }
363 
364  // Step 6: show results
365  $this->addPager($result,'max_page');
366 
367  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
369  $presentation->setResults($result->getResultsForPresentation());
370  $presentation->setSubitemIds($result->getSubitemIds());
371  $presentation->setPreviousNext($this->prev_link, $this->next_link);
372 
373  if($presentation->render())
374  {
375 // $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML());
376  $this->tpl->setVariable('RESULTS_TABLE',$presentation->getHTML(true));
377  }
378 
379  return true;
380  }
381 
382 
383 
384  function prepareOutput()
385  {
386  global $ilTabs, $ilHelp;
387 
389 
390  $ilHelp->setScreenIdComponent("src");
391 
392  $ilTabs->addTab("search", $this->lng->txt("search"),
393  $this->ctrl->getLinkTarget($this));
394 
395  if (!$this->settings->getHideAdvancedSearch())
396  {
397  $ilTabs->addTab("adv_search", $this->lng->txt("search_advanced"),
398  $this->ctrl->getLinkTargetByClass('iladvancedsearchgui'));
399  }
400 
401  $ilTabs->activateTab("search");
402  }
403 
404  // PRIVATE
405  function &__performDetailsSearch(&$query_parser,&$result)
406  {
407  foreach($this->getDetails() as $type => $enabled)
408  {
409  if(!$enabled)
410  {
411  continue;
412  }
413 
414  switch($type)
415  {
416  case 'crs':
417  $crs_search = ilObjectSearchFactory::_getObjectSearchInstance($query_parser);
418  $crs_search->setFilter(array('crs'));
419  $result->mergeEntries($crs_search->performSearch());
420  break;
421 
422  case 'grp':
423  $grp_search = ilObjectSearchFactory::_getObjectSearchInstance($query_parser);
424  $grp_search->setFilter(array('grp'));
425  $result->mergeEntries($grp_search->performSearch());
426  break;
427 
428  case 'lms':
429  $content_search =& ilObjectSearchFactory::_getLMContentSearchInstance($query_parser);
430  $content_search->setFilter($this->__getFilter());
431  $result->mergeEntries($content_search->performSearch());
432  break;
433 
434  case 'frm':
435  $forum_search =& ilObjectSearchFactory::_getForumSearchInstance($query_parser);
436  $forum_search->setFilter($this->__getFilter());
437  $result->mergeEntries($forum_search->performSearch());
438  break;
439 
440  case 'glo':
441  // Glossary term definition pages
442  $gdf_search =& ilObjectSearchFactory::_getLMContentSearchInstance($query_parser);
443  $gdf_search->setFilter(array('gdf'));
444  $result->mergeEntries($gdf_search->performSearch());
445  // Glossary terms
446  $gdf_term_search =& ilObjectSearchFactory::_getGlossaryDefinitionSearchInstance($query_parser);
447  $result->mergeEntries($gdf_term_search->performSearch());
448  break;
449 
450  case 'exc':
451  $exc_search =& ilObjectSearchFactory::_getExerciseSearchInstance($query_parser);
452  $exc_search->setFilter($this->__getFilter());
453  $result->mergeEntries($exc_search->performSearch());
454  break;
455 
456  case 'mcst':
457  $mcst_search =& ilObjectSearchFactory::_getMediaCastSearchInstance($query_parser);
458  $result->mergeEntries($mcst_search->performSearch());
459  break;
460 
461  case 'tst':
462  $tst_search =& ilObjectSearchFactory::_getTestSearchInstance($query_parser);
463  $tst_search->setFilter($this->__getFilter());
464  $result->mergeEntries($tst_search->performSearch());
465  break;
466 
467  case 'mep':
468  $mep_search =& ilObjectSearchFactory::_getMediaPoolSearchInstance($query_parser);
469  $mep_search->setFilter($this->__getFilter());
470  $result->mergeEntries($mep_search->performSearch());
471 
472  // Mob keyword search
473  $mob_search = ilObjectSearchFactory::_getMediaPoolSearchInstance($query_parser);
474  $mob_search->setFilter($this->__getFilter());
475  $result->mergeEntries($mob_search->performKeywordSearch());
476 
477  break;
478 
479  case 'wiki':
480  $wiki_search =& ilObjectSearchFactory::_getWikiContentSearchInstance($query_parser);
481  $wiki_search->setFilter($this->__getFilter());
482  $result->mergeEntries($wiki_search->performSearch());
483 
484  /*$result_meta =& $this->__searchMeta($query_parser,'title');
485  $result->mergeEntries($result_meta);
486  $result_meta =& $this->__searchMeta($query_parser,'description');
487  $result->mergeEntries($result_meta);*/
488  break;
489 
490  }
491  }
492  return $result;
493  }
494 
500  function &__parseQueryString()
501  {
502  include_once 'Services/Search/classes/class.ilQueryParser.php';
503 
504  $query_parser = new ilQueryParser(ilUtil::stripSlashes($this->getString()));
505  $query_parser->setCombination($this->getCombination());
506  $query_parser->parse();
507 
508  if(!$query_parser->validate())
509  {
510  return $query_parser->getMessage();
511  }
512  return $query_parser;
513  }
519  function &__searchObjects(&$query_parser)
520  {
521  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
522 
523  $obj_search =& ilObjectSearchFactory::_getObjectSearchInstance($query_parser);
525  {
526  $obj_search->setFilter($this->__getFilter());
527  }
528  return $obj_search->performSearch();
529  }
530 
531 
537  function &__searchMeta(&$query_parser,$a_type)
538  {
539  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
540 
541  $meta_search =& ilObjectSearchFactory::_getMetaDataSearchInstance($query_parser);
543  {
544  $meta_search->setFilter($this->__getFilter());
545  }
546  switch($a_type)
547  {
548  case 'keyword':
549  $meta_search->setMode('keyword');
550  break;
551 
552  case 'contribute':
553  $meta_search->setMode('contribute');
554  break;
555 
556  case 'title':
557  $meta_search->setMode('title');
558  break;
559 
560  case 'description':
561  $meta_search->setMode('description');
562  break;
563  }
564  return $meta_search->performSearch();
565  }
571  function __getFilter()
572  {
574  {
575  return false;
576  }
577 
578  foreach($this->getDetails() as $key => $detail_type)
579  {
580  if(!$detail_type)
581  {
582  continue;
583  }
584 
585  switch($key)
586  {
587  case 'lms':
588  $filter[] = 'lm';
589  $filter[] = 'dbk';
590  $filter[] = 'pg';
591  $filter[] = 'st';
592  $filter[] = 'sahs';
593  $filter[] = 'htlm';
594  break;
595 
596  case 'frm':
597  $filter[] = 'frm';
598  break;
599 
600  case 'glo':
601  $filter[] = 'glo';
602  break;
603 
604  case 'exc':
605  $filter[] = 'exc';
606  break;
607 
608  case 'mcst':
609  $filter[] = 'mcst';
610  break;
611 
612  case 'tst':
613  $filter[] = 'tst';
614  $filter[] = 'svy';
615  $filter[] = 'qpl';
616  $filter[] = 'spl';
617  break;
618 
619  case 'mep':
620  $filter[] = 'mep';
621  $filter[] = 'mob';
622  break;
623 
624  case 'fil':
625  $filter[] = 'file';
626  break;
627 
628  case 'wiki':
629  $filter[] = 'wpg';
630  break;
631 
632  default:
633  $filter[] = $key;
634  }
635  }
636  return $filter ? $filter : array();
637  }
638 
645  protected function initUserSearchCache()
646  {
647  global $ilUser;
648 
649  include_once('Services/Search/classes/class.ilUserSearchCache.php');
650  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
651  $this->search_cache->switchSearchType(ilUserSearchCache::DEFAULT_SEARCH);
652  if($_GET['page_number'])
653  {
654  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
655  }
656  if(isset($_POST['cmd']['performSearch']))
657  {
658  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['term']));
659  $this->search_cache->save();
660  }
661  }
662 }
663 ?>