1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Cadmium\System\Modules\Filemanager |
5
|
|
|
* @author Anton Romanov |
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
7
|
|
|
* @link http://cadmium-cms.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Modules\Filemanager\Utils { |
11
|
|
|
|
12
|
|
|
use Explorer, Str; |
13
|
|
|
|
14
|
|
|
class Loader { |
15
|
|
|
|
16
|
|
|
private $parent = null, $index = 0, $display = 0, $items = [], $total = 0; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Constructor |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
public function __construct(Container $parent) { |
23
|
|
|
|
24
|
|
|
$this->parent = $parent; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Load items from the filesystem |
29
|
|
|
* |
30
|
|
|
* @param $index a page index |
31
|
|
|
* @param $display a number of results per page |
32
|
|
|
* |
33
|
|
|
* @return Modules\Filemanager\Utils\Loader : the current loader object |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
public function load(int $index = 0, int $display = 0) : Loader { |
37
|
|
|
|
38
|
|
|
if (!(($index >= 0) && ($display >= 0))) return $this; |
39
|
|
|
|
40
|
|
|
$dirs = []; $files = []; |
41
|
|
|
|
42
|
|
|
# Read parent directory contents |
43
|
|
|
|
44
|
|
|
foreach (Explorer::iterate($this->parent->getPathFull()) as $name) { |
45
|
|
|
|
46
|
|
|
if (!($entity = new Entity($this->parent, $name))->isInited()) continue; |
47
|
|
|
|
48
|
|
|
if ($entity->isDir()) $dirs[] = $entity; else $files[] = $entity; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
# Sort arrays |
52
|
|
|
|
53
|
|
|
$sort = function (Entity $a, Entity $b) { |
54
|
|
|
|
55
|
|
|
return strcmp(Str::toLower($a->getName()), Str::toLower($b->getName())); |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
usort($dirs, $sort); usort($files, $sort); |
59
|
|
|
|
60
|
|
|
# Extract a set of items to display |
61
|
|
|
|
62
|
|
|
$items = array_merge($dirs, $files); $total = count($items); |
63
|
|
|
|
64
|
|
|
if (($index > 0) && ($display > 0)) $items = array_splice($items, (($index - 1) * $display), $display); |
65
|
|
|
|
66
|
|
|
# Set properties |
67
|
|
|
|
68
|
|
|
$this->index = $index; $this->display = $display; $this->items = $items; $this->total = $total; |
69
|
|
|
|
70
|
|
|
# ------------------------ |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the page index |
77
|
|
|
*/ |
78
|
|
|
|
79
|
|
|
public function getIndex() : int { |
80
|
|
|
|
81
|
|
|
return $this->index; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the number of results per page |
86
|
|
|
*/ |
87
|
|
|
|
88
|
|
|
public function getDisplay() : int { |
89
|
|
|
|
90
|
|
|
return $this->display; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the items list |
95
|
|
|
*/ |
96
|
|
|
|
97
|
|
|
public function getItems() : array { |
98
|
|
|
|
99
|
|
|
return $this->items; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the total number of items |
104
|
|
|
*/ |
105
|
|
|
|
106
|
|
|
public function getTotal() : int { |
107
|
|
|
|
108
|
|
|
return $this->total; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|