Apply PSR-12 code style (#3886)
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / clipboard / action / MediaClipboardAction.class.php
CommitLineData
59ab4d0f 1<?php
a9229942 2
59ab4d0f 3namespace wcf\system\clipboard\action;
a9229942 4
59ab4d0f 5use wcf\data\clipboard\action\ClipboardAction;
4c7569ac 6use wcf\data\media\Media;
59ab4d0f 7use wcf\data\media\MediaAction;
599b8699 8use wcf\system\category\CategoryHandler;
59ab4d0f
MS
9use wcf\system\WCF;
10
11/**
12 * Clipboard action implementation for media files.
a9229942
TD
13 *
14 * @author Matthias Schmidt
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\System\Clipboard\Action
18 * @since 3.0
59ab4d0f 19 */
a9229942
TD
20class MediaClipboardAction extends AbstractClipboardAction
21{
22 /**
23 * @inheritDoc
24 */
25 protected $actionClassActions = ['delete'];
26
27 /**
28 * @inheritDoc
29 */
30 protected $supportedActions = [
31 'delete',
32 'insert',
33 'setCategory',
34 ];
35
36 /**
37 * @inheritDoc
38 */
39 public function execute(array $objects, ClipboardAction $action)
40 {
41 $item = parent::execute($objects, $action);
42
43 if ($item === null) {
44 return;
45 }
46
47 // handle actions
48 switch ($action->actionName) {
49 case 'delete':
50 $item->addInternalData(
51 'confirmMessage',
52 WCF::getLanguage()->getDynamicVariable(
53 'wcf.clipboard.item.com.woltlab.wcf.media.delete.confirmMessage',
54 [
55 'count' => $item->getCount(),
56 ]
57 )
58 );
59 break;
60 }
61
62 return $item;
63 }
64
65 /**
66 * @inheritDoc
67 */
68 public function getClassName()
69 {
70 return MediaAction::class;
71 }
72
73 /**
74 * @inheritDoc
75 */
76 public function getTypeName()
77 {
78 return 'com.woltlab.wcf.media';
79 }
80
81 /**
82 * Returns the ids of the media files which can be deleted.
83 *
84 * @return int[]
85 */
86 public function validateDelete()
87 {
88 if (!WCF::getSession()->getPermission('admin.content.cms.canManageMedia')) {
89 return [];
90 }
91
92 $mediaIDs = \array_keys($this->objects);
93
94 if (WCF::getSession()->getPermission('admin.content.cms.canOnlyAccessOwnMedia')) {
95 $mediaIDs = [];
96
97 /** @var Media $media */
98 foreach ($this->objects as $media) {
99 if ($media->userID == WCF::getUser()->userID) {
100 $mediaIDs[] = $media->mediaID;
101 }
102 }
103 }
104
105 return $mediaIDs;
106 }
107
108 /**
109 * Returns the ids of the media files which can be inserted.
110 *
111 * @return int[]
112 */
113 public function validateInsert()
114 {
115 return \array_keys($this->objects);
116 }
117
118 /**
119 * Returns the ids of the media files whose category can be set.
120 *
121 * @return int[]
122 */
123 public function validateSetCategory()
124 {
125 if (!WCF::getSession()->getPermission('admin.content.cms.canManageMedia')) {
126 return [];
127 }
128
129 // category can only be set if any category exists
130 if (empty(CategoryHandler::getInstance()->getCategories('com.woltlab.wcf.media.category'))) {
131 return [];
132 }
133
134 $mediaIDs = \array_keys($this->objects);
135
136 if (WCF::getSession()->getPermission('admin.content.cms.canOnlyAccessOwnMedia')) {
137 $mediaIDs = [];
138
139 /** @var Media $media */
140 foreach ($this->objects as $media) {
141 if ($media->userID == WCF::getUser()->userID) {
142 $mediaIDs[] = $media->mediaID;
143 }
144 }
145 }
146
147 return $mediaIDs;
148 }
59ab4d0f 149}