1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Entitizer\Entity { |
4
|
|
|
|
5
|
|
|
use Modules\Auth, Modules\Entitizer, DB; |
6
|
|
|
|
7
|
|
|
class Page extends Entitizer\Utils\Entity { |
8
|
|
|
|
9
|
|
|
use Entitizer\Common\Page; |
10
|
|
|
|
11
|
|
|
# Init by slug |
12
|
|
|
|
13
|
|
|
public function initBySlug(string $slug) { |
14
|
|
|
|
15
|
|
|
if (0 !== $this->id) return false; |
|
|
|
|
16
|
|
|
|
17
|
|
|
# Process value |
18
|
|
|
|
19
|
|
|
$slug = $this->definition->param('slug')->cast($slug); |
20
|
|
|
|
21
|
|
|
# Process selection |
22
|
|
|
|
23
|
|
|
$selection = array_keys($this->definition->params()); |
24
|
|
|
|
25
|
|
|
foreach ($selection as $key => $field) $selection[$key] = ('ent.' . $field); |
26
|
|
|
|
27
|
|
|
# Process query |
28
|
|
|
|
29
|
|
|
$query = ("SELECT " . implode(', ', $selection) .", rel.ancestor as parent_id ") . |
30
|
|
|
|
31
|
|
|
("FROM " . static::$table . " ent ") . |
32
|
|
|
|
33
|
|
|
("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") . |
34
|
|
|
|
35
|
|
|
("WHERE ent.visibility = " . VISIBILITY_PUBLISHED . " AND ent.access <= " . Auth::user()->rank . " AND ") . |
36
|
|
|
|
37
|
|
|
("ent.locked = 0 AND ent.slug = '" . addslashes($slug) . "' LIMIT 1"); |
38
|
|
|
|
39
|
|
|
# Select entity from DB |
40
|
|
|
|
41
|
|
|
if (!(DB::send($query) && (DB::last()->rows === 1))) return false; |
42
|
|
|
|
43
|
|
|
# ------------------------ |
44
|
|
|
|
45
|
|
|
return $this->setData(DB::last()->row()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
# Update slugs |
49
|
|
|
|
50
|
|
|
public function updateSlugs() { |
51
|
|
|
|
52
|
|
|
if (0 === $this->id) return false; |
|
|
|
|
53
|
|
|
|
54
|
|
|
# Send lock/update request |
55
|
|
|
|
56
|
|
|
$query = ("UPDATE " . static::$table . " ent JOIN (") . |
57
|
|
|
|
58
|
|
|
("SELECT rel.descendant as id, GROUP_CONCAT(ens.name ORDER BY rls.depth DESC SEPARATOR '/') slug ") . |
59
|
|
|
|
60
|
|
|
("FROM " . static::$table_relations . " rel ") . |
61
|
|
|
|
62
|
|
|
("JOIN " . static::$table_relations . " rls ON rls.descendant = rel.descendant ") . |
63
|
|
|
|
64
|
|
|
("JOIN " . static::$table . " ens ON ens.id = rls.ancestor ") . |
65
|
|
|
|
66
|
|
|
("WHERE rel.ancestor = " . $this->id . " GROUP BY rel.descendant") . |
|
|
|
|
67
|
|
|
|
68
|
|
|
(") slg ON slg.id = ent.id SET ent.locked = 1, ent.slug = slg.slug"); |
69
|
|
|
|
70
|
|
|
if (!(DB::send($query) && DB::last()->status)) return false; |
71
|
|
|
|
72
|
|
|
# Send unlock request |
73
|
|
|
|
74
|
|
|
$query = ("UPDATE " . static::$table . " ent JOIN (") . |
75
|
|
|
|
76
|
|
|
("SELECT rel.descendant as id FROM " . static::$table_relations . " rel ") . |
77
|
|
|
|
78
|
|
|
("JOIN " . static::$table . " enc ON enc.id = rel.descendant AND enc.locked = 1 ") . |
79
|
|
|
|
80
|
|
|
("LEFT JOIN " . static::$table . " end ON end.id != enc.id AND end.slug = enc.slug ") . |
81
|
|
|
|
82
|
|
|
("WHERE end.id IS NULL GROUP BY rel.descendant") . |
83
|
|
|
|
84
|
|
|
(") chk ON chk.id = ent.id SET ent.locked = 0"); |
85
|
|
|
|
86
|
|
|
if (!(DB::send($query) && DB::last()->status)) return false; |
87
|
|
|
|
88
|
|
|
# ------------------------ |
89
|
|
|
|
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
# Create page entry in DB |
94
|
|
|
|
95
|
|
|
public function create(array $data) { |
96
|
|
|
|
97
|
|
|
if (!parent::create($data)) return false; |
98
|
|
|
|
99
|
|
|
$this->updateSlugs(); |
100
|
|
|
|
101
|
|
|
# ------------------------ |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
# Edit page entry in DB |
107
|
|
|
|
108
|
|
|
public function edit(array $data) { |
109
|
|
|
|
110
|
|
|
if (!parent::edit($data)) return false; |
111
|
|
|
|
112
|
|
|
$this->updateSlugs(); |
113
|
|
|
|
114
|
|
|
# ------------------------ |
115
|
|
|
|
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
# Move page to new parent |
120
|
|
|
|
121
|
|
|
public function move(int $parent_id) { |
122
|
|
|
|
123
|
|
|
if (!parent::move($parent_id)) return false; |
124
|
|
|
|
125
|
|
|
$this->updateSlugs(); |
126
|
|
|
|
127
|
|
|
# ------------------------ |
128
|
|
|
|
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read 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.