Merge pull request #5987 from WoltLab/acp-dahsboard-box-hight
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / notice / Notice.class.php
CommitLineData
20933e61 1<?php
a9229942 2
20933e61 3namespace wcf\data\notice;
a9229942 4
614a6b4d 5use wcf\data\condition\Condition;
20933e61
MS
6use wcf\data\DatabaseObject;
7use wcf\system\condition\ConditionHandler;
8use wcf\system\request\IRouteController;
9use wcf\system\user\storage\UserStorageHandler;
10use wcf\system\WCF;
43573447 11use wcf\util\StringUtil;
20933e61
MS
12
13/**
14 * Represents a notice.
e9335ed9 15 *
a9229942
TD
16 * @author Matthias Schmidt
17 * @copyright 2001-2019 WoltLab GmbH
18 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
a9229942
TD
19 *
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`
20933e61 28 */
a9229942
TD
29class Notice extends DatabaseObject implements IRouteController
30{
8e0295be
MW
31 /**
32 * Available notice types.
33 * @var string[]
34 * @since 6.1
35 */
36 const TYPES = ['info', 'success', 'warning', 'error'];
37
a9229942
TD
38 /**
39 * true if the active user has dismissed the notice
40 * @var bool
41 */
42 protected $isDismissed;
43
44 /**
45 * Returns the textual representation of the notice.
46 *
a9229942
TD
47 * @since 3.0
48 */
83c324d3 49 public function __toString(): string
a9229942
TD
50 {
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,
56 ]);
57
58 if (!$this->noticeUseHtml) {
59 $text = \nl2br(StringUtil::encodeHTML($text), false);
60 }
61
62 return $text;
63 }
64
65 /**
66 * Returns the conditions of the notice.
67 *
68 * @return Condition[]
69 */
70 public function getConditions()
71 {
72 return ConditionHandler::getInstance()->getConditions('com.woltlab.wcf.condition.notice', $this->noticeID);
73 }
74
75 /**
76 * @inheritDoc
77 */
a4a634aa 78 public function getTitle(): string
a9229942
TD
79 {
80 return $this->noticeName;
81 }
82
83 /**
84 * Returns true if the active user has dismissed the notice.
85 *
86 * @return bool
87 */
88 public function isDismissed()
89 {
90 if (!$this->isDismissible) {
91 return false;
92 }
93
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
100 WHERE userID = ?";
101 $statement = WCF::getDB()->prepareStatement($sql);
102 $statement->execute([
103 WCF::getUser()->userID,
104 ]);
105
106 $noticeIDs = [];
107 while ($noticeID = $statement->fetchColumn()) {
108 $noticeIDs[] = $noticeID;
109
110 if ($noticeID == $this->noticeID) {
111 $this->isDismissed = true;
112 }
113 }
114
115 UserStorageHandler::getInstance()->update(
116 WCF::getUser()->userID,
117 'dismissedNotices',
118 \serialize($noticeIDs)
119 );
120 } else {
121 $dismissedNoticeIDs = @\unserialize($dismissedNotices);
122 $this->isDismissed = \in_array($this->noticeID, $dismissedNoticeIDs);
123 }
124 } else {
125 $dismissedNotices = WCF::getSession()->getVar('dismissedNotices');
126 if ($dismissedNotices !== null) {
127 $dismissedNotices = @\unserialize($dismissedNotices);
128 $this->isDismissed = \in_array($this->noticeID, $dismissedNotices);
129 } else {
130 $this->isDismissed = false;
131 }
132 }
133 }
134
135 return $this->isDismissed;
136 }
8e0295be
MW
137
138 /**
139 * @since 6.1
140 */
141 public function isCustom(): bool
142 {
143 return !\in_array($this->cssClassName, self::TYPES);
144 }
20933e61 145}