Add EmailLogListPage
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / user / UserBirthdayAction.class.php
1 <?php
2
3 namespace wcf\data\user;
4
5 use wcf\data\DatabaseObject;
6 use wcf\data\IGroupedUserListAction;
7 use wcf\data\user\option\UserOption;
8 use wcf\system\cache\builder\UserOptionCacheBuilder;
9 use wcf\system\cache\runtime\UserProfileRuntimeCache;
10 use wcf\system\exception\UserInputException;
11 use wcf\system\user\UserBirthdayCache;
12 use wcf\system\WCF;
13
14 /**
15 * Shows a list of user birthdays.
16 *
17 * @author Marcel Werk
18 * @copyright 2001-2019 WoltLab GmbH
19 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @package WoltLabSuite\Core\Data\User
21 */
22 class UserBirthdayAction extends UserProfileAction implements IGroupedUserListAction
23 {
24 /**
25 * @inheritDoc
26 */
27 protected $allowGuestAccess = ['getGroupedUserList'];
28
29 /**
30 * @inheritDoc
31 */
32 public function validateGetGroupedUserList()
33 {
34 $this->readString('date');
35 $this->readString('sortField', true);
36 $this->readString('sortOrder', true);
37
38 if (!\preg_match('/\d{4}-\d{2}-\d{2}/', $this->parameters['date'])) {
39 throw new UserInputException();
40 }
41
42 if ($this->parameters['sortField'] && $this->parameters['sortOrder']) {
43 if (!\in_array($this->parameters['sortField'], ['username', 'activityPoints', 'registrationDate'])) {
44 throw new UserInputException('sortField');
45 }
46
47 if (!\in_array($this->parameters['sortOrder'], ['ASC', 'DESC'])) {
48 throw new UserInputException('sortOrder');
49 }
50 }
51 }
52
53 /**
54 * @inheritDoc
55 */
56 public function getGroupedUserList()
57 {
58 $year = $month = $day = 0;
59 $value = \explode('-', $this->parameters['date']);
60 if (isset($value[0])) {
61 $year = \intval($value[0]);
62 }
63 if (isset($value[1])) {
64 $month = \intval($value[1]);
65 }
66 if (isset($value[2])) {
67 $day = \intval($value[2]);
68 }
69
70 // get users
71 $users = [];
72 $userOptions = UserOptionCacheBuilder::getInstance()->getData([], 'options');
73 if (isset($userOptions['birthday'])) {
74 /** @var UserOption $birthdayUserOption */
75 $birthdayUserOption = $userOptions['birthday'];
76
77 $userIDs = UserBirthdayCache::getInstance()->getBirthdays($month, $day);
78 $userProfiles = UserProfileRuntimeCache::getInstance()->getObjects($userIDs);
79
80 foreach ($userProfiles as $user) {
81 $birthdayUserOption->setUser($user->getDecoratedObject());
82
83 if (!$user->isProtected() && $birthdayUserOption->isVisible() && $user->getAge($year) >= 0) {
84 $users[] = $user;
85 }
86 }
87 }
88
89 if ($this->parameters['sortField'] && $this->parameters['sortOrder']) {
90 DatabaseObject::sort($users, $this->parameters['sortField'], $this->parameters['sortOrder']);
91 }
92
93 WCF::getTPL()->assign([
94 'users' => $users,
95 'year' => $year,
96 ]);
97
98 return [
99 'pageCount' => 1,
100 'template' => WCF::getTPL()->fetch('userBirthdayList'),
101 ];
102 }
103 }