Merge branch '5.3'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / data / attachment / AdministrativeAttachmentList.class.php
1 <?php
2
3 namespace wcf\data\attachment;
4
5 use wcf\data\object\type\ObjectTypeCache;
6 use wcf\system\WCF;
7
8 /**
9 * Represents a list of attachments.
10 *
11 * @author Marcel Werk
12 * @copyright 2001-2019 WoltLab GmbH
13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
14 * @package WoltLabSuite\Core\Data\Attachment
15 *
16 * @method AdministrativeAttachment current()
17 * @method AdministrativeAttachment[] getObjects()
18 * @method AdministrativeAttachment|null search($objectID)
19 * @property AdministrativeAttachment[] $objects
20 */
21 class AdministrativeAttachmentList extends AttachmentList
22 {
23 /**
24 * @inheritDoc
25 */
26 public $decoratorClassName = AdministrativeAttachment::class;
27
28 /**
29 * Creates a new AdministrativeAttachmentList object.
30 */
31 public function __construct()
32 {
33 parent::__construct();
34
35 $this->sqlSelects = 'user_table.username';
36 $this->sqlJoins = "
37 LEFT JOIN wcf" . WCF_N . "_user user_table
38 ON user_table.userID = attachment.userID";
39 }
40
41 /**
42 * @inheritDoc
43 */
44 public function readObjects()
45 {
46 parent::readObjects();
47
48 // cache objects
49 $groupedObjectIDs = [];
50 foreach ($this->objects as $attachment) {
51 if (!isset($groupedObjectIDs[$attachment->objectTypeID])) {
52 $groupedObjectIDs[$attachment->objectTypeID] = [];
53 }
54 $groupedObjectIDs[$attachment->objectTypeID][] = $attachment->objectID;
55 }
56
57 foreach ($groupedObjectIDs as $objectTypeID => $objectIDs) {
58 $objectType = ObjectTypeCache::getInstance()->getObjectType($objectTypeID);
59 $objectType->getProcessor()->cacheObjects($objectIDs);
60 }
61 }
62
63 /**
64 * Returns a list of available mime types.
65 *
66 * @return string[]
67 */
68 public function getAvailableFileTypes()
69 {
70 $fileTypes = [];
71 $sql = "SELECT DISTINCT attachment.fileType
72 FROM wcf" . WCF_N . "_attachment attachment
73 " . $this->getConditionBuilder();
74 $statement = WCF::getDB()->prepareStatement($sql);
75 $statement->execute($this->getConditionBuilder()->getParameters());
76 while ($row = $statement->fetchArray()) {
77 if ($row['fileType']) {
78 $fileTypes[$row['fileType']] = $row['fileType'];
79 }
80 }
81
82 \ksort($fileTypes);
83
84 return $fileTypes;
85 }
86
87 /**
88 * Returns attachment statistics.
89 *
90 * @return int[]
91 */
92 public function getStats()
93 {
94 $sql = "SELECT COUNT(*) AS count,
95 COALESCE(SUM(attachment.filesize), 0) AS size,
96 COALESCE(SUM(downloads), 0) AS downloads
97 FROM wcf" . WCF_N . "_attachment attachment
98 " . $this->getConditionBuilder();
99 $statement = WCF::getDB()->prepareStatement($sql);
100 $statement->execute($this->getConditionBuilder()->getParameters());
101
102 return $statement->fetchArray();
103 }
104 }