ILIAS  Release_4_4_x_branch Revision 61816
 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 
304  protected function getTabs()
305  {
306  $this->tabs_gui->addTarget('search',$this->ctrl->getLinkTargetByClass('illucenesearchgui'));
307 
308  if(ilSearchSettings::getInstance()->isLuceneUserSearchEnabled())
309  {
310  $this->tabs_gui->addTarget('search_user',$this->ctrl->getLinkTargetByClass('illuceneusersearchgui'));
311  }
312 
313  if(
314  !ilSearchSettings::getInstance()->getHideAdvancedSearch() and
315  $this->fields->getActiveFields())
316  {
317  $this->tabs_gui->addTarget('search_advanced',$this->ctrl->getLinkTarget($this));
318  }
319 
320  $this->tabs_gui->setTabActive('search_advanced');
321  }
322 
329  protected function initUserSearchCache()
330  {
331  global $ilUser;
332 
333  include_once('Services/Search/classes/class.ilUserSearchCache.php');
334  $this->search_cache = ilUserSearchCache::_getInstance($ilUser->getId());
335  $this->search_cache->switchSearchType(ilUserSearchCache::LUCENE_ADVANCED);
336  if((int) $_GET['page_number'])
337  {
338  $this->search_cache->setResultPageNumber((int) $_GET['page_number']);
339  }
340  if(isset($_POST['query']))
341  {
342  $this->search_cache->setQuery($_POST['query']);
343  }
344  }
345  protected function fillAdminPanel()
346  {
347  global $lng;
348 
349  $adm_view_cmp = $adm_cmds = $creation_selector = $adm_view = false;
350 
351  // admin panel commands
352  if ((count($this->admin_panel_commands) > 0))
353  {
354  foreach($this->admin_panel_commands as $cmd)
355  {
356  $this->tpl->setCurrentBlock("lucene_admin_panel_cmd");
357  $this->tpl->setVariable("LUCENE_PANEL_CMD", $cmd["cmd"]);
358  $this->tpl->setVariable("LUCENE_TXT_PANEL_CMD", $cmd["txt"]);
359  $this->tpl->parseCurrentBlock();
360  }
361 
362  $adm_cmds = true;
363  }
364  if ($adm_cmds)
365  {
366  $this->tpl->setCurrentBlock("lucene_adm_view_components");
367  $this->tpl->setVariable("LUCENE_ADM_IMG_ARROW", ilUtil::getImagePath("arrow_upright.png"));
368  $this->tpl->setVariable("LUCENE_ADM_ALT_ARROW", $lng->txt("actions"));
369  $this->tpl->parseCurrentBlock();
370  $adm_view_cmp = true;
371  }
372 
373  // admin view button
374  if (is_array($this->admin_view_button))
375  {
376  if (is_array($this->admin_view_button))
377  {
378  $this->tpl->setCurrentBlock("lucene_admin_button");
379  $this->tpl->setVariable("LUCENE_ADMIN_MODE_LINK",
380  $this->admin_view_button["link"]);
381  $this->tpl->setVariable("LUCENE_TXT_ADMIN_MODE",
382  $this->admin_view_button["txt"]);
383  $this->tpl->parseCurrentBlock();
384  }
385  $this->tpl->setCurrentBlock("lucene_admin_view");
386  $this->tpl->parseCurrentBlock();
387  $adm_view = true;
388  }
389 
390  // creation selector
391  if (is_array($this->creation_selector))
392  {
393  $this->tpl->setCurrentBlock("lucene_add_commands");
394  if ($adm_cmds)
395  {
396  $this->tpl->setVariable("LUCENE_ADD_COM_WIDTH", 'width="1"');
397  }
398  $this->tpl->setVariable("LUCENE_SELECT_OBJTYPE_REPOS",
399  $this->creation_selector["options"]);
400  $this->tpl->setVariable("LUCENE_BTN_NAME_REPOS",
401  $this->creation_selector["command"]);
402  $this->tpl->setVariable("LUCENE_TXT_ADD_REPOS",
403  $this->creation_selector["txt"]);
404  $this->tpl->parseCurrentBlock();
405  $creation_selector = true;
406  }
407  if ($adm_view || $creation_selector)
408  {
409  $this->tpl->setCurrentBlock("lucene_adm_panel");
410  if ($adm_view_cmp)
411  {
412  $this->tpl->setVariable("LUCENE_ADM_TBL_WIDTH", 'width:"100%";');
413  }
414  $this->tpl->parseCurrentBlock();
415  }
416  }
417 
421  protected function addAdminPanelCommand($a_cmd, $a_txt)
422  {
423  $this->admin_panel_commands[] =
424  array("cmd" => $a_cmd, "txt" => $a_txt);
425  }
426 
430  protected function setAdminViewButton($a_link, $a_txt)
431  {
432  $this->admin_view_button =
433  array("link" => $a_link, "txt" => $a_txt);
434  }
435 
436  protected function setPageFormAction($a_action)
437  {
438  $this->page_form_action = $a_action;
439  }
440 }
441 ?>