Lister::handle()   D
last analyzed

Complexity

Conditions 9
Paths 25

Size

Total Lines 44
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 25
nop 1
dl 0
loc 44
rs 4.909
c 0
b 0
f 0
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 Frames, Modules\Filemanager, Modules\Settings, Utils\Pagination, Utils\Popup, Utils\Uploader, Utils\View;
13
	use Ajax, Language, Number, Request, Template, Url;
14
15
	abstract class Lister extends Frames\Admin\Area\Panel {
16
17
		protected $_title = 'TITLE_CONTENT_FILEMANAGER';
18
19
		private $parent = null, $form = null, $loader = null;
20
21
		/**
22
		 * Process the bar block
23
		 */
24
25
		private function processBar(Template\Block $bar) {
26
27
			if (!static::$permissions['manage']) { $bar->disable(); return; }
28
29
			# Set link
30
31
			$query = (('' !== $this->parent->getPath()) ? ('?parent=' . $this->parent->getPath()) : '');
32
33
			$bar->link = (INSTALL_PATH . '/admin/content/filemanager' . $query);
0 ignored issues
show
Documentation introduced by
The property link does not exist on object<Template\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
34
35
			# Implement form
36
37
			$this->form->implement($bar);
38
		}
39
40
		/**
41
		 * Get the pagination block
42
		 *
43
		 * @return Template\Block|false : a block or false if there is only a single page
44
		 */
45
46
		private function getPaginationBlock() {
47
48
			$query = (('' !== $this->parent->getPath()) ? ('?parent=' . $this->parent->getPath()) : '');
49
50
			$url = new Url(INSTALL_PATH . '/admin/content/filemanager' . $query);
51
52
			# ------------------------
53
54
			return Pagination::getBlock($this->loader->getIndex(), $this->loader->getDisplay(), $this->loader->getTotal(), $url);
55
		}
56
57
		/**
58
		 * Get the contents block
59
		 */
60
61
		private function getContents(bool $ajax = false) : Template\Block {
62
63
			$contents = View::get(!$ajax ? 'Blocks/Filemanager/Lister/Main' : 'Blocks/Filemanager/Ajax/Main');
64
65
			# Set parent
66
67
			if (!$ajax) $contents->parent = $this->parent->getPath();
0 ignored issues
show
Documentation introduced by
The property parent does not exist on object<Template\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
69
			# Set origin and title
70
71
			$contents->origin = static::$origin; $contents->title = Language::get(static::$title);
0 ignored issues
show
Documentation introduced by
The property origin does not exist on object<Template\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property title does not exist on object<Template\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
72
73
			# Set breadcrumbs
74
75
			$contents->breadcrumbs = $this->parent->getBreadcrumbs();
0 ignored issues
show
Documentation introduced by
The property breadcrumbs does not exist on object<Template\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
76
77
			# Process bar
78
79
			if (!$ajax) $this->processBar($contents->getBlock('bar'));
80
81
			# Set items
82
83
			$items = $contents->getBlock('items');
84
85
			foreach ($this->loader->getItems() as $entity) {
86
87
				if ($entity->isDir()) $items->addItem(Lister\Dir::getBlock($entity, $ajax));
88
89
				else $items->addItem(Lister\File::getBlock($entity, $ajax));
90
			}
91
92
			# Set pagination
93
94
			if (!$ajax) $contents->pagination = $this->getPaginationBlock();
0 ignored issues
show
Documentation introduced by
The property pagination does not exist on object<Template\Block>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
95
96
			# ------------------------
97
98
			return $contents;
99
		}
100
101
		/**
102
		 * Handle the ajax request
103
		 */
104
105
		private function handleAjax() : Ajax\Response {
106
107
			$ajax = Ajax::createResponse();
108
109
			# Create parent
110
111
			$this->parent = new static::$container_class(Request::get('parent'));
112
113
			# Load items
114
115
			$this->loader = (new Loader($this->parent))->load();
116
117
			# ------------------------
118
119
			return $ajax->set('contents', $this->getContents(true)->getContents());
120
		}
121
122
		/**
123
		 * Handle the request
124
		 *
125
		 * @return Template\Block|Ajax\Response : a block object if the ajax param was set to false, otherwise an ajax response
126
		 */
127
128
		protected function handle(bool $ajax = false) {
129
130
			if ($ajax) return $this->handleAjax();
131
132
			# Create parent
133
134
			$this->parent = new static::$container_class(Request::get('parent'));
135
136
			# Create form
137
138
			$this->form = new Filemanager\Form\Create;
139
140
			# Handle form
141
142
			if (static::$permissions['manage']) {
143
144
				if (Uploader::handle('upload', $this->parent->getPathFull(), true)) $submitted = 'upload';
145
146
				else if ($this->form->handle(new Filemanager\Controller\Create($this->parent), true)) $submitted = 'create';
147
			}
148
149
			if (isset($submitted)) {
150
151
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager/' . static::$origin . '?' . (('' !==
152
153
					($parent = $this->parent->getPath())) ? ('parent=' . $parent . '&') : '') . 'submitted=' . $submitted);
154
			}
155
156
			# Display success message
157
158
			if (Request::get('submitted') === 'upload') Popup::set('positive', Language::get('FILEMANAGER_SUCCESS_UPLOAD'));
159
160
			else if (Request::get('submitted') === 'create') Popup::set('positive', Language::get('FILEMANAGER_SUCCESS_DIR_CREATE'));
161
162
			# Load items
163
164
			$index = Number::forceInt(Request::get('index'), 1, 999999);
165
166
			$this->loader = (new Loader($this->parent))->load($index, Settings::get('admin_display_files'));
167
168
			# ------------------------
169
170
			return $this->getContents();
171
		}
172
	}
173
}
174