Add source code for fourth part of tutorial series
[GitHub/WoltLab/woltlab.github.io.git] / snippets / tutorial / tutorial-series / part-4 / files / lib / system / page / handler / PersonPageHandler.class.php
1 <?php
2
3 namespace wcf\system\page\handler;
4
5 use wcf\data\page\Page;
6 use wcf\data\person\PersonList;
7 use wcf\data\user\online\UserOnline;
8 use wcf\system\cache\runtime\PersonRuntimeCache;
9 use wcf\system\database\util\PreparedStatementConditionBuilder;
10 use wcf\system\WCF;
11
12 /**
13 * Page handler implementation for person page.
14 *
15 * @author Matthias Schmidt
16 * @copyright 2001-2019 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package WoltLabSuite\Core\System\Page\Handler
19 */
20 class PersonPageHandler extends AbstractLookupPageHandler implements IOnlineLocationPageHandler
21 {
22 use TOnlineLocationPageHandler;
23
24 /**
25 * @inheritDoc
26 */
27 public function getLink($objectID)
28 {
29 return PersonRuntimeCache::getInstance()->getObject($objectID)->getLink();
30 }
31
32 /**
33 * Returns the textual description if a user is currently online viewing this page.
34 *
35 * @see IOnlineLocationPageHandler::getOnlineLocation()
36 *
37 * @param Page $page visited page
38 * @param UserOnline $user user online object with request data
39 * @return string
40 */
41 public function getOnlineLocation(Page $page, UserOnline $user)
42 {
43 if ($user->pageObjectID === null) {
44 return '';
45 }
46
47 $person = PersonRuntimeCache::getInstance()->getObject($user->pageObjectID);
48 if ($person === null) {
49 return '';
50 }
51
52 return WCF::getLanguage()->getDynamicVariable('wcf.page.onlineLocation.' . $page->identifier, ['person' => $person]);
53 }
54
55 /**
56 * @inheritDoc
57 */
58 public function isValid($objectID = null)
59 {
60 return PersonRuntimeCache::getInstance()->getObject($objectID) !== null;
61 }
62
63 /**
64 * @inheritDoc
65 */
66 public function lookup($searchString)
67 {
68 $conditionBuilder = new PreparedStatementConditionBuilder(false, 'OR');
69 $conditionBuilder->add('person.firstName LIKE ?', ['%' . $searchString . '%']);
70 $conditionBuilder->add('person.lastName LIKE ?', ['%' . $searchString . '%']);
71
72 $personList = new PersonList();
73 $personList->getConditionBuilder()->add($conditionBuilder, $conditionBuilder->getParameters());
74 $personList->readObjects();
75
76 $results = [];
77 foreach ($personList as $person) {
78 $results[] = [
79 'image' => 'fa-user',
80 'link' => $person->getLink(),
81 'objectID' => $person->personID,
82 'title' => $person->getTitle(),
83 ];
84 }
85
86 return $results;
87 }
88
89 /**
90 * Prepares fetching all necessary data for the textual description if a user is currently online
91 * viewing this page.
92 *
93 * @see IOnlineLocationPageHandler::prepareOnlineLocation()
94 *
95 * @param Page $page visited page
96 * @param UserOnline $user user online object with request data
97 */
98 public function prepareOnlineLocation(Page $page, UserOnline $user)
99 {
100 if ($user->pageObjectID !== null) {
101 PersonRuntimeCache::getInstance()->cacheObjectID($user->pageObjectID);
102 }
103 }
104 }