Commit | Line | Data |
---|---|---|
e3369fd2 | 1 | <?php |
320f4a6d MW |
2 | namespace wcf\form; |
3 | use wcf\acp\form\UserOptionListForm; | |
4 | use wcf\data\search\SearchEditor; | |
320f4a6d MW |
5 | use wcf\system\database\util\PreparedStatementConditionBuilder; |
6 | use wcf\system\exception\UserInputException; | |
ac4ff35d | 7 | use wcf\system\page\PageLocationManager; |
320f4a6d | 8 | use wcf\system\request\LinkHandler; |
320f4a6d MW |
9 | use wcf\system\WCF; |
10 | use wcf\util\HeaderUtil; | |
11 | use wcf\util\StringUtil; | |
12 | ||
13 | /** | |
14 | * Shows the user search form. | |
15 | * | |
16 | * @author Marcel Werk | |
83aa1dc1 | 17 | * @copyright 2001-2016 WoltLab GmbH |
320f4a6d MW |
18 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
19 | * @package com.woltlab.wcf | |
20 | * @subpackage form | |
21 | * @category Community Framework | |
22 | */ | |
23 | class UserSearchForm extends UserOptionListForm { | |
789a109c | 24 | /** |
83aa1dc1 | 25 | * @inheritDoc |
789a109c | 26 | */ |
83aa1dc1 | 27 | public $neededModules = ['MODULE_MEMBERS_LIST']; |
789a109c | 28 | |
320f4a6d MW |
29 | /** |
30 | * username | |
31 | * @var string | |
32 | */ | |
33 | public $username = ''; | |
34 | ||
35 | /** | |
36 | * matches | |
83aa1dc1 | 37 | * @var integer[] |
320f4a6d | 38 | */ |
83aa1dc1 | 39 | public $matches = []; |
320f4a6d MW |
40 | |
41 | /** | |
592454d4 MS |
42 | * condition builder object |
43 | * @var PreparedStatementConditionBuilder | |
320f4a6d MW |
44 | */ |
45 | public $conditions = null; | |
46 | ||
47 | /** | |
48 | * search id | |
49 | * @var integer | |
50 | */ | |
51 | public $searchID = 0; | |
52 | ||
53 | /** | |
54 | * number of results | |
55 | * @var integer | |
56 | */ | |
57 | public $maxResults = 1000; | |
58 | ||
59 | /** | |
83aa1dc1 MS |
60 | * option tree |
61 | * @var array | |
62 | */ | |
63 | public $optionTree = []; | |
64 | ||
65 | /** | |
66 | * @inheritDoc | |
320f4a6d MW |
67 | */ |
68 | public function readFormParameters() { | |
69 | parent::readFormParameters(); | |
70 | ||
71 | if (isset($_POST['username'])) $this->username = StringUtil::trim($_POST['username']); | |
72 | } | |
73 | ||
74 | /** | |
83aa1dc1 | 75 | * @inheritDoc |
320f4a6d MW |
76 | */ |
77 | protected function initOptionHandler() { | |
78 | $this->optionHandler->enableSearchMode(); | |
79 | $this->optionHandler->init(); | |
80 | } | |
81 | ||
82 | /** | |
83aa1dc1 | 83 | * @inheritDoc |
320f4a6d MW |
84 | */ |
85 | public function readData() { | |
86 | parent::readData(); | |
87 | ||
88 | $this->readOptionTree(); | |
89 | ||
90 | // add breadcrumbs | |
ac4ff35d | 91 | if (MODULE_MEMBERS_LIST) PageLocationManager::getInstance()->addParentLocation('com.woltlab.wcf.MembersList'); |
320f4a6d MW |
92 | } |
93 | ||
94 | /** | |
95 | * Reads option tree on page init. | |
96 | */ | |
97 | protected function readOptionTree() { | |
98 | $this->optionTree = $this->optionHandler->getOptionTree(); | |
99 | } | |
100 | ||
101 | /** | |
83aa1dc1 | 102 | * @inheritDoc |
320f4a6d MW |
103 | */ |
104 | public function assignVariables() { | |
105 | parent::assignVariables(); | |
106 | ||
83aa1dc1 | 107 | WCF::getTPL()->assign([ |
320f4a6d | 108 | 'username' => $this->username, |
b1a21267 | 109 | 'optionTree' => $this->optionTree |
83aa1dc1 | 110 | ]); |
320f4a6d MW |
111 | } |
112 | ||
113 | /** | |
83aa1dc1 | 114 | * @inheritDoc |
320f4a6d MW |
115 | */ |
116 | public function save() { | |
117 | parent::save(); | |
118 | ||
119 | // store search result in database | |
83aa1dc1 | 120 | $search = SearchEditor::create([ |
320f4a6d | 121 | 'userID' => WCF::getUser()->userID ?: null, |
83aa1dc1 | 122 | 'searchData' => serialize(['matches' => $this->matches]), |
320f4a6d MW |
123 | 'searchTime' => TIME_NOW, |
124 | 'searchType' => 'users' | |
83aa1dc1 | 125 | ]); |
320f4a6d MW |
126 | |
127 | // get new search id | |
128 | $this->searchID = $search->searchID; | |
129 | $this->saved(); | |
130 | ||
131 | // forward to result page | |
83aa1dc1 | 132 | $url = LinkHandler::getInstance()->getLink('MembersList', ['id' => $this->searchID]); |
320f4a6d MW |
133 | HeaderUtil::redirect($url); |
134 | exit; | |
135 | } | |
136 | ||
137 | /** | |
83aa1dc1 | 138 | * @inheritDoc |
320f4a6d MW |
139 | */ |
140 | public function validate() { | |
141 | AbstractForm::validate(); | |
142 | ||
143 | // do search | |
144 | $this->search(); | |
145 | ||
146 | if (empty($this->matches)) { | |
147 | throw new UserInputException('search', 'noMatches'); | |
148 | } | |
149 | } | |
150 | ||
151 | /** | |
152 | * Search for users which fit to the search values. | |
153 | */ | |
154 | protected function search() { | |
83aa1dc1 | 155 | $this->matches = []; |
320f4a6d MW |
156 | $sql = "SELECT user_table.userID |
157 | FROM wcf".WCF_N."_user user_table | |
158 | LEFT JOIN wcf".WCF_N."_user_option_value option_value | |
159 | ON (option_value.userID = user_table.userID)"; | |
160 | ||
161 | // build search condition | |
162 | $this->conditions = new PreparedStatementConditionBuilder(); | |
163 | ||
164 | // static fields | |
165 | $this->buildStaticConditions(); | |
166 | ||
167 | // dynamic fields | |
168 | $this->buildDynamicConditions(); | |
169 | ||
567b90a3 | 170 | // if no conditions exists, no need to send query |
4e09aa1a | 171 | if (!$this->conditions->__toString()) { |
567b90a3 MS |
172 | return; |
173 | } | |
174 | ||
320f4a6d MW |
175 | // do search |
176 | $statement = WCF::getDB()->prepareStatement($sql.$this->conditions, $this->maxResults); | |
177 | $statement->execute($this->conditions->getParameters()); | |
cd975610 | 178 | $this->matches = $statement->fetchAll(\PDO::FETCH_COLUMN); |
320f4a6d MW |
179 | } |
180 | ||
181 | /** | |
182 | * Builds the static conditions. | |
183 | */ | |
184 | protected function buildStaticConditions() { | |
185 | if (!empty($this->username)) { | |
83aa1dc1 | 186 | $this->conditions->add("user_table.username LIKE ?", ['%'.addcslashes($this->username, '_%').'%']); |
320f4a6d MW |
187 | } |
188 | } | |
189 | ||
190 | /** | |
191 | * Builds the dynamic conditions. | |
192 | */ | |
193 | protected function buildDynamicConditions() { | |
194 | foreach ($this->optionHandler->getCategoryOptions('profile') as $option) { | |
195 | $option = $option['object']; | |
196 | ||
197 | $value = isset($this->optionHandler->optionValues[$option->optionName]) ? $this->optionHandler->optionValues[$option->optionName] : null; | |
198 | $this->optionHandler->getTypeObject($option->optionType)->getCondition($this->conditions, $option, $value); | |
199 | } | |
200 | } | |
201 | } |