summaryrefslogtreecommitdiff
path: root/core/libraries/ORM_MPTT.php
blob: 88e79194c57213ab0f80aaf5913d19ababb1644f (plain)
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
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2008 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * Implement Modified Preorder Tree Traversal on top of ORM.
 *
 * MPTT is an efficient way to store and retrieve hierarchical data in a single database table.
 * For a good description, read http://www.sitepoint.com/article/hierarchical-data-database/3/
 *
 * This code was heavily influenced by code from:
 *  - http://code.google.com/p/kohana-mptt/
 *  - http://code.google.com/p/kohana-mptt/wiki/Documentation
 *  - http://code.google.com/p/s7ncms/source/browse/trunk/modules/s7ncms/libraries/ORM_MPTT.php
 *
 * Unfortunately that code was not ready for production and I did not want to absorb their code
 * and licensing issues so I've reimplemented just the features that we need.
 */
class ORM_MPTT_Core extends ORM {
  private $model_name = null;
  private $parent = null;
  private $parents = null;
  private $children = null;
  private $children_count = null;
  private $descendants = array();
  private $descendants_count = array();

  function __construct($id=null) {
    parent::__construct($id);
    $this->model_name = inflector::singular($this->table_name);
  }

  /**
   * Add this node as a child of the parent provided.
   *
   * @chainable
   * @param integer $parent_id the id of the parent node
   * @return ORM
   */
  function add_to_parent($parent_id) {
    $this->_lock();

    try {
      $parent = ORM::factory($this->model_name, $parent_id);
      $parent->_grow();
      $this->left = $parent->right - 2;
      $this->right = $parent->right - 1;
      $this->parent_id = $parent->id;
      $this->level = $parent->level + 1;
      $this->save();
    } catch (Exception $e) {
      $this->_unlock();
      throw $e;
    }

    $this->_unlock();
    return $this;
  }

  /**
   * Return the parent of this node
   *
   * @return ORM
   */
  function parent() {
    if (!isset($this->parent)) {
      $this->parent =
        ORM::factory($this->model_name)->where("id", $this->parent_id)->find();
    }
    return $this->parent;
  }

  /**
   * Return all the parents of this node, in order from root to this node's immediate parent.
   *
   * @return array ORM
   */
  function parents() {
    if (!isset($this->parents)) {
      $this->parents = $this
        ->where("`left` <= {$this->left}")
        ->where("`right` >= {$this->right}")
        ->where("id <> {$this->id}")
        ->orderby("left", "ASC")
        ->find_all();
    }
    return $this->parents;
  }

  /**
   * Return all of the children of this node, ordered by id.
   *
   * @chainable
   * @param   integer  SQL limit
   * @param   integer  SQL offset
   * @return array ORM
   */
  function children($limit=NULL, $offset=0) {
    if (!isset($this->children)) {
      $this->children =
        $this->where("parent_id", $this->id)
        ->orderby("id", "ASC")
        ->find_all($limit, $offset);
    }
    return $this->children;
  }

  /**
   * Return all of the children of this node, ordered by id.
   *
   * @chainable
   * @param   integer  SQL limit
   * @param   integer  SQL offset
   * @return array ORM
   */
  function children_count() {
    if (!isset($this->children_count)) {
      $this->children_count = $this->where("parent_id", $this->id)->count_all();
    }
    return $this->children_count;
  }

  /**
   * Return all of the children of the specified type, ordered by id.
   *
   * @param   integer  SQL limit
   * @param   integer  SQL offset
   * @param   string   type to return
   * @return object ORM_Iterator
   */
  function descendants($limit=NULL, $offset=0, $type=null) {
    if (!isset($this->descendants[$type][$offset])) {
      $this->where("left >", $this->left)
        ->where("right <=", $this->right);
      if ($type) {
        $this->where("type", $type);
      }

      // @todo: make the order column data driven
      $this->orderby("id", "ASC");

      $this->descendants[$type][$offset] = $this->find_all($limit, $offset);
    }
    return $this->descendants[$type][$offset];
  }

  /**
   * Return the count of all the children of the specified type.
   *
   * @param   string   type to count
   * @return   integer  child count
   */
  function descendants_count($type=null) {
    if (!isset($this->descendants_count[$type])) {
      $this->where("left >", $this->left)
        ->where("right <=", $this->right);
      if ($type) {
        $this->where("type", $type);
      }
      $this->descendants_count[$type] = $this->count_all();
    }

    return $this->descendants_count[$type];
  }

  /**
   * @see ORM::reload
   */
  function reload() {
    $this->parent = null;
    $this->parents = null;
    $this->children = null;
    $this->children_count = null;
    return parent::reload();
  }

  /**
   * Grow this node's space enough to make room for 1 or more new nodes.
   *
   * @param integer $count the number of new nodes to add
   */
  private function _grow($count=1) {
    $size = $count * 2;
    $this->db->query(
      "UPDATE `{$this->table_name}` SET `left` = `left` + $size WHERE `left` >= {$this->right}");
    $this->db->query(
      "UPDATE `{$this->table_name}` SET `right` = `right` + $size WHERE `right` >= {$this->right}");
    $this->right += 2;
  }

  /**
   * Lock the tree to prevent concurrent modification.
   */
  private function _lock() {
    $result = $this->db->query("SELECT GET_LOCK('{$this->table_name}', 1) AS L")->current();
    if (empty($result->L)) {
      throw new Exception("@todo UNABLE_TO_LOCK_EXCEPTION");
    }
  }

  /**
   * Unlock the tree.
   */
  private function _unlock() {
    $this->db->query("SELECT RELEASE_LOCK('{$this->table_name}')");
  }
}