Add permission to restrict access to own media
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / acp / page / MediaListPage.class.php
CommitLineData
59ab4d0f
MS
1<?php
2namespace wcf\acp\page;
cf4e950c 3use wcf\data\category\CategoryNodeTree;
59ab4d0f
MS
4use wcf\data\media\ViewableMediaList;
5use wcf\page\SortablePage;
340866e3 6use wcf\system\clipboard\ClipboardHandler;
6d8afc41 7use wcf\system\request\LinkHandler;
340866e3 8use wcf\system\WCF;
6d8afc41 9use wcf\util\StringUtil;
59ab4d0f
MS
10
11/**
12 * Shows the list of media entries.
13 *
14 * @author Matthias Schmidt
c839bd49 15 * @copyright 2001-2018 WoltLab GmbH
59ab4d0f 16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4
MW
17 * @package WoltLabSuite\Core\Acp\Page
18 * @since 3.0
fe4f149a
MS
19 *
20 * @property ViewableMediaList $objectList
59ab4d0f
MS
21 */
22class MediaListPage extends SortablePage {
23 /**
340866e3 24 * @inheritDoc
59ab4d0f 25 */
cf4e950c
MS
26 public $activeMenuItem = 'wcf.acp.menu.link.media.list';
27
28 /**
29 * id of the selected media category
30 * @var integer
31 */
32 public $categoryID = 0;
33
34 /**
35 * node tree with all available media categories
36 * @var \RecursiveIteratorIterator
37 */
38 public $categoryList;
59ab4d0f
MS
39
40 /**
340866e3 41 * @inheritDoc
59ab4d0f
MS
42 */
43 public $defaultSortField = 'uploadTime';
44
45 /**
340866e3 46 * @inheritDoc
59ab4d0f
MS
47 */
48 public $defaultSortOrder = 'DESC';
49
6d8afc41 50 /**
5cd59148 51 * searched media query
6d8afc41
MS
52 * @var string
53 */
5cd59148 54 public $query = '';
6d8afc41 55
6d8afc41
MS
56 /**
57 * @inheritDoc
58 */
59 public $forceCanonicalURL = true;
60
59ab4d0f 61 /**
340866e3 62 * @inheritDoc
59ab4d0f
MS
63 */
64 public $neededPermissions = ['admin.content.cms.canManageMedia'];
65
66 /**
340866e3 67 * @inheritDoc
59ab4d0f
MS
68 */
69 public $objectListClassName = ViewableMediaList::class;
70
6d8afc41
MS
71 /**
72 * name of the user who uploaded the searched media files
73 * @var string
74 */
75 public $username = '';
76
59ab4d0f 77 /**
340866e3 78 * @inheritDoc
59ab4d0f
MS
79 */
80 public $validSortFields = [
81 'filename',
82 'filesize',
83 'mediaID',
84 'title',
85 'uploadTime'
86 ];
87
88 /**
340866e3
MS
89 * @inheritDoc
90 */
91 public function assignVariables() {
92 parent::assignVariables();
93
6d8afc41 94 WCF::getTPL()->assign([
cf4e950c
MS
95 'categoryID' => $this->categoryID,
96 'categoryList' => $this->categoryList,
5cd59148 97 'q' => $this->query,
6d8afc41
MS
98 'hasMarkedItems' => ClipboardHandler::getInstance()->hasMarkedItems(ClipboardHandler::getInstance()->getObjectTypeID('com.woltlab.wcf.media')),
99 'username' => $this->username
100 ]);
101 }
102
103 /**
104 * @inheritDoc
105 */
106 protected function initObjectList() {
107 parent::initObjectList();
108
4c7569ac
MS
109 if (WCF::getSession()->getPermission('admin.content.cms.canOnlyAccessOwnMedia')) {
110 $this->objectList->getConditionBuilder()->add('media.userID = ?', [WCF::getUser()->userID]);
111 }
112
cf4e950c
MS
113 if ($this->categoryID) {
114 $this->objectList->getConditionBuilder()->add('media.categoryID = ?', [$this->categoryID]);
115 }
5cd59148
MW
116 if ($this->query) {
117 $this->objectList->addSearchConditions($this->query);
6d8afc41 118 }
6d8afc41
MS
119 if ($this->username) {
120 $this->objectList->getConditionBuilder()->add('media.username LIKE ?', ['%'.addcslashes($this->username, '_%').'%']);
121 }
122 }
123
cf4e950c
MS
124 /**
125 * @inheritDoc
126 */
127 public function readData() {
128 parent::readData();
129
130 $this->categoryList = (new CategoryNodeTree('com.woltlab.wcf.media.category'))->getIterator();
131 $this->categoryList->setMaxDepth(0);
132 }
133
6d8afc41
MS
134 /**
135 * @inheritDoc
136 */
137 public function readParameters() {
138 parent::readParameters();
139
cf4e950c 140 if (isset($_REQUEST['categoryID'])) $this->categoryID = intval($_REQUEST['categoryID']);
5cd59148 141 if (isset($_REQUEST['q'])) $this->query = StringUtil::trim($_REQUEST['q']);
6d8afc41
MS
142 if (isset($_REQUEST['username'])) $this->username = StringUtil::trim($_REQUEST['username']);
143
6d8afc41
MS
144 $parameters = [];
145 if ($this->sortField) $parameters['sortField'] = $this->sortField;
146 if ($this->sortOrder) $parameters['sortOrder'] = $this->sortOrder;
5cd59148 147 if ($this->query) $parameters['q'] = $this->query;
6d8afc41 148 if ($this->username) $parameters['username'] = $this->username;
08104517 149 if ($this->categoryID) $parameters['categoryID'] = $this->categoryID;
6d8afc41
MS
150
151 $this->canonicalURL = LinkHandler::getInstance()->getLink('MediaList', $parameters);
340866e3
MS
152 }
153
154 /**
155 * @inheritDoc
59ab4d0f
MS
156 */
157 protected function readObjects() {
158 if ($this->sqlOrderBy && $this->sortField == 'mediaID') {
159 $this->sqlOrderBy = 'media.'.$this->sortField.' '.$this->sortOrder;
160 }
161
162 parent::readObjects();
163 }
164}