/
FolderItemList.php
393 lines (343 loc) · 13.6 KB
/
FolderItemList.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<?php
namespace Concrete\Core\File;
use Closure;
use Concrete\Core\Attribute\Key\FileKey;
use Concrete\Core\Permission\Access\Entity\FileUploaderEntity;
use Concrete\Core\Permission\Checker as Permissions;
use Concrete\Core\Permission\Key\FileFolderKey;
use Concrete\Core\Search\ItemList\Database\AttributedItemList;
use Concrete\Core\Search\ItemList\Pager\Manager\FolderItemListPagerManager;
use Concrete\Core\Search\ItemList\Pager\PagerProviderInterface;
use Concrete\Core\Search\ItemList\Pager\QueryString\VariableFactory;
use Concrete\Core\Search\Pagination\PaginationProviderInterface;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Tree\Node\Node;
use Concrete\Core\Tree\Node\Type\FileFolder;
use Concrete\Core\User\User;
use Pagerfanta\Adapter\DoctrineDbalAdapter;
class FolderItemList extends AttributedItemList implements PagerProviderInterface, PaginationProviderInterface
{
protected $parent;
protected $searchSubFolders = false;
protected $permissionsChecker;
/**
* Determines whether the list should automatically always sort by a column that's in the automatic sort.
* This is the default, but it's better to be able to use the AutoSortColumnRequestModifier on a search
* result class instead. In order to do that we disable the auto sort here, while still providing the array
* of possible auto sort columns.
*
* @var bool
*/
protected $enableAutomaticSorting = false;
protected $autoSortColumns = [
'name',
'dateModified',
'type',
'size',
'f.fID',
'fv.fvFilename',
'fv.fvTitle',
];
public function __construct()
{
$u = Application::getFacadeApplication()->make(User::class);
if ($u->isSuperUser()) {
$this->ignorePermissions();
}
parent::__construct();
}
public function enableSubFolderSearch()
{
$this->searchSubFolders = true;
}
public function enableAutomaticSorting()
{
$this->enableAutomaticSorting = true;
}
/**
* @return mixed
*/
public function getPermissionsChecker()
{
return $this->permissionsChecker;
}
public function getPagerVariableFactory()
{
return new VariableFactory($this, $this->getSearchRequest());
}
public function getPagerManager()
{
return new FolderItemListPagerManager($this);
}
public function setPermissionsChecker(?Closure $checker = null)
{
$this->permissionsChecker = $checker;
}
public function enablePermissions()
{
unset($this->permissionsChecker);
}
public function ignorePermissions()
{
$this->permissionsChecker = -1;
}
public function createQuery()
{
$this->query->select('distinct n.treeNodeID')
->addSelect('if(nt.treeNodeTypeHandle=\'file\', fv.fvTitle, n.treeNodeName) as name')
->addSelect('if(nt.treeNodeTypeHandle=\'file\', fv.fvDateAdded, n.dateModified) as dateModified')
->addSelect('case when nt.treeNodeTypeHandle=\'file_folder\' then 1 else (10 + fvType) end as type')
->addSelect('fv.fvSize as size')
->from('TreeNodes', 'n')
->innerJoin('n', 'TreeNodeTypes', 'nt', 'nt.treeNodeTypeID = n.treeNodeTypeID')
->leftJoin('n', 'TreeFileNodes', 'tf', 'tf.treeNodeID = n.treeNodeID')
->leftJoin('tf', 'FileVersions', 'fv', 'tf.fID = fv.fID and fv.fvIsApproved = 1')
->leftJoin('fv', 'Files', 'f', 'fv.fID = f.fID')
->leftJoin('f', 'Users', 'u', 'f.uID = u.uID')
->leftJoin('f', 'FileSearchIndexAttributes', 'fsi', 'f.fID = fsi.fID')
;
}
public function getTotalResults()
{
if (isset($this->permissionsChecker) && $this->permissionsChecker === -1) {
$query = $this->deliverQueryObject();
return $query->resetQueryParts(['groupBy', 'orderBy'])->select('count(distinct n.treeNodeID)')->setMaxResults(1)->execute()->fetchColumn();
}
return -1; // unknown
}
public function getPaginationAdapter()
{
$adapter = new DoctrineDbalAdapter($this->deliverQueryObject(), function ($query) {
$query->resetQueryParts(['groupBy', 'orderBy'])->select('count(distinct n.treeNodeID)')->setMaxResults(1);
});
return $adapter;
}
public function getResult($queryRow)
{
$f = Node::getByID($queryRow['treeNodeID']);
if (is_object($f) && $this->checkPermissions($f)) {
return $f;
}
}
public function checkPermissions($mixed)
{
if (isset($this->permissionsChecker)) {
if ($this->permissionsChecker === -1) {
return true;
}
return call_user_func_array($this->permissionsChecker, [$mixed]);
}
$fp = new Permissions($mixed);
return $fp->canViewTreeNode();
}
public function filterByParentFolder(FileFolder $folder)
{
$this->parent = $folder;
}
public function filterByType($type)
{
$this->query->andWhere('fv.fvType = :fvType');
$this->query->setParameter('fvType', $type);
}
/**
* Filters by public date.
*
* @param string $date
* @param string $comparison
*/
public function filterByDateAdded($date, $comparison = '=')
{
$this->query->andWhere($this->query->expr()->comparison(
'f.fDateAdded',
$comparison,
$this->query->createNamedParameter($date)
));
}
public function filterByOriginalPageID($ocID)
{
$this->query->andWhere('f.ocID = :ocID');
$this->query->setParameter('ocID', $ocID);
}
/**
* Filter the files by their storage location using a storage location id.
*
* @param int $fslID storage location id
*/
public function filterByStorageLocationID($fslID)
{
$fslID = (int) $fslID;
$this->query->andWhere('f.fslID = :fslID');
$this->query->setParameter('fslID', $fslID);
}
/**
* Filter the files by their storage location using a storage location object.
*
* @param \Concrete\Core\Entity\File\StorageLocation\StorageLocation|int $storageLocation storage location object
*/
public function filterByStorageLocation($storageLocation)
{
if ($storageLocation instanceof \Concrete\Core\Entity\File\StorageLocation\StorageLocation) {
$this->filterByStorageLocationID($storageLocation->getID());
} elseif (!is_object($storageLocation)) {
$this->filterByStorageLocationID($storageLocation);
} else {
throw new \Exception(t('Invalid file storage location.'));
}
}
/**
* Filter the files by their extension.
*
* @param string|string[] $extension one or more file extensions (with or without leading dot)
*/
public function filterByExtension($extension)
{
$extensions = is_array($extension) ? $extension : [$extension];
if (count($extensions) > 0) {
$expr = $this->query->expr();
$or = $expr->orX();
foreach ($extensions as $extension) {
$extension = ltrim((string) $extension, '.');
$or->add($expr->eq('fv.fvExtension', $this->query->createNamedParameter($extension)));
if ($extension === '') {
$or->add($expr->isNull('fv.fvExtension'));
}
}
$this->query->andWhere($or);
}
}
public function filterBySet($fs)
{
$table = 'fsf' . $fs->getFileSetID();
$this->query->leftJoin('f', 'FileSetFiles', $table, 'f.fID = ' . $table . '.fID');
$this->query->andWhere($table . '.fsID = :fsID' . $fs->getFileSetID());
$this->query->setParameter('fsID' . $fs->getFileSetID(), $fs->getFileSetID());
}
/**
* Filters the file list by file size (in kilobytes).
*
* @param int|float $from
* @param int|float $to
*/
public function filterBySize($from, $to)
{
if ($from > 0) {
$this->query->andWhere('fv.fvSize >= :fvSizeFrom');
$this->query->setParameter('fvSizeFrom', $from * 1024);
}
if ($to > 0) {
$this->query->andWhere('fv.fvSize <= :fvSizeTo');
$this->query->setParameter('fvSizeTo', $to * 1024);
}
}
/**
* Filters by "keywords" (which searches everything including filenames,
* title, folder names, etc....
*
* @param string $keywords
*/
public function filterByKeywords($keywords)
{
$expressions = [
$this->query->expr()->like('fv.fvFilename', ':keywords'),
$this->query->expr()->like('fv.fvDescription', ':keywords'),
$this->query->expr()->like('treeNodeName', ':keywords'),
$this->query->expr()->like('fv.fvTags', ':keywords'),
$this->query->expr()->eq('uName', ':keywords'),
];
$keys = FileKey::getSearchableIndexedList();
foreach ($keys as $ak) {
$cnt = $ak->getController();
$expressions[] = $cnt->searchKeywords($keywords, $this->query);
}
$expr = $this->query->expr();
$this->query->andWhere(call_user_func_array([$expr, 'orX'], $expressions));
$this->query->setParameter('keywords', '%' . $keywords . '%');
}
public function deliverQueryObject()
{
$filesystem = new Filesystem();
$rootFolder = $filesystem->getRootFolder();
if (!isset($this->parent)) {
$this->parent = $rootFolder;
}
// If there is no parent set, OR if we are set to search the root of the site and all sub-folders (which
// effectively means that there SHOULD be no parents set, then we simply return the deliveryQueryObject
// of the parent, because there is no need for any further filtering.
if (isset($this->parent) &&
$this->parent->getTreeNodeID() == $rootFolder->getTreeNodeID() && $this->searchSubFolders) {
// Before we can simply return, however, we need to ensure we're only returning nodes that the
// file manager cares about.
$this->query->andWhere(
$this->query->expr()->in('nt.treeNodeTypeHandle', array_map([$this->query->getConnection(), 'quote'], ['file', 'file_folder']))
);
// if we don't add this we're gonna see the File Manager node.
$this->query->andWhere('n.treeNodeParentID > 0');
return parent::deliverQueryObject();
}
// If we've gotten down here, we have a parent object.
if ($this->searchSubFolders) {
// determine how many subfolders are within the parent folder.
$subFolders = $this->parent->getHierarchicalNodesOfType('file_folder', 1, false, true);
$subFolderIds = [];
foreach ($subFolders as $subFolder) {
$subFolderIds[] = $subFolder['treeNodeID'];
}
$this->query->andWhere(
$this->query->expr()->in('n.treeNodeParentID', array_map([$this->query->getConnection(), 'quote'], $subFolderIds))
);
} else {
$this->query->andWhere('n.treeNodeParentID = :treeNodeParentID');
$this->query->setParameter('treeNodeParentID', $this->parent->getTreeNodeID());
}
return parent::deliverQueryObject();
}
public function finalizeQuery(\Doctrine\DBAL\Query\QueryBuilder $query)
{
$u = Application::getFacadeApplication()->make(User::class);
// Super user can search any files
if (!$u->isSuperUser()) {
/** @var FileFolderKey $pk */
$pk = FileFolderKey::getByHandle('search_file_folder');
if (is_object($pk)) {
$pk->setPermissionObject($this->parent);
/** @var \Concrete\Core\Permission\Access\Access $pa */
$pa = $pk->getPermissionAccessObject();
// Check whether or not current user can search files in the current folder
if (is_object($pa) && $pa->validate()) {
// Get all access entities without "File Uploader" entity
$accessEntitiesWithoutFileUploader = [];
$accessEntities = $u->getUserAccessEntityObjects();
foreach ($accessEntities as $accessEntity) {
if (!$accessEntity instanceof FileUploaderEntity) {
$accessEntitiesWithoutFileUploader[] = $accessEntity;
}
}
/*
* For performance reason, if the current user can not search files without "File Uploader" entity,
* we filter only files that uploaded by the current user or permission overridden.
*/
if (!$pa->validateAccessEntities($accessEntitiesWithoutFileUploader)) {
$query
->leftJoin('tf', 'Files', 'f', 'tf.fID = f.fID')
->andWhere('(f.uID = :fileUploaderID OR f.fOverrideSetPermissions = 1) OR nt.treeNodeTypeHandle != \'file\'')
->setParameter('fileUploaderID', $u->getUserID())
;
}
}
}
}
return $query;
}
public function sortByNodeName()
{
$this->sortBy('name', 'asc');
}
public function sortByNodeType()
{
$this->sortBy('type', 'asc');
}
protected function getAttributeKeyClassName()
{
return '\\Concrete\\Core\\Attribute\\Key\\FileKey';
}
}