3 namespace wcf\data\notice
;
5 use wcf\data\condition\Condition
;
6 use wcf\data\DatabaseObject
;
7 use wcf\system\condition\ConditionHandler
;
8 use wcf\system\request\IRouteController
;
9 use wcf\system\user\storage\UserStorageHandler
;
11 use wcf\util\StringUtil
;
14 * Represents a notice.
16 * @author Matthias Schmidt
17 * @copyright 2001-2019 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
20 * @property-read int $noticeID unique id of the notice
21 * @property-read string $noticeName name of the notice shown in ACP
22 * @property-read string $notice text of the notice or name of language item which contains the text
23 * @property-read int $noticeUseHtml is `1` if the notice text will be rendered as HTML, otherwise `0`
24 * @property-read string $cssClassName css class name(s) used for the notice HTML element
25 * @property-read int $showOrder position of the notice in relation to the other notices
26 * @property-read int $isDisabled is `1` if the notice is disabled and thus not shown, otherwise `0`
27 * @property-read int $isDismissible is `1` if the notice can be dismissed by users, otherwise `0`
29 class Notice
extends DatabaseObject
implements IRouteController
32 * Available notice types.
36 const TYPES
= ['info', 'success', 'warning', 'error'];
39 * true if the active user has dismissed the notice
42 protected $isDismissed;
45 * Returns the textual representation of the notice.
49 public function __toString(): string
51 // replace `{$username}` with the active user's name and `{$email}`
52 // with the active user's email address
53 $text = \
strtr(WCF
::getLanguage()->get($this->notice
), [
54 '{$username}' => $this->noticeUseHtml ? StringUtil
::encodeHTML(WCF
::getUser()->username
) : WCF
::getUser()->username
,
55 '{$email}' => $this->noticeUseHtml ? StringUtil
::encodeHTML(WCF
::getUser()->email
) : WCF
::getUser()->email
,
58 if (!$this->noticeUseHtml
) {
59 $text = \nl
2br
(StringUtil
::encodeHTML($text), false);
66 * Returns the conditions of the notice.
70 public function getConditions()
72 return ConditionHandler
::getInstance()->getConditions('com.woltlab.wcf.condition.notice', $this->noticeID
);
78 public function getTitle(): string
80 return $this->noticeName
;
84 * Returns true if the active user has dismissed the notice.
88 public function isDismissed()
90 if (!$this->isDismissible
) {
94 if ($this->isDismissed
=== null) {
95 if (WCF
::getUser()->userID
) {
96 $dismissedNotices = UserStorageHandler
::getInstance()->getField('dismissedNotices');
97 if ($dismissedNotices === null) {
98 $sql = "SELECT noticeID
99 FROM wcf" . WCF_N
. "_notice_dismissed
101 $statement = WCF
::getDB()->prepareStatement($sql);
102 $statement->execute([
103 WCF
::getUser()->userID
,
107 while ($noticeID = $statement->fetchColumn()) {
108 $noticeIDs[] = $noticeID;
110 if ($noticeID == $this->noticeID
) {
111 $this->isDismissed
= true;
115 UserStorageHandler
::getInstance()->update(
116 WCF
::getUser()->userID
,
118 \
serialize($noticeIDs)
121 $dismissedNoticeIDs = @\
unserialize($dismissedNotices);
122 $this->isDismissed
= \
in_array($this->noticeID
, $dismissedNoticeIDs);
125 $dismissedNotices = WCF
::getSession()->getVar('dismissedNotices');
126 if ($dismissedNotices !== null) {
127 $dismissedNotices = @\
unserialize($dismissedNotices);
128 $this->isDismissed
= \
in_array($this->noticeID
, $dismissedNotices);
130 $this->isDismissed
= false;
135 return $this->isDismissed
;
141 public function isCustom(): bool
143 return !\
in_array($this->cssClassName
, self
::TYPES
);