1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Entitizer\Utils { |
4
|
|
|
|
5
|
|
|
use Modules\Entitizer, Utils\Pagination, Utils\View, Ajax, Number, Request, Template, Url; |
6
|
|
|
|
7
|
|
|
abstract class Listview { |
8
|
|
|
|
9
|
|
|
protected $index = 0, $parent = null, $items = []; |
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')->id = $this->parent->id; |
22
|
|
|
|
23
|
|
|
# Set edit button |
24
|
|
|
|
25
|
|
|
if (0 === $this->parent->id) $parent->block('edit')->disable(); |
26
|
|
|
|
27
|
|
|
else $parent->block('edit')->id = $this->parent->id; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
# Get items block |
31
|
|
|
|
32
|
|
|
private function getItemsBlock(bool $ajax = false) { |
33
|
|
|
|
34
|
|
|
$items = Template::group(); |
35
|
|
|
|
36
|
|
|
foreach ($this->items['list'] as $item) { |
37
|
|
|
|
38
|
|
|
$items->add($view = View::get($ajax ? static::$view_ajax_item : static::$view_item)); |
39
|
|
|
|
40
|
|
|
# Set data |
41
|
|
|
|
42
|
|
|
$view->id = $item['entity']->id; |
43
|
|
|
|
44
|
|
|
$view->set(static::$naming, $item['entity']->__get(static::$naming)); |
45
|
|
|
|
46
|
|
|
# Set remove button |
47
|
|
|
|
48
|
|
|
$super = (static::$super && ($item['entity']->id === 1)); |
49
|
|
|
|
50
|
|
|
$has_children = (static::$nesting && ($item['children'] > 0)); |
51
|
|
|
|
52
|
|
|
$view->block('remove')->class = ((!$super && !$has_children) ? 'negative' : 'disabled'); |
53
|
|
|
|
54
|
|
|
# Add item additional data |
55
|
|
|
|
56
|
|
|
$this->processItem($view, $item['entity'], (static::$nesting ? $item['children'] : 0)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
# ------------------------ |
60
|
|
|
|
61
|
|
|
return $items; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
# Get pagination block |
65
|
|
|
|
66
|
|
|
private function getPaginationBlock() { |
67
|
|
|
|
68
|
|
|
$query = (static::$nesting ? ('?parent_id=' . $this->parent->id) : ''); |
69
|
|
|
|
70
|
|
|
$url = new Url(INSTALL_PATH . static::$link . $query); |
71
|
|
|
|
72
|
|
|
# ------------------------ |
73
|
|
|
|
74
|
|
|
return Pagination::block($this->index, static::$display, $this->items['total'], $url); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
# Get contents |
78
|
|
|
|
79
|
|
|
private function getContents(bool $ajax = false) { |
80
|
|
|
|
81
|
|
|
$contents = View::get($ajax ? static::$view_ajax_main : static::$view_main); |
82
|
|
|
|
83
|
|
|
# Set path |
84
|
|
|
|
85
|
|
|
if (static::$nesting) $contents->path = $this->parent->path; |
86
|
|
|
|
87
|
|
|
# Process parent block |
88
|
|
|
|
89
|
|
|
if (static::$nesting && !$ajax) $this->processParent($contents->block('parent')); |
90
|
|
|
|
91
|
|
|
# Set items |
92
|
|
|
|
93
|
|
|
$items = $this->getItemsBlock($ajax); |
94
|
|
|
|
95
|
|
|
if ($items->count() > 0) $contents->items = $items; |
96
|
|
|
|
97
|
|
|
# Set pagination |
98
|
|
|
|
99
|
|
|
if (!$ajax) $contents->pagination = $this->getPaginationBlock(); |
100
|
|
|
|
101
|
|
|
# Add additional data for specific entity |
102
|
|
|
|
103
|
|
|
$this->processEntity($contents); |
104
|
|
|
|
105
|
|
|
# ------------------------ |
106
|
|
|
|
107
|
|
|
return $contents; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
# Handle ajax request |
111
|
|
|
|
112
|
|
|
private function handleAjax() { |
113
|
|
|
|
114
|
|
|
$ajax = Ajax::response(); |
115
|
|
|
|
116
|
|
|
# Create parent entity |
117
|
|
|
|
118
|
|
|
if (static::$nesting) $this->parent = Entitizer::get(static::$type, Number::format(Request::get('parent_id'))); |
119
|
|
|
|
120
|
|
|
# Get children items |
121
|
|
|
|
122
|
|
|
$lister = new static::$lister(); $parent_id = (static::$nesting ? $this->parent->id : 0); |
123
|
|
|
|
124
|
|
|
$this->items = $lister->select(0, 0, $parent_id, Number::format(Request::post('id'))); |
125
|
|
|
|
126
|
|
|
# ------------------------ |
127
|
|
|
|
128
|
|
|
return $ajax->set('contents', $this->getContents(true)->contents(true)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
# Handle common request |
132
|
|
|
|
133
|
|
|
public function handle() { |
134
|
|
|
|
135
|
|
|
if (Request::isAjax()) return $this->handleAjax(); |
136
|
|
|
|
137
|
|
|
$this->index = Number::format(Request::get('index'), 1, 999999); |
|
|
|
|
138
|
|
|
|
139
|
|
|
# Create parent entity |
140
|
|
|
|
141
|
|
|
if (static::$nesting) $this->parent = Entitizer::get(static::$type, Number::format(Request::get('parent_id'))); |
142
|
|
|
|
143
|
|
|
# Get children items |
144
|
|
|
|
145
|
|
|
$lister = new static::$lister(); $parent_id = (static::$nesting ? $this->parent->id : 0); |
146
|
|
|
|
147
|
|
|
$this->items = $lister->select($this->index, static::$display, $parent_id); |
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.