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