|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2011 - 2015 Oleksandr Torosh (http://yonastudio.com) |
|
4
|
|
|
* @author Oleksandr Torosh <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Menu\Model\Menu; |
|
7
|
|
|
|
|
8
|
|
|
use Application\Mvc\Model\Model; |
|
9
|
|
|
use Phalcon\Mvc\Model\Validator\Uniqueness; |
|
10
|
|
|
|
|
11
|
|
|
class Menu extends Model |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
public function getSource() |
|
15
|
|
|
{ |
|
16
|
|
|
return "menu"; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
protected $translateModel = 'Menu\Model\Translate\MenuTranslate'; // translate |
|
20
|
|
|
|
|
21
|
|
|
private $id; |
|
22
|
|
|
private $root = 'top'; |
|
23
|
|
|
private $parent_id; |
|
24
|
|
|
private $work_title; |
|
25
|
|
|
private $depth = 0; |
|
26
|
|
|
private $left_key; |
|
27
|
|
|
private $right_key; |
|
28
|
|
|
private $created_at; |
|
29
|
|
|
private $updated_at; |
|
30
|
|
|
public $title; // translate |
|
31
|
|
|
|
|
32
|
|
|
public static $roots = [ |
|
33
|
|
|
'top' => 'Top Menu', |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
public function initialize() |
|
37
|
|
|
{ |
|
38
|
|
|
$this->belongsTo('parent_id', 'Category\Model\Category', 'id', ['alias' => 'Parent']); |
|
39
|
|
|
$this->hasMany("id", $this->translateModel, "foreign_id"); // translate |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function validation() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->validate(new Uniqueness( |
|
|
|
|
|
|
45
|
|
|
[ |
|
46
|
|
|
"field" => "slug", |
|
47
|
|
|
"message" => "Category with slug '" . $this->slug . "' is already exists. Take another title" |
|
|
|
|
|
|
48
|
|
|
] |
|
49
|
|
|
)); |
|
50
|
|
|
|
|
51
|
|
|
return $this->validationHasFailed() != true; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function beforeCreate() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->created_at = date("Y-m-d H:i:s"); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function beforeUpdate() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->updated_at = date("Y-m-d H:i:s"); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public static function treeUpperLeafs($root) |
|
65
|
|
|
{ |
|
66
|
|
|
$entries = Menu::find([ |
|
|
|
|
|
|
67
|
|
|
'root = :root: AND parent_id IS NULL', |
|
68
|
|
|
'order' => 'left_key', |
|
69
|
|
|
'bind' => ['root' => $root] |
|
70
|
|
|
]); |
|
71
|
|
|
return $entries; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
View Code Duplication |
public function children() |
|
75
|
|
|
{ |
|
76
|
|
|
$entries = $this->find([ |
|
|
|
|
|
|
77
|
|
|
'left_key >= :left_key: AND right_key <= :right_key: AND depth = :depth_plus: AND id <> :id: AND root = :root:', |
|
78
|
|
|
'order' => 'left_key ASC', |
|
79
|
|
|
'bind' => [ |
|
80
|
|
|
'id' => $this->getId(), |
|
81
|
|
|
'root' => $this->getRoot(), |
|
82
|
|
|
'depth_plus' => $this->getDepth() + 1, |
|
83
|
|
|
'left_key' => $this->getLeftKey(), |
|
84
|
|
|
'right_key' => $this->getRightKey(), |
|
85
|
|
|
] |
|
86
|
|
|
]); |
|
87
|
|
|
return $entries; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function hasChildren() |
|
91
|
|
|
{ |
|
92
|
|
|
if (abs($this->getRightKey() - $this->getLeftKey()) > 1) { |
|
93
|
|
|
return true; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getId() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->id; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param mixed $id |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setId($id) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->id = $id; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getRoot() |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->root; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param string $root |
|
123
|
|
|
*/ |
|
124
|
|
|
public function setRoot($root) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->root = $root; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return mixed |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getParentId() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->parent_id; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param mixed $parent_id |
|
139
|
|
|
*/ |
|
140
|
|
|
public function setParentId($parent_id) |
|
141
|
|
|
{ |
|
142
|
|
|
$this->parent_id = $parent_id; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return mixed |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getDepth() |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->depth; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @param mixed $depth |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setDepth($depth) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->depth = $depth; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return mixed |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getLeftKey() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->left_key; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param mixed $left_key |
|
171
|
|
|
*/ |
|
172
|
|
|
public function setLeftKey($left_key) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->left_key = $left_key; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @return mixed |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getRightKey() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->right_key; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param mixed $right_key |
|
187
|
|
|
*/ |
|
188
|
|
|
public function setRightKey($right_key) |
|
189
|
|
|
{ |
|
190
|
|
|
$this->right_key = $right_key; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return mixed |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getTitle() |
|
197
|
|
|
{ |
|
198
|
|
|
return $this->getMLVariable('title'); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param mixed $title |
|
203
|
|
|
*/ |
|
204
|
|
|
public function setTitle($title) |
|
205
|
|
|
{ |
|
206
|
|
|
$this->setMLVariable('title', $title); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return mixed |
|
211
|
|
|
*/ |
|
212
|
|
|
public function getCreatedAt() |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->created_at; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param mixed $created_at |
|
219
|
|
|
*/ |
|
220
|
|
|
public function setCreatedAt($created_at) |
|
221
|
|
|
{ |
|
222
|
|
|
$this->created_at = $created_at; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* @return mixed |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getUpdatedAt() |
|
229
|
|
|
{ |
|
230
|
|
|
return $this->updated_at; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @param mixed $updated_at |
|
235
|
|
|
*/ |
|
236
|
|
|
public function setUpdatedAt($updated_at) |
|
237
|
|
|
{ |
|
238
|
|
|
$this->updated_at = $updated_at; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @return mixed |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getWorkTitle() |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->work_title; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param mixed $work_title |
|
251
|
|
|
*/ |
|
252
|
|
|
public function setWorkTitle($work_title) |
|
253
|
|
|
{ |
|
254
|
|
|
$this->work_title = $work_title; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: