ILIAS  eassessment Revision 61809
 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  $ilBench->start('Lucene','1500_fo');
126  $this->addPager($filter,'max_page');
127  $ilBench->stop('Lucene','1500_fo');
128  $presentation->setPreviousNext($this->prev_link, $this->next_link);
129 
130  if($presentation->render())
131  {
132  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
133  }
134  elseif(strlen(trim($qp->getQuery())))
135  {
136  ilUtil::sendInfo($this->lng->txt('search_no_match'));
137  }
138 
139  // and finally add search form
140  $this->initFormSearch();
141  $this->tpl->setVariable('SEARCH_TABLE',$this->form->getHTML());
142 
143  if($filter->getResultIds())
144  {
145  $this->fillAdminPanel();
146  }
147  }
148 
152  protected function initFormSearch()
153  {
154  global $tree;
155 
156  include_once './Services/Form/classes/class.ilPropertyFormGUI.php';
157 
158  $this->form = new ilPropertyFormGUI();
159  $this->form->setFormAction($this->ctrl->getFormAction($this,'search'));
160  $this->form->setTitle($this->lng->txt('search_advanced'));
161  $this->form->addCommandButton('search',$this->lng->txt('search'));
162  $this->form->addCommandButton('reset',$this->lng->txt('reset'));
163 
164  foreach($this->fields->getActiveSections() as $definition)
165  {
166  if($definition['name'] != 'default')
167  {
168  $section = new ilFormSectionHeaderGUI();
169  $section->setTitle($definition['name']);
170  $this->form->addItem($section);
171  }
172 
173  foreach($definition['fields'] as $field_name)
174  {
175  if(is_object($element = $this->fields->getFormElement($this->search_cache->getQuery(),$field_name)))
176  {
177  $this->form->addItem($element);
178  }
179  }
180  }
181  return true;
182  }
183 
187  protected function remoteSearch()
188  {
189  $this->search_cache->setRoot((int) $_POST['root_id']);
190  $this->search_cache->setQuery(array('lom_content' => ilUtil::stripSlashes($_POST['queryString'])));
191  $this->search_cache->save();
192  $this->search();
193  }
194 
195  protected function search()
196  {
197  if(!is_array($this->search_cache->getQuery()))
198  {
199  // TOD: handle empty advances search
200  ilUtil::sendInfo($this->lng->txt('msg_no_search_string'));
201  $this->showSavedResults();
202  return false;
203  }
204  unset($_SESSION['max_page']);
205  $this->search_cache->deleteCachedEntries();
206 
207  // Reset details
208  include_once './Services/Object/classes/class.ilSubItemListGUI.php';
210 
211  $this->performSearch();
212  }
213 
217  protected function reset()
218  {
219  $this->search_cache->setQuery(array());
220  $this->search_cache->save();
221  $this->showSavedResults();
222  }
223 
227  protected function performSearch()
228  {
229  global $ilUser,$ilBench;
230 
231  unset($_SESSION['vis_references']);
232 
233  include_once './Services/Search/classes/Lucene/class.ilLuceneSearcher.php';
234  include_once './Services/Search/classes/Lucene/class.ilLuceneAdvancedQueryParser.php';
235  $qp = new ilLuceneAdvancedQueryParser($this->search_cache->getQuery());
236  $qp->parse();
237  if(!strlen(trim($qp->getQuery())))
238  {
239  ilUtil::sendInfo($this->lng->txt('msg_no_search_string'));
240  $this->showSavedResults();
241  return false;
242  }
243 
244  $searcher = ilLuceneSearcher::getInstance($qp);
245  $searcher->search();
246 
247  // Filter results
248  $ilBench->start('Lucene','ResultFilter');
249  include_once './Services/Search/classes/Lucene/class.ilLuceneSearchResultFilter.php';
250  include_once './Services/Search/classes/Lucene/class.ilLucenePathFilter.php';
251  $filter = ilLuceneSearchResultFilter::getInstance($ilUser->getId());
252  $filter->addFilter(new ilLucenePathFilter($this->search_cache->getRoot()));
253  $filter->setCandidates($searcher->getResult());
254  $filter->filter();
255  $ilBench->stop('Lucene','ResultFilter');
256 
257  if($filter->getResultObjIds()) {
258  $searcher->highlight($filter->getResultObjIds());
259  }
260 
261  // Show results
262  $this->tpl->addBlockFile('ADM_CONTENT','adm_content','tpl.lucene_search.html','Services/Search');
263  include_once './Services/Search/classes/class.ilSearchResultPresentation.php';
264  $presentation = new ilSearchResultPresentation($this);
265  $presentation->setResults($filter->getResultIds());
266  $presentation->setSearcher($searcher);
267 
268  // TODO: other handling required
269  $ilBench->start('Lucene','1500_fo');
270  $this->addPager($filter,'max_page');
271  $ilBench->stop('Lucene','1500_fo');
272  $presentation->setPreviousNext($this->prev_link, $this->next_link);
273 
274  if($presentation->render())
275  {
276  $this->tpl->setVariable('SEARCH_RESULTS',$presentation->getHTML(true));
277  }
278  else
279  {
280  ilUtil::sendInfo($this->lng->txt('search_no_match'));
281  }
282 
283  // and finally add search form
284  $this->initFormSearch();
285  $this->tpl->setVariable('SEARCH_TABLE',$this->form->getHTML());
286 
287  if($filter->getResultIds())
288  {
289  $this->fillAdminPanel();
290  }
291  }
292 
296  public function prepareOutput()
297  {
299  $this->getTabs();
300  return true;
301 
302  global $ilAccess, $ilSetting;
303  global $ilUser;
304 
305  if($_SESSION['il_cont_admin_panel'])
306  {
307  $this->setAdminViewButton(
308  $this->ctrl->getLinkTarget($this, "disableAdministrationPanel"),
309  $this->lng->txt("basic_commands"));
310 
311  $this->addAdminPanelCommand("delete",
312  $this->lng->txt("delete_selected_items"));
313 
314  if(!$_SESSION["clipboard"])
315  {
316  $this->addAdminPanelCommand("cut",
317  $this->lng->txt("move_selected_items"));
318 
319  $this->addAdminPanelCommand("link",
320  $this->lng->txt("link_selected_items"));
321  }
322  else
323  {
324  #$this->addAdminPanelCommand("paste",
325  # $this->lng->txt("paste_clipboard_items"));
326  $this->addAdminPanelCommand("clear",
327  $this->lng->txt("clear_clipboard"));
328  }
329  }
330  elseif($ilUser->getId() != ANONYMOUS_USER_ID)
331  {
332  $this->setAdminViewButton(
333  $this->ctrl->getLinkTarget($this, "enableAdministrationPanel"),
334  $this->lng->txt("all_commands"));
335  }
336 
337  $this->ctrl->setParameter($this, "type", "");
338  $this->ctrl->setParameter($this, "item_ref_id", "");
339  $this->tpl->setPageFormAction($this->ctrl->getFormAction($this));
340 
341  }
342 
346  protected function getTabs()
347  {
348  if(ilSearchSettings::getInstance()->getHideAdvancedSearch())
349  {
350  return false;
351  }
352 
353  $this->tabs_gui->addTarget('search',$this->ctrl->getLinkTargetByClass('illucenesearchgui'));
354 
355  if($this->fields->getActiveFields())
356  {
357  $this->tabs_gui->addTarget('search_advanced',$this->ctrl->getLinkTarget($this));
358  }
359 
360  $this->tabs_gui->setTabActive('search_advanced');
361  }
362 
369  protected function initUserSearchCache()
370  {
371  global $ilUser;
372 
373  include_once('Services/Search/classes/class.ilUserSearchCache.php');
374  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
375  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_ADVANCED);
376  if((int) $_GET['page_number'])
377  {
378  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
379  }
380  if(isset($_POST['query']))
381  {
382  $this->search_cache->setQuery($_POST['query']);
383  }
384  }
385  protected function fillAdminPanel()
386  {
387  global $lng;
388 
389  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
390 
391  // admin panel commands
392  if ((count($this->admin_panel_commands) > 0))
393  {
394  foreach($this->admin_panel_commands as $cmd)
395  {
396  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
397  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
398  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
399  $this->tpl->parseCurrentBlock();
400  }
401 
402  $adm_cmds = true;
403  }
404  if ($adm_cmds)
405  {
406  $this->tpl->setCurrentBlock("lucene_adm_view_components");
407  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("arrow_upright.gif"));
408  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $lng->txt("actions"));
409  $this->tpl->parseCurrentBlock();
410  $adm_view_cmp = true;
411  }
412 
413  // admin view button
414  if (is_array($this->admin_view_button))
415  {
416  if (is_array($this->admin_view_button))
417  {
418  $this->tpl->setCurrentBlock("lucene_admin_button");
419  $this->tpl->setVariable("LUCENE_ADMIN_MODE_LINK",
420  $this->admin_view_button["link"]);
421  $this->tpl->setVariable("LUCENE_TXT_ADMIN_MODE",
422  $this->admin_view_button["txt"]);
423  $this->tpl->parseCurrentBlock();
424  }
425  $this->tpl->setCurrentBlock("lucene_admin_view");
426  $this->tpl->parseCurrentBlock();
427  $adm_view = true;
428  }
429 
430  // creation selector
431  if (is_array($this->creation_selector))
432  {
433  $this->tpl->setCurrentBlock("lucene_add_commands");
434  if ($adm_cmds)
435  {
436  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
437  }
438  $this->tpl->setVariable("LUCENE_SELECT_OBJTYPE_REPOS",
439  $this->creation_selector["options"]);
440  $this->tpl->setVariable("LUCENE_BTN_NAME_REPOS",
441  $this->creation_selector["command"]);
442  $this->tpl->setVariable("LUCENE_TXT_ADD_REPOS",
443  $this->creation_selector["txt"]);
444  $this->tpl->parseCurrentBlock();
445  $creation_selector = true;
446  }
447  if ($adm_view || $creation_selector)
448  {
449  $this->tpl->setCurrentBlock("lucene_adm_panel");
450  if ($adm_view_cmp)
451  {
452  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
453  }
454  $this->tpl->parseCurrentBlock();
455  }
456  }
457 
461  protected function addAdminPanelCommand($a_cmd, $a_txt)
462  {
463  $this->admin_panel_commands[] =
464  array("cmd" => $a_cmd, "txt" => $a_txt);
465  }
466 
470  protected function setAdminViewButton($a_link, $a_txt)
471  {
472  $this->admin_view_button =
473  array("link" => $a_link, "txt" => $a_txt);
474  }
475 
476  protected function setPageFormAction($a_action)
477  {
478  $this->page_form_action = $a_action;
479  }
480 }
481 ?>