Commit | Line | Data |
---|---|---|
320f4a6d MW |
1 | <?php |
2 | namespace wcf\data\user\follow; | |
3 | use wcf\data\user\UserProfile; | |
4 | use wcf\data\AbstractDatabaseObjectAction; | |
5 | use wcf\data\IGroupedUserListAction; | |
6 | use wcf\system\exception\PermissionDeniedException; | |
7 | use wcf\system\exception\UserInputException; | |
8 | use wcf\system\user\activity\event\UserActivityEventHandler; | |
9 | use wcf\system\user\notification\object\UserFollowUserNotificationObject; | |
10 | use wcf\system\user\notification\UserNotificationHandler; | |
11 | use wcf\system\user\storage\UserStorageHandler; | |
12 | use wcf\system\user\GroupedUserList; | |
13 | use wcf\system\WCF; | |
14 | ||
15 | /** | |
16 | * Executes follower-related actions. | |
17 | * | |
18 | * @author Alexander Ebert | |
2b6cb5c2 | 19 | * @copyright 2001-2015 WoltLab GmbH |
320f4a6d | 20 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
f4f05aa5 | 21 | * @package com.woltlab.wcf |
320f4a6d MW |
22 | * @subpackage data.user.follow |
23 | * @category Community Framework | |
24 | */ | |
25 | class UserFollowAction extends AbstractDatabaseObjectAction implements IGroupedUserListAction { | |
26 | /** | |
0ad90fc3 | 27 | * @see \wcf\data\AbstractDatabaseObjectAction::$allowGuestAccess |
320f4a6d MW |
28 | */ |
29 | protected $allowGuestAccess = array('getGroupedUserList'); | |
30 | ||
31 | /** | |
32 | * user profile object | |
0ad90fc3 | 33 | * @var \wcf\data\user\UserProfile; |
320f4a6d MW |
34 | */ |
35 | public $userProfile = null; | |
36 | ||
37 | /** | |
38 | * Validates given parameters. | |
39 | */ | |
40 | public function validateFollow() { | |
41 | $this->readInteger('userID', false, 'data'); | |
42 | ||
43 | // validate if you're retarded | |
44 | if ($this->parameters['data']['userID'] == WCF::getUser()->userID) { | |
45 | throw new PermissionDeniedException(); | |
46 | } | |
47 | } | |
48 | ||
49 | /** | |
50 | * Follows an user. | |
51 | * | |
52 | * @return array | |
53 | */ | |
54 | public function follow() { | |
55 | $follow = UserFollow::getFollow(WCF::getUser()->userID, $this->parameters['data']['userID']); | |
56 | ||
57 | // not following right now | |
58 | if (!$follow->followID) { | |
59 | $follow = UserFollowEditor::create(array( | |
60 | 'userID' => WCF::getUser()->userID, | |
61 | 'followUserID' => $this->parameters['data']['userID'], | |
62 | 'time' => TIME_NOW | |
63 | )); | |
64 | ||
65 | // send notification | |
66 | UserNotificationHandler::getInstance()->fireEvent('following', 'com.woltlab.wcf.user.follow', new UserFollowUserNotificationObject($follow), array($follow->followUserID)); | |
67 | ||
68 | // fire activity event | |
69 | UserActivityEventHandler::getInstance()->fireEvent('com.woltlab.wcf.user.recentActivityEvent.follow', $this->parameters['data']['userID']); | |
70 | ||
71 | // reset storage | |
72 | UserStorageHandler::getInstance()->reset(array($this->parameters['data']['userID']), 'followerUserIDs'); | |
73 | UserStorageHandler::getInstance()->reset(array(WCF::getUser()->userID), 'followingUserIDs'); | |
74 | } | |
75 | ||
76 | return array( | |
77 | 'following' => 1 | |
78 | ); | |
79 | } | |
80 | ||
81 | /** | |
0ad90fc3 | 82 | * @see \wcf\data\user\follow\UserFollowAction::validateFollow() |
320f4a6d MW |
83 | */ |
84 | public function validateUnfollow() { | |
85 | $this->validateFollow(); | |
86 | } | |
87 | ||
88 | /** | |
89 | * Stops following an user. | |
90 | * | |
91 | * @return array | |
92 | */ | |
93 | public function unfollow() { | |
94 | $follow = UserFollow::getFollow(WCF::getUser()->userID, $this->parameters['data']['userID']); | |
95 | ||
96 | if ($follow->followID) { | |
97 | $followEditor = new UserFollowEditor($follow); | |
98 | $followEditor->delete(); | |
99 | ||
100 | // remove activity event | |
585892fa | 101 | UserActivityEventHandler::getInstance()->removeEvent('com.woltlab.wcf.user.recentActivityEvent.follow', $this->parameters['data']['userID']); |
320f4a6d MW |
102 | } |
103 | ||
104 | // reset storage | |
105 | UserStorageHandler::getInstance()->reset(array($this->parameters['data']['userID']), 'followerUserIDs'); | |
106 | UserStorageHandler::getInstance()->reset(array(WCF::getUser()->userID), 'followingUserIDs'); | |
107 | ||
108 | return array( | |
109 | 'following' => 0 | |
110 | ); | |
111 | } | |
112 | ||
113 | /** | |
0ad90fc3 | 114 | * @see \wcf\data\AbstractDatabaseObjectAction::validateDelete() |
320f4a6d MW |
115 | */ |
116 | public function validateDelete() { | |
117 | // read objects | |
118 | if (empty($this->objects)) { | |
119 | $this->readObjects(); | |
120 | ||
121 | if (empty($this->objects)) { | |
122 | throw new UserInputException('objectIDs'); | |
123 | } | |
124 | } | |
125 | ||
126 | // validate ownership | |
127 | foreach ($this->objects as $follow) { | |
128 | if ($follow->userID != WCF::getUser()->userID) { | |
129 | throw new PermissionDeniedException(); | |
130 | } | |
131 | } | |
132 | } | |
133 | ||
134 | /** | |
0ad90fc3 | 135 | * @see \wcf\data\AbstractDatabaseObjectAction::delete() |
320f4a6d MW |
136 | */ |
137 | public function delete() { | |
138 | $returnValues = parent::delete(); | |
139 | ||
140 | $followUserIDs = array(); | |
141 | foreach ($this->objects as $follow) { | |
142 | $followUserIDs[] = $follow->followUserID; | |
143 | // remove activity event | |
144 | UserActivityEventHandler::getInstance()->removeEvents('com.woltlab.wcf.user.recentActivityEvent.follow', array($follow->followUserID)); | |
145 | } | |
146 | ||
147 | // reset storage | |
148 | UserStorageHandler::getInstance()->reset($followUserIDs, 'followerUserIDs'); | |
149 | UserStorageHandler::getInstance()->reset(array(WCF::getUser()->userID), 'followingUserIDs'); | |
150 | ||
151 | return $returnValues; | |
152 | } | |
153 | ||
154 | /** | |
0ad90fc3 | 155 | * @see \wcf\data\IGroupedUserListAction::validateGetGroupedUserList() |
320f4a6d MW |
156 | */ |
157 | public function validateGetGroupedUserList() { | |
158 | $this->readInteger('pageNo'); | |
159 | $this->readInteger('userID'); | |
160 | ||
161 | $this->userProfile = UserProfile::getUserProfile($this->parameters['userID']); | |
162 | if ($this->userProfile->isProtected()) { | |
163 | throw new PermissionDeniedException(); | |
164 | } | |
165 | } | |
166 | ||
167 | /** | |
0ad90fc3 | 168 | * @see \wcf\data\IGroupedUserListAction::getGroupedUserList() |
320f4a6d MW |
169 | */ |
170 | public function getGroupedUserList() { | |
171 | // resolve page count | |
172 | $sql = "SELECT COUNT(*) AS count | |
173 | FROM wcf".WCF_N."_user_follow | |
174 | WHERE followUserID = ?"; | |
175 | $statement = WCF::getDB()->prepareStatement($sql); | |
176 | $statement->execute(array($this->parameters['userID'])); | |
177 | $row = $statement->fetchArray(); | |
178 | $pageCount = ceil($row['count'] / 20); | |
179 | ||
180 | // get user ids | |
181 | $sql = "SELECT userID | |
182 | FROM wcf".WCF_N."_user_follow | |
183 | WHERE followUserID = ?"; | |
184 | $statement = WCF::getDB()->prepareStatement($sql, 20, ($this->parameters['pageNo'] - 1) * 20); | |
185 | $statement->execute(array($this->parameters['userID'])); | |
186 | $userIDs = array(); | |
187 | while ($row = $statement->fetchArray()) { | |
188 | $userIDs[] = $row['userID']; | |
189 | } | |
190 | ||
191 | // create group | |
192 | $group = new GroupedUserList(); | |
193 | $group->addUserIDs($userIDs); | |
194 | ||
195 | // load user profiles | |
196 | GroupedUserList::loadUsers(); | |
197 | ||
198 | WCF::getTPL()->assign(array( | |
199 | 'groupedUsers' => array($group) | |
200 | )); | |
201 | ||
202 | return array( | |
203 | 'pageCount' => $pageCount, | |
204 | 'template' => WCF::getTPL()->fetch('groupedUserList') | |
205 | ); | |
206 | } | |
207 | } |