1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Entitizer\Entity { |
4
|
|
|
|
5
|
|
|
use Modules\Auth, Modules\Entitizer, Modules\Settings, DB; |
6
|
|
|
|
7
|
|
|
class Page extends Entitizer\Utils\Entity { |
8
|
|
|
|
9
|
|
|
use Entitizer\Common\Page; |
10
|
|
|
|
11
|
|
|
# Check if active |
12
|
|
|
|
13
|
|
|
private function getActive() { |
14
|
|
|
|
15
|
|
|
return (($this->visibility === VISIBILITY_PUBLISHED) && !$this->locked); |
|
|
|
|
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
# Get link |
19
|
|
|
|
20
|
|
|
private function getLink() { |
21
|
|
|
|
22
|
|
|
if ('' === $this->data['slug']) return ''; |
23
|
|
|
|
24
|
|
|
return (INSTALL_PATH . '/' . $this->data['slug']); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
# Get canonical |
28
|
|
|
|
29
|
|
|
private function getCanonical() { |
30
|
|
|
|
31
|
|
|
if ('' === $this->data['slug']) return ''; |
32
|
|
|
|
33
|
|
|
return (Settings::get('system_url') . (($this->id !== 1) ? ('/' . $this->data['slug']) : '')); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
# Implement entity |
37
|
|
|
|
38
|
|
|
protected function implement() { |
39
|
|
|
|
40
|
|
|
$this->data['active'] = $this->getActive(); |
41
|
|
|
|
42
|
|
|
$this->data['link'] = $this->getLink(); |
43
|
|
|
|
44
|
|
|
$this->data['canonical'] = $this->getCanonical(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
# Init by slug |
48
|
|
|
|
49
|
|
|
public function initBySlug(string $slug) { |
50
|
|
|
|
51
|
|
|
if (!$this->modifiable || (0 !== $this->id)) return false; |
|
|
|
|
52
|
|
|
|
53
|
|
|
# Process value |
54
|
|
|
|
55
|
|
|
$slug = $this->definition->param('slug')->cast($slug); |
56
|
|
|
|
57
|
|
|
# Process selection |
58
|
|
|
|
59
|
|
|
$selection = array_keys($this->definition->params()); |
60
|
|
|
|
61
|
|
|
foreach ($selection as $key => $field) $selection[$key] = ('ent.' . $field); |
62
|
|
|
|
63
|
|
|
# Process query |
64
|
|
|
|
65
|
|
|
$query = ("SELECT " . implode(', ', $selection) .", rel.ancestor as parent_id ") . |
66
|
|
|
|
67
|
|
|
("FROM " . static::$table . " ent ") . |
68
|
|
|
|
69
|
|
|
("LEFT JOIN " . static::$table_relations . " rel ON rel.descendant = ent.id AND rel.depth = 1 ") . |
70
|
|
|
|
71
|
|
|
("WHERE ent.visibility = " . VISIBILITY_PUBLISHED . " AND ent.access <= " . Auth::user()->rank . " AND ") . |
72
|
|
|
|
73
|
|
|
("ent.locked = 0 AND ent.slug = '" . addslashes($slug) . "' LIMIT 1"); |
74
|
|
|
|
75
|
|
|
# Select entity from DB |
76
|
|
|
|
77
|
|
View Code Duplication |
if (($this->error = !(DB::send($query) && DB::last()->status)) || (DB::last()->rows !== 1)) return false; |
|
|
|
|
78
|
|
|
|
79
|
|
|
# ------------------------ |
80
|
|
|
|
81
|
|
|
return $this->setData(DB::last()->row()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
# Update slugs |
85
|
|
|
|
86
|
|
|
public function updateSlugs() { |
87
|
|
|
|
88
|
|
|
if (!$this->modifiable || (0 === $this->id)) return false; |
|
|
|
|
89
|
|
|
|
90
|
|
|
# Send lock/update request |
91
|
|
|
|
92
|
|
|
$query = ("UPDATE " . static::$table . " ent JOIN (") . |
93
|
|
|
|
94
|
|
|
("SELECT rel.descendant as id, GROUP_CONCAT(ens.name ORDER BY rls.depth DESC SEPARATOR '/') slug ") . |
95
|
|
|
|
96
|
|
|
("FROM " . static::$table_relations . " rel ") . |
97
|
|
|
|
98
|
|
|
("JOIN " . static::$table_relations . " rls ON rls.descendant = rel.descendant ") . |
99
|
|
|
|
100
|
|
|
("JOIN " . static::$table . " ens ON ens.id = rls.ancestor ") . |
101
|
|
|
|
102
|
|
|
("WHERE rel.ancestor = " . $this->id . " GROUP BY rel.descendant") . |
|
|
|
|
103
|
|
|
|
104
|
|
|
(") slg ON slg.id = ent.id SET ent.locked = 1, ent.slug = slg.slug"); |
105
|
|
|
|
106
|
|
|
if (!(DB::send($query) && DB::last()->status)) return false; |
107
|
|
|
|
108
|
|
|
# Send unlock request |
109
|
|
|
|
110
|
|
|
$query = ("UPDATE " . static::$table . " ent JOIN (") . |
111
|
|
|
|
112
|
|
|
("SELECT rel.descendant as id FROM " . static::$table_relations . " rel ") . |
113
|
|
|
|
114
|
|
|
("JOIN " . static::$table . " enc ON enc.id = rel.descendant AND enc.locked = 1 ") . |
115
|
|
|
|
116
|
|
|
("LEFT JOIN " . static::$table . " end ON end.id != enc.id AND end.slug = enc.slug ") . |
117
|
|
|
|
118
|
|
|
("WHERE end.id IS NULL GROUP BY rel.descendant") . |
119
|
|
|
|
120
|
|
|
(") chk ON chk.id = ent.id SET ent.locked = 0"); |
121
|
|
|
|
122
|
|
|
if (!(DB::send($query) && DB::last()->status)) return false; |
123
|
|
|
|
124
|
|
|
# ------------------------ |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
# Create page entry in DB |
130
|
|
|
|
131
|
|
|
public function create(array $data) { |
132
|
|
|
|
133
|
|
|
if (!parent::create($data)) return false; |
134
|
|
|
|
135
|
|
|
$this->updateSlugs(); |
136
|
|
|
|
137
|
|
|
# ------------------------ |
138
|
|
|
|
139
|
|
|
return true; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
# Edit page entry in DB |
143
|
|
|
|
144
|
|
|
public function edit(array $data) { |
145
|
|
|
|
146
|
|
|
if (!parent::edit($data)) return false; |
147
|
|
|
|
148
|
|
|
$this->updateSlugs(); |
149
|
|
|
|
150
|
|
|
# ------------------------ |
151
|
|
|
|
152
|
|
|
return true; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
# Move page to new parent |
156
|
|
|
|
157
|
|
|
public function move(int $parent_id) { |
158
|
|
|
|
159
|
|
|
if (!parent::move($parent_id)) return false; |
160
|
|
|
|
161
|
|
|
$this->updateSlugs(); |
162
|
|
|
|
163
|
|
|
# ------------------------ |
164
|
|
|
|
165
|
|
|
return true; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
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.