|
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, Modules\Settings, Utils\Pagination, Utils\View, Ajax, Number, Request, Template, Url; |
|
13
|
|
|
|
|
14
|
|
|
abstract class Lister extends Frames\Admin\Area\Panel { |
|
15
|
|
|
|
|
16
|
|
|
protected $parent = null, $entity = null, $path = [], $depth = 0, $index = 0; |
|
17
|
|
|
|
|
18
|
|
|
protected $items = ['list' => [], 'total' => 0]; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Process the parent block |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
private function processParent(Template\Block $parent) { |
|
25
|
|
|
|
|
26
|
|
|
# Set parent id |
|
27
|
|
|
|
|
28
|
|
|
$parent->id = $this->parent->id; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
# Set create button |
|
31
|
|
|
|
|
32
|
|
|
if (count($this->path) < CONFIG_ENTITIZER_MAX_DEPTH) $parent->getBlock('create')->id = $this->parent->id; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
else { $parent->getBlock('create')->disable(); $parent->getBlock('create_disabled')->enable(); } |
|
35
|
|
|
|
|
36
|
|
|
# Set edit button |
|
37
|
|
|
|
|
38
|
|
View Code Duplication |
if (0 !== $this->parent->id) $parent->getBlock('edit')->id = $this->parent->id; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
else { $parent->getBlock('edit')->disable(); $parent->getBlock('edit_disabled')->enable(); } |
|
41
|
|
|
|
|
42
|
|
|
# Add parent additional data |
|
43
|
|
|
|
|
44
|
|
|
$this->processEntityParent($parent); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Process the items block |
|
49
|
|
|
*/ |
|
50
|
|
|
|
|
51
|
|
|
private function processItems(Template\Block $items, bool $ajax = false) { |
|
52
|
|
|
|
|
53
|
|
|
foreach ($this->items['list'] as $item) { |
|
54
|
|
|
|
|
55
|
|
|
if ((null !== $this->entity) && ($item['dataset']->id === $this->entity->id)) continue; |
|
56
|
|
|
|
|
57
|
|
|
$items->addItem($view = View::get(!$ajax ? static::$view_item : static::$view_ajax_item)); |
|
58
|
|
|
|
|
59
|
|
|
# Set data |
|
60
|
|
|
|
|
61
|
|
|
$view->id = $item['dataset']->id; |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
$view->set(static::$naming, $item['dataset']->get(static::$naming)); |
|
64
|
|
|
|
|
65
|
|
|
# Set buttons |
|
66
|
|
|
|
|
67
|
|
|
if (!$ajax) { |
|
68
|
|
|
|
|
69
|
|
|
if (static::$nesting) { |
|
70
|
|
|
|
|
71
|
|
|
$fertile = ((count($this->path) + 1) < CONFIG_ENTITIZER_MAX_DEPTH); |
|
72
|
|
|
|
|
73
|
|
|
$view->getBlock('create')->class = ($fertile ? 'olive' : 'disabled'); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
$view->getBlock('create')->id = $item['dataset']->id; |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$super = (static::$super && ($item['dataset']->id === 1)); |
|
79
|
|
|
|
|
80
|
|
|
$has_children = (static::$nesting && ($item['children'] > 0)); |
|
81
|
|
|
|
|
82
|
|
|
$view->getBlock('remove')->class = ((!$super && !$has_children) ? 'negative' : 'disabled'); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
} else { |
|
85
|
|
|
|
|
86
|
|
|
$selectable = ((count($this->path) + $this->depth + 1) < CONFIG_ENTITIZER_MAX_DEPTH); |
|
87
|
|
|
|
|
88
|
|
|
$view->getBlock('select')->class = ($selectable ? 'grey' : 'disabled'); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
# Add item additional data |
|
92
|
|
|
|
|
93
|
|
|
$this->processItem($view, $item['dataset'], (static::$nesting ? $item['children'] : 0)); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the pagination block |
|
99
|
|
|
* |
|
100
|
|
|
* @return Template\Block|false : a block or false if there is only a single page |
|
101
|
|
|
*/ |
|
102
|
|
|
|
|
103
|
|
|
private function getPaginationBlock() { |
|
104
|
|
|
|
|
105
|
|
|
$query = (static::$nesting ? ('?parent_id=' . $this->parent->id) : ''); |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
$url = new Url(INSTALL_PATH . static::$link . $query); |
|
108
|
|
|
|
|
109
|
|
|
# ------------------------ |
|
110
|
|
|
|
|
111
|
|
|
return Pagination::getBlock($this->index, Settings::get('admin_display_entities'), $this->items['total'], $url); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Get the contents block |
|
116
|
|
|
*/ |
|
117
|
|
|
|
|
118
|
|
|
private function getContents(bool $ajax = false) : Template\Block { |
|
119
|
|
|
|
|
120
|
|
|
$contents = View::get(!$ajax ? static::$view_main : static::$view_ajax_main); |
|
121
|
|
|
|
|
122
|
|
|
# Set path |
|
123
|
|
|
|
|
124
|
|
|
if (static::$nesting) $contents->path = $this->path; |
|
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
# Process parent block |
|
127
|
|
|
|
|
128
|
|
|
if (static::$nesting && !$ajax) $this->processParent($contents->getBlock('parent')); |
|
129
|
|
|
|
|
130
|
|
|
# Process items block |
|
131
|
|
|
|
|
132
|
|
|
$this->processItems($contents->getBlock('items'), $ajax); |
|
133
|
|
|
|
|
134
|
|
|
# Set pagination |
|
135
|
|
|
|
|
136
|
|
|
if (!$ajax) $contents->pagination = $this->getPaginationBlock(); |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
# ------------------------ |
|
139
|
|
|
|
|
140
|
|
|
return $contents; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Handle the ajax request |
|
145
|
|
|
*/ |
|
146
|
|
|
|
|
147
|
|
|
private function handleAjax() : Ajax\Response { |
|
148
|
|
|
|
|
149
|
|
|
$ajax = Ajax::createResponse(); |
|
150
|
|
|
|
|
151
|
|
|
# Create parent entity |
|
152
|
|
|
|
|
153
|
|
|
$parent_id = (static::$nesting ? Number::forceInt(Request::get('parent_id')) : 0); |
|
154
|
|
|
|
|
155
|
|
|
$this->parent = Entitizer::get(static::$table, $parent_id); |
|
156
|
|
|
|
|
157
|
|
|
# Create active entity |
|
158
|
|
|
|
|
159
|
|
|
$this->entity = Entitizer::get(static::$table, Number::forceInt(Request::post('id'))); |
|
160
|
|
|
|
|
161
|
|
|
# Get path and depth |
|
162
|
|
|
|
|
163
|
|
|
if (false !== ($path = $this->parent->getPath())) $this->path = $path; |
|
164
|
|
|
|
|
165
|
|
|
if ((0 !== $this->entity->id) && (false !== ($depth = $this->entity->getSubtreeDepth()))) $this->depth = $depth; |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
# Get items list |
|
168
|
|
|
|
|
169
|
|
|
$lister = (static::$nesting ? 'getChildren' : 'getItems'); |
|
170
|
|
|
|
|
171
|
|
|
if (false !== ($items = $this->parent->$lister())) $this->items = $items; |
|
172
|
|
|
|
|
173
|
|
|
# ------------------------ |
|
174
|
|
|
|
|
175
|
|
|
return $ajax->set('contents', $this->getContents(true)->getContents()); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Handle the request |
|
180
|
|
|
* |
|
181
|
|
|
* @return Template\Block|Ajax\Response : a block object if the ajax param was set to false, otherwise an ajax response |
|
182
|
|
|
*/ |
|
183
|
|
|
|
|
184
|
|
|
protected function handle(bool $ajax = false) { |
|
185
|
|
|
|
|
186
|
|
|
if ($ajax) return $this->handleAjax(); |
|
187
|
|
|
|
|
188
|
|
|
# Create parent entity |
|
189
|
|
|
|
|
190
|
|
|
$parent_id = (static::$nesting ? Number::forceInt(Request::get('parent_id')) : 0); |
|
191
|
|
|
|
|
192
|
|
|
$this->parent = Entitizer::get(static::$table, $parent_id); |
|
193
|
|
|
|
|
194
|
|
|
# Get path |
|
195
|
|
|
|
|
196
|
|
|
if (false !== ($path = $this->parent->getPath())) $this->path = $path; |
|
197
|
|
|
|
|
198
|
|
|
# Get index |
|
199
|
|
|
|
|
200
|
|
|
$this->index = Number::forceInt(Request::get('index'), 1, 999999); |
|
201
|
|
|
|
|
202
|
|
|
# Get items list |
|
203
|
|
|
|
|
204
|
|
|
$lister = (static::$nesting ? 'getChildren' : 'getItems'); $display = Settings::get('admin_display_entities'); |
|
205
|
|
|
|
|
206
|
|
|
if (false !== ($items = $this->parent->$lister([], [], $this->index, $display))) $this->items = $items; |
|
207
|
|
|
|
|
208
|
|
|
# ------------------------ |
|
209
|
|
|
|
|
210
|
|
|
return $this->getContents(); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
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@propertyannotation 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.