Completed
Push — master ( f07209...e7c824 )
by Anton
04:34
created

Handler::handleAjax()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 8

Duplication

Lines 4
Ratio 15.38 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 4
loc 26
rs 8.439
c 1
b 1
f 0
cc 5
eloc 8
nc 5
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
			# Init entity
53
54
			if (!$this->entity->init(Request::get('name'))) {
55
56
				return $ajax->error(Language::get(static::$message_error_remove));
57
			}
58
59
			# Process remove action
60
61 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...
62
63
				if (!$this->entity->remove()) return $ajax->error(Language::get(static::$message_error_remove));
64
			}
65
66
			# ------------------------
67
68
			return $ajax;
69
		}
70
71
		# Handle request
72
73
		public function handle() {
74
75
			# Create parent
76
77
			$this->parent = Filemanager::open(Request::get('parent'));
78
79
			# Create entity
80
81
			$this->entity = Filemanager::get(static::$type, $this->parent);
82
83
			# Handle ajax request
84
85
			if (Request::isAjax()) return $this->handleAjax();
86
87
			# Init entity
88
89 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...
90
91
				$query = (('' !== $this->parent->path()) ? ('?parent=' . $this->parent->path()) : '');
92
93
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager' . $query);
94
			}
95
96
			# Create form
97
98
			$this->form = new Filemanager\Form\Rename($this->entity);
99
100
			# Handle form
101
102
			if ($this->form->handle(new Filemanager\Controller\Rename($this->entity))) {
103
104
				$query = ('?parent=' . $this->parent->path() . '&name=' . $this->entity->name() . '&submitted');
105
106
				Request::redirect(INSTALL_PATH . '/admin/content/filemanager/' . static::$type . $query);
107
			}
108
109
			# Display success message
110
111
			if (false !== Request::get('submitted')) Messages::success(Language::get(static::$message_success_rename));
112
113
			# ------------------------
114
115
			return $this->getContents();
116
		}
117
	}
118
}
119