Revert "Revert "Removed obsolete trailing slashes from void elements""
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / notice / Notice.class.php
1 <?php
2 namespace wcf\data\notice;
3 use wcf\data\condition\Condition;
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
14 * @copyright 2001-2016 WoltLab GmbH
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
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
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 /**
37 * @inheritDoc
38 */
39 protected static $databaseTableName = 'notice';
40
41 /**
42 * @inheritDoc
43 */
44 protected static $databaseTableIndexName = 'noticeID';
45
46 /**
47 * Returns the textual representation of the notice.
48 *
49 * @return string
50 * @since 2.2
51 */
52 public function __toString() {
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 ]);
59
60 if (!$this->noticeUseHtml) {
61 $text = nl2br(htmlspecialchars($text), false);
62 }
63
64 return $text;
65 }
66
67 /**
68 * Returns the conditions of the notice.
69 *
70 * @return Condition[]
71 */
72 public function getConditions() {
73 return ConditionHandler::getInstance()->getConditions('com.woltlab.wcf.condition.notice', $this->noticeID);
74 }
75
76 /**
77 * @inheritDoc
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) {
93 $dismissedNotices = UserStorageHandler::getInstance()->getField('dismissedNotices');
94 if ($dismissedNotices === null) {
95 $sql = "SELECT noticeID
96 FROM wcf".WCF_N."_notice_dismissed
97 WHERE userID = ?";
98 $statement = WCF::getDB()->prepareStatement($sql);
99 $statement->execute([
100 WCF::getUser()->userID
101 ]);
102
103 $noticeIDs = [];
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 {
115 $dismissedNoticeIDs = @unserialize($dismissedNotices);
116 $this->isDismissed = in_array($this->noticeID, $dismissedNoticeIDs);
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 }