Restrict positions for UserListBox
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / box / UserListBoxController.class.php
1 <?php
2
3 namespace wcf\system\box;
4
5 use wcf\data\condition\Condition;
6 use wcf\data\DatabaseObject;
7 use wcf\data\user\UserProfileList;
8 use wcf\system\cache\builder\MostActiveMembersCacheBuilder;
9 use wcf\system\cache\builder\MostLikedMembersCacheBuilder;
10 use wcf\system\cache\builder\NewestMembersCacheBuilder;
11 use wcf\system\cache\runtime\UserProfileRuntimeCache;
12 use wcf\system\condition\IObjectListCondition;
13 use wcf\system\event\EventHandler;
14 use wcf\system\request\LinkHandler;
15 use wcf\system\WCF;
16
17 /**
18 * Box controller for a list of users.
19 *
20 * @author Matthias Schmidt
21 * @copyright 2001-2019 WoltLab GmbH
22 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
23 * @since 3.0
24 */
25 class UserListBoxController extends AbstractDatabaseObjectListBoxController
26 {
27 /**
28 * maps special sort fields to cache builders
29 * @var string[]
30 */
31 public $cacheBuilders = [
32 'activityPoints' => MostActiveMembersCacheBuilder::class,
33 'likesReceived' => MostLikedMembersCacheBuilder::class,
34 'registrationDate' => NewestMembersCacheBuilder::class,
35 ];
36
37 /**
38 * @inheritDoc
39 */
40 protected $conditionDefinition = 'com.woltlab.wcf.box.userList.condition';
41
42 /**
43 * @inheritDoc
44 */
45 protected static $supportedPositions = ['sidebarLeft', 'sidebarRight'];
46
47 /**
48 * @inheritDoc
49 */
50 public $defaultLimit = 5;
51
52 /**
53 * @inheritDoc
54 */
55 protected $sortFieldLanguageItemPrefix = 'wcf.user.sortField';
56
57 /**
58 * ids of the shown users loaded from cache
59 * @var int[]|null
60 */
61 public $userIDs;
62
63 /**
64 * @inheritDoc
65 */
66 public $validSortFields = [
67 'username',
68 'activityPoints',
69 'registrationDate',
70 ];
71
72 /**
73 * @inheritDoc
74 */
75 public function __construct()
76 {
77 if (!empty($this->validSortFields) && MODULE_LIKE) {
78 $this->validSortFields[] = 'likesReceived';
79 }
80
81 parent::__construct();
82 }
83
84 /**
85 * @inheritDoc
86 */
87 public function getLink(): string
88 {
89 if (MODULE_MEMBERS_LIST) {
90 $parameters = '';
91 if ($this->sortField) {
92 $parameters = 'sortField=' . $this->sortField . '&sortOrder=' . $this->sortOrder;
93 }
94
95 return LinkHandler::getInstance()->getLink('MembersList', [], $parameters);
96 }
97
98 return '';
99 }
100
101 /**
102 * @inheritDoc
103 */
104 protected function getObjectList()
105 {
106 // use specialized cache builders
107 if ($this->sortOrder && $this->sortField && isset($this->cacheBuilders[$this->sortField])) {
108 $conditions = \array_filter($this->box->getConditions(), static function (Condition $condition) {
109 return $condition->getObjectType()->getProcessor() instanceof IObjectListCondition;
110 });
111
112 $this->userIDs = \call_user_func([$this->cacheBuilders[$this->sortField], 'getInstance'])->getData([
113 'conditions' => $conditions,
114 'limit' => $this->limit,
115 'sortOrder' => $this->sortOrder,
116 ]);
117 }
118
119 if ($this->userIDs !== null) {
120 UserProfileRuntimeCache::getInstance()->cacheObjectIDs($this->userIDs);
121 }
122
123 return new UserProfileList();
124 }
125
126 /**
127 * @inheritDoc
128 */
129 protected function getTemplate()
130 {
131 $userProfiles = [];
132 if ($this->userIDs !== null) {
133 $userProfiles = UserProfileRuntimeCache::getInstance()->getObjects($this->userIDs);
134
135 // filter `null` values of users that have been deleted in the meantime
136 $userProfiles = \array_filter($userProfiles, static function ($userProfile) {
137 return $userProfile !== null;
138 });
139
140 DatabaseObject::sort($userProfiles, $this->sortField, $this->sortOrder);
141 }
142
143 return WCF::getTPL()->fetch('boxUserList', 'wcf', [
144 'boxUsers' => $this->userIDs !== null ? $userProfiles : $this->objectList->getObjects(),
145 'boxSortField' => $this->box->sortField,
146 ], true);
147 }
148
149 /**
150 * @inheritDoc
151 */
152 public function hasContent()
153 {
154 $hasContent = parent::hasContent();
155
156 if ($this->userIDs !== null) {
157 return !empty($this->userIDs);
158 }
159
160 return $hasContent;
161 }
162
163 /**
164 * @inheritDoc
165 */
166 public function hasLink()
167 {
168 return MODULE_MEMBERS_LIST == 1;
169 }
170
171 /**
172 * @inheritDoc
173 */
174 public function readObjects()
175 {
176 if ($this->userIDs === null) {
177 parent::readObjects();
178 } else {
179 EventHandler::getInstance()->fireAction($this, 'readObjects');
180 }
181 }
182 }