Commit | Line | Data |
---|---|---|
20933e61 MS |
1 | <?php |
2 | namespace wcf\data\notice; | |
614a6b4d | 3 | use wcf\data\condition\Condition; |
20933e61 MS |
4 | use wcf\data\DatabaseObject; |
5 | use wcf\system\condition\ConditionHandler; | |
6 | use wcf\system\request\IRouteController; | |
7 | use wcf\system\user\storage\UserStorageHandler; | |
8 | use wcf\system\WCF; | |
9 | ||
10 | /** | |
11 | * Represents a notice. | |
12 | * | |
13 | * @author Matthias Schmidt | |
614a6b4d | 14 | * @copyright 2001-2016 WoltLab GmbH |
20933e61 MS |
15 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
16 | * @package com.woltlab.wcf | |
17 | * @subpackage data.notice | |
18 | * @category Community Framework | |
e9335ed9 MS |
19 | * |
20 | * @property-read integer $noticeID | |
21 | * @property-read string $noticeName | |
22 | * @property-read string $notice | |
23 | * @property-read integer $noticeUseHtml | |
24 | * @property-read string $cssClassName | |
25 | * @property-read integer $showOrder | |
26 | * @property-read integer $isDisabled | |
27 | * @property-read integer $isDismissible | |
20933e61 MS |
28 | */ |
29 | class Notice extends DatabaseObject implements IRouteController { | |
30 | /** | |
31 | * true if the active user has dismissed the notice | |
32 | * @var boolean | |
33 | */ | |
34 | protected $isDismissed = null; | |
35 | ||
36 | /** | |
614a6b4d | 37 | * @inheritDoc |
20933e61 MS |
38 | */ |
39 | protected static $databaseTableName = 'notice'; | |
40 | ||
41 | /** | |
614a6b4d | 42 | * @inheritDoc |
20933e61 MS |
43 | */ |
44 | protected static $databaseTableIndexName = 'noticeID'; | |
45 | ||
614a6b4d MS |
46 | /** |
47 | * Returns the textual representation of the notice. | |
48 | * | |
49 | * @return string | |
406e0d01 | 50 | * @since 2.2 |
614a6b4d MS |
51 | */ |
52 | public function __toString() { | |
30de055a MS |
53 | // replace `{$username}` with the active user's name and `{$email}` |
54 | // with the active user's email address | |
55 | $text = strtr(WCF::getLanguage()->get($this->notice), [ | |
56 | '{$username}' => WCF::getUser()->username, | |
57 | '{$email}' => WCF::getUser()->email | |
58 | ]); | |
614a6b4d MS |
59 | |
60 | if (!$this->noticeUseHtml) { | |
e5f9b56c | 61 | $text = nl2br(htmlspecialchars($text), false); |
614a6b4d MS |
62 | } |
63 | ||
64 | return $text; | |
65 | } | |
66 | ||
20933e61 MS |
67 | /** |
68 | * Returns the conditions of the notice. | |
69 | * | |
614a6b4d | 70 | * @return Condition[] |
20933e61 MS |
71 | */ |
72 | public function getConditions() { | |
73 | return ConditionHandler::getInstance()->getConditions('com.woltlab.wcf.condition.notice', $this->noticeID); | |
74 | } | |
75 | ||
76 | /** | |
614a6b4d | 77 | * @inheritDoc |
20933e61 MS |
78 | */ |
79 | public function getTitle() { | |
80 | return $this->noticeName; | |
81 | } | |
82 | ||
83 | /** | |
84 | * Returns true if the active user has dismissed the notice. | |
85 | * | |
86 | * @return boolean | |
87 | */ | |
88 | public function isDismissed() { | |
89 | if (!$this->isDismissible) return false; | |
90 | ||
91 | if ($this->isDismissed === null) { | |
92 | if (WCF::getUser()->userID) { | |
46028d80 MS |
93 | $dismissedNotices = UserStorageHandler::getInstance()->getField('dismissedNotices'); |
94 | if ($dismissedNotices === null) { | |
20933e61 MS |
95 | $sql = "SELECT noticeID |
96 | FROM wcf".WCF_N."_notice_dismissed | |
97 | WHERE userID = ?"; | |
98 | $statement = WCF::getDB()->prepareStatement($sql); | |
058cbd6a | 99 | $statement->execute([ |
20933e61 | 100 | WCF::getUser()->userID |
058cbd6a | 101 | ]); |
20933e61 | 102 | |
058cbd6a | 103 | $noticeIDs = []; |
20933e61 MS |
104 | while ($noticeID = $statement->fetchColumn()) { |
105 | $noticeIDs[] = $noticeID; | |
106 | ||
107 | if ($noticeID == $this->noticeID) { | |
108 | $this->isDismissed = true; | |
109 | } | |
110 | } | |
111 | ||
112 | UserStorageHandler::getInstance()->update(WCF::getUser()->userID, 'dismissedNotices', serialize($noticeIDs)); | |
113 | } | |
114 | else { | |
46028d80 | 115 | $dismissedNoticeIDs = @unserialize($dismissedNotices); |
5e1b9379 | 116 | $this->isDismissed = in_array($this->noticeID, $dismissedNoticeIDs); |
20933e61 MS |
117 | } |
118 | } | |
119 | else { | |
120 | $dismissedNotices = WCF::getSession()->getVar('dismissedNotices'); | |
121 | if ($dismissedNotices !== null) { | |
122 | $dismissedNotices = @unserialize($dismissedNotices); | |
123 | $this->isDismissed = in_array($this->noticeID, $dismissedNotices); | |
124 | } | |
125 | else { | |
126 | $this->isDismissed = false; | |
127 | } | |
128 | } | |
129 | } | |
130 | ||
131 | return $this->isDismissed; | |
132 | } | |
133 | } |