Completed
Push — master ( 416cb1...222de5 )
by Anton
03:44
created

Listview::getDirItemBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.4286
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
namespace Modules\Filemanager\Handler {
4
5
	use Modules\Filemanager, Utils\Messages, Utils\Pagination, Utils\Uploader, Utils\View;
6
	use Ajax, Arr, Explorer, Language, Number, Request, Template, Url;
7
8
	class Listview {
9
10
		private $parent = null, $form = null, $index = 0, $items = [];
11
12
		# Get items
13
14
		private function getItems() {
15
16
			$dirs = []; $files = [];
17
18
			$prefix = (('' !== $this->parent->path()) ? ($this->parent->path() . '/') : '');
19
20
			# Read parent directory contents
21
22
			if (false !== ($handler = @opendir($this->parent->pathFull()))) {
23
24
				while (false !== ($name = readdir($handler))) {
25
26
					if (in_array($name, ['.', '..', '.empty'], true)) continue;
27
28
					$path = ($prefix . $name); $path_full = ($this->parent->pathFull() . $name);
29
30
					$data = ['name' => $name, 'path' => $path, 'path_full' => $path_full];
31
32
					if (@is_dir($path_full)) $dirs[] = array_merge($data, ['type' => FILEMANAGER_TYPE_DIR]);
33
34
					else if (@is_file($path_full)) $files[] = array_merge($data, ['type' => FILEMANAGER_TYPE_FILE]);
35
				}
36
37
				closedir($handler);
38
			}
39
40
			# Sort arrays
41
42
			Arr::sortby($dirs, 'name'); Arr::sortby($files, 'name');
43
44
			# Extract a set of items to display
45
46
			$list = array_merge($dirs, $files); $total = count($list); $display = CONFIG_ADMIN_FILEMANAGER_ITEMS_DISPLAY;
47
48
			$list = array_splice($list, (($this->index - 1) * $display), $display);
49
50
			# ------------------------
51
52
			return ['list' => $list, 'total' => $total];
53
		}
54
55
		# Get directory item block
56
57
		private function getDirItemBlock(array $data) {
58
59
			$view = View::get('Blocks\Filemanager\Listview\Item\Dir');
60
61
			# Set data
62
63
			$view->name = $data['name']; $view->path = $data['path'];
64
65
			$view->parent = $this->parent->path();
66
67
			# ------------------------
68
69
			return $view;
70
		}
71
72
		# Get file item block
73
74
		private function getFileItemBlock(array $data) {
75
76
			$view = View::get('Blocks\Filemanager\Listview\Item\File');
77
78
			# Set data
79
80
			$view->name = $data['name']; $view->path = $data['path'];
81
82
			$view->type = Filemanager\Utils\Mime::type($data['name']);
83
84
			$view->size = Number::size(@filesize($data['path_full']));
85
86
			$view->parent = $this->parent->path();
87
88
			# ------------------------
89
90
			return $view;
91
		}
92
93
		# Get items block
94
95
		private function getItemsBlock() {
96
97
			$items = Template::group();
98
99
			foreach ($this->items['list'] as $item) {
100
101
				if ($item['type'] === FILEMANAGER_TYPE_DIR) $items->add($this->getDirItemBlock($item));
102
103
				else if ($item['type'] === FILEMANAGER_TYPE_FILE) $items->add($this->getFileItemBlock($item));
104
			}
105
106
			# ------------------------
107
108
			return $items;
109
		}
110
111
		# Get pagination block
112
113
		private function getPaginationBlock() {
114
115
			$query = (('' !== $this->parent->path()) ? ('?parent=' . $this->parent->path()) : '');
116
117
			$url = new Url(INSTALL_PATH . '/admin/content/filemanager' . $query);
118
119
			# ------------------------
120
121
			return Pagination::block($this->index, CONFIG_ADMIN_FILEMANAGER_ITEMS_DISPLAY, $this->items['total'], $url);
122
		}
123
124
		# Get contents
125
126
		private function getContents() {
127
128
			$contents = View::get('Blocks\Filemanager\Listview\Main');
129
130
			# Set parent
131
132
			$contents->parent = $this->parent->path();
133
134
			# Set breadcrumbs
135
136
			$contents->breadcrumbs = $this->parent->breadcrumbs();
137
138
			# Set link
139
140
			$query = (('' !== $this->parent->path()) ? ('?parent=' . $this->parent->path()) : '');
141
142
			$contents->link = (INSTALL_PATH . '/admin/content/filemanager' . $query);
143
144
			# Implement form
145
146
			$this->form->implement($contents);
147
148
			# Set items
149
150
			$items = $this->getItemsBlock();
151
152
			if ($items->count() > 0) $contents->items = $items;
153
154
			# Set pagination
155
156
			$contents->pagination = $this->getPaginationBlock();
157
158
			# ------------------------
159
160
			return $contents;
161
		}
162
163
		# Handle request
164
165
		public function handle() {
166
167
			# Create parent
168
169
			$this->parent = Filemanager::open(Request::get('parent'));
170
171
			# Create form
172
173
			$this->form = new Filemanager\Form\Create();
174
175
			# Save uploaded file
176
177
			$upload = (Uploader::submit('upload', $this->parent->pathFull()));
178
179
			# Handle form
180
181 View Code Duplication
			if ($upload || $this->form->handle(new Filemanager\Controller\Create($this->parent))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
183
				$query = (('' !== $this->parent->path()) ? ('?parent=' . $this->parent->path()) : '');
184
185
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager' . $query);
186
			}
187
188
			# Get index
189
190
			$this->index = Number::format(Request::get('index'), 1, 999999);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Number::format(\Request...et('index'), 1, 999999) can also be of type double or string. However, the property $index is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
191
192
			# Get items
193
194
			$this->items = $this->getItems();
195
196
			# ------------------------
197
198
			return $this->getContents();
199
		}
200
	}
201
}
202