Fix undefined class (documentation) issues
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / message / QuickReplyManager.class.php
1 <?php
2 namespace wcf\system\message;
3 use wcf\data\DatabaseObjectDecorator;
4 use wcf\data\IAttachmentMessageQuickReplyAction;
5 use wcf\data\IMessage;
6 use wcf\data\IMessageQuickReplyAction;
7 use wcf\system\bbcode\PreParser;
8 use wcf\system\event\EventHandler;
9 use wcf\system\exception\SystemException;
10 use wcf\system\exception\UserInputException;
11 use wcf\system\SingletonFactory;
12 use wcf\system\WCF;
13 use wcf\util\ArrayUtil;
14 use wcf\util\MessageUtil;
15 use wcf\util\StringUtil;
16
17 /**
18 * Manages quick replies and stored messages.
19 *
20 * @author Alexander Ebert
21 * @copyright 2001-2015 WoltLab GmbH
22 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
23 * @package com.woltlab.wcf
24 * @subpackage system.message
25 * @category Community Framework
26 */
27 class QuickReplyManager extends SingletonFactory {
28 /**
29 * list of allowed bbcodes
30 * @var array<string>
31 */
32 public $allowedBBodes = null;
33
34 /**
35 * container object
36 * @var \wcf\data\DatabaseObject
37 */
38 public $container = null;
39
40 /**
41 * object id
42 * @var integer
43 */
44 public $objectID = 0;
45
46 /**
47 * object type
48 * @var string
49 */
50 public $type = '';
51
52 /**
53 * Returns a stored message from session.
54 *
55 * @param string $type
56 * @param integer $objectID
57 * @return string
58 */
59 public function getMessage($type, $objectID) {
60 $this->type = $type;
61 $this->objectID = $objectID;
62
63 // allow manipulation before fetching data
64 EventHandler::getInstance()->fireAction($this, 'getMessage');
65
66 $message = WCF::getSession()->getVar('quickReply-'.$this->type.'-'.$this->objectID);
67 return ($message === null ? '' : $message);
68 }
69
70 /**
71 * Stores a message in session.
72 *
73 * @param string $type
74 * @param integer $objectID
75 * @param string $message
76 */
77 public function setMessage($type, $objectID, $message) {
78 WCF::getSession()->register('quickReply-'.$type.'-'.$objectID, $message);
79 }
80
81 /**
82 * Removes a stored message from session.
83 *
84 * @param string $type
85 * @param integer $objectID
86 */
87 public function removeMessage($type, $objectID) {
88 WCF::getSession()->unregister('quickReply-'.$type.'-'.$objectID);
89 }
90
91 /**
92 * Sets the allowed bbcodes.
93 *
94 * @param array<string> $allowedBBCodes
95 */
96 public function setAllowedBBCodes(array $allowedBBCodes = null) {
97 $this->allowedBBodes = $allowedBBCodes;
98 }
99
100 /**
101 * Validates parameters for current request.
102 *
103 * @param IMessageQuickReplyAction $object
104 * @param array<array> $parameters
105 * @param string $containerClassName
106 * @param string $containerDecoratorClassName
107 */
108 public function validateParameters(IMessageQuickReplyAction $object, array &$parameters, $containerClassName, $containerDecoratorClassName = '') {
109 if (!isset($parameters['data']['message'])) {
110 throw new UserInputException('message');
111 }
112
113 $parameters['data']['message'] = StringUtil::trim(MessageUtil::stripCrap($parameters['data']['message']));
114
115 if (empty($parameters['data']['message'])) {
116 throw new UserInputException('message', WCF::getLanguage()->get('wcf.global.form.error.empty'));
117 }
118
119 $parameters['lastPostTime'] = (isset($parameters['lastPostTime'])) ? intval($parameters['lastPostTime']) : 0;
120 if (!$parameters['lastPostTime']) {
121 throw new UserInputException('lastPostTime');
122 }
123
124 $parameters['pageNo'] = (isset($parameters['pageNo'])) ? intval($parameters['pageNo']) : 0;
125 if (!$parameters['pageNo']) {
126 throw new UserInputException('pageNo');
127 }
128
129 $parameters['objectID'] = (isset($parameters['objectID'])) ? intval($parameters['objectID']) : 0;
130 if (!$parameters['objectID']) {
131 throw new UserInputException('objectID');
132 }
133
134 $this->container = new $containerClassName($parameters['objectID']);
135 if (!empty($containerDecoratorClassName)) {
136 if (!is_subclass_of($containerDecoratorClassName, 'wcf\data\DatabaseObjectDecorator')) {
137 throw new SystemException("'".$containerDecoratorClassName."' does not extend 'wcf\data\DatabaseObjectDecorator'");
138 }
139
140 $this->container = new $containerDecoratorClassName($this->container);
141 }
142 $object->validateContainer($this->container);
143
144 // validate message
145 $object->validateMessage($this->container, $parameters['data']['message']);
146
147 // check for message quote ids
148 $parameters['removeQuoteIDs'] = (isset($parameters['removeQuoteIDs']) && is_array($parameters['removeQuoteIDs'])) ? ArrayUtil::trim($parameters['removeQuoteIDs']) : array();
149
150 // check for tmp hash (attachments)
151 $parameters['tmpHash'] = '';
152 if (isset($parameters['data']['tmpHash'])) {
153 $parameters['tmpHash'] = StringUtil::trim($parameters['data']['tmpHash']);
154 unset($parameters['data']['tmpHash']);
155 }
156
157 // message settings
158 $parameters['data'] = array_merge($parameters['data'], MessageFormSettingsHandler::getSettings($parameters));
159
160 $parameters['data']['enableHtml'] = 0;
161 $parameters['data']['showSignature'] = (WCF::getUser()->userID ? WCF::getUser()->showSignature : 0);
162
163 EventHandler::getInstance()->fireAction($this, 'validateParameters', $parameters);
164 }
165
166 /**
167 * Creates a new message and returns the parsed template.
168 *
169 * @param \wcf\data\IMessageQuickReplyAction $object
170 * @param array<array> $parameters
171 * @param string $containerActionClassName
172 * @param string $sortOrder
173 * @param string $templateName
174 * @param string $application
175 * @return array
176 */
177 public function createMessage(IMessageQuickReplyAction $object, array &$parameters, $containerActionClassName, $sortOrder, $templateName, $application = 'wcf') {
178 $additionalFields = array();
179 EventHandler::getInstance()->fireAction($this, 'createMessage', $additionalFields);
180
181 $tableIndexName = call_user_func(array($this->container, 'getDatabaseTableIndexName'));
182 $parameters['data'][$tableIndexName] = $parameters['objectID'];
183 $parameters['data']['time'] = TIME_NOW;
184 $parameters['data']['userID'] = WCF::getUser()->userID ?: null;
185 $parameters['data']['username'] = WCF::getUser()->username;
186
187 // pre-parse message text
188 if ($parameters['data']['preParse']) {
189 $parameters['data']['message'] = PreParser::getInstance()->parse($parameters['data']['message'], $this->allowedBBodes);
190 }
191 unset($parameters['data']['preParse']);
192
193 $parameters['data'] = array_merge($additionalFields, $parameters['data']);
194
195 // attachment support
196 if (MODULE_ATTACHMENT && !empty($parameters['tmpHash']) && $object instanceof IAttachmentMessageQuickReplyAction) {
197 $parameters['attachmentHandler'] = $object->getAttachmentHandler($this->container);
198 }
199
200 $message = $object->create();
201 $eventParameters = array('message' => $message);
202 EventHandler::getInstance()->fireAction($this, 'createdMessage', $eventParameters);
203
204 if ($message instanceof IMessage && !$message->isVisible()) {
205 return array(
206 'isVisible' => false
207 );
208 }
209
210 // resolve the page no
211 list($pageNo, $count) = $object->getPageNo($this->container);
212
213 // we're still on current page
214 if ($pageNo == $parameters['pageNo']) {
215 // check for additional messages
216 $messageList = $object->getMessageList($this->container, $parameters['lastPostTime']);
217
218 // calculate start index
219 $startIndex = $count - (count($messageList) - 1);
220
221 WCF::getTPL()->assign(array(
222 'attachmentList' => $messageList->getAttachmentList(),
223 'container' => $this->container,
224 'objects' => $messageList,
225 'startIndex' => $startIndex,
226 'sortOrder' => $sortOrder,
227 ));
228
229 // assign 'to top' link
230 if (isset($parameters['anchor'])) {
231 WCF::getTPL()->assign('anchor', $parameters['anchor']);
232 }
233
234 // update visit time (messages shouldn't occur as new upon next visit)
235 if (is_subclass_of($containerActionClassName, 'wcf\data\IVisitableObjectAction')) {
236 $containerAction = new $containerActionClassName(array(($this->container instanceof DatabaseObjectDecorator ? $this->container->getDecoratedObject() : $this->container)), 'markAsRead');
237 $containerAction->executeAction();
238 }
239
240 return array(
241 'lastPostTime' => $message->time,
242 'template' => WCF::getTPL()->fetch($templateName, $application)
243 );
244 }
245 else {
246 // redirect
247 return array(
248 'url' => $object->getRedirectUrl($this->container, $message)
249 );
250 }
251 }
252
253 /**
254 * Returns the container object.
255 *
256 * @return \wcf\data\DatabaseObject
257 */
258 public function getContainer() {
259 return $this->container;
260 }
261
262 /**
263 * Stores tmpHash in current session, used in combination with the extended form.
264 *
265 * @param string $tmpHash
266 */
267 public function setTmpHash($tmpHash) {
268 WCF::getSession()->register('__wcfAttachmentTmpHash', $tmpHash);
269 }
270 }