SearchController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
25 
26 /**
27  * SearchController executes a search and returns matching objects in a paged list.
28  * Internally it uses Zend Lucene indexed search.
29  *
30  * The controller supports the following actions:
31  *
32  * <div class="controller-action">
33  * <div> __Action__ _default_ </div>
34  * <div>
35  * Search.
36  * | Parameter | Description
37  * |------------------------|-------------------------
38  * | _in_ / _out_ `query` | The query string
39  * | __Response Actions__ | |
40  * | `ok` | In all cases
41  * </div>
42  * </div>
43  *
44  * For additional actions and parameters see [ListController actions](@ref ListController).
45  *
46  * @author ingo herwig <ingo@wemove.com>
47  */
49 
50  private $hits = [];
51  private $search = null;
52 
53  /**
54  * Constructor
55  * @param $session
56  * @param $persistenceFacade
57  * @param $permissionManager
58  * @param $actionMapper
59  * @param $localization
60  * @param $message
61  * @param $configuration
62  * @param $search
63  */
64  public function __construct(Session $session,
65  PersistenceFacade $persistenceFacade,
66  PermissionManager $permissionManager,
67  ActionMapper $actionMapper,
68  Localization $localization,
69  Message $message,
70  Configuration $configuration,
71  Search $search) {
72  parent::__construct($session, $persistenceFacade, $permissionManager,
73  $actionMapper, $localization, $message, $configuration);
74  $this->search = $search;
75  }
76 
77  /**
78  * @see Controller::validate()
79  */
80  protected function validate() {
81  // skip validation
82  return true;
83  }
84 
85  /**
86  * @see ListController::getObjects()
87  */
88  protected function getObjects($type, $queryCondition, $sortArray, $pagingInfo) {
89  $permissionManager = $this->getPermissionManager();
90 
91  // search with searchterm (even if empty) if no query is given
92  $this->hits = $this->search->find($queryCondition, $pagingInfo);
93 
94  $oids = [];
95  foreach ($this->hits as $hit) {
96  $oids[] = ObjectId::parse($hit['oid']);
97  }
98 
99  // load the objects
100  $persistenceFacade = $this->getPersistenceFacade();
101  $objects = [];
102  foreach($oids as $oid) {
103  if ($permissionManager->authorize($oid, '', PersistenceAction::READ)) {
104  $obj = $persistenceFacade->load($oid);
105  $objects[] = $obj;
106  }
107  }
108  return $objects;
109  }
110 
111  /**
112  * @see ListController::modifyModel()
113  */
114  protected function modifyModel(&$nodes) {
115  parent::modifyModel($nodes);
116 
117  // add common values
118  $persistenceFacade = $this->getPersistenceFacade();
119  for ($i=0, $count=sizeof($nodes); $i<$count; $i++) {
120  $curNode = $nodes[$i];
121  $hit = $this->hits[$curNode->getOID()->__toString()];
122  $curNode->setValue('_displayValue', $curNode->getDisplayValue(), true);
123  $curNode->setValue('_summary', "... ".$hit['summary']." ...", true);
124  $curNode->setValue('_type', $persistenceFacade->getSimpleType($curNode->getType()), true);
125  }
126 
127  // sort
128  $request = $this->getRequest();
129  if ($request->hasValue('sortFieldName')) {
130  $sortDir = $request->hasValue('sortDirection') ? $request->getValue('sortDirection') : 'asc';
131  $sortCriteria = [
132  $request->getValue('sortFieldName') => $sortDir == 'asc' ?
134  ];
135  $comparator = new ObjectComparator($sortCriteria);
136  usort($nodes, [$comparator, 'compare']);
137  }
138  }
139 }
140 ?>