ILIAS  Release_5_0_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  include_once("./Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php");
223 
224  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.search.html','Services/Search');
225  $this->tpl->setVariable("FORM_ACTION", $ilCtrl->getFormAction($this,'performSearch'));
226  $this->tpl->setVariable("TERM", ilUtil::prepareFormOutput($this->getString()));
227  include_once("./Services/UIComponent/Button/classes/class.ilSubmitButton.php");
229  $btn->setCommand("performSearch");
230  $btn->setCaption("search");
231  $this->tpl->setVariable("SUBMIT_BTN",$btn->render());
232  $this->tpl->setVariable("TXT_OPTIONS", $lng->txt("options"));
233  $this->tpl->setVariable("ARR_IMG", ilGlyphGUI::get(ilGlyphGUI::CARET));
234  $this->tpl->setVariable("TXT_COMBINATION", $lng->txt("search_term_combination"));
235  $this->tpl->setVariable('TXT_COMBINATION_DEFAULT', ilSearchSettings::getInstance()->getDefaultOperator() == ilSearchSettings::OPERATOR_AND ? $lng->txt('search_all_words') : $lng->txt('search_any_word'));
236 
237  if (ilSearchSettings::getInstance()->isLuceneItemFilterEnabled())
238  {
239  $this->tpl->setCurrentBlock("type_sel");
240  $this->tpl->setVariable('TXT_TYPE_DEFAULT',$lng->txt("search_fast_info"));
241  $this->tpl->setVariable("TXT_TYPE", $lng->txt("search_type"));
243  $this->tpl->setVariable("ARR_IMGT", ilGlyphGUI::get(ilGlyphGUI::CARET));
244  $this->tpl->setVariable("FORM", $this->form->getHTML());
245  $this->tpl->parseCurrentBlock();
246  }
247 
248  $this->tpl->setVariable("TXT_AREA", $lng->txt("search_area"));
249 
250 
251  // search area form
252  $this->tpl->setVariable('SEARCH_AREA_FORM', $this->getSearchAreaForm()->getHTML());
253 
254  return true;
255  }
256 
257  function showSavedResults()
258  {
259  global $ilUser;
260 
261  // Read old result sets
262  include_once 'Services/Search/classes/class.ilSearchResult.php';
263 
264  $result_obj = new ilSearchResult($ilUser->getId());
265  $result_obj->read();
266  $result_obj->filterResults($this->getRootNode());
267 
268  $this->showSearch();
269 
270  // Show them
271  if(count($result_obj->getResults()))
272  {
273  $this->addPager($result_obj,'max_page');
274 
275  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
277  $presentation->setResults($result_obj->getResultsForPresentation());
278  $presentation->setSubitemIds($result_obj->getSubitemIds());
279  $presentation->setPreviousNext($this->prev_link, $this->next_link);
280  #$presentation->setSearcher($searcher);
281 
282  if($presentation->render())
283  {
284 // $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML());
285  $this->tpl->setVariable('RESULTS_TABLE',$presentation->getHTML(true));
286  }
287  }
288 
289  return true;
290  }
291 
292 
296  function performSearch()
297  {
298  global $ilUser;
299 
300  if(!isset($_GET['page_number']) and $this->search_mode != 'in_results' )
301  {
302  unset($_SESSION['max_page']);
303  $this->search_cache->deleteCachedEntries();
304  }
305 
306  if($this->getType() == ilSearchBaseGUI::SEARCH_DETAILS and !$this->getDetails())
307  {
308  ilUtil::sendInfo($this->lng->txt('search_choose_object_type'));
309  $this->showSearch();
310  return false;
311  }
312 
313  // Step 1: parse query string
314  if(!is_object($query_parser =& $this->__parseQueryString()))
315  {
316  ilUtil::sendInfo($query_parser);
317  $this->showSearch();
318 
319  return false;
320  }
321  // Step 2: perform object search. Get an ObjectSearch object via factory. Depends on fulltext or like search type.
322  $result =& $this->__searchObjects($query_parser);
323 
324  // Step 3: perform meta keyword search. Get an MetaDataSearch object.
325  $result_meta =& $this->__searchMeta($query_parser,'keyword');
326  $result->mergeEntries($result_meta);
327 
328  $result_meta =& $this->__searchMeta($query_parser,'contribute');
329  $result->mergeEntries($result_meta);
330 
331  $result_meta =& $this->__searchMeta($query_parser,'title');
332  $result->mergeEntries($result_meta);
333 
334  $result_meta =& $this->__searchMeta($query_parser,'description');
335  $result->mergeEntries($result_meta);
336 
337  // Perform details search in object specific tables
339  {
340  $result = $this->__performDetailsSearch($query_parser,$result);
341  }
342  // Step 5: Search in results
343  if($this->search_mode == 'in_results')
344  {
345  include_once 'Services/Search/classes/class.ilSearchResult.php';
346 
347  $old_result_obj = new ilSearchResult($ilUser->getId());
348  $old_result_obj->read();
349 
350  $result->diffEntriesFromResult($old_result_obj);
351  }
352 
353 
354  // Step 4: merge and validate results
355  $result->filter($this->getRootNode(),$query_parser->getCombination() == 'and');
356  $result->save();
357  $this->showSearch();
358 
359  if(!count($result->getResults()))
360  {
361  ilUtil::sendInfo($this->lng->txt('search_no_match'));
362  }
363 
364  if($result->isLimitReached())
365  {
366  #$message = sprintf($this->lng->txt('search_limit_reached'),$this->settings->getMaxHits());
367  #ilUtil::sendInfo($message);
368  }
369 
370  // Step 6: show results
371  $this->addPager($result,'max_page');
372 
373  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
375  $presentation->setResults($result->getResultsForPresentation());
376  $presentation->setSubitemIds($result->getSubitemIds());
377  $presentation->setPreviousNext($this->prev_link, $this->next_link);
378 
379  if($presentation->render())
380  {
381 // $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML());
382  $this->tpl->setVariable('RESULTS_TABLE',$presentation->getHTML(true));
383  }
384 
385  return true;
386  }
387 
388 
389 
390  function prepareOutput()
391  {
392  global $ilTabs, $ilHelp;
393 
395 
396  $ilHelp->setScreenIdComponent("src");
397 
398  $ilTabs->addTab("search", $this->lng->txt("search"),
399  $this->ctrl->getLinkTarget($this));
400 
401  if (!$this->settings->getHideAdvancedSearch())
402  {
403  $ilTabs->addTab("adv_search", $this->lng->txt("search_advanced"),
404  $this->ctrl->getLinkTargetByClass('iladvancedsearchgui'));
405  }
406 
407  $ilTabs->activateTab("search");
408  }
409 
410  // PRIVATE
411  function &__performDetailsSearch(&$query_parser,&$result)
412  {
413  foreach($this->getDetails() as $type => $enabled)
414  {
415  if(!$enabled)
416  {
417  continue;
418  }
419 
420  switch($type)
421  {
422  case 'crs':
423  $crs_search = ilObjectSearchFactory::_getObjectSearchInstance($query_parser);
424  $crs_search->setFilter(array('crs'));
425  $result->mergeEntries($crs_search->performSearch());
426  break;
427 
428  case 'grp':
429  $grp_search = ilObjectSearchFactory::_getObjectSearchInstance($query_parser);
430  $grp_search->setFilter(array('grp'));
431  $result->mergeEntries($grp_search->performSearch());
432  break;
433 
434  case 'lms':
435  $content_search =& ilObjectSearchFactory::_getLMContentSearchInstance($query_parser);
436  $content_search->setFilter($this->__getFilter());
437  $result->mergeEntries($content_search->performSearch());
438  break;
439 
440  case 'frm':
441  $forum_search =& ilObjectSearchFactory::_getForumSearchInstance($query_parser);
442  $forum_search->setFilter($this->__getFilter());
443  $result->mergeEntries($forum_search->performSearch());
444  break;
445 
446  case 'glo':
447  // Glossary term definition pages
448  $gdf_search =& ilObjectSearchFactory::_getLMContentSearchInstance($query_parser);
449  $gdf_search->setFilter(array('gdf'));
450  $result->mergeEntries($gdf_search->performSearch());
451  // Glossary terms
452  $gdf_term_search =& ilObjectSearchFactory::_getGlossaryDefinitionSearchInstance($query_parser);
453  $result->mergeEntries($gdf_term_search->performSearch());
454  break;
455 
456  case 'exc':
457  $exc_search =& ilObjectSearchFactory::_getExerciseSearchInstance($query_parser);
458  $exc_search->setFilter($this->__getFilter());
459  $result->mergeEntries($exc_search->performSearch());
460  break;
461 
462  case 'mcst':
463  $mcst_search =& ilObjectSearchFactory::_getMediaCastSearchInstance($query_parser);
464  $result->mergeEntries($mcst_search->performSearch());
465  break;
466 
467  case 'tst':
468  $tst_search =& ilObjectSearchFactory::_getTestSearchInstance($query_parser);
469  $tst_search->setFilter($this->__getFilter());
470  $result->mergeEntries($tst_search->performSearch());
471  break;
472 
473  case 'mep':
474  $mep_search =& ilObjectSearchFactory::_getMediaPoolSearchInstance($query_parser);
475  $mep_search->setFilter($this->__getFilter());
476  $result->mergeEntries($mep_search->performSearch());
477 
478  // Mob keyword search
479  $mob_search = ilObjectSearchFactory::_getMediaPoolSearchInstance($query_parser);
480  $mob_search->setFilter($this->__getFilter());
481  $result->mergeEntries($mob_search->performKeywordSearch());
482 
483  break;
484 
485  case 'wiki':
486  $wiki_search =& ilObjectSearchFactory::_getWikiContentSearchInstance($query_parser);
487  $wiki_search->setFilter($this->__getFilter());
488  $result->mergeEntries($wiki_search->performSearch());
489 
490  /*$result_meta =& $this->__searchMeta($query_parser,'title');
491  $result->mergeEntries($result_meta);
492  $result_meta =& $this->__searchMeta($query_parser,'description');
493  $result->mergeEntries($result_meta);*/
494  break;
495 
496  }
497  }
498  return $result;
499  }
500 
506  function &__parseQueryString()
507  {
508  include_once 'Services/Search/classes/class.ilQueryParser.php';
509 
510  $query_parser = new ilQueryParser(ilUtil::stripSlashes($this->getString()));
511  $query_parser->setCombination($this->getCombination());
512  $query_parser->parse();
513 
514  if(!$query_parser->validate())
515  {
516  return $query_parser->getMessage();
517  }
518  return $query_parser;
519  }
525  function &__searchObjects(&$query_parser)
526  {
527  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
528 
529  $obj_search =& ilObjectSearchFactory::_getObjectSearchInstance($query_parser);
531  {
532  $obj_search->setFilter($this->__getFilter());
533  }
534  return $obj_search->performSearch();
535  }
536 
537 
543  function &__searchMeta(&$query_parser,$a_type)
544  {
545  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
546 
547  $meta_search =& ilObjectSearchFactory::_getMetaDataSearchInstance($query_parser);
549  {
550  $meta_search->setFilter($this->__getFilter());
551  }
552  switch($a_type)
553  {
554  case 'keyword':
555  $meta_search->setMode('keyword');
556  break;
557 
558  case 'contribute':
559  $meta_search->setMode('contribute');
560  break;
561 
562  case 'title':
563  $meta_search->setMode('title');
564  break;
565 
566  case 'description':
567  $meta_search->setMode('description');
568  break;
569  }
570  return $meta_search->performSearch();
571  }
577  function __getFilter()
578  {
580  {
581  return false;
582  }
583 
584  foreach($this->getDetails() as $key => $detail_type)
585  {
586  if(!$detail_type)
587  {
588  continue;
589  }
590 
591  switch($key)
592  {
593  case 'lms':
594  $filter[] = 'lm';
595  $filter[] = 'dbk';
596  $filter[] = 'pg';
597  $filter[] = 'st';
598  $filter[] = 'sahs';
599  $filter[] = 'htlm';
600  break;
601 
602  case 'frm':
603  $filter[] = 'frm';
604  break;
605 
606  case 'glo':
607  $filter[] = 'glo';
608  break;
609 
610  case 'exc':
611  $filter[] = 'exc';
612  break;
613 
614  case 'mcst':
615  $filter[] = 'mcst';
616  break;
617 
618  case 'tst':
619  $filter[] = 'tst';
620  $filter[] = 'svy';
621  $filter[] = 'qpl';
622  $filter[] = 'spl';
623  break;
624 
625  case 'mep':
626  $filter[] = 'mep';
627  $filter[] = 'mob';
628  break;
629 
630  case 'fil':
631  $filter[] = 'file';
632  break;
633 
634  case 'wiki':
635  $filter[] = 'wpg';
636  break;
637 
638  default:
639  $filter[] = $key;
640  }
641  }
642  return $filter ? $filter : array();
643  }
644 
651  protected function initUserSearchCache()
652  {
653  global $ilUser;
654 
655  include_once('Services/Search/classes/class.ilUserSearchCache.php');
656  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
657  $this->search_cache->switchSearchType(ilUserSearchCache::DEFAULT_SEARCH);
658  if($_GET['page_number'])
659  {
660  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
661  }
662  if(isset($_POST['cmd']['performSearch']))
663  {
664  $this->search_cache->setQuery(ilUtil::stripSlashes($_POST['term']));
665  $this->search_cache->save();
666  }
667  }
668 }
669 ?>