Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. id name left right
  2.  
  3. //
  4. * -- create new space for subtree
  5. * UPDATE tags SET lpos = lpos + :width WHERE lpos >= :newpos
  6. * UPDATE tags SET rpos = rpos + :width WHERE rpos >= :newpos
  7. *
  8. * -- move subtree into new space
  9. * UPDATE tags SET lpos = lpos + :distance, rpos = rpos + :distance
  10. * WHERE lpos >= :tmppos AND rpos < :tmppos + :width
  11. *
  12. * -- remove old space vacated by subtree
  13. * UPDATE tags SET lpos = lpos - :width WHERE lpos > :oldrpos
  14. * UPDATE tags SET rpos = rpos - :width WHERE rpos > :oldrpos
  15. */
  16.  
  17. // calculate position adjustment variables
  18. int width = node.getRpos() - node.getLpos() + 1;
  19. int distance = newpos - node.getLpos();
  20. int tmppos = node.getLpos();
  21.  
  22. // backwards movement must account for new space
  23. if (distance < 0) {
  24. distance -= width;
  25. tmppos += width;
  26. }
  27.  
  28. $step = 1+ $this->_categoriesTable->rgt
  29. - $this->_categoriesTable->lft;
  30. $lft = $this->_categoriesTable->lft;
  31. $rgt = $this->_categoriesTable->rgt;
  32. $id = $this->_categoriesTable->id;
  33. $distance = $lft - $parentLeft - 1;
  34. $query = '
  35. UPDATE %s SET lft=-lft, rgt=-rgt
  36. WHERE lft>=%d AND lft<=%d;
  37. UPDATE %s SET lft=lft+%d WHERE lft>%d AND lft<%d;
  38. UPDATE %s SET rgt=rgt+%d WHERE rgt>%d AND rgt<%d;
  39. UPDATE %s SET lft=-lft-%d, rgt=-rgt-%d WHERE lft<=-%d
  40. AND lft>=-%d;
  41. UPDATE %s SET parent_id=%d, title=%s, description=%s,
  42. metadescription=%s WHERE id=%s';
  43.  
  44. $query = sprintf($query,
  45. $this->_db->nameQuote('#__categories'),
  46. $lft, $rgt,
  47. $this->_db->nameQuote('#__categories'), $step,
  48. $parentLeft, $lft,
  49. $this->_db->nameQuote('#__categories'), $step,
  50. $parentLeft, $lft,
  51. $this->_db->nameQuote('#__categories'), $distance,
  52. $distance, $lft, $rgt,
  53. $this->_db->nameQuote('#__categories'),
  54. $data['parent_id'],
  55. $this->_db->Quote($this->_categoriesTable->title),
  56. $this->_db->Quote($this->_categoriesTable->description),
  57. $this->_db->Quote(
  58. $this->_categoriesTable->metadescription),
  59. $this->_db->Quote($id));
  60.  
  61. // and for the moving to the "right" case
  62. $step = 1+ $this->_categoriesTable->rgt
  63. - $this->_categoriesTable->lft;
  64. $distance = $parentLeft - $this->_categoriesTable->rgt;
  65.  
  66. // Memorize this because we bind and we need the old values
  67. $lft = $this->_categoriesTable->lft;
  68. $rgt = $this->_categoriesTable->rgt;
  69. $id = $this->_categoriesTable->id;
  70.  
  71. $query = sprintf($query,
  72. $this->_db->nameQuote('#__categories'),
  73. $lft, $rgt,
  74. $this->_db->nameQuote('#__categories'), $step,
  75. $rgt, $parentLeft,
  76. $this->_db->nameQuote('#__categories'), $step,
  77. $rgt, $parentLeft,
  78. $this->_db->nameQuote('#__categories'), $distance,
  79. $distance, $lft, $rgt,
  80. $this->_db->nameQuote('#__categories'),
  81. $data['parent_id'],
  82. $this->_db->Quote($this->_categoriesTable->title),
  83. $this->_db->Quote($this->_categoriesTable->description),
  84. $this->_db->Quote(
  85. $this->_categoriesTable->metadescription),
  86. $this->_db->Quote($id));
  87.  
  88. #Set IDs
  89. SET @dirId := :dirId; #folder (subtree) you wanna move
  90. SET @targetId := :folderId; #target
  91.  
  92. #get datas
  93. SELECT rgt, lft, rgt-lft+1, level INTO @dir_rgt, @dir_lft, @dir_size, @dir_level FROM files WHERE id = @dirId;
  94.  
  95. #put the moving tree aside (lft and rgt columns must allow negative int)
  96. UPDATE files SET lft = 0-lft, rgt = 0-rgt WHERE lft BETWEEN @dir_lft AND @dir_rgt;
  97.  
  98. #fill the empty space
  99. UPDATE files SET rgt = rgt-@dir_size WHERE rgt > @dir_rgt;
  100. UPDATE files SET lft = lft-@dir_size WHERE lft > @dir_rgt;
  101.  
  102. #get datas of the target-folder
  103. SELECT lft, level INTO @target_lft, @target_level FROM files WHERE id = @targetId;
  104.  
  105. #create space in the target-folder
  106. UPDATE files SET rgt = rgt+@dir_size WHERE rgt >= @target_lft;
  107. UPDATE files SET lft = lft+@dir_size WHERE lft > @target_lft;
  108.  
  109. #edit all nodes in the moving-tree
  110. UPDATE files SET
  111. lft = 0 - lft - (@dir_lft - @target_lft - 1), #this formula fits for all moving directions
  112. rgt = 0 - rgt - (@dir_lft - @target_lft - 1),
  113. level = level - (@dir_level - @target_level) + 1
  114.  
  115. WHERE
  116. lft < 0; #that could be more precise...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement