Completed
Branch 0.2.1 (e70612)
by Anton
09:15
created

Handler::getContents()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 28
rs 8.8571
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Modules\Filemanager\Utils {
4
5
	use Modules\Filemanager, Modules\Informer, Utils\Messages, Utils\View, Ajax, Language, Request;
6
7
	abstract class Handler {
8
9
		protected $parent = null, $entity = null, $form = null;
10
11
		# Get contents
12
13
		private function getContents() {
14
15
			$contents = View::get(static::$view);
16
17
			# Set breadcrumbs
18
19
			$contents->breadcrumbs = $this->parent->breadcrumbs();
20
21
			# Set parent
22
23
			$contents->parent = $this->parent->path();
24
25
			# Set name
26
27
			$contents->name = $this->entity->name();
28
29
			# Implement form
30
31
			$this->form->implement($contents);
32
33
			# Set info
34
35
			$this->processInfo($contents->block('info'));
36
37
			# ------------------------
38
39
			return $contents;
40
		}
41
42
		# Handle ajax request
43
44
		private function handleAjax() {
45
46
			$ajax = Ajax::response();
47
48
			# Check for demo mode
49
50
			if (Informer::isDemoMode()) return $ajax->error(Language::get('DEMO_MODE_RESTRICTION'));
51
52
			# Remove item
53
54 View Code Duplication
			if (Request::post('action') === 'remove') {
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...
55
56
				if (!$this->entity->remove()) return $ajax->error(Language::get(static::$message_error_remove));
57
			}
58
59
			# ------------------------
60
61
			return $ajax;
62
		}
63
64
		# Handle request
65
66
		public function handle() {
67
68
			# Create parent
69
70
			$this->parent = Filemanager::open(Request::get('parent'));
71
72
			# Create entity
73
74
			$this->entity = Filemanager::get(static::$type, $this->parent);
75
76
			# Init entity
77
78 View Code Duplication
			if (!$this->entity->init(Request::get('name'))) {
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...
79
80
				$query = (('' !== $this->parent->path()) ? ('?parent=' . $this->parent->path()) : '');
81
82
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager' . $query);
83
			}
84
85
			# Handle ajax request
86
87
			if (Request::isAjax()) return $this->handleAjax();
88
89
			# Create form
90
91
			$this->form = new Filemanager\Form\Rename($this->entity);
92
93
			# Handle form
94
95
			if ($this->form->handle(new Filemanager\Controller\Rename($this->entity))) {
96
97
				$query = ('?parent=' . $this->parent->path() . '&name=' . $this->entity->name() . '&submitted');
98
99
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager/' . static::$type . $query);
100
			}
101
102
			# Display success message
103
104
			if (false !== Request::get('submitted')) Messages::success(Language::get(static::$message_success_rename));
105
106
			# ------------------------
107
108
			return $this->getContents();
109
		}
110
	}
111
}
112