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