diff options
Diffstat (limited to 'modules/gallery/libraries/ORM_MPTT.php')
| -rw-r--r-- | modules/gallery/libraries/ORM_MPTT.php | 112 | 
1 files changed, 52 insertions, 60 deletions
| diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index 0ea519c9..3668d42d 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -40,50 +40,52 @@ class ORM_MPTT_Core extends ORM {    }    /** -   * Add this node as a child of the parent provided. +   * Overload ORM::save() to update the MPTT tree when we add new items to the hierarchy.     *     * @chainable -   * @param integer $parent_id the id of the parent node -   * @return ORM +   * @return  ORM     */ -  function add_to_parent($parent) { -    $this->lock(); -    $parent->reload();  // Assume that the prior lock holder may have changed the parent +  function save() { +    if (!$this->loaded()) { +      $this->lock(); +      $parent = ORM::factory("item", $this->parent_id); -    try { -      // Make a hole in the parent for this new item -      $this->db_builder -        ->update($this->table_name) -        ->set("left_ptr", new Database_Expression("`left_ptr` + 2")) -        ->where("left_ptr", ">=", $parent->right_ptr) -        ->execute(); -      $this->db_builder -        ->update($this->table_name) -        ->set("right_ptr", new Database_Expression("`right_ptr` + 2")) -        ->where("right_ptr", ">=", $parent->right_ptr) -        ->execute(); -      $parent->right_ptr += 2; +      try { +        // Make a hole in the parent for this new item +        db::build() +          ->update($this->table_name) +          ->set("left_ptr", new Database_Expression("`left_ptr` + 2")) +          ->where("left_ptr", ">=", $parent->right_ptr) +          ->execute(); +        db::build() +          ->update($this->table_name) +          ->set("right_ptr", new Database_Expression("`right_ptr` + 2")) +          ->where("right_ptr", ">=", $parent->right_ptr) +          ->execute(); +        $parent->right_ptr += 2; -      // Insert this item into the hole -      $this->left_ptr = $parent->right_ptr - 2; -      $this->right_ptr = $parent->right_ptr - 1; -      $this->parent_id = $parent->id; -      $this->level = $parent->level + 1; -      $this->save(); -      $parent->reload(); -    } catch (Exception $e) { +        // Insert this item into the hole +        $this->left_ptr = $parent->right_ptr - 2; +        $this->right_ptr = $parent->right_ptr - 1; +        $this->parent_id = $parent->id; +        $this->level = $parent->level + 1; +      } catch (Exception $e) { +        $this->unlock(); +        throw $e; +      } +      parent::save();        $this->unlock(); -      throw $e; +    } else { +      parent::save();      } -    $this->unlock();      return $this;    }    /**     * Delete this node and all of its children.     */ -  public function delete() { +  public function delete($ignored_id=null) {      $children = $this->children();      if ($children) {        foreach ($this->children() as $item) { @@ -100,12 +102,12 @@ class ORM_MPTT_Core extends ORM {      $this->lock();      $this->reload();  // Assume that the prior lock holder may have changed this entry      try { -      $this->db_builder +      db::build()          ->update($this->table_name)          ->set("left_ptr", new Database_Expression("`left_ptr` - 2"))          ->where("left_ptr", ">", $this->right_ptr)          ->execute(); -      $this->db_builder +      db::build()          ->update($this->table_name)          ->set("right_ptr", new Database_Expression("`right_ptr` - 2"))          ->where("right_ptr", ">", $this->right_ptr) @@ -165,11 +167,8 @@ class ORM_MPTT_Core extends ORM {     * @return array ORM     */    function children($limit=null, $offset=null, $where=null, $order_by=array("id" => "ASC")) { -    if ($where) { -      $this->merge_where($where); -    } -      return $this +      ->merge_where($where)        ->where("parent_id", "=", $this->id)        ->order_by($order_by)        ->find_all($limit, $offset); @@ -183,11 +182,8 @@ class ORM_MPTT_Core extends ORM {     * @return array ORM     */    function children_count($where=null) { -    if ($where) { -      $this->merge_where($where); -    } -      return $this +      ->merge_where($where)        ->where("parent_id", "=", $this->id)        ->count_all();    } @@ -202,11 +198,8 @@ class ORM_MPTT_Core extends ORM {     * @return object ORM_Iterator     */    function descendants($limit=null, $offset=null, $where=null, $order_by=array("id" => "ASC")) { -    if ($where) { -      $this->merge_where($where); -    } -      return $this +      ->merge_where($where)        ->where("left_ptr", ">", $this->left_ptr)        ->where("right_ptr", "<=", $this->right_ptr)        ->order_by($order_by) @@ -220,11 +213,8 @@ class ORM_MPTT_Core extends ORM {     * @return   integer  child count     */    function descendants_count($where=null) { -    if ($where) { -      $this->merge_where($where); -    } -      return $this +      ->merge_where($where)        ->where("left_ptr", ">", $this->left_ptr)        ->where("right_ptr", "<=", $this->right_ptr)        ->count_all(); @@ -237,11 +227,15 @@ class ORM_MPTT_Core extends ORM {     * @param   Item_Model $target Target node     * @return  ORM_MTPP     */ -  function move_to($target) { +  protected function move_to($target) {      if ($this->contains($target)) {        throw new Exception("@todo INVALID_TARGET can't move item inside itself");      } +    $this->lock(); +    $this->reload();  // Assume that the prior lock holder may have changed this entry +    $target->reload(); +      $number_to_move = (int)(($this->right_ptr - $this->left_ptr) / 2 + 1);      $size_of_hole = $number_to_move * 2;      $original_left_ptr = $this->left_ptr; @@ -249,13 +243,10 @@ class ORM_MPTT_Core extends ORM {      $target_right_ptr = $target->right_ptr;      $level_delta = ($target->level + 1) - $this->level; -    $this->lock(); -    $this->reload();  // Assume that the prior lock holder may have changed this entry -    $target->reload();      try {        if ($level_delta) {          // Update the levels for the to-be-moved items -        $this->db_builder +        db::build()            ->update($this->table_name)            ->set("level", new Database_Expression("`level` + $level_delta"))            ->where("left_ptr", ">=", $original_left_ptr) @@ -264,25 +255,26 @@ class ORM_MPTT_Core extends ORM {        }        // Make a hole in the target for the move -      $target->db_builder +      db::build()          ->update($this->table_name)          ->set("left_ptr", new Database_Expression("`left_ptr` + $size_of_hole"))          ->where("left_ptr", ">=", $target_right_ptr)          ->execute(); -      $target->db_builder +      db::build()          ->update($this->table_name)          ->set("right_ptr", new Database_Expression("`right_ptr` + $size_of_hole"))          ->where("right_ptr", ">=", $target_right_ptr)          ->execute();        // Change the parent. -      $this->db_builder +      db::build()          ->update($this->table_name)          ->set("parent_id", $target->id)          ->where("id", "=", $this->id)          ->execute(); -      // If the source is to the right of the target then we just adjusted its left_ptr and right_ptr above. +      // If the source is to the right of the target then we just adjusted its left_ptr and +      // right_ptr above.        $left_ptr = $original_left_ptr;        $right_ptr = $original_right_ptr;        if ($original_left_ptr > $target_right_ptr) { @@ -291,7 +283,7 @@ class ORM_MPTT_Core extends ORM {        }        $new_offset = $target->right_ptr - $left_ptr; -      $this->db_builder +      db::build()          ->update($this->table_name)          ->set("left_ptr", new Database_Expression("`left_ptr` + $new_offset"))          ->set("right_ptr", new Database_Expression("`right_ptr` + $new_offset")) @@ -300,12 +292,12 @@ class ORM_MPTT_Core extends ORM {          ->execute();        // Close the hole in the source's parent after the move -      $this->db_builder +      db::build()          ->update($this->table_name)          ->set("left_ptr", new Database_Expression("`left_ptr` - $size_of_hole"))          ->where("left_ptr", ">", $right_ptr)          ->execute(); -      $this->db_builder +      db::build()          ->update($this->table_name)          ->set("right_ptr", new Database_Expression("`right_ptr` - $size_of_hole"))          ->where("right_ptr", ">", $right_ptr) | 
