1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Cadmium\System\Modules\Entitizer |
5
|
|
|
* @author Anton Romanov |
6
|
|
|
* @copyright Copyright (c) 2015-2017, Anton Romanov |
7
|
|
|
* @link http://cadmium-cms.com |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Modules\Entitizer\Utils { |
11
|
|
|
|
12
|
|
|
use Frames, Modules\Entitizer, Utils\Popup, Utils\View, Ajax, Language, Number, Request, Template; |
13
|
|
|
|
14
|
|
|
abstract class Handler extends Frames\Admin\Area\Panel { |
15
|
|
|
|
16
|
|
|
protected $create = false, $entity = null, $parent = null, $path = [], $form = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Process the parent block |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
private function processParent(Template\Block $parent) { |
23
|
|
|
|
24
|
|
|
# Set parent id |
25
|
|
|
|
26
|
|
|
$parent->id = $this->parent->id; |
|
|
|
|
27
|
|
|
|
28
|
|
|
# Set create button |
29
|
|
|
|
30
|
|
|
if (count($this->path) < CONFIG_ENTITIZER_MAX_DEPTH) { |
31
|
|
|
|
32
|
|
|
$parent->getBlock('create')->class = ($this->create ? 'active item' : 'item'); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$parent->getBlock('create')->id = $this->parent->id; |
|
|
|
|
35
|
|
|
|
36
|
|
|
} else { $parent->getBlock('create')->disable(); $parent->getBlock('create_disabled')->enable(); } |
37
|
|
|
|
38
|
|
|
# Set edit button |
39
|
|
|
|
40
|
|
|
if (0 !== $this->parent->id) { |
|
|
|
|
41
|
|
|
|
42
|
|
|
$parent->getBlock('edit')->class = (!$this->create ? 'active item' : 'item'); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$parent->getBlock('edit')->id = $this->parent->id; |
|
|
|
|
45
|
|
|
|
46
|
|
|
} else { $parent->getBlock('edit')->disable(); $parent->getBlock('edit_disabled')->enable(); } |
47
|
|
|
|
48
|
|
|
# Add parent additional data |
49
|
|
|
|
50
|
|
|
$this->processEntityParent($parent); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Process the selector block |
55
|
|
|
*/ |
56
|
|
|
|
57
|
|
|
private function processSelector(Template\Block $selector) { |
58
|
|
|
|
59
|
|
|
if ($this->create) return $selector->disable(); |
60
|
|
|
|
61
|
|
|
$selector->parent_id = $this->entity->parent_id; |
|
|
|
|
62
|
|
|
|
63
|
|
|
$parent = Entitizer::get(static::$table, $this->entity->parent_id); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$selector->super_parent_id = $parent->parent_id; |
|
|
|
|
66
|
|
|
|
67
|
|
|
$selector->set(static::$naming, $parent->get(static::$naming)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get the contents block |
72
|
|
|
*/ |
73
|
|
|
|
74
|
|
|
private function getContents() : Template\Block { |
75
|
|
|
|
76
|
|
|
$contents = View::get(static::$view); |
77
|
|
|
|
78
|
|
|
# Set id |
79
|
|
|
|
80
|
|
|
$contents->id = $this->entity->id; |
|
|
|
|
81
|
|
|
|
82
|
|
|
# Set path / title |
83
|
|
|
|
84
|
|
|
if (static::$nesting) $contents->path = $this->path; |
|
|
|
|
85
|
|
|
|
86
|
|
|
else $contents->title = ($this->create ? Language::get(static::$naming_new) : $this->entity->get(static::$naming)); |
|
|
|
|
87
|
|
|
|
88
|
|
|
# Process parent block |
89
|
|
|
|
90
|
|
|
if (static::$nesting) $this->processParent($contents->getBlock('parent')); |
91
|
|
|
|
92
|
|
|
# Set link |
93
|
|
|
|
94
|
|
|
$link = (INSTALL_PATH . static::$link . '/'); |
95
|
|
|
|
96
|
|
|
if (static::$nesting) $contents->link = ($link . ($this->create ? 'create' : 'edit') . '?id=' . $this->parent->id); |
|
|
|
|
97
|
|
|
|
98
|
|
|
else $contents->link = ($link . ($this->create ? 'create' : ('edit?id=' . $this->entity->id))); |
|
|
|
|
99
|
|
|
|
100
|
|
|
# Process selector block |
101
|
|
|
|
102
|
|
|
if (static::$nesting) $this->processSelector($contents->getBlock('selector')); |
103
|
|
|
|
104
|
|
|
# Implement form |
105
|
|
|
|
106
|
|
|
$this->form->implement($contents); |
107
|
|
|
|
108
|
|
|
# Add additional data for specific entity |
109
|
|
|
|
110
|
|
|
$this->processEntity($contents); |
111
|
|
|
|
112
|
|
|
# ------------------------ |
113
|
|
|
|
114
|
|
|
return $contents; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Handle the ajax request |
119
|
|
|
*/ |
120
|
|
|
|
121
|
|
|
private function handleAjax() : Ajax\Response { |
122
|
|
|
|
123
|
|
|
$ajax = Ajax::createResponse(); |
124
|
|
|
|
125
|
|
|
# Create entity |
126
|
|
|
|
127
|
|
|
$id = Number::forceInt(Request::get('id')); |
128
|
|
|
|
129
|
|
|
$this->entity = Entitizer::get(static::$table, (!$this->create ? $id : 0)); |
130
|
|
|
|
131
|
|
|
# Process actions |
132
|
|
|
|
133
|
|
|
if (Request::post('action') === 'move') { |
134
|
|
|
|
135
|
|
|
$parent_id = Number::forceInt(Request::post('parent_id')); |
136
|
|
|
|
137
|
|
|
if (!$this->entity->move($parent_id)) return $ajax->setError(Language::get(static::$message_error_move)); |
138
|
|
|
|
139
|
|
View Code Duplication |
} else if (Request::post('action') === 'remove') { |
|
|
|
|
140
|
|
|
|
141
|
|
|
if (!$this->entity->remove()) return $ajax->setError(Language::get(static::$message_error_remove)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
# ------------------------ |
145
|
|
|
|
146
|
|
|
return $ajax; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Handle the request |
151
|
|
|
* |
152
|
|
|
* @return Template\Block|Ajax\Response : a block object if the ajax param was set to false, otherwise an ajax response |
153
|
|
|
*/ |
154
|
|
|
|
155
|
|
|
protected function handle(bool $ajax = false) { |
156
|
|
|
|
157
|
|
|
if (!$this->create && $ajax) return $this->handleAjax(); |
158
|
|
|
|
159
|
|
|
# Create entity |
160
|
|
|
|
161
|
|
|
$id = Number::forceInt(Request::get('id')); |
162
|
|
|
|
163
|
|
|
$this->entity = Entitizer::get(static::$table, (!$this->create ? $id : 0)); |
164
|
|
|
|
165
|
|
|
if (!$this->create && (0 === $this->entity->id)) return Request::redirect(INSTALL_PATH . static::$link); |
|
|
|
|
166
|
|
|
|
167
|
|
|
# Create parent entity |
168
|
|
|
|
169
|
|
|
$this->parent = Entitizer::get(static::$table, (static::$nesting ? $id : 0)); |
170
|
|
|
|
171
|
|
|
# Get path |
172
|
|
|
|
173
|
|
|
if (false !== ($path = $this->parent->getPath())) $this->path = $path; |
174
|
|
|
|
175
|
|
|
# Create form |
176
|
|
|
|
177
|
|
|
$this->form = new static::$form_class($this->entity); |
178
|
|
|
|
179
|
|
|
# Handle form |
180
|
|
|
|
181
|
|
|
if ($this->form->handle(new static::$controller_class($this->entity), true)) { |
182
|
|
|
|
183
|
|
|
if ($this->create && (0 !== $this->parent->id)) $this->entity->move($this->parent->id); |
|
|
|
|
184
|
|
|
|
185
|
|
|
Request::redirect(INSTALL_PATH . static::$link . '/edit?id=' . $this->entity->id . '&submitted'); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
# Display success message |
189
|
|
|
|
190
|
|
|
if (!$this->create && (false !== Request::get('submitted'))) { |
191
|
|
|
|
192
|
|
|
Popup::set('positive', Language::get(static::$message_success_save)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
# ------------------------ |
196
|
|
|
|
197
|
|
|
return $this->getContents(); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
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.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.