Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / page / SearchResultPage.class.php
CommitLineData
749cca08
MW
1<?php
2namespace wcf\page;
9fa8ee15 3use wcf\data\search\ISearchResultObject;
749cca08 4use wcf\data\search\Search;
03cd5854 5use wcf\system\application\ApplicationHandler;
749cca08
MW
6use wcf\system\event\EventHandler;
7use wcf\system\exception\IllegalLinkException;
7b9ff46b 8use wcf\system\exception\ImplementationException;
ac4ff35d 9use wcf\system\page\PageLocationManager;
749cca08
MW
10use wcf\system\search\SearchEngine;
11use wcf\system\WCF;
12
13/**
14 * Shows the result of a search request.
15 *
16 * @author Marcel Werk
c839bd49 17 * @copyright 2001-2018 WoltLab GmbH
749cca08 18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 19 * @package WoltLabSuite\Core\Page
749cca08
MW
20 */
21class SearchResultPage extends MultipleLinkPage {
22 /**
0fcfe5f6 23 * @inheritDoc
749cca08
MW
24 */
25 public $itemsPerPage = SEARCH_RESULTS_PER_PAGE;
26
27 /**
28 * highlight string
29 * @var string
30 */
31 public $highlight = '';
32
33 /**
34 * search id
35 * @var integer
36 */
37 public $searchID = 0;
38
39 /**
40 * search object
4e25add7 41 * @var Search
749cca08
MW
42 */
43 public $search = null;
44
45 /**
46 * messages
47 * @var array
48 */
058cbd6a 49 public $messages = [];
749cca08
MW
50
51 /**
52 * search data
53 * @var array
54 */
55 public $searchData = null;
56
57 /**
06355ec3
MS
58 * result list template
59 * @var string
749cca08
MW
60 */
61 public $resultListTemplateName = 'searchResultList';
62
63 /**
64 * result list template's application
06355ec3 65 * @var string
749cca08
MW
66 */
67 public $resultListApplication = 'wcf';
68
69 /**
0fcfe5f6 70 * @inheritDoc
749cca08
MW
71 */
72 public function readParameters() {
73 parent::readParameters();
74
75 if (isset($_REQUEST['highlight'])) $this->highlight = $_REQUEST['highlight'];
76 if (isset($_REQUEST['id'])) $this->searchID = intval($_REQUEST['id']);
77 $this->search = new Search($this->searchID);
78 if (!$this->search->searchID || $this->search->searchType != 'messages') {
79 throw new IllegalLinkException();
80 }
81 if ($this->search->userID && $this->search->userID != WCF::getUser()->userID) {
82 throw new IllegalLinkException();
83 }
84
85 // get search data
86 $this->searchData = unserialize($this->search->searchData);
749cca08
MW
87 }
88
89 /**
0fcfe5f6 90 * @inheritDoc
749cca08
MW
91 */
92 public function readData() {
93 parent::readData();
94
95 // cache message data
96 $this->cacheMessageData();
97
98 // get messages
99 $this->readMessages();
231573d1
MW
100
101 // set active menu item
ea15e565 102 if (isset($this->searchData['selectedObjectTypes']) && count($this->searchData['selectedObjectTypes']) == 1) {
f28a031b 103 $objectType = SearchEngine::getInstance()->getObjectType(reset($this->searchData['selectedObjectTypes']));
c2804c28 104 $objectType->setLocation();
231573d1 105 }
78edd448
MW
106
107 // add breadcrumbs
ac4ff35d 108 PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.Search');
749cca08
MW
109 }
110
111 /**
112 * Caches the message data.
113 */
114 protected function cacheMessageData() {
058cbd6a 115 $types = [];
749cca08
MW
116
117 // group object id by object type
118 for ($i = $this->startIndex - 1; $i < $this->endIndex; $i++) {
119 $type = $this->searchData['results'][$i]['objectType'];
120 $objectID = $this->searchData['results'][$i]['objectID'];
121
058cbd6a 122 if (!isset($types[$type])) $types[$type] = [];
749cca08
MW
123 $types[$type][] = $objectID;
124 }
125
126 foreach ($types as $type => $objectIDs) {
127 $objectType = SearchEngine::getInstance()->getObjectType($type);
128 $objectType->cacheObjects($objectIDs, (isset($this->searchData['additionalData'][$type]) ? $this->searchData['additionalData'][$type] : null));
129 }
130 }
131
132 /**
9fa8ee15 133 * Reads the data of the search result messages.
749cca08
MW
134 */
135 protected function readMessages() {
136 for ($i = $this->startIndex - 1; $i < $this->endIndex; $i++) {
137 $type = $this->searchData['results'][$i]['objectType'];
138 $objectID = $this->searchData['results'][$i]['objectID'];
139
140 $objectType = SearchEngine::getInstance()->getObjectType($type);
141 if (($message = $objectType->getObject($objectID)) !== null) {
9fa8ee15 142 if (!($message instanceof ISearchResultObject)) {
7b9ff46b 143 throw new ImplementationException(get_class($message), ISearchResultObject::class);
9fa8ee15
MS
144 }
145
749cca08
MW
146 $this->messages[] = $message;
147 }
148 }
149 }
150
151 /**
0fcfe5f6 152 * @inheritDoc
749cca08
MW
153 */
154 public function assignVariables() {
155 parent::assignVariables();
156
5f43eff7
AE
157 $searchPreselectObjectType = 'everywhere';
158 if (count($this->searchData['selectedObjectTypes']) === 1) $searchPreselectObjectType = reset($this->searchData['selectedObjectTypes']);
159
058cbd6a 160 WCF::getTPL()->assign([
749cca08
MW
161 'query' => $this->searchData['query'],
162 'objects' => $this->messages,
163 'searchData' => $this->searchData,
164 'searchID' => $this->searchID,
165 'highlight' => $this->highlight,
166 'sortField' => $this->searchData['sortField'],
167 'sortOrder' => $this->searchData['sortOrder'],
63b9817b 168 'alterable' => !empty($this->searchData['alterable']) ? 1 : 0,
749cca08
MW
169 'objectTypes' => SearchEngine::getInstance()->getAvailableObjectTypes(),
170 'resultListTemplateName' => $this->resultListTemplateName,
03cd5854 171 'resultListApplication' => $this->resultListApplication,
5f43eff7
AE
172 'application' => ApplicationHandler::getInstance()->getAbbreviation(ApplicationHandler::getInstance()->getActiveApplication()->packageID),
173 'searchPreselectObjectType' => $searchPreselectObjectType
058cbd6a 174 ]);
749cca08
MW
175 }
176
177 /**
0fcfe5f6 178 * @inheritDoc
749cca08
MW
179 */
180 public function countItems() {
181 // call countItems event
182 EventHandler::getInstance()->fireAction($this, 'countItems');
183
184 return count($this->searchData['results']);
185 }
186
187 /**
0fcfe5f6 188 * @inheritDoc
749cca08
MW
189 */
190 protected function initObjectList() { }
191
192 /**
0fcfe5f6 193 * @inheritDoc
749cca08
MW
194 */
195 protected function readObjects() { }
196}