1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Entitizer\Utils\Entity { |
4
|
|
|
|
5
|
|
|
use Modules\Entitizer, DB; |
6
|
|
|
|
7
|
|
|
trait Modify { |
8
|
|
|
|
9
|
|
|
protected $definition = null, $error = false, $modifiable = false, $data = []; |
10
|
|
|
|
11
|
|
|
# Init entity subtree connection |
12
|
|
|
|
13
|
|
|
private function initSubtree() { |
14
|
|
|
|
15
|
|
|
$dataset = ['ancestor' => $this->id, 'descendant' => $this->id, 'depth' => 0]; |
|
|
|
|
16
|
|
|
|
17
|
|
|
DB::insert(static::$table_relations, $dataset, false, true); |
18
|
|
|
|
19
|
|
|
# ------------------------ |
20
|
|
|
|
21
|
|
|
return (DB::last() && DB::last()->status); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
# Disconnect subtree from current position |
25
|
|
|
|
26
|
|
View Code Duplication |
private function disconnectSubtree() { |
|
|
|
|
27
|
|
|
|
28
|
|
|
$query = ("DELETE rla FROM " . static::$table_relations . " rla ") . |
29
|
|
|
|
30
|
|
|
("JOIN " . static::$table_relations . " rlb ON rlb.descendant = rla.descendant ") . |
31
|
|
|
|
32
|
|
|
("LEFT JOIN " . static::$table_relations . " rlx ") . |
33
|
|
|
|
34
|
|
|
("ON rlx.ancestor = rlb.ancestor AND rlx.descendant = rla.ancestor ") . |
35
|
|
|
|
36
|
|
|
("WHERE rlb.ancestor = " . $this->id . " AND rlx.ancestor IS NULL"); |
37
|
|
|
|
38
|
|
|
if (!(DB::send($query) && DB::last()->status)) return false; |
39
|
|
|
|
40
|
|
|
# Set path |
41
|
|
|
|
42
|
|
|
$this->data['parent_id'] = 0; |
43
|
|
|
|
44
|
|
|
# ------------------------ |
45
|
|
|
|
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
# Connect subtree under new position |
50
|
|
|
|
51
|
|
View Code Duplication |
private function connectSubtree(int $parent_id) { |
|
|
|
|
52
|
|
|
|
53
|
|
|
$query = ("INSERT INTO " . static::$table_relations . " (ancestor, descendant, depth) ") . |
54
|
|
|
|
55
|
|
|
("SELECT sup.ancestor, sub.descendant, sup.depth + sub.depth + 1 ") . |
56
|
|
|
|
57
|
|
|
("FROM " . static::$table_relations . " sup ") . |
58
|
|
|
|
59
|
|
|
("JOIN " . static::$table_relations . " sub ") . |
60
|
|
|
|
61
|
|
|
("WHERE sub.ancestor = " . $this->id . " AND sup.descendant = " . $parent_id); |
62
|
|
|
|
63
|
|
|
if (!(DB::send($query) && DB::last()->status)) return false; |
64
|
|
|
|
65
|
|
|
# Set path |
66
|
|
|
|
67
|
|
|
$this->data['parent_id'] = $parent_id; |
68
|
|
|
|
69
|
|
|
# ------------------------ |
70
|
|
|
|
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
# Create entity entry in DB |
75
|
|
|
|
76
|
|
|
public function create(array $data) { |
77
|
|
|
|
78
|
|
|
if (!$this->modifiable || (0 !== $this->id)) return false; |
79
|
|
|
|
80
|
|
|
$data = $this->definition->cast($data); |
81
|
|
|
|
82
|
|
|
if (static::$auto_increment && isset($data['id'])) unset($data['id']); |
83
|
|
|
|
84
|
|
|
# Insert entity |
85
|
|
|
|
86
|
|
|
DB::insert(static::$table, $data); |
87
|
|
|
|
88
|
|
|
if (!(DB::last() && DB::last()->status)) return false; |
89
|
|
|
|
90
|
|
|
# Set data |
91
|
|
|
|
92
|
|
|
$this->error = false; |
93
|
|
|
|
94
|
|
|
if (static::$auto_increment) $this->data['id'] = DB::last()->id; |
95
|
|
|
|
96
|
|
|
foreach ($data as $name => $value) $this->data[$name] = $value; |
97
|
|
|
|
98
|
|
|
# Implement entity |
99
|
|
|
|
100
|
|
|
$this->implement(); |
|
|
|
|
101
|
|
|
|
102
|
|
|
# Init subtreee |
103
|
|
|
|
104
|
|
|
if (static::$nesting) $this->initSubtree(); |
105
|
|
|
|
106
|
|
|
# Cache entity |
107
|
|
|
|
108
|
|
|
self::$cache[static::$table][$this->id] = $this; |
109
|
|
|
|
110
|
|
|
# ------------------------ |
111
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
# Edit entity entry in DB |
116
|
|
|
|
117
|
|
|
public function edit(array $data) { |
118
|
|
|
|
119
|
|
|
if (!$this->modifiable || (0 === $this->id)) return false; |
120
|
|
|
|
121
|
|
|
$data = $this->definition->cast($data); |
122
|
|
|
|
123
|
|
|
if (isset($data['id'])) unset($data['id']); |
124
|
|
|
|
125
|
|
|
# Update entity |
126
|
|
|
|
127
|
|
|
DB::update(static::$table, $data, ['id' => $this->id]); |
128
|
|
|
|
129
|
|
|
if (!(DB::last() && DB::last()->status)) return false; |
130
|
|
|
|
131
|
|
|
# Set data |
132
|
|
|
|
133
|
|
|
foreach ($data as $name => $value) $this->data[$name] = $value; |
134
|
|
|
|
135
|
|
|
# Implement entity |
136
|
|
|
|
137
|
|
|
$this->implement(); |
|
|
|
|
138
|
|
|
|
139
|
|
|
# Init subtreee |
140
|
|
|
|
141
|
|
|
if (static::$nesting) $this->initSubtree(); |
142
|
|
|
|
143
|
|
|
# ------------------------ |
144
|
|
|
|
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
# Move entity to new parent |
149
|
|
|
|
150
|
|
|
public function move(int $parent_id) { |
151
|
|
|
|
152
|
|
|
if (!$this->modifiable || (0 === $this->id)) return false; |
153
|
|
|
|
154
|
|
|
# Re-connect entity if not in tree |
155
|
|
|
|
156
|
|
|
if (!$this->initSubtree()) return false; |
157
|
|
|
|
158
|
|
|
# Create parent entity |
159
|
|
|
|
160
|
|
|
if (0 !== ($parent = Entitizer::get(static::$table, $parent_id))->id) { |
161
|
|
|
|
162
|
|
|
if (false === ($path = $parent->path())) return false; |
163
|
|
|
|
164
|
|
|
if (false === ($depth = $this->subtreeDepth())) return false; |
|
|
|
|
165
|
|
|
|
166
|
|
|
if ((count($path) + $depth + 1) > CONFIG_ENTITIZER_MAX_DEPTH) return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
# Disconnect subtree from current position |
170
|
|
|
|
171
|
|
|
if (!$this->disconnectSubtree()) return false; |
172
|
|
|
|
173
|
|
|
# Connect subtree under new position |
174
|
|
|
|
175
|
|
|
if (0 !== $parent->id) $this->connectSubtree($parent->id); |
176
|
|
|
|
177
|
|
|
# ------------------------ |
178
|
|
|
|
179
|
|
|
return true; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: