ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilShopAdvancedSearchGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once 'Services/Payment/classes/class.ilShopBaseGUI.php';
5 
15 {
16  const SEARCH_OR = 'or';
17  const SEARCH_AND = 'and';
18 
19  private $string = '';
20  private $combination = '';
21  private $details = array();
22  private $topic_id = 0;
23  private $sort_type_topics = '';
24  private $sort_direction_topics = '';
25  private $sort_field = '';
26  private $sort_direction = '';
27 
28  public function __construct()
29  {
31 
32  // set filter
33  $this->setCombination($_SESSION['shop_advanced_search']['combination']);
34  $this->setString($_SESSION['shop_advanced_search']['string']);
35  $this->setDetails($_SESSION['shop_advanced_search']['details']);
36  $this->setTopicId($_SESSION['shop_advanced_search']['topic']);
37 
38  // set sorting
39  $this->setSortingTypeTopics($_SESSION['shop_advanced_search']['order_topics_sorting_type']);
40  $this->setSortingDirectionTopics($_SESSION['shop_advanced_search']['shop_topics_sorting_direction']);
41 
42  $this->setSortField($_SESSION['shop_advanced_search']['shop_order_field']);
43  $this->setSortDirection($_SESSION['shop_advanced_search']['shop_order_direction']);
44  }
45 
46  public function setSorting()
47  {
48  $this->setSortingTypeTopics($_POST['topics_sorting_type']);
49  $this->setSortingDirectionTopics($_POST['topics_sorting_direction']);
50  $this->setSortField($_POST['order_field']);
51  $this->setSortDirection($_POST['order_direction']);
52 
53  $this->performSearch();
54 
55  return true;
56  }
57 
58  public function executeCommand()
59  {
60  $next_class = $this->ctrl->getNextClass($this);
61  $cmd = $this->ctrl->getCmd();
62  switch($next_class)
63  {
64  default:
65  if(!$cmd)
66  {
67  $cmd = 'performSearch';
68  }
69  $this->prepareOutput();
70  $this->$cmd();
71 
72  break;
73  }
74 
75  return true;
76  }
77 
78  public function performSearch()
79  {
80  if(isset($_POST['search']['combination'])) $this->setCombination($_POST['search']['combination']);
81  if(isset($_POST['search']['string'])) $this->setString($_POST['search']['string']);
82  if(isset($_POST['search']['details'])) $this->setDetails($_POST['search']['details']);
83  if(isset($_POST['search']['topic'])) $this->setTopicId($_POST['search']['topic']);
84 
85  if(!$this->getDetails())
86  {
87  if(method_exists($this, $this->ctrl->getCmd()))
88  ilUtil::sendInfo($this->lng->txt('search_choose_object_type'));
90 
91  return false;
92  }
93 
94  // Step 1: parse query string
95  if(!is_object($query_parser = $this->parseQueryString()))
96  {
97  ilUtil::sendInfo($query_parser);
99 
100  return false;
101  }
102 
103  // Step 2: perform object search. Get an ObjectSearch object via factory. Depends on fulltext or like search type.
104  $result = $this->searchObjects($query_parser);
105 
106  // Step 3:
107  $result->filter(ROOT_FOLDER_ID, $query_parser->getCombination() == 'and');
108  $result->save();
109 
110  if(!count($result->getResults()))
111  {
112  ilUtil::sendInfo($this->lng->txt('payment_shop_not_objects_found'));
113  }
114 
115  $this->showForm($result);
116 
117  return true;
118  }
119 
120  private function parseQueryString()
121  {
122  include_once 'Services/Search/classes/class.ilQueryParser.php';
123 
124  $query_parser = new ilQueryParser(ilUtil::stripSlashes($this->getString()));
125  $query_parser->setCombination($this->getCombination());
126  $query_parser->parse();
127 
128  if (!$query_parser->validate())
129  {
130  return $query_parser->getMessage();
131  }
132 
133  return $query_parser;
134  }
135 
136  private function searchObjects($query_parser)
137  {
138  // create new search result object and assign the sorted topics
139  $oSearchResult = new ilShopSearchResult(SHOP_ADVANCED_SEARCH);
140  if((bool)$this->oGeneralSettings->get('topics_allow_custom_sorting'))
141  {
142  ilShopTopics::_getInstance()->setIdFilter((int)$this->getTopicId());
143  ilShopTopics::_getInstance()->enableCustomSorting(true);
144  ilShopTopics::_getInstance()->setSortingType((int)$this->getSortingTypeTopics());
145  ilShopTopics::_getInstance()->setSortingDirection(strtoupper($this->getSortingDirectionTopics()));
146  ilShopTopics::_getInstance()->read();
147  }
148  else
149  {
150  ilShopTopics::_getInstance()->setIdFilter((int)$this->getTopicId());
151  ilShopTopics::_getInstance()->enableCustomSorting(false);
152  ilShopTopics::_getInstance()->setSortingType((int)$this->oGeneralSettings->get('topics_sorting_type'));
153  ilShopTopics::_getInstance()->setSortingDirection(strtoupper($this->oGeneralSettings->get('topics_sorting_direction')));
154  ilShopTopics::_getInstance()->read();
155  }
156  $oSearchResult->setTopics(ilShopTopics::_getInstance()->getTopics());
157  $oSearchResult->setResultPageNumber((int)$_GET['page_number']);
158 
159  include_once 'Services/Search/classes/class.ilObjectSearchFactory.php';
160  $res = null;
161 
162  $obj_search = ilObjectSearchFactory::_getShopObjectSearchInstance($query_parser);
163  $obj_search->setFilterShopTopicId((int)$this->getTopicId());
164  $obj_search->setFilter($this->getFilter());
165  $obj_search->setCustomSearchResultObject($oSearchResult);
166  $res = $obj_search->performSearch();
167 
168  $meta_search_c = ilObjectSearchFactory::_getShopMetaDataSearchInstance($query_parser);
169  $meta_search_c->setMode('contribute');
170  $meta_search_c->setFilter($this->getFilter());
171  $meta_search_c->setFilterShopTopicId((int)$this->getTopicId());
172  $meta_search_c->setCustomSearchResultObject($oSearchResult);
173  $res->mergeEntries($meta_search_c->performSearch());
174 
175  $meta_search_t = ilObjectSearchFactory::_getShopMetaDataSearchInstance($query_parser);
176  $meta_search_t->setMode('title');
177  $meta_search_t->setFilter($this->getFilter());
178  $meta_search_t->setCustomSearchResultObject($oSearchResult);
179  $meta_search_t->setFilterShopTopicId((int)$this->getTopicId());
180  $res->mergeEntries($meta_search_t->performSearch());
181 
182  $meta_search_k = ilObjectSearchFactory::_getShopMetaDataSearchInstance($query_parser);
183  $meta_search_k->setMode('keyword');
184  $meta_search_k->setFilter($this->getFilter());
185  $meta_search_k->setCustomSearchResultObject($oSearchResult);
186  $meta_search_k->setFilterShopTopicId((int)$this->getTopicId());
187  $res->mergeEntries($meta_search_k->performSearch());
188 
189  return $res;
190  }
191 
192  private function getFilter()
193  {
194  foreach($this->getDetails() as $key => $detail_type)
195  {
196  switch($key)
197  {
198  case 'crs':
199  $filter[] = 'crs';
200  break;
201 
202  case 'lms':
203  $filter[] = 'lm';
204  $filter[] = 'sahs';
205  $filter[] = 'htlm';
206  break;
207 
208  case 'tst':
209  $filter[] = 'tst';
210  break;
211 
212  case 'fil':
213  $filter[] = 'file';
214  break;
215  }
216  }
217  return $filter ? $filter : array();
218  }
219 
220  public function showForm($result = null)
221  {
222  global $ilUser;
223 
224  $this->tpl->addBlockFile('ADM_CONTENT', 'adm_content', 'tpl.shop_advanced_search.html', 'Services/Payment');
225 
226  $this->tpl->setVariable('TBL_TITLE',$this->lng->txt('advanced_search'));
227  $this->tpl->setVariable('SEARCH_ACTION',$this->ctrl->getFormAction($this));
228  $this->tpl->setVariable('TXT_SEARCHTERM',$this->lng->txt('search_search_term'));
229  $this->tpl->setVariable('TXT_AND',$this->lng->txt('search_all_words'));
230  $this->tpl->setVariable('TXT_OR',$this->lng->txt('search_any_word'));
231  $this->tpl->setVariable('TXT_OBJECT_TYPE',$this->lng->txt('obj_type'));
232  $this->tpl->setVariable('TXT_TOPIC',$this->lng->txt('topic'));
233  $this->tpl->setVariable('BTN_SEARCH',$this->lng->txt('search'));
234 
235  $this->tpl->setVariable('FORM_SEARCH_STR', ilUtil::prepareFormOutput($this->getString(), true));
236 
237  if ($this->getCombination() == self::SEARCH_AND)
238  {
239  $this->tpl->setVariable('AND_CHECKED', 'checked="checked"');
240  }
241  else
242  {
243  $this->tpl->setVariable('OR_CHECKED', 'checked="checked"');
244  }
245 
246  $this->tpl->setVariable('CRS',$this->lng->txt('courses'));
247  $this->tpl->setVariable('LMS',$this->lng->txt('learning_resources'));
248  $this->tpl->setVariable('TST',$this->lng->txt('tests'));
249  $this->tpl->setVariable('FIL',$this->lng->txt('objs_file'));
250  $details = $this->getDetails();
251  $this->tpl->setVariable('CHECK_CRS', ilUtil::formCheckbox($details['crs'] ? 1 : 0,'search[details][crs]', 1));
252  $this->tpl->setVariable('CHECK_LMS', ilUtil::formCheckbox($details['lms'] ? 1 : 0,'search[details][lms]', 1));
253  $this->tpl->setVariable('CHECK_TST', ilUtil::formCheckbox($details['tst'] ? 1 : 0,'search[details][tst]', 1));
254  $this->tpl->setVariable('CHECK_FIL', ilUtil::formCheckbox($details['fil'] ? 1 : 0,'search[details][fil]', 1));
255 
256  $selectable_topics = array();
257  $selectable_topics[''] = $this->lng->txt('search_any');;
258  ilShopTopics::_getInstance()->setIdFilter(false);
259  ilShopTopics::_getInstance()->read();
260  foreach(ilShopTopics::_getInstance()->getTopics() as $oTopic)
261  {
262  $selectable_topics[$oTopic->getId()] = $oTopic->getTitle();
263  }
264 
265  $this->tpl->setVariable('SELECT_TOPIC', ilUtil::formSelect(array($this->getTopicId()), 'search[topic]', $selectable_topics, false, true));
266 
267  // show results
268  if(count($result->getResults()))
269  {
270  include_once 'Services/Payment/classes/class.ilShopResultPresentationGUI.php';
271  $search_result_presentation = new ilShopResultPresentationGUI($result);
272 
273  $this->tpl->setVariable('RESULTS', $search_result_presentation->showResults());
274 
275 
276  $objects = (bool)$this->oGeneralSettings->get('objects_allow_custom_sorting');
277  $topics = (bool)$this->oGeneralSettings->get('topics_allow_custom_sorting');
278  if($objects)
279  {
280  $this->tpl->setCurrentBlock('objects_sort_block');
281  $order_fields = array(
282  'title' => $this->lng->txt('title'),
283  'author' => $this->lng->txt('author'),
284  'price' => $this->lng->txt('price_a')
285  );
286 
287  foreach($order_fields as $key => $value)
288  {
289  $this->tpl->setCurrentBlock('order_field');
290  $this->tpl->setVariable('ORDER_FIELD_VALUE', $key);
291  $this->tpl->setVariable('ORDER_FIELD_TEXT', $value);
292  if (strcmp(trim($this->getSortField()), $key) == 0)
293  {
294  $this->tpl->setVariable('ORDER_FIELD_SELECTED', ' selected="selected"');
295  }
296  $this->tpl->parseCurrentBlock();
297  }
298 
299  $this->tpl->setVariable('SORT_BY_TEXT', $this->lng->txt('sort_by'));
300  $this->tpl->setVariable('ASCENDING_TEXT', $this->lng->txt('sort_asc'));
301  $this->tpl->setVariable('DESCENDING_TEXT', $this->lng->txt('sort_desc'));
302  $this->tpl->setVariable('ORDER_DIRECTION_'.strtoupper(trim($this->getSortDirection())).'_SELECTED', " selected=\"selected\"");
303 
304  $this->tpl->parseCurrentBlock();
305  }
306 
307  if($topics)
308  {
309  $this->tpl->setCurrentBlock('topics_sort_block');
310 
311  $this->tpl->setVariable('SORT_TOPICS_BY_TEXT', $this->lng->txt('sort_topics_by'));
312 
313  $this->tpl->setVariable('SORTING_TYPE_BY_TITLE', ilShopTopics::TOPICS_SORT_BY_TITLE);
314  $this->tpl->setVariable('SORTING_TYPE_BY_TITLE_TEXT', $this->lng->txt('sort_topics_by_title'));
316  {
317  $this->tpl->setVariable('SORTING_TYPE_BY_TITLE_SELECTED', ' selected="selected"');
318  }
319 
320  $this->tpl->setVariable('SORTING_TYPE_BY_DATE', ilShopTopics::TOPICS_SORT_BY_CREATEDATE);
321  $this->tpl->setVariable('SORTING_TYPE_BY_DATE_TEXT', $this->lng->txt('sort_topics_by_date'));
323  {
324  $this->tpl->setVariable('SORTING_TYPE_BY_DATE_SELECTED', ' selected="selected"');
325  }
326 
327  if(ANONYMOUS_USER_ID != $ilUser->getId())
328  {
329  $this->tpl->setCurrentBlock('sort_manually');
330  $this->tpl->setVariable('SORTING_TYPE_MANUALLY', ilShopTopics::TOPICS_SORT_MANUALLY);
331  $this->tpl->setVariable('SORTING_TYPE_MANUALLY_TEXT', $this->lng->txt('sort_topics_manually'));
333  {
334  $this->tpl->setVariable('SORTING_TYPE_MANUALLY_SELECTED', ' selected="selected"');
335  }
336  $this->tpl->parseCurrentBlock();
337  }
338 
339  $this->tpl->setVariable('SORTING_DIRECTION_ASCENDING_TEXT', $this->lng->txt('sort_asc'));
340  $this->tpl->setVariable('SORTING_DIRECTION_DESCENDING_TEXT', $this->lng->txt('sort_desc'));
341  if(in_array(strtoupper($this->getSortingDirectionTopics()), array('ASC', 'DESC')))
342  {
343  $this->tpl->setVariable('SORTING_DIRECTION_'.strtoupper($this->getSortingDirectionTopics()).'_SELECTED',
344  ' selected="selected"');
345  }
346  else
347  {
348  $this->tpl->setVariable('SORTING_DIRECTION_'.strtoupper(ilShopTopics::DEFAULT_SORTING_DIRECTION).'_SELECTED', ' selected="selected"');
349  }
350 
351  $this->tpl->parseCurrentBlock();
352  }
353 
354  if($objects || $topics)
355  {
356  $this->tpl->setCurrentBlock('sort_button');
357 
358  $this->tpl->setVariable('SORTING_FORM_ACTION', $this->ctrl->getFormAction($this, 'setSorting'));
359  $this->tpl->setVariable('CMD_SORT', 'setSorting');
360  $this->tpl->setVariable('SORT_TEXT', $this->lng->txt('sort'));
361  $this->tpl->parseCurrentBlock();
362  }
363 
364 
365  $this->tpl->setCurrentBlock('sorting');
366  $this->tpl->parseCurrentBlock();
367  }
368 
369  $this->addPager($result, 'shop_advanced_search_maxpage');
370 
371  return true;
372  }
373 
374  public function setCombination($a_combination)
375  {
376  $_SESSION['shop_advanced_search']['combination'] = $this->combination = $a_combination;
377  }
378  public function getCombination()
379  {
380  return $this->combination ? $this->combination : self::SEARCH_OR;
381  }
382  public function setString($a_str)
383  {
384  $_SESSION['shop_advanced_search']['string'] = $this->string = $a_str;
385  }
386  public function getString()
387  {
388  return $this->string;
389  }
390  public function setDetails($a_details)
391  {
392  $_SESSION['shop_advanced_search']['details'] = $this->details = $a_details;
393  }
394  public function getDetails()
395  {
396  return $this->details ? $this->details : array();
397  }
398  public function setTopicId($a_topic)
399  {
400  $_SESSION['shop_advanced_search']['topic'] = $this->topic_id = $a_topic;
401  }
402  public function getTopicId()
403  {
404  return $this->topic_id;
405  }
406 
407  public function setSortDirection($a_sort_direction)
408  {
409  $_SESSION['shop_advanced_search']['order_direction'] = $this->sort_direction = $a_sort_direction;
410  }
411  public function getSortDirection()
412  {
413  return $this->sort_direction;
414  }
415  public function setSortField($a_field)
416  {
417  $_SESSION['shop_advanced_search']['shop_order_field'] = $this->sort_field = $a_field;
418  }
419  public function getSortField()
420  {
421  return $this->sort_field;
422  }
423  public function setSortingTypeTopics($a_field)
424  {
425  global $ilUser;
426 
427  if(ANONYMOUS_USER_ID == $ilUser->getId() &&
429  {
431  }
432 
433  $_SESSION['shop_advanced_search']['order_topics_sorting_type'] = $this->sort_type_topics = $a_field;
434  }
435  public function getSortingTypeTopics()
436  {
437  global $ilUser;
438 
439  if(ANONYMOUS_USER_ID == $ilUser->getId() &&
440  $this->sort_type_topics == ilShopTopics::TOPICS_SORT_MANUALLY)
441  {
442  $this->sort_type_topics = ilShopTopics::TOPICS_SORT_BY_TITLE;
443  }
444 
446  }
447  public function setSortingDirectionTopics($a_sort_direction)
448  {
449  $_SESSION['shop_advanced_search']['shop_topics_sorting_direction'] = $this->sort_direction_topics = $a_sort_direction;
450  }
451  public function getSortingDirectionTopics()
452  {
454  }
455 
456  protected function prepareOutput()
457  {
458  global $ilTabs;
459 
461 
462  $ilTabs->setTabActive('advanced_search');
463  }
464 }
465 ?>