/
EBehavior.php
executable file
·261 lines (244 loc) · 9.08 KB
/
EBehavior.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
<?php
/**
* Description of EBehavior
* This behavior has some functions used in the Actions and
* initializes the following: classname, id, text.
* For more info check in code.
*
* Example (add this to the controller class):
* public function behaviors() {
* return array(
* 'EJNestedTreeActions'=>array(
* 'class'=>'ext.EJNestedTreeActions.EBehavior',
* 'classname'=>'Rutree',
* 'id'=>'id',
* 'text'=>'text',
* ),
* );
* }
*
* @version 0.3beta
* @author Dimitrios Meggidis <tydeas.dr@gmail.com>
* @copyright Evresis A.E. <www.evresis.gr>
*/
class EBehavior extends CBehavior {
/**
* @var string the name of the model class
*/
public $classname;
/**
* @var string the name of the id attribute of model
*/
public $identity;
/**
*
* @var string the attribute of model that will displayed
* as node title
*/
public $text;
/**
* @var array the attributes that will be inherited from parent to children
*/
public $inherit;
/**
* The following boolean attributes are have same behavior for different
* senarios:
* mvinherit is for moving nodes.
* cpinherit is for copying nodes.
* crtinherit is for creating node.
*
* If true at the senario the nodes will inherit the inherit values from
* the new parent
* @var boolean
*/
public $mvinherit;
public $cpinherit;
public $crtinherit;
/**
* If when creating node should check the future brothers for same name and change the new
* node name to old i++
* @var boolean
*/
public $nodenaming;
/**
* New node's default name
*/
public $defaultNodeName = 'New folder';
/**
* New root's default name
*/
public $defaultRootName = 'New root';
/**
* Used internal function that takes a node and returns it as string.
*
* @param mied of CActiveRecord array of $models or single model
* @return string In jstree format.
*/
public function formatNode( $models ) {
if( is_array( $models ) ) {
$nodesarray=array();
foreach( $models as $i => $node ) {
$nodesarray[$i]=array(
'attributes'=>array(
'id'=>$node->getAttribute($this->identity)
),
'data'=>$node->getAttribute($this->text),
);
if(!$node->isLeaf()) $nodesarray[$i]['state']="closed";
}
return $nodesarray;
}
$jstreeformat=array(
'attributes'=>array(
'id'=>$models->getAttribute($this->identity)
),
'data'=>$models->getAttribute($this->text),
);
if(!$models->isLeaf()) $jstreeformat['state']="closed";
return $jstreeformat;
}
/**
* This method search for brother nodes with same name.
* Used internally when trying to rename,create or move
* @param CActiveRecord $parent the parent of the node if null then $node is root
* @param CActiveRecord $node the node that' been created/moved/renamed
* @return CActiveRecord $node the node with the proper name
*/
public function nodeNaming($parent=null,$node) {
if($parent!=null) {
$brothers=$parent->children()->findAll(array(
'condition'=>'`'.$this->text.'` LIKE :dfname',
'params'=>array(':dfname'=>$node->getAttribute($this->text).'%'),
)
);
} else {
$brothers = CActiveRecord::model($this->classname)->roots()->findall();
}
$name=$node->getAttribute($this->text);
$i=1;
$namenotfound=true;
do {
$namenotfound=$this->nameExist($brothers,$node);
if ($namenotfound) {
$node->setAttribute($this->text,$name." ".$i);
$i++;
}
} while($namenotfound);
return $node;
}
/**
* This method is used internally. It takes the an array of the nodes and
* second node as well.
* At create node the array of nodes is the possible brothers of the new node.
* Because new node has not an id yet the second part will return true.
* At rename the array of nodes contains the new node as well this is why the
* second part of if checks if the $bro id is different from the $new id.
* The second part of if is needed for the move node action too.
* @param CActiveRecord $bro
* @param CActiveRecord $new
* @return boolean true if name already exist false if not exist
*/
public function nameExist($bro,$new){
foreach( $bro as $i=>$bro ){
if( $bro->getAttribute($this->text) == $new->getAttribute($this->text) && $bro->getAttribute($this->identity) != $new->getAttribute($this->identity) ) {
return true;
}
}
return false;
}
/**
* This method is used internaly by the Createnode and Copynode actions.
* It get's a new node to insert and depend type it insert it's appropriate
* place relative to the refnode (reference node);
* @param CActiverecord::model $newnode new node to insert
* @param CActiverecord::model $refnode reference node to insertion
* @param string $type where to insert node
* @param boolean $nodenaming if there should be a nodenaming check default true
* @param boolean $inheritvalues if inherit values must be copied or not.
* @return CJSON::encode the node inserted in json encode
*/
public function insertingnode( $newnode=null,$refnode=null,$type=null, $nodenaming=true , $inheritvalues=false ) {
// if ( $type=="inside") {
// $parent=$refnode;
// } else {
// $parent=$refnode->parent();
// }
$parent = ($type=="inside") ? $refnode : $refnode->parent();
$newnode= ($nodenaming) ? $this->nodeNaming($parent, $newnode) : $newnode;
$success=false;
switch ( $type ) {
case "inside":
if ( $refnode->append($newnode,false) ) {
$success=true;
}
break;
case "before":
if ( $newnode->insertBefore($refnode,false) ) {
$success=true;
}
break;
case "after":
if ( $newnode->insertAfter($refnode,false) ) {
$success=true;
}
break;
}
if($success) {
if($inheritvalues) {
$this->inheritvalues( $newnode , $parent );
}
$jsondata=$this->formatNode($newnode);
return CJSON::encode($jsondata);
}
return $success;
}
/**
* This methond is used mainly from the copynode action. It copies a node
* and his child if there are exist.
* @param string $id The id of the node to be copied
* @param string $ref The id of a reference node that is used with type to determine the new position
* @param string $type Where the new node will be copied
*/
public function copytree( $id, $ref, $type ){
$classname=$this->classname;
$node=CActiveRecord::model($this->classname)->findByPk($id);
$refnode=CActiveRecord::model($this->classname)->findByPk($ref);
if($node->isLeaf()) {
$copy=new $classname();
$copy->attributes=$node->attributes;
$this->insertingnode( $copy,$refnode,$type,true,$this->cpinherit );
} else {
$copy=new $classname();
$copy->attributes=$node->attributes;
$this->insertingnode( $copy,$refnode,$type,true,$this->cpinherit );
$childs = $node->children()->findall();
foreach( $childs as $i => $chnode ) {
$this->copytree( $chnode->getAttribute($this->identity) , $copy->getAttribute($this->identity) , "inside" , false );
}
}
//echo $this->getController()->copytree($copy,$refnode,$type);
}
/**
* This method is used to at create/copy or move. If the last $inherit attribute is true
* the nodes under current and current also will inherit the values from the new parent.
* @param CActiveRecord $current node that is created/copied or moved
* @param CActiveRecord $parent the new parent in either senario
*/
public function inheritvalues( $current , $parent ) {
// $differentparent = $parent->getAttribute($this->identity)!=$current->parent()->getAttribute($this->identity);
// if ( $differentparent ) {
foreach ( $this->inherit as $attr ) {
$current->setAttribute($attr,$parent->getAttribute($attr));
}
$current->saveNode();
$descendants = $current->descendants()->findAll();
foreach ( $descendants as $i => $node ) {
foreach ( $this->inherit as $attr ) {
$node->setAttribute($attr,$parent->getAttribute($attr));
}
$node->saveNode();
}
//}
}
}
?>