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