1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Entitizer\Utils { |
4
|
|
|
|
5
|
|
|
use Modules\Entitizer, Utils\Messages, Utils\View, Ajax, Language, Number, Request, Template; |
6
|
|
|
|
7
|
|
|
abstract class Handler { |
8
|
|
|
|
9
|
|
|
protected $create = false, $parent = null, $entity = null, $form = null; |
10
|
|
|
|
11
|
|
|
# Process parent block |
12
|
|
|
|
13
|
|
|
private function processParent(Template\Asset\Block $parent) { |
14
|
|
|
|
15
|
|
|
# Set parent id |
16
|
|
|
|
17
|
|
|
$parent->id = $this->parent->id; |
|
|
|
|
18
|
|
|
|
19
|
|
|
# Set create button |
20
|
|
|
|
21
|
|
|
$parent->block('create')->class = ($this->create ? 'active item' : 'item'); |
22
|
|
|
|
23
|
|
|
$parent->block('create')->id = $this->parent->id; |
24
|
|
|
|
25
|
|
|
# Set edit button |
26
|
|
|
|
27
|
|
|
if (0 === $this->parent->id) $parent->block('edit')->disable(); else { |
28
|
|
|
|
29
|
|
|
$parent->block('edit')->class = (!$this->create ? 'active item' : 'item'); |
30
|
|
|
|
31
|
|
|
$parent->block('edit')->id = $this->parent->id; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
# Process selector block |
36
|
|
|
|
37
|
|
|
private function processSelector(Template\Asset\Block $selector) { |
38
|
|
|
|
39
|
|
|
if ($this->create) return $selector->disable(); |
40
|
|
|
|
41
|
|
|
$parent = Entitizer::get(static::$type, $this->entity->parent_id); |
42
|
|
|
|
43
|
|
|
$selector->set(static::$naming, $parent->__get(static::$naming)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
# Get contents |
47
|
|
|
|
48
|
|
|
private function getContents() { |
49
|
|
|
|
50
|
|
|
$contents = View::get(static::$view); |
51
|
|
|
|
52
|
|
|
# Set id |
53
|
|
|
|
54
|
|
|
$contents->id = $this->entity->id; |
55
|
|
|
|
56
|
|
|
# Set path / title |
57
|
|
|
|
58
|
|
|
if (static::$nesting) $contents->path = $this->parent->path; |
59
|
|
|
|
60
|
|
|
else $contents->title = ($this->create ? Language::get(static::$naming_new) : $this->entity->__get(static::$naming)); |
61
|
|
|
|
62
|
|
|
# Set link |
63
|
|
|
|
64
|
|
|
$link = (INSTALL_PATH . static::$link . '/'); |
65
|
|
|
|
66
|
|
|
if (static::$nesting) $contents->link = ($link . ($this->create ? 'create' : 'edit') . '?id=' . $this->parent->id); |
67
|
|
|
|
68
|
|
|
else $contents->link = ($link . ($this->create ? 'create' : ('edit?id=' . $this->entity->id))); |
69
|
|
|
|
70
|
|
|
# Process parent block |
71
|
|
|
|
72
|
|
|
if (static::$nesting) $this->processParent($contents->block('parent')); |
73
|
|
|
|
74
|
|
|
# Process selector block |
75
|
|
|
|
76
|
|
|
if (static::$nesting) $this->processSelector($contents->block('selector')); |
77
|
|
|
|
78
|
|
|
# Implement form |
79
|
|
|
|
80
|
|
|
$this->form->implement($contents); |
81
|
|
|
|
82
|
|
|
# Add additional data for specific entity |
83
|
|
|
|
84
|
|
|
$this->processEntity($contents); |
85
|
|
|
|
86
|
|
|
# ------------------------ |
87
|
|
|
|
88
|
|
|
return $contents; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
# Handle ajax request |
92
|
|
|
|
93
|
|
|
private function handleAjax() { |
94
|
|
|
|
95
|
|
|
$ajax = Ajax::response(); |
96
|
|
|
|
97
|
|
|
# Create entity |
98
|
|
|
|
99
|
|
|
$this->entity = Entitizer::get(static::$type, Number::format(Request::get('id'))); |
100
|
|
|
|
101
|
|
|
# Process remove action |
102
|
|
|
|
103
|
|
View Code Duplication |
if (Request::post('action') === 'remove') { |
|
|
|
|
104
|
|
|
|
105
|
|
|
if (!$this->entity->remove()) return $ajax->error(Language::get(static::$message_error_remove)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
# ------------------------ |
109
|
|
|
|
110
|
|
|
return $ajax; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
# Handle request |
114
|
|
|
|
115
|
|
|
public function handle(bool $create = false) { |
116
|
|
|
|
117
|
|
|
if (!($this->create = $create) && Request::isAjax()) return $this->handleAjax(); |
118
|
|
|
|
119
|
|
|
# Create entity |
120
|
|
|
|
121
|
|
|
if (static::$nesting) $this->parent = Entitizer::get(static::$type, Number::format(Request::get('id'))); |
122
|
|
|
|
123
|
|
|
$this->entity = Entitizer::get(static::$type, (!$this->create ? Number::format(Request::get('id')) : 0)); |
124
|
|
|
|
125
|
|
|
# Redirect if entity not found |
126
|
|
|
|
127
|
|
|
if (!$this->create && (0 === $this->entity->id)) return Request::redirect(INSTALL_PATH . static::$link); |
128
|
|
|
|
129
|
|
|
# Create form |
130
|
|
|
|
131
|
|
|
$this->form = new static::$form_class($this->entity); |
132
|
|
|
|
133
|
|
|
if (static::$nesting && $this->create) $this->form->get('parent_id')->set($this->parent->id); |
134
|
|
|
|
135
|
|
|
# Handle form |
136
|
|
|
|
137
|
|
|
if ($this->form->handle(new static::$controller($this->entity))) { |
138
|
|
|
|
139
|
|
|
Request::redirect(INSTALL_PATH . static::$link . '/edit?id=' . $this->entity->id . '&submitted'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
# Display success message |
143
|
|
|
|
144
|
|
|
if (!$this->create && (false !== Request::get('submitted'))) { |
145
|
|
|
|
146
|
|
|
Messages::success(Language::get(static::$message_success_save)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
# ------------------------ |
150
|
|
|
|
151
|
|
|
return $this->getContents(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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.