forked from yiiext/adjacency-list-behavior
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAdjacencyListBehavior.php
317 lines (277 loc) · 7.73 KB
/
AdjacencyListBehavior.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* AdjacencyListBehavior
*
* @author Alexander Kochetov <[email protected]>
* @link https://github.com/yiiext/adjacency-list-behavior
* @author Jon Langevin <[email protected]>
* @link https://github.com/intel352/adjacency-list-behavior
*/
/**
* Provides adjacency list functionality for a model.
*
* @version 0.30
* @package yiiext.behaviors.model.trees.al
*/
class AdjacencyListBehavior extends CActiveRecordBehavior
{
const PARENT='parent';
const CHILDREN='children';
const BOTH='both';
public $hasLevel=false;
public $hasWeight=false;
public $parentAttribute='parent_id';
public $levelAttribute='level';
public $weightAttribute='weight';
private $_ignoreEvent=false;
private $_deleted=false;
private $_id;
/**
* Named scope. Gets ancestors for node.
* @param int depth
* @return CActiveRecord the owner
*/
public function ancestors($depth=null)
{
return $this->genealogy(self::PARENT, $depth);
}
/**
* Named scope. Gets descendants for node.
* @param int depth
* @return CActiveRecord the owner
*/
public function descendants($depth=null)
{
return $this->genealogy(self::CHILDREN, $depth);
}
/**
* Named scope. Gets genealogy (family tree) for node.
* @param int depth
* @return CActiveRecord the owner
*/
public function genealogy($type=self::BOTH, $depth=null)
{
/**
* @todo check hasLevel if $depth===null
*/
if($type==self::BOTH)
$type=array(self::PARENT,self::CHILDREN);
else
$type=array($type);
$owner=$this->getOwner();
$db=$owner->getDbConnection();
$criteria=$owner->getDbCriteria();
$alias=$owner->getTableAlias(true);
foreach($type AS $t)
{
if($owner->primaryKey==null) {
$depth=1;
}else{
switch($t) {
case self::PARENT:
if($owner->{$this->parentAttribute}==null)
continue;
$criteria->addColumnCondition(array($alias.'.'.$db->quoteColumnName($this->owner->getTableSchema()->getTable()->primaryKey)=>$owner->{$this->parentAttribute}));
break;
case self::CHILDREN:
$criteria->addColumnCondition(array($alias.'.'.$db->quoteColumnName($this->parentAttribute)=>$owner->primaryKey));
break;
}
}
if($depth>0)
$with=array($t);
if($depth>1)
{
for($i=1;$i<$depth-1;$i++)
{
$with=array(
$t=>array(
'with'=>$with,
'alias'=>$t.$i,
),
);
}
}
if(!empty($with))
$criteria->mergeWith(array('with'=>$with));
}
return $owner;
}
/**
* Named scope. Gets children for node (direct descendants only).
* @return CActiveRecord the owner
*/
public function withChildren()
{
return $this->descendants(1);
}
public function roots()
{
$owner=$this->getOwner();
$db=$owner->getDbConnection();
$criteria=$owner->getDbCriteria();
$alias=$owner->getTableAlias(true);
$criteria->addColumnCondition(array($alias.'.'.$db->quoteColumnName($this->parentAttribute)=>null));
return $owner;
}
/**
* Gets root node.
* @todo Is the original intention here to get the root of the current tree? If so, this logic fails
* @todo Using level, build query condition instead of looping records
* @return CActiveRecord the record found. Null if no record is found
*/
public function getRoot()
{
$owner=$this->getOwner();
#$db=$owner->getDbConnection();
#$owner->getDbCriteria()->addColumnCondition(array($owner->getTableAlias(true).'.'.$db->quoteColumnName($this->parentAttribute)=>null));
if(!$this->getParent())
return $owner;
else
return $this->getParent()->getRoot();
}
/**
* Gets record of node parent.
* @return CActiveRecord the record found. Null if no record is found
*/
public function getParent()
{
/**
* Return the record using already defined relationship, fall back to Yii AR handling
*/
return $this->getOwner()->{self::PARENT};
}
/**
* Gets record of node children.
* @return CActiveRecord the records found. Null if no record is found
*/
public function getChildren()
{
/**
* Return the record using already defined relationship, fall back to Yii AR handling
*/
return $this->getOwner()->{self::CHILDREN};
}
/**
* Gets record of previous sibling.
* @return CActiveRecord the record found. Null if no record is found
*/
public function getPrevSibling()
{
//only if level future is on
}
/**
* Gets record of next sibling.
* @return CActiveRecord the record found. Null if no record is found
*/
public function getNextSibling()
{
//only if level future is on
}
/**
* Update node if it's not new.
* @return boolean whether the saving succeeds
*/
public function save($runValidation=true,$attributes=null)
{
$owner=$this->getOwner();
if($runValidation && !$owner->validate($attributes))
return false;
if($owner->getIsNewRecord())
throw new CDbException(Yii::t('yiiext','Placeholder error'));
$this->_ignoreEvent=true;
$result=$owner->update($attributes);
$this->_ignoreEvent=false;
return $result;
}
public function saveNode($runValidation=true,$attributes=null)
{
return $this->save($runValidation,$attributes);
}
/**
* Deletes node and it's descendants.
* @return boolean whether the deletion is successful
*/
public function delete()
{
// cascad ?
}
public function deleteNode()
{
return $this->delete();
}
/**
* Appends node to target as last child.
* @return boolean whether the appending succeeds
*/
public function appendTo($target,$runValidation=true,$attributes=null)
{
}
/**
* Appends target to node as last child.
* @return boolean whether the appending succeeds
*/
public function append($target,$runValidation=true,$attributes=null)
{
return $target->appendTo($this->getOwner(),$runValidation,$attributes);
}
/**
* Move node as last child of target.
* @return boolean whether the moving succeeds
*/
public function moveAsLast($target)
{
// check hasWeight
}
/**
* Determines if node is descendant of subject node.
* @return boolean
*/
public function isDescendantOf($subj)
{
}
/**
* Determines if node is leaf.
* @return boolean
*/
public function isLeaf()
{
}
public function getIsDeletedRecord()
{
return $this->_deleted;
}
public function setIsDeletedRecord($value)
{
$this->_deleted=$value;
}
public function attach($owner) {
parent::attach($owner);
$owner=$this->getOwner();
/**
* Adding relation types
*/
$owner->getMetaData()->addRelation(self::PARENT,array(CActiveRecord::HAS_ONE,get_class($owner),$this->parentAttribute));
$owner->getMetaData()->addRelation(self::CHILDREN,array(CActiveRecord::HAS_MANY,get_class($owner),$this->parentAttribute));
}
public function beforeSave($event)
{
if($this->_ignoreEvent)
return true;
else
throw new CDbException(Yii::t('yiiext','You should not use CActiveRecord::save() method when AjacencyListBehavior attached.'));
}
public function beforeDelete($event)
{
if($this->_ignoreEvent)
return true;
else
throw new CDbException(Yii::t('yiiext','You should not use CActiveRecord::delete() method when AjacencyListBehavior attached.'));
}
private function correctCachedOnDelete()
{
}
private function correctCachedOnAddNode()
{
}
}