a66d831ec000dfb496b55037a1ae2da52c3a8885
[GitHub/WoltLab/WCF.git] /
1 <?php
2 declare(strict_types=1);
3 namespace wcf\system\message\embedded\object;
4 use wcf\data\attachment\AttachmentList;
5 use wcf\data\object\type\ObjectTypeCache;
6 use wcf\system\html\input\HtmlInputProcessor;
7
8 /**
9 * IMessageEmbeddedObjectHandler implementation for attachments.
10 *
11 * @author Marcel Werk
12 * @copyright 2001-2018 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 }