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