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 = " LEFT JOIN wcf" . WCF_N . "_user user_table ON (user_table.userID = attachment.userID)";
37 }
38
39 /**
40 * @inheritDoc
41 */
42 public function readObjects()
43 {
44 parent::readObjects();
45
46 // cache objects
47 $groupedObjectIDs = [];
48 foreach ($this->objects as $attachment) {
49 if (!isset($groupedObjectIDs[$attachment->objectTypeID])) {
50 $groupedObjectIDs[$attachment->objectTypeID] = [];
51 }
52 $groupedObjectIDs[$attachment->objectTypeID][] = $attachment->objectID;
53 }
54
55 foreach ($groupedObjectIDs as $objectTypeID => $objectIDs) {
56 $objectType = ObjectTypeCache::getInstance()->getObjectType($objectTypeID);
57 $objectType->getProcessor()->cacheObjects($objectIDs);
58 }
59 }
60
61 /**
62 * Returns a list of available mime types.
63 *
64 * @return string[]
65 */
66 public function getAvailableFileTypes()
67 {
68 $fileTypes = [];
69 $sql = "SELECT DISTINCT attachment.fileType
70 FROM wcf" . WCF_N . "_attachment attachment
71 " . $this->getConditionBuilder();
72 $statement = WCF::getDB()->prepareStatement($sql);
73 $statement->execute($this->getConditionBuilder()->getParameters());
74 while ($row = $statement->fetchArray()) {
75 if ($row['fileType']) {
76 $fileTypes[$row['fileType']] = $row['fileType'];
77 }
78 }
79
80 \ksort($fileTypes);
81
82 return $fileTypes;
83 }
84
85 /**
86 * Returns attachment statistics.
87 *
88 * @return int[]
89 */
90 public function getStats()
91 {
92 $sql = "SELECT COUNT(*) AS count,
93 COALESCE(SUM(attachment.filesize), 0) AS size,
94 COALESCE(SUM(downloads), 0) AS downloads
95 FROM wcf" . WCF_N . "_attachment attachment
96 " . $this->getConditionBuilder();
97 $statement = WCF::getDB()->prepareStatement($sql);
98 $statement->execute($this->getConditionBuilder()->getParameters());
99
100 return $statement->fetchArray();
101 }
102 }