ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilLuceneAdvancedSearchGUI.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.ilSearchBaseGUI.php';
25 include_once './Services/Search/classes/Lucene/class.ilLuceneAdvancedSearchFields.php';
26 include_once './Services/PersonalDesktop/interfaces/interface.ilDesktopItemHandling.php';
27 include_once './Services/Administration/interfaces/interface.ilAdministrationCommandHandling.php';
28 
43 {
44  protected $ilTabs;
45 
46  protected $fields = null;
47 
51  public function __construct()
52  {
53  global $ilTabs;
54 
55  $this->tabs_gui = $ilTabs;
57 
59  $this->initUserSearchCache();
60  }
61 
65  public function executeCommand()
66  {
67  $next_class = $this->ctrl->getNextClass($this);
68  $cmd = $this->ctrl->getCmd();
69 
70  $this->prepareOutput();
71  switch($next_class)
72  {
73  case 'ilobjectcopygui':
74  include_once './Services/Object/classes/class.ilObjectCopyGUI.php';
75  $cp = new ilObjectCopyGUI($this);
76  $this->ctrl->forwardCommand($cp);
77  break;
78 
79 
80  default:
81  if(!$cmd)
82  {
83  $cmd = "showSavedResults";
84  }
85  $this->handleCommand($cmd);
86  break;
87  }
88  return true;
89  }
90 
91 
96  public function showSavedResults()
97  {
98  global $ilUser,$ilBench;
99 
100  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
101  include_once './Services/Search/classes/Lucene/class.ilLuceneAdvancedQueryParser.php';
102  $qp = new ilLuceneAdvancedQueryParser($this->search_cache->getQuery());
103  $qp->parse();
104  $searcher = ilLuceneSearcher::getInstance($qp);
105  $searcher->search();
106 
107  // Load saved results
108  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
109  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
110  $filter->loadFromDb();
111 
112  // Highlight
113  if($filter->getResultObjIds()) {
114  $searcher->highlight($filter->getResultObjIds());
115  }
116 
117  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.lucene_adv_search.html','Services/Search');
118  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
119  $presentation = new ilSearchResultPresentation($this);
120  $presentation->setResults($filter->getResultIds());
121  $presentation->setSearcher($searcher);
122 
123 
124  // TODO: other handling required
125  $this->addPager($filter,'max_page');
126  $presentation->setPreviousNext($this->prev_link, $this->next_link);
127 
128  if($presentation->render())
129  {
130  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
131  }
132  elseif(strlen(trim($qp->getQuery())))
133  {
134  ilUtil::sendInfo($this->lng->txt('search_no_match'));
135  }
136 
137  // and finally add search form
138  $this->initFormSearch();
139  $this->tpl->setVariable('SEARCH_TABLE',$this->form->getHTML());
140 
141  if($filter->getResultIds())
142  {
143  $this->fillAdminPanel();
144  }
145  }
146 
150  protected function initFormSearch()
151  {
152  global $tree;
153 
154  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
155 
156  $this->form = new ilPropertyFormGUI();
157  $this->form->setFormAction($this->ctrl->getFormAction($this,'search'));
158  $this->form->setTitle($this->lng->txt('search_advanced'));
159  $this->form->addCommandButton('search',$this->lng->txt('search'));
160  $this->form->addCommandButton('reset',$this->lng->txt('reset'));
161 
162  foreach($this->fields->getActiveSections() as $definition)
163  {
164  if($definition['name'] != 'default')
165  {
167  $section->setTitle($definition['name']);
168  $this->form->addItem($section);
169  }
170 
171  foreach($definition['fields'] as $field_name)
172  {
173  if(is_object($element = $this->fields->getFormElement($this->search_cache->getQuery(),$field_name)))
174  {
175  $this->form->addItem($element);
176  }
177  }
178  }
179  return true;
180  }
181 
185  protected function remoteSearch()
186  {
187  $this->search_cache->setRoot((int) $_POST['root_id']);
188  $this->search_cache->setQuery(array('lom_content' => ilUtil::stripSlashes($_POST['queryString'])));
189  $this->search_cache->save();
190  $this->search();
191  }
192 
193  protected function search()
194  {
195  if(!is_array($this->search_cache->getQuery()))
196  {
197  // TOD: handle empty advances search
198  ilUtil::sendInfo($this->lng->txt('msg_no_search_string'));
199  $this->showSavedResults();
200  return false;
201  }
202  unset($_SESSION['max_page']);
203  $this->search_cache->deleteCachedEntries();
204 
205  // Reset details
206  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
208 
209  $this->performSearch();
210  }
211 
215  protected function reset()
216  {
217  $this->search_cache->setQuery(array());
218  $this->search_cache->save();
219  $this->showSavedResults();
220  }
221 
225  protected function performSearch()
226  {
227  global $ilUser,$ilBench;
228 
229  unset($_SESSION['vis_references']);
230 
231  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
232  include_once './Services/Search/classes/Lucene/class.ilLuceneAdvancedQueryParser.php';
233  $qp = new ilLuceneAdvancedQueryParser($this->search_cache->getQuery());
234  $qp->parse();
235  if(!strlen(trim($qp->getQuery())))
236  {
237  ilUtil::sendInfo($this->lng->txt('msg_no_search_string'));
238  $this->showSavedResults();
239  return false;
240  }
241 
242  $searcher = ilLuceneSearcher::getInstance($qp);
243  $searcher->search();
244 
245  // Filter results
246  $ilBench->start('Lucene','ResultFilter');
247  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
248  include_once './Services/Search/classes/Lucene/class.ilLucenePathFilter.php';
249  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
250  $filter->addFilter(new ilLucenePathFilter($this->search_cache->getRoot()));
251  $filter->setCandidates($searcher->getResult());
252  $filter->filter();
253  $ilBench->stop('Lucene','ResultFilter');
254 
255  if($filter->getResultObjIds()) {
256  $searcher->highlight($filter->getResultObjIds());
257  }
258 
259  // Show results
260  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.lucene_search.html','Services/Search');
261  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
262  $presentation = new ilSearchResultPresentation($this);
263  $presentation->setResults($filter->getResultIds());
264  $presentation->setSearcher($searcher);
265 
266  // TODO: other handling required
267  $ilBench->start('Lucene','1500_fo');
268  $this->addPager($filter,'max_page');
269  $ilBench->stop('Lucene','1500_fo');
270  $presentation->setPreviousNext($this->prev_link, $this->next_link);
271 
272  if($presentation->render())
273  {
274  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
275  }
276  else
277  {
278  ilUtil::sendInfo($this->lng->txt('search_no_match'));
279  }
280 
281  // and finally add search form
282  $this->initFormSearch();
283  $this->tpl->setVariable('SEARCH_TABLE',$this->form->getHTML());
284 
285  if($filter->getResultIds())
286  {
287  $this->fillAdminPanel();
288  }
289  }
290 
294  public function prepareOutput()
295  {
297  $this->getTabs();
298  return true;
299 
300  global $ilAccess, $ilSetting;
301  global $ilUser;
302 
303  if($_SESSION['il_cont_admin_panel'])
304  {
305  $this->setAdminViewButton(
306  $this->ctrl->getLinkTarget($this, "disableAdministrationPanel"),
307  $this->lng->txt("basic_commands"));
308 
309  $this->addAdminPanelCommand("delete",
310  $this->lng->txt("delete_selected_items"));
311 
312  if(!$_SESSION["clipboard"])
313  {
314  $this->addAdminPanelCommand("cut",
315  $this->lng->txt("move_selected_items"));
316 
317  $this->addAdminPanelCommand("link",
318  $this->lng->txt("link_selected_items"));
319  }
320  else
321  {
322  #$this->addAdminPanelCommand("paste",
323  # $this->lng->txt("paste_clipboard_items"));
324  $this->addAdminPanelCommand("clear",
325  $this->lng->txt("clear_clipboard"));
326  }
327  }
328  elseif($ilUser->getId() != ANONYMOUS_USER_ID)
329  {
330  $this->setAdminViewButton(
331  $this->ctrl->getLinkTarget($this, "enableAdministrationPanel"),
332  $this->lng->txt("all_commands"));
333  }
334 
335  $this->ctrl->setParameter($this, "type", "");
336  $this->ctrl->setParameter($this, "item_ref_id", "");
337  $this->tpl->setPageFormAction($this->ctrl->getFormAction($this));
338 
339  }
340 
344  protected function getTabs()
345  {
346  if(ilSearchSettings::getInstance()->getHideAdvancedSearch())
347  {
348  return false;
349  }
350 
351  $this->tabs_gui->addTarget('search',$this->ctrl->getLinkTargetByClass('illucenesearchgui'));
352 
353  if($this->fields->getActiveFields())
354  {
355  $this->tabs_gui->addTarget('search_advanced',$this->ctrl->getLinkTarget($this));
356  }
357 
358  $this->tabs_gui->setTabActive('search_advanced');
359  }
360 
367  protected function initUserSearchCache()
368  {
369  global $ilUser;
370 
371  include_once('Services/Search/classes/class.ilUserSearchCache.php');
372  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
373  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_ADVANCED);
374  if((int) $_GET['page_number'])
375  {
376  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
377  }
378  if(isset($_POST['query']))
379  {
380  $this->search_cache->setQuery($_POST['query']);
381  }
382  }
383  protected function fillAdminPanel()
384  {
385  global $lng;
386 
387  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
388 
389  // admin panel commands
390  if ((count($this->admin_panel_commands) > 0))
391  {
392  foreach($this->admin_panel_commands as $cmd)
393  {
394  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
395  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
396  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
397  $this->tpl->parseCurrentBlock();
398  }
399 
400  $adm_cmds = true;
401  }
402  if ($adm_cmds)
403  {
404  $this->tpl->setCurrentBlock("lucene_adm_view_components");
405  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("arrow_upright.png"));
406  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $lng->txt("actions"));
407  $this->tpl->parseCurrentBlock();
408  $adm_view_cmp = true;
409  }
410 
411  // admin view button
412  if (is_array($this->admin_view_button))
413  {
414  if (is_array($this->admin_view_button))
415  {
416  $this->tpl->setCurrentBlock("lucene_admin_button");
417  $this->tpl->setVariable("LUCENE_ADMIN_MODE_LINK",
418  $this->admin_view_button["link"]);
419  $this->tpl->setVariable("LUCENE_TXT_ADMIN_MODE",
420  $this->admin_view_button["txt"]);
421  $this->tpl->parseCurrentBlock();
422  }
423  $this->tpl->setCurrentBlock("lucene_admin_view");
424  $this->tpl->parseCurrentBlock();
425  $adm_view = true;
426  }
427 
428  // creation selector
429  if (is_array($this->creation_selector))
430  {
431  $this->tpl->setCurrentBlock("lucene_add_commands");
432  if ($adm_cmds)
433  {
434  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
435  }
436  $this->tpl->setVariable("LUCENE_SELECT_OBJTYPE_REPOS",
437  $this->creation_selector["options"]);
438  $this->tpl->setVariable("LUCENE_BTN_NAME_REPOS",
439  $this->creation_selector["command"]);
440  $this->tpl->setVariable("LUCENE_TXT_ADD_REPOS",
441  $this->creation_selector["txt"]);
442  $this->tpl->parseCurrentBlock();
443  $creation_selector = true;
444  }
445  if ($adm_view || $creation_selector)
446  {
447  $this->tpl->setCurrentBlock("lucene_adm_panel");
448  if ($adm_view_cmp)
449  {
450  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
451  }
452  $this->tpl->parseCurrentBlock();
453  }
454  }
455 
459  protected function addAdminPanelCommand($a_cmd, $a_txt)
460  {
461  $this->admin_panel_commands[] =
462  array("cmd" => $a_cmd, "txt" => $a_txt);
463  }
464 
468  protected function setAdminViewButton($a_link, $a_txt)
469  {
470  $this->admin_view_button =
471  array("link" => $a_link, "txt" => $a_txt);
472  }
473 
474  protected function setPageFormAction($a_action)
475  {
476  $this->page_form_action = $a_action;
477  }
478 }
479 ?>