@@ -209,7 +209,7 @@ function getRoot($folder)
209
209
} // end getRoot()
210
210
211
211
/** Get the folder tree */
212
- function getAllChildren ($ folder , $ userDao )
212
+ function getAllChildren ($ folder , $ userDao, $ admin = false )
213
213
{
214
214
$ isAdmin = false ;
215
215
if ($ userDao == null )
@@ -228,6 +228,11 @@ function getAllChildren($folder, $userDao)
228
228
$ isAdmin = true ;
229
229
}
230
230
}
231
+
232
+ if ($ admin )
233
+ {
234
+ $ isAdmin = true ;
235
+ }
231
236
232
237
if (!$ folder instanceof FolderDao)
233
238
{
@@ -349,6 +354,7 @@ public function move($folder, $parent)
349
354
}
350
355
351
356
$ tmpParent = $ parent ->getParent ();
357
+ $ currentParent = $ folder ->getParent ();
352
358
while ($ tmpParent != false )
353
359
{
354
360
if ($ tmpParent ->getKey () == $ folder ->getKey ())
@@ -366,23 +372,82 @@ public function move($folder, $parent)
366
372
{
367
373
throw new Zend_Exception ("Error parameter. " );
368
374
}
369
- $ leftIndice = $ folder ->getLeftIndice ();
370
- $ this ->database ->getDB ()->update ('folder ' , array ('left_indice ' => new Zend_Db_Expr ('left_indice - 2 ' )),
371
- array ('left_indice >= ? ' => $ leftIndice ));
372
- $ this ->database ->getDB ()->update ('folder ' , array ('right_indice ' => new Zend_Db_Expr ('right_indice - 2 ' )),
373
- array ('right_indice >= ? ' => $ leftIndice ));
374
- $ folder ->setParentId ($ parent ->getKey ());
375
- $ rightParent = $ parent ->getRightIndice ();
376
375
377
- $ folder ->setRightIndice ($ rightParent + 1 );
378
- $ folder ->setLeftIndice ($ rightParent );
376
+ $ node_id = $ folder ->getKey ();
377
+ $ node_pos_left = $ folder ->getLeftIndice ();
378
+ $ node_pos_right = $ folder ->getRightIndice ();
379
+ $ parent_id = $ parent ->getKey ();
380
+
381
+ $ parent_pos_right = $ parent ->getRightIndice ();
382
+ $ node_size = $ node_pos_right - $ node_pos_left + 1 ;
383
+
384
+ // step 1: temporary "remove" moving node
385
+ $ this ->database ->getDB ()->update ('folder ' , array ('left_indice ' => new Zend_Db_Expr ('0 - left_indice ' ), 'right_indice ' => new Zend_Db_Expr ('0 - right_indice ' )),
386
+ 'left_indice >= ' .$ node_pos_left .' AND right_indice <= ' .$ node_pos_right );
379
387
380
- $ this ->database ->getDB ()->update ('folder ' , array ('right_indice ' => new Zend_Db_Expr ('2 + right_indice ' )),
381
- array ('right_indice >= ? ' => $ rightParent ));
382
- $ this ->database ->getDB ()->update ('folder ' , array ('left_indice ' => new Zend_Db_Expr ('2 + left_indice ' )),
383
- array ('left_indice >= ? ' => $ rightParent ));
388
+ // step 2: decrease left and/or right position values of currently 'lower' items (and parents)
389
+ $ this ->database ->getDB ()->update ('folder ' , array ('left_indice ' => new Zend_Db_Expr ('left_indice - ' .$ node_size )),
390
+ array ('left_indice > ? ' => $ node_pos_right ));
391
+ $ this ->database ->getDB ()->update ('folder ' , array ('right_indice ' => new Zend_Db_Expr ('right_indice - ' .$ node_size )),
392
+ array ('right_indice > ? ' => $ node_pos_right ));
384
393
394
+ // step 3: increase left and/or right position values of future 'lower' items (and parents)
395
+ $ cond = ($ parent_pos_right > $ node_pos_right ? $ parent_pos_right - $ node_size : $ parent_pos_right );
396
+ $ this ->database ->getDB ()->update ('folder ' , array ('left_indice ' => new Zend_Db_Expr ('left_indice + ' .$ node_size )),
397
+ array ('left_indice >= ? ' => $ cond ));
398
+ $ this ->database ->getDB ()->update ('folder ' , array ('right_indice ' => new Zend_Db_Expr ('right_indice + ' .$ node_size )),
399
+ array ('right_indice >= ? ' => $ cond ));
400
+
401
+ // step 4: move node (ant it's subnodes) and update it's parent item id
402
+ $ cond = ($ parent_pos_right > $ node_pos_right ) ? $ parent_pos_right - $ node_pos_right - 1 : $ parent_pos_right - $ node_pos_right - 1 + $ node_size ;
403
+ $ this ->database ->getDB ()->update ('folder ' , array ('left_indice ' => new Zend_Db_Expr ('0 - left_indice + ' .$ cond ), 'right_indice ' => new Zend_Db_Expr ('0 - right_indice + ' .$ cond )),
404
+ 'left_indice <= ' .(0 - $ node_pos_left ).' AND right_indice >= ' .(0 - $ node_pos_right ));
405
+
406
+ $ folder = $ this ->load ($ folder ->getKey ());
407
+ $ folder ->setParentId ($ parent ->getKey ());
385
408
parent ::save ($ folder );
409
+ /*
410
+
411
+ $allChildren = $this->getAllChildren($folder, null, true);
412
+
413
+ $leftIndice = $folder->getLeftIndice();
414
+ $rightIndice = $folder->getRightIndice();
415
+ $rightParentIndice = $currentParent->getRightIndice();
416
+ $this->database->getDB()->update('folder', array('left_indice' => new Zend_Db_Expr('left_indice - '.$diff)),
417
+ array('left_indice > ?' => $rightIndice));
418
+ $this->database->getDB()->update('folder', array('right_indice' => new Zend_Db_Expr('right_indice - '.$diff)),
419
+ array('right_indice > ?' => $rightIndice));
420
+
421
+
422
+ $childrenIds = array();
423
+ foreach($allChildren as $child)
424
+ {
425
+ $childrenIds[] = $child->getKey();
426
+ }
427
+ $childrenIds[] = $folder->getKey();
428
+ $folder = $this->load($folder->getKey());
429
+ $parent = $this->load($parent->getKey());
430
+
431
+ $diff = $folder->getLeftIndice() - $parent->getRightIndice();
432
+
433
+ $this->database->getDB()->update('folder', array('left_indice' => new Zend_Db_Expr('left_indice - '.$diff)),
434
+ array('folder_id IN (?)' => $childrenIds));
435
+ $this->database->getDB()->update('folder', array('right_indice' => new Zend_Db_Expr('right_indice - '.$diff)),
436
+ array('folder_id IN (?)' => $childrenIds));
437
+
438
+ $this->getLogger()->info('2:'.$diff);
439
+ $parent = $this->load($parent->getKey());
440
+ $folder = $this->load($folder->getKey());
441
+ $diff = $folder->getRightIndice() - $parent->getRightIndice() + 1;
442
+
443
+ $this->database->getDB()->update('folder', array('right_indice' => new Zend_Db_Expr($diff.' + right_indice')),
444
+ array('right_indice >= ?' => $parent->getRightIndice()));
445
+ $this->database->getDB()->update('folder', array('left_indice' => new Zend_Db_Expr($diff.' + left_indice')),
446
+ array('left_indice >= ?' => $parent->getRightIndice()));
447
+ $folder = $this->load($folder->getKey());
448
+ $folder->setParentId($parent->getKey());
449
+ $this->getLogger()->info('3:'.$diff);
450
+ parent::save($folder);*/
386
451
}//end move
387
452
388
453
/** Custom save function*/
0 commit comments