1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Entitizer\Utils { |
4
|
|
|
|
5
|
|
|
use Modules\Entitizer, DB; |
6
|
|
|
|
7
|
|
|
abstract class Entity extends Entitizer { |
8
|
|
|
|
9
|
|
|
use Entity\Action\Init, Entity\Action\Create, Entity\Action\Fill, Entity\Action\Edit, Entity\Action\Remove; |
10
|
|
|
|
11
|
|
|
protected $definition = null, $error = false, $id = 0, $data = []; |
12
|
|
|
|
13
|
|
|
# Init parent entities |
14
|
|
|
|
15
|
|
|
private function getPath() { |
|
|
|
|
16
|
|
|
|
17
|
|
|
$entity = $this; $path = [array_merge(['id' => $this->id], $this->data)]; |
18
|
|
|
|
19
|
|
|
while (0 !== $entity->data['parent_id']) { |
20
|
|
|
|
21
|
|
|
$entity = self::get(static::$type, $entity->data['parent_id']); |
22
|
|
|
|
23
|
|
|
if (0 === $entity->id) return []; |
24
|
|
|
|
25
|
|
|
$path[] = array_merge(['id' => $entity->id], $entity->data); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
# ------------------------ |
29
|
|
|
|
30
|
|
|
return array_reverse($path); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
# Get dataset |
34
|
|
|
|
35
|
|
|
private function getDataset(array $data, bool $initial = false) { |
|
|
|
|
36
|
|
|
|
37
|
|
|
$set = []; |
38
|
|
|
|
39
|
|
|
if ($initial && isset($data['id'])) { |
40
|
|
|
|
41
|
|
|
$set['id'] = $this->definition->id()->cast($data['id']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($this->definition->params() as $name => $param) { |
45
|
|
|
|
46
|
|
|
if (isset($data[$name])) $set[$name] = $param->cast($data[$name]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
# ------------------------ |
50
|
|
|
|
51
|
|
|
return $set; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
# Constructor |
55
|
|
|
|
56
|
|
|
public function __construct(int $id = 0) { |
57
|
|
|
|
58
|
|
|
# Create definition object |
59
|
|
|
|
60
|
|
|
$this->definition = Entitizer\Definition::get(static::$type); |
61
|
|
|
|
62
|
|
|
# Preset data array |
63
|
|
|
|
64
|
|
|
foreach ($this->definition->params() as $name => $param) $this->data[$name] = $param->cast(null); |
65
|
|
|
|
66
|
|
|
# Preset path |
67
|
|
|
|
68
|
|
|
if (static::$nesting) $this->data['path'] = []; |
69
|
|
|
|
70
|
|
|
# Implement entity |
71
|
|
|
|
72
|
|
|
$this->implement(); |
73
|
|
|
|
74
|
|
|
# Init entity |
75
|
|
|
|
76
|
|
|
if ($id > 0) $this->init($id); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
# Check if unique param value exists |
80
|
|
|
|
81
|
|
|
public function check(string $name, $value) { |
82
|
|
|
|
83
|
|
View Code Duplication |
if ((false === ($param = $this->definition->get($name))) || !$param->unique()) return false; |
|
|
|
|
84
|
|
|
|
85
|
|
|
# Select entity from DB |
86
|
|
|
|
87
|
|
|
$name = $param->name(); $value = $param->cast($value); |
88
|
|
|
|
89
|
|
|
$condition = ($name . " = '" . addslashes($value) . "' AND id != " . $this->id); |
90
|
|
|
|
91
|
|
|
DB::select(static::$table, 'id', $condition, null, 1); |
92
|
|
|
|
93
|
|
|
# ------------------------ |
94
|
|
|
|
95
|
|
|
return ((DB::last() && DB::last()->status) ? DB::last()->rows : false); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
# Count children |
99
|
|
|
|
100
|
|
|
public function children() { |
101
|
|
|
|
102
|
|
|
DB::select(static::$table, 'COUNT(id) as count', ['parent_id' => $this->id]); |
103
|
|
|
|
104
|
|
|
if (!(DB::last() && DB::last()->status)) return false; |
105
|
|
|
|
106
|
|
|
# ------------------------ |
107
|
|
|
|
108
|
|
|
return intval(DB::last()->row()['count']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
# Check for error |
112
|
|
|
|
113
|
|
|
public function error() { |
114
|
|
|
|
115
|
|
|
return $this->error; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
# Return id |
119
|
|
|
|
120
|
|
|
public function id() { |
121
|
|
|
|
122
|
|
|
return $this->id; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
# Return data |
126
|
|
|
|
127
|
|
|
public function data() { |
128
|
|
|
|
129
|
|
|
return $this->data; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
# Getter |
133
|
|
|
|
134
|
|
|
public function __get(string $name) { |
135
|
|
|
|
136
|
|
|
if ($name === 'id') return $this->id; |
137
|
|
|
|
138
|
|
|
return (isset($this->data[$name]) ? $this->data[$name] : null); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|