Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / condition / UserCoverPhotoCondition.class.php
1 <?php
2
3 namespace wcf\system\condition;
4
5 use wcf\data\condition\Condition;
6 use wcf\data\DatabaseObjectList;
7 use wcf\data\user\User;
8 use wcf\data\user\UserList;
9 use wcf\system\exception\InvalidObjectArgument;
10 use wcf\system\WCF;
11
12 /**
13 * Condition implementation for the cover photo of a user.
14 *
15 * @author Matthias Schmidt
16 * @copyright 2001-2020 WoltLab GmbH
17 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
18 * @package WoltLabSuite\Core\System\Condition
19 * @since 5.3
20 */
21 class UserCoverPhotoCondition extends AbstractSelectCondition implements
22 IContentCondition,
23 IObjectListCondition,
24 IUserCondition
25 {
26 use TObjectListUserCondition;
27
28 /**
29 * @inheritDoc
30 */
31 protected $fieldName = 'userCoverPhoto';
32
33 /**
34 * @inheritDoc
35 */
36 protected $label = 'wcf.user.condition.coverPhoto';
37
38 /**
39 * value of the "user has no cover photo" option
40 * @var int
41 */
42 const NO_COVER_PHOTO = 0;
43
44 /**
45 * value of the "user has a cover photo" option
46 * @var int
47 */
48 const COVER_PHOTO = 1;
49
50 /**
51 * @inheritDoc
52 */
53 public function addObjectListCondition(DatabaseObjectList $objectList, array $conditionData)
54 {
55 if (!($objectList instanceof UserList)) {
56 throw new InvalidObjectArgument($objectList, UserList::class, 'Object list');
57 }
58
59 switch ($conditionData['userCoverPhoto']) {
60 case self::NO_COVER_PHOTO:
61 $objectList->getConditionBuilder()->add(
62 '(user_table.coverPhotoHash = ? OR user_table.coverPhotoHash IS NULL)',
63 ['']
64 );
65 break;
66
67 case self::COVER_PHOTO:
68 $objectList->getConditionBuilder()->add(
69 '(user_table.coverPhotoHash <> ? AND user_table.coverPhotoHash IS NOT NULL)',
70 ['']
71 );
72 break;
73 }
74 }
75
76 /**
77 * @inheritDoc
78 */
79 public function checkUser(Condition $condition, User $user)
80 {
81 switch ($condition->userCoverPhoto) {
82 case self::NO_COVER_PHOTO:
83 return $user->coverPhotoExtension === '' || $user->coverPhotoExtension === null;
84 break;
85
86 case self::COVER_PHOTO:
87 return $user->coverPhotoExtension !== '' && $user->coverPhotoExtension !== null;
88 break;
89 }
90 }
91
92 /**
93 * @inheritDoc
94 */
95 protected function getOptions()
96 {
97 return [
98 self::NO_SELECTION_VALUE => 'wcf.global.noSelection',
99 self::NO_COVER_PHOTO => 'wcf.user.condition.coverPhoto.noCoverPhoto',
100 self::COVER_PHOTO => 'wcf.user.condition.coverPhoto.coverPhoto',
101 ];
102 }
103
104 /**
105 * @inheritDoc
106 */
107 public function showContent(Condition $condition)
108 {
109 if (!WCF::getUser()->userID) {
110 return false;
111 }
112
113 return $this->checkUser($condition, WCF::getUser());
114 }
115 }