af22326d3ec8784395b888b46fdf59a6299f4b96
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\message\embedded\object;
3 use wcf\data\attachment\AttachmentList;
4 use wcf\data\object\type\ObjectTypeCache;
5 use wcf\system\html\input\HtmlInputProcessor;
6 use wcf\util\ArrayUtil;
7
8 /**
9 * IMessageEmbeddedObjectHandler implementation for attachments.
10 *
11 * @author Marcel Werk
12 * @copyright 2001-2016 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package WoltLabSuite\Core\System\Message\Embedded\Object
15 */
16 class AttachmentMessageEmbeddedObjectHandler extends AbstractMessageEmbeddedObjectHandler {
17 /**
18 * @inheritDoc
19 */
20 public function parse(HtmlInputProcessor $htmlInputProcessor, array $embeddedData) {
21 if (empty($embeddedData['attach'])) {
22 return [];
23 }
24
25 $attachmentIDs = [];
26 for ($i = 0, $length = count($embeddedData['attach']); $i < $length; $i++) {
27 $attributes = $embeddedData['attach'][$i];
28 $attachmentID = (!empty($attributes[0])) ? intval($attributes[0]) : 0;
29
30 if ($attachmentID > 0) {
31 $attachmentIDs[] = $attachmentID;
32 }
33 }
34
35 if (!empty($attachmentIDs)) {
36 $attachmentList = new AttachmentList();
37 $attachmentList->getConditionBuilder()->add("attachment.attachmentID IN (?)", [$attachmentIDs]);
38 $attachmentList->readObjectIDs();
39
40 return $attachmentList->getObjectIDs();
41 }
42
43 return [];
44 }
45
46 /**
47 * @inheritDoc
48 */
49 public function loadObjects(array $objectIDs) {
50 $attachmentList = new AttachmentList();
51 $attachmentList->setObjectIDs($objectIDs);
52 $attachmentList->readObjects();
53
54 // group attachments by object type
55 $groupedAttachments = [];
56 foreach ($attachmentList->getObjects() as $attachment) {
57 if (!isset($groupedAttachments[$attachment->objectTypeID])) $groupedAttachments[$attachment->objectTypeID] = [];
58 $groupedAttachments[$attachment->objectTypeID][] = $attachment;
59 }
60
61 // check permissions
62 foreach ($groupedAttachments as $objectTypeID => $attachments) {
63 $processor = ObjectTypeCache::getInstance()->getObjectType($objectTypeID)->getProcessor();
64 if ($processor !== null) {
65 $processor->setPermissions($attachments);
66 }
67 }
68
69 return $attachmentList->getObjects();
70 }
71 }