diff options
Diffstat (limited to 'modules/gallery/libraries')
| -rw-r--r-- | modules/gallery/libraries/ORM_MPTT.php | 80 | 
1 files changed, 35 insertions, 45 deletions
| diff --git a/modules/gallery/libraries/ORM_MPTT.php b/modules/gallery/libraries/ORM_MPTT.php index 0ea519c9..404d61ff 100644 --- a/modules/gallery/libraries/ORM_MPTT.php +++ b/modules/gallery/libraries/ORM_MPTT.php @@ -40,43 +40,45 @@ 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")->where("id", "=", $this->parent_id)->find(); -    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 +        $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; -      // 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;    } @@ -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(); | 
