/
FileList.php
324 lines (286 loc) · 9.47 KB
/
FileList.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
<?php
namespace Concrete\Core\File;
use Concrete\Core\File\StorageLocation\StorageLocationFactory;
use Concrete\Core\Search\ItemList\Database\AttributedItemList as DatabaseItemList;
use Concrete\Core\Search\ItemList\Pager\Manager\FileListPagerManager;
use Concrete\Core\Search\ItemList\Pager\PagerProviderInterface;
use Concrete\Core\Search\ItemList\Pager\QueryString\VariableFactory;
use Concrete\Core\Search\Pagination\PaginationProviderInterface;
use Concrete\Core\Search\StickyRequest;
use Concrete\Core\User\User;
use Concrete\Core\Support\Facade\Application;
use FileAttributeKey;
use Pagerfanta\Adapter\DoctrineDbalAdapter;
use Exception;
class FileList extends DatabaseItemList implements PagerProviderInterface, PaginationProviderInterface
{
/**
* @var \Closure|int|null
*/
protected $permissionsChecker;
protected $paginationPageParameter = 'ccm_paging_fl';
/**
* Columns in this array can be sorted via the request.
*
* @var array
*/
protected $autoSortColumns = [
'fv.fvFilename',
'fv.fvTitle',
'f.fDateAdded',
'fv.fvDateAdded',
'fv.fvSize',
];
public function __construct(StickyRequest $req = null)
{
$u = Application::getFacadeApplication()->make(User::class);
if ($u->isSuperUser()) {
$this->ignorePermissions();
}
parent::__construct($req);
}
public function getPermissionsChecker()
{
return $this->permissionsChecker;
}
public function getPagerManager()
{
return new FileListPagerManager($this);
}
public function getPagerVariableFactory()
{
return new VariableFactory($this, $this->getSearchRequest());
}
public function setPermissionsChecker(\Closure $checker = null)
{
$this->permissionsChecker = $checker;
}
public function ignorePermissions()
{
$this->permissionsChecker = -1;
}
public function enablePermissions()
{
unset($this->permissionsChecker);
}
public function createQuery()
{
$this->query->select('f.fID')
->from('Files', 'f')
->innerJoin('f', 'FileVersions', 'fv', 'f.fID = fv.fID and fv.fvIsApproved = 1')
->leftJoin('f', 'FileSearchIndexAttributes', 'fsi', 'f.fID = fsi.fID')
->leftJoin('f', 'Users', 'u', 'f.uID = u.uID');
}
public function getTotalResults()
{
if ($this->permissionsChecker === -1) {
$query = $this->deliverQueryObject();
return $query->resetQueryParts([
'groupBy',
'orderBy',
])->select('count(distinct f.fID)')->setMaxResults(1)->execute()->fetchColumn();
} else {
return -1; // unknown
}
}
public function getPaginationAdapter()
{
$adapter = new DoctrineDbalAdapter($this->deliverQueryObject(), function ($query) {
$query->resetQueryParts(['groupBy', 'orderBy'])->select('count(distinct f.fID)')->setMaxResults(1);
});
return $adapter;
}
/**
* @param $queryRow
*
* @return \Concrete\Core\Entity\File\File
*/
public function getResult($queryRow)
{
$f = File::getByID($queryRow['fID']);
if (is_object($f) && $this->checkPermissions($f)) {
return $f;
}
}
public function checkPermissions($mixed)
{
if (isset($this->permissionsChecker)) {
if ($this->permissionsChecker === -1) {
return true;
} else {
return call_user_func_array($this->permissionsChecker, [$mixed]);
}
}
$fp = new \Permissions($mixed);
return $fp->canViewFile();
}
public function filterByType($type)
{
$this->filter('fvType', $type);
}
/**
* 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);
}
}
/**
* 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 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);
}
/**
* Filters by "keywords" (which searches everything including filenames,
* title, users who uploaded the file, tags).
*
* @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('fv.fvTitle', ':keywords'),
$this->query->expr()->like('fv.fvTags', ':keywords'),
$this->query->expr()->eq('uName', ':keywords'),
];
$keys = FileAttributeKey::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 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());
}
public function filterByNoSet()
{
$this->query->leftJoin('f', 'FileSetFiles', 'fsex', 'f.fID = fsex.fID');
$this->query->andWhere('fsex.fsID is null');
}
/**
* Filters the file list by file size (in kilobytes).
*
* @param int|float $from
* @param int|float $to
*/
public function filterBySize($from, $to)
{
$this->query->andWhere('fv.fvSize >= :fvSizeFrom');
$this->query->andWhere('fv.fvSize <= :fvSizeTo');
$this->query->setParameter('fvSizeFrom', $from * 1024);
$this->query->setParameter('fvSizeTo', $to * 1024);
}
/**
* 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);
}
/**
* filters a FileList by the uID of the approving User.
*
* @param int $uID
*/
public function filterByApproverUserID($uID)
{
$this->query->andWhere('fv.fvApproverUID = :fvApproverUID');
$this->query->setParameter('fvApproverUID', $uID);
}
/**
* filters a FileList by the uID of the owning User.
*
* @param int $uID
*
* @since 5.4.1.1+
*/
public function filterByAuthorUserID($uID)
{
$this->query->andWhere('fv.fvAuthorUID = :fvAuthorUID');
$this->query->setParameter('fvAuthorUID', $uID);
}
/**
* Filters by "tags" only.
*
* @param string $tags
*/
public function filterByTags($tags)
{
$this->query->andWhere(
$this->query->expr()->andX(
$this->query->expr()->like('fv.fvTags', ':tags')
)
);
$this->query->setParameter('tags', '%' . $tags . '%');
}
/**
* Sorts by filename in ascending order.
*/
public function sortByFilenameAscending()
{
$this->query->orderBy('fv.fvFilename', 'asc');
}
/**
* Sorts by file set display order in ascending order.
*/
public function sortByFileSetDisplayOrder()
{
$this->query->orderBy('fsDisplayOrder', 'asc');
}
protected function getAttributeKeyClassName()
{
return '\\Concrete\\Core\\Attribute\\Key\\FileKey';
}
}