[1] | 1 | <?php |
---|
| 2 | /** |
---|
[42] | 3 | * NodeHeirarchy.inc.php |
---|
[1] | 4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
| 5 | */ |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * Objective: This class provides the tools to organize pieces of data into a |
---|
| 9 | * heirarchy of nodes. Any form of data (article, product, image) can be |
---|
| 10 | * represented as a node in this heirarchy. This class does not manipulate the |
---|
| 11 | * data, nor is it involved in storing or retrieving the data. In fact it does |
---|
| 12 | * not access the tables where data exists and cannot find out info about the |
---|
| 13 | * data. You must provide identification of a piece of data (type and ID) to |
---|
| 14 | * insert it into the heirarchy. The node heirarchy is completely |
---|
| 15 | * separate from data storage and retreival. You must separatly store the data |
---|
| 16 | * using whatever logic is specific to the data then also call these functions. |
---|
| 17 | * Nodes are not the data. The nodes are mere singularities in virtual space |
---|
| 18 | * that represent a piece of data's relationship with another. The heirarchy |
---|
| 19 | * is an inverted tree structure. Each node can have virtually infinite |
---|
| 20 | * children. Each child can have multiple parents. |
---|
| 21 | * |
---|
| 22 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
| 23 | * @version 1.0 |
---|
| 24 | */ |
---|
[42] | 25 | |
---|
[1] | 26 | class NodeHeirarchy { |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Array containing object parameters. |
---|
| 30 | * @var array $params |
---|
| 31 | * |
---|
| 32 | * $params['child_type'] = 'article' |
---|
| 33 | * $params['child_id'] = 23 |
---|
| 34 | * |
---|
| 35 | */ |
---|
| 36 | var $params = array(); |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * The active node type. |
---|
| 41 | * @var string $child_type |
---|
| 42 | */ |
---|
| 43 | var $child_type; |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * The active node id. |
---|
| 47 | * @var string $child_id |
---|
| 48 | */ |
---|
| 49 | var $child_id; |
---|
| 50 | |
---|
| 51 | /** |
---|
[42] | 52 | * Boolean indicating whether or not we've set |
---|
[1] | 53 | * the 'active' node type and id. |
---|
| 54 | * @var bool $node_init |
---|
| 55 | */ |
---|
| 56 | var $node_init = false; |
---|
[42] | 57 | |
---|
[1] | 58 | /** |
---|
| 59 | * Constructor |
---|
| 60 | * @param resource $dbh A database handler if we are already connected. |
---|
| 61 | * @param array $params A hash containing any additional |
---|
| 62 | * configuration or connection parameters. |
---|
| 63 | */ |
---|
| 64 | function NodeHeirarchy($params=array()) |
---|
| 65 | { |
---|
| 66 | $this->params = $params; |
---|
| 67 | } |
---|
[42] | 68 | |
---|
[1] | 69 | /** |
---|
| 70 | * Defines the default child_type and child_id for this object. |
---|
| 71 | * @child_type string this node's type |
---|
| 72 | * @child_id string this node's id |
---|
| 73 | * @return string the previous node identifier, or the current one |
---|
| 74 | * if no new ones are specified. |
---|
| 75 | */ |
---|
| 76 | function currentNode($child_type=null, $child_id=null) |
---|
| 77 | { |
---|
| 78 | $old_type = isset($this->child_type) ? $this->child_type : $child_type; |
---|
| 79 | $old_id = isset($this->child_id) ? $this->child_id : $child_id; |
---|
| 80 | if (isset($child_type) && isset($child_id)) { |
---|
| 81 | $this->child_type = $child_type; |
---|
| 82 | $this->child_id = $child_id; |
---|
| 83 | $this->node_init = true; |
---|
| 84 | } |
---|
| 85 | return $this->toStringID($old_type, $old_id); |
---|
| 86 | } |
---|
[42] | 87 | |
---|
[1] | 88 | /** |
---|
| 89 | * Takes a node type and id and returns them as a serialized identifier like |
---|
| 90 | * article__24 or category__84. If the first argument is an array, and the |
---|
| 91 | * second is -1, we loop through the array and serialize the identifiers inside. |
---|
| 92 | * @param mixed $child_type |
---|
| 93 | * @param string $child_id |
---|
| 94 | * @return mixed If a single type and id are provided, a single vector identifier is returned, |
---|
| 95 | * otherwise if an array is provided, an array of identifiers is returned. |
---|
| 96 | */ |
---|
| 97 | function toStringID($child_type=null, $child_id=null) |
---|
| 98 | { |
---|
| 99 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 100 | if ($this->node_init) { |
---|
| 101 | $child_type =& $this->child_type; |
---|
| 102 | $child_id =& $this->child_id; |
---|
| 103 | } else { |
---|
| 104 | App::logMsg(_("toStringID failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 105 | return false; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | if (is_array($child_type) && $child_id === -1) { |
---|
| 109 | // We take an array in the first argument. It really has nothing |
---|
| 110 | // to do with $child_type. |
---|
| 111 | if (!isset($child_type[0]['child_type'])) { |
---|
| 112 | // It's not a properly configured array. |
---|
| 113 | return false; |
---|
| 114 | } |
---|
| 115 | foreach ($child_type as $node) { |
---|
| 116 | $serialized_arr[] = $node['child_type'] . '__' . $node['child_id']; |
---|
| 117 | } |
---|
| 118 | return $serialized_arr; |
---|
| 119 | } else { |
---|
| 120 | return $child_type . '__' . $child_id; |
---|
| 121 | } |
---|
| 122 | } |
---|
[42] | 123 | |
---|
[1] | 124 | /** |
---|
| 125 | * Takes a singlar node identifier and returns it as components of an array. |
---|
| 126 | * @param string $node |
---|
| 127 | * @return mixed Array of node type and id on success, false on failure. |
---|
| 128 | */ |
---|
| 129 | function toArrayID(&$node) |
---|
| 130 | { |
---|
| 131 | if (preg_match('/^([[:alnum:]]+)__-?([[:digit:]]+)$/', $node, $node_parts)) { |
---|
| 132 | return array('node_type' => $node_parts[1], 'node_id' => $node_parts[2]); |
---|
| 133 | } else { |
---|
| 134 | App::logMsg(_("Cannot parse node identifier, not formated correctly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 135 | return false; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * Adds a single node. Tests if it already exists first. |
---|
| 141 | * @param string $child_type |
---|
| 142 | * @param string $child_id |
---|
| 143 | * @param string $parents A serialized array of serialized parent identifiers |
---|
| 144 | * @param string $relationship_type |
---|
| 145 | * @return bool true on sucess, false on error. |
---|
| 146 | */ |
---|
| 147 | function insertNode($parents, $child_type=null, $child_id=null, $relationship_type=null, $title='') |
---|
| 148 | { |
---|
| 149 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 150 | if ($this->node_init) { |
---|
| 151 | $child_type =& $this->child_type; |
---|
| 152 | $child_id =& $this->child_id; |
---|
| 153 | } else { |
---|
| 154 | App::logMsg(_("insertNode failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 155 | return false; |
---|
| 156 | } |
---|
| 157 | } |
---|
[42] | 158 | |
---|
[1] | 159 | // Make sure this is not empty and an array, even if it has only one value. |
---|
| 160 | if ('' == $parents) { |
---|
| 161 | App::raiseMsg(sprintf(_("Cannot add node <strong>%s %s</strong>, no parent was specified."), $child_type, $child_id), MSG_ERR, __FILE__, __LINE__); |
---|
| 162 | App::logMsg(sprintf(_("Cannot add node <strong>%s %s</strong>, no parent was specified."), $child_type, $child_id), LOG_ERR, __FILE__, __LINE__); |
---|
| 163 | return false; |
---|
| 164 | } else if (!is_array($parents)) { |
---|
| 165 | $parents = array($parents); |
---|
| 166 | } |
---|
[42] | 167 | |
---|
[1] | 168 | // Remove duplicates. |
---|
| 169 | $parents = array_unique($parents); |
---|
[42] | 170 | |
---|
[1] | 171 | // Test that this node does not already exist and that the new parents |
---|
| 172 | // do exist before we continue. |
---|
| 173 | foreach ($parents as $parent_string) { |
---|
| 174 | $parent = $this->toArrayID($parent_string); |
---|
| 175 | if ($this->nodeExists($child_type, $child_id, $parent['node_type'], $parent['node_id'], $relationship_type)) { |
---|
| 176 | App::raiseMsg(sprintf(_("Cannot add node <strong>%s %s</strong> to parent <strong>%s %s</strong>. It already exists there"), $child_type, $child_id, $parent['node_type'], $parent['node_id']), MSG_ERR, __FILE__, __LINE__); |
---|
| 177 | App::logMsg(sprintf(_("Cannot add node <strong>%s %s</strong> to parent <strong>%s %s</strong>. It already exists there"), $child_type, $child_id, $parent['node_type'], $parent['node_id']), LOG_ERR, __FILE__, __LINE__); |
---|
| 178 | return false; |
---|
| 179 | } |
---|
| 180 | if (!$this->nodeExists($parent['node_type'], $parent['node_id'])) { |
---|
| 181 | App::raiseMsg(sprintf(_("Cannot add node <strong>%s %s</strong> to nonexistent parent <strong>%s %s</strong>."), $child_type, $child_id, $parent['node_type'], $parent['node_id']), MSG_ERR, __FILE__, __LINE__); |
---|
| 182 | App::logMsg(sprintf(_("Cannot add node <strong>%s %s</strong> to nonexistent parent <strong>%s %s</strong>."), $child_type, $child_id, $parent['node_type'], $parent['node_id']), LOG_ERR, __FILE__, __LINE__); |
---|
| 183 | return false; |
---|
[42] | 184 | } |
---|
[1] | 185 | } |
---|
[42] | 186 | |
---|
[1] | 187 | // Insert new nodes with the new parents. |
---|
| 188 | foreach ($parents as $parent_string) { |
---|
| 189 | $parent = $this->toArrayID($parent_string); |
---|
| 190 | DB::query(" |
---|
| 191 | INSERT INTO node_tbl ( |
---|
[42] | 192 | parent_type, |
---|
| 193 | parent_id, |
---|
| 194 | child_type, |
---|
| 195 | child_id, |
---|
[1] | 196 | relationship_type, |
---|
| 197 | title |
---|
| 198 | ) VALUES ( |
---|
[42] | 199 | '" . addslashes($parent['node_type']) . "', |
---|
| 200 | '" . addslashes($parent['node_id']) . "', |
---|
| 201 | '" . addslashes($child_type) . "', |
---|
| 202 | '" . addslashes($child_id) . "', |
---|
[1] | 203 | " . (is_null($relationship_type) ? "NULL" : "'" . addslashes($relationship_type) . "'") . ", |
---|
| 204 | '" . addslashes($title) . "' |
---|
| 205 | ) |
---|
| 206 | "); |
---|
| 207 | App::logMsg(sprintf('insertNode: Added node %s %s with parent %s %s.', $child_type, $child_id, $parent['node_type'], $parent['node_id']), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 208 | } |
---|
| 209 | return true; |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | /** |
---|
| 213 | * Deletes a node using the following algorithm: |
---|
| 214 | * (1) Find each of this node's parents. Place serialized identifiers in array. |
---|
| 215 | * (2) Find all children of this node, and move each to all the parents of this node. |
---|
| 216 | * (3) Delete this node. |
---|
| 217 | * |
---|
| 218 | * @param string $child_type |
---|
| 219 | * @param string $child_id |
---|
| 220 | * |
---|
| 221 | * @return bool false on error, true otherwise. |
---|
| 222 | */ |
---|
| 223 | function deleteNode($child_type=null, $child_id=null) |
---|
| 224 | { |
---|
| 225 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 226 | if ($this->node_init) { |
---|
| 227 | $child_type =& $this->child_type; |
---|
| 228 | $child_id =& $this->child_id; |
---|
| 229 | } else { |
---|
| 230 | App::logMsg(_("deleteNode failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 231 | return false; |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | // Move children to parents of node being deleted. |
---|
| 236 | $new_parents = $this->getParents($child_type, $child_id); |
---|
| 237 | $children = $this->getChildren($child_type, $child_id); |
---|
| 238 | |
---|
| 239 | if ($children && $new_parents) { |
---|
| 240 | foreach ($children as $child) { |
---|
| 241 | $this->moveNode($new_parents, $child['child_type'], $child['child_id']); |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | DB::query(" |
---|
| 246 | DELETE FROM node_tbl |
---|
| 247 | WHERE child_type = '" . addslashes($child_type) . "' |
---|
| 248 | AND child_id = '" . addslashes($child_id) . "' |
---|
| 249 | "); |
---|
| 250 | App::logMsg(sprintf('deleteNode: Deleted node %s %s.', $child_type, $child_id), LOG_DEBUG, __FILE__, __LINE__); |
---|
| 251 | |
---|
| 252 | return true; |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | /** |
---|
| 256 | * Moves a node using the following algorithm: |
---|
| 257 | * (1) Ensure the newly assigned parents exist. |
---|
| 258 | * (2) Delete old nodes with the specified type and id. |
---|
| 259 | * (3) Add the node with new parents. |
---|
| 260 | * |
---|
| 261 | * @param string $child_type |
---|
| 262 | * @param string $child_id |
---|
| 263 | * @param array $new_parents An array of serialized node identifiers |
---|
| 264 | * that the node will belong to. Example: |
---|
| 265 | * $new_parents[0] = 'category__23'; |
---|
| 266 | * @param string $relationship_type |
---|
| 267 | * |
---|
| 268 | * @return bool false on error, true otherwise. |
---|
[42] | 269 | */ |
---|
[1] | 270 | function moveNode($new_parents=null, $child_type=null, $child_id=null, $relationship_type=null, $title='') |
---|
| 271 | { |
---|
| 272 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 273 | if ($this->node_init) { |
---|
| 274 | $child_type =& $this->child_type; |
---|
| 275 | $child_id =& $this->child_id; |
---|
| 276 | } else { |
---|
| 277 | App::logMsg(_("moveNode failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 278 | return false; |
---|
| 279 | } |
---|
| 280 | } |
---|
[42] | 281 | |
---|
[1] | 282 | // Make sure this is not empty and an array, even if it has only one value. |
---|
| 283 | if (empty($new_parents)) { |
---|
| 284 | App::raiseMsg(sprintf(_("Cannot move node <strong>%s %s</strong>, no parent was specified."), $child_type, $child_id), MSG_ERR, __FILE__, __LINE__); |
---|
| 285 | App::logMsg(sprintf(_("Cannot move node <strong>%s %s</strong>, no parent was specified."), $child_type, $child_id), LOG_ERR, __FILE__, __LINE__); |
---|
| 286 | return false; |
---|
| 287 | } else if (!is_array($new_parents)) { |
---|
| 288 | $new_parents = array($new_parents); |
---|
| 289 | } |
---|
[42] | 290 | |
---|
[1] | 291 | // Remove duplicates. |
---|
| 292 | $new_parents = array_unique($new_parents); |
---|
| 293 | |
---|
| 294 | // Test that the new parents exist before we delete the old ones. |
---|
| 295 | foreach ($new_parents as $parent_string) { |
---|
| 296 | $parent = $this->toArrayID($parent_string); |
---|
| 297 | if (!$this->nodeExists($parent['node_type'], $parent['node_id'])) { |
---|
| 298 | App::raiseMsg(sprintf(_("Cannot move node <strong>%s %s</strong> to nonexistent parent <strong>%s %s</strong>."), $child_type, $child_id, $parent['node_type'], $parent['node_id']), MSG_ERR, __FILE__, __LINE__); |
---|
| 299 | App::logMsg(sprintf(_("Cannot move node <strong>%s %s</strong> to nonexistent parent <strong>%s %s</strong>."), $child_type, $child_id, $parent['node_type'], $parent['node_id']), LOG_ERR, __FILE__, __LINE__); |
---|
| 300 | return false; |
---|
[42] | 301 | } |
---|
[1] | 302 | if ($this->isAncestor($child_type, $child_id, $parent['node_type'], $parent['node_id'])) { |
---|
| 303 | App::raiseMsg(sprintf(_("Cannot move node <strong>%s %s</strong> to parent <strong>%s %s</strong> because a node cannot have itself as a parent."), $child_type, $child_id, $parent['node_type'], $parent['node_id']), MSG_ERR, __FILE__, __LINE__); |
---|
| 304 | App::logMsg(sprintf(_("Cannot move node <strong>%s %s</strong> to parent <strong>%s %s</strong> because a node cannot have itself as a parent."), $child_type, $child_id, $parent['node_type'], $parent['node_id']), LOG_ERR, __FILE__, __LINE__); |
---|
| 305 | return false; |
---|
[42] | 306 | } |
---|
[1] | 307 | } |
---|
[42] | 308 | |
---|
[1] | 309 | if (empty($title)) { |
---|
| 310 | // Select the title of the node we are moving, so we can add it again with the same info. |
---|
| 311 | $qid = DB::query(" |
---|
| 312 | SELECT title FROM node_tbl |
---|
| 313 | WHERE child_type = '" . addslashes($child_type) . "' |
---|
| 314 | AND child_id = '" . addslashes($child_id) . "' |
---|
| 315 | AND relationship_type " . (is_null($relationship_type) ? "IS NULL" : "= '" . addslashes($relationship_type) . "'") . " |
---|
| 316 | "); |
---|
| 317 | list($title) = mysql_fetch_row($qid); |
---|
| 318 | } |
---|
[42] | 319 | |
---|
[1] | 320 | // Delete the nodes with the old parents. |
---|
| 321 | DB::query(" |
---|
| 322 | DELETE FROM node_tbl |
---|
| 323 | WHERE child_type = '" . addslashes($child_type) . "' |
---|
| 324 | AND child_id = '" . addslashes($child_id) . "' |
---|
| 325 | AND relationship_type " . (is_null($relationship_type) ? "IS NULL" : "= '" . addslashes($relationship_type) . "'") . " |
---|
| 326 | "); |
---|
| 327 | App::logMsg(sprintf('moveNode: Deleted node %s %s.', $child_type, $child_id), LOG_DEBUG, __FILE__, __LINE__); |
---|
[42] | 328 | |
---|
[1] | 329 | // Insert new nodes with the new parents. |
---|
| 330 | $this->insertNode($new_parents, $child_type, $child_id, $relationship_type, $title); |
---|
[42] | 331 | |
---|
[1] | 332 | return true; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | /** |
---|
| 336 | * Returns an array of all the parents of the current node (just the ones |
---|
| 337 | * immediatly above this node). You may need to call array_unique if you |
---|
| 338 | * don't want duplicate nodes returned. |
---|
| 339 | * |
---|
| 340 | * @param string $child_type |
---|
| 341 | * @param string $child_id |
---|
| 342 | * @return string The parents as an array of serialized node identifiers. |
---|
| 343 | */ |
---|
| 344 | function getParents($child_type=null, $child_id=null) |
---|
| 345 | { |
---|
| 346 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 347 | if ($this->node_init) { |
---|
| 348 | $child_type =& $this->child_type; |
---|
| 349 | $child_id =& $this->child_id; |
---|
| 350 | } else { |
---|
| 351 | App::logMsg(_("getParents failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 352 | return false; |
---|
| 353 | } |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | $qid = DB::query(" |
---|
| 357 | SELECT parent_type, parent_id |
---|
[42] | 358 | FROM node_tbl |
---|
[1] | 359 | WHERE child_type = '" . addslashes($child_type) . "' |
---|
[42] | 360 | AND child_id = '" . addslashes($child_id) . "' |
---|
[1] | 361 | "); |
---|
[42] | 362 | |
---|
[1] | 363 | $parents = array(); |
---|
| 364 | while ($row = mysql_fetch_assoc($qid)) { |
---|
| 365 | $parents[] = $this->toStringID($row['parent_type'], $row['parent_id']); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | if (sizeof($parents) > 0) { |
---|
| 369 | return $parents; |
---|
| 370 | } else { |
---|
| 371 | return false; |
---|
| 372 | } |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | /** |
---|
| 376 | * Returns details of specified node. |
---|
| 377 | * |
---|
| 378 | * @param string $child_type |
---|
| 379 | * @param string $child_id |
---|
| 380 | * @return array type, id, title, subnode_quantity. |
---|
| 381 | */ |
---|
| 382 | function getNode($child_type=null, $child_id=null) |
---|
| 383 | { |
---|
| 384 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 385 | if ($this->node_init) { |
---|
| 386 | $child_type =& $this->child_type; |
---|
| 387 | $child_id =& $this->child_id; |
---|
| 388 | } else { |
---|
| 389 | App::logMsg(_("getNode failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 390 | return false; |
---|
| 391 | } |
---|
| 392 | } |
---|
[42] | 393 | |
---|
[1] | 394 | $qid = DB::query(" |
---|
| 395 | SELECT child_type, child_id, title, subnode_quantity |
---|
[42] | 396 | FROM node_tbl |
---|
[1] | 397 | WHERE child_type = '" . addslashes($child_type) . "' |
---|
| 398 | AND child_id = '" . addslashes($child_id) . "' |
---|
| 399 | "); |
---|
[42] | 400 | |
---|
[1] | 401 | $children = array(); |
---|
| 402 | while ($row = mysql_fetch_assoc($qid)) { |
---|
| 403 | $children[] = $row; |
---|
| 404 | } |
---|
| 405 | if (sizeof($children) > 0) { |
---|
| 406 | return $children; |
---|
| 407 | } else { |
---|
| 408 | return false; |
---|
| 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | /** |
---|
| 413 | * Returns an array of all the children of the current node (just the ones |
---|
| 414 | * immediatly below this node). You may need to call array_unique if you |
---|
| 415 | * don't want duplicate nodes returned. |
---|
| 416 | * |
---|
| 417 | * @param string $child_type |
---|
| 418 | * @param string $child_id |
---|
| 419 | * @param string $type_constraint An array of node types to restrict the search to. |
---|
| 420 | * |
---|
| 421 | * @return string The children as an array of serialized node identifiers. |
---|
| 422 | */ |
---|
| 423 | function getChildren($child_type=null, $child_id=null, $type_constraint=null, $order='') |
---|
| 424 | { |
---|
| 425 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 426 | if ($this->node_init) { |
---|
| 427 | $child_type =& $this->child_type; |
---|
| 428 | $child_id =& $this->child_id; |
---|
| 429 | } else { |
---|
| 430 | App::logMsg(_("getChildren failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 431 | return false; |
---|
| 432 | } |
---|
| 433 | } |
---|
[42] | 434 | |
---|
[1] | 435 | $in_clause = ''; |
---|
| 436 | if (isset($type_constraint)) { |
---|
| 437 | if (!is_array($type_constraint)) { |
---|
| 438 | $type_constraint = array($type_constraint); |
---|
| 439 | } |
---|
| 440 | $in_clause = "AND child_type IN ('" . join("','", array_map('addslashes', $type_constraint)) . "')"; |
---|
| 441 | } |
---|
[42] | 442 | |
---|
[1] | 443 | $qid = DB::query(" |
---|
| 444 | SELECT * |
---|
[42] | 445 | FROM node_tbl |
---|
[1] | 446 | WHERE parent_type = '" . addslashes($child_type) . "' |
---|
| 447 | AND parent_id = '" . addslashes($child_id) . "' |
---|
| 448 | $in_clause |
---|
| 449 | " . addslashes($order) . " |
---|
| 450 | "); |
---|
[42] | 451 | |
---|
[1] | 452 | $children = array(); |
---|
| 453 | while ($row = mysql_fetch_assoc($qid)) { |
---|
| 454 | $children[] = $row; |
---|
| 455 | } |
---|
| 456 | if (sizeof($children) > 0) { |
---|
| 457 | return $children; |
---|
| 458 | } else { |
---|
| 459 | return false; |
---|
| 460 | } |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | /** |
---|
| 464 | * Give the number of children a category has. We are talking about the |
---|
| 465 | * direct children, on the next level. |
---|
[42] | 466 | * |
---|
[1] | 467 | * @param string optional $parent The name of the parent from where we begin. |
---|
| 468 | * @param string $type_constraint An array of node types to restrict the search to. |
---|
| 469 | * |
---|
| 470 | * @return integer |
---|
| 471 | */ |
---|
| 472 | function getNumberChildren($child_type=null, $child_id=null, $type_constraint=null) |
---|
| 473 | { |
---|
| 474 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 475 | if ($this->node_init) { |
---|
| 476 | $child_type =& $this->child_type; |
---|
| 477 | $child_id =& $this->child_id; |
---|
| 478 | } else { |
---|
| 479 | App::logMsg(_("getNumberChildren failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 480 | return false; |
---|
| 481 | } |
---|
| 482 | } |
---|
[42] | 483 | |
---|
[1] | 484 | $in_clause = ''; |
---|
| 485 | if (isset($type_constraint)) { |
---|
| 486 | if (!is_array($type_constraint)) { |
---|
| 487 | $type_constraint = array($type_constraint); |
---|
| 488 | } |
---|
| 489 | $in_clause = "AND child_type IN ('" . join("','", array_map('addslashes', $type_constraint)) . "')"; |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | $qid = DB::query(" |
---|
| 493 | SELECT COUNT(*) |
---|
[42] | 494 | FROM node_tbl |
---|
[1] | 495 | WHERE parent_type = '" . addslashes($child_type) . "' |
---|
| 496 | AND parent_id = '" . addslashes($child_id) . "' |
---|
| 497 | $in_clause |
---|
| 498 | "); |
---|
| 499 | list($num_children) = mysql_fetch_row($qid); |
---|
| 500 | |
---|
| 501 | return $num_children; |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | /** |
---|
| 505 | * Tests of the specified node is a leaf (i.e. it has no children). |
---|
| 506 | * @param string $child_type |
---|
| 507 | * @param string $child_id |
---|
| 508 | * @return bool true if a leaf, or false if not or an error |
---|
| 509 | */ |
---|
| 510 | function isLeaf($child_type=null, $child_id=null) |
---|
| 511 | { |
---|
| 512 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 513 | if ($this->node_init) { |
---|
| 514 | $child_type =& $this->child_type; |
---|
| 515 | $child_id =& $this->child_id; |
---|
| 516 | } else { |
---|
| 517 | App::logMsg(_("isLeaf failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 518 | return false; |
---|
| 519 | } |
---|
| 520 | } |
---|
[42] | 521 | |
---|
[1] | 522 | if ($this->getNumberChildren($child_type, $child_id) <= 0) { |
---|
| 523 | return true; |
---|
| 524 | } else { |
---|
| 525 | return false; |
---|
| 526 | } |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | /** |
---|
| 530 | * This function will tell you if a specified node (the child) is actually |
---|
| 531 | * an ancestor to another node that perhaps is considered to be a parent. If |
---|
| 532 | * the specified node IS an ancestor of a node made into it's parent, we would |
---|
| 533 | * have a circular reference that would cause an infinite loop with any |
---|
| 534 | * recursive queries of the heirarchy. |
---|
| 535 | * @param string $child_type |
---|
| 536 | * @param string $child_id |
---|
| 537 | * @param string $considered_parent_type |
---|
| 538 | * @param string $considered_parent_id |
---|
| 539 | * @return bool true if the child is an ancestor to the considered |
---|
| 540 | * parent, or false otherwise, or in case of failure. |
---|
| 541 | */ |
---|
| 542 | function isAncestor($child_type, $child_id, $considered_parent_type, $considered_parent_id) |
---|
| 543 | { |
---|
| 544 | $family_tree = $this->getAllAncestors($considered_parent_type, $considered_parent_id); |
---|
| 545 | $family_tree = $this->toStringID($family_tree, -1); |
---|
[42] | 546 | |
---|
[1] | 547 | if (in_array($this->toStringID($child_type, $child_id), $family_tree)) { |
---|
| 548 | return true; |
---|
| 549 | } else { |
---|
| 550 | return false; |
---|
| 551 | } |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | /** |
---|
| 555 | * This function builds an array of all the parents starting from a |
---|
| 556 | * specified node, up to the root of the tree. It will follow paths for |
---|
| 557 | * nodes with multiple parents, branching off and filling the array with |
---|
| 558 | * ALL ancestors to the specified node. I'm not sure what the order will be |
---|
| 559 | * but that probably isn't useful anyways. I use this to prevent circular |
---|
| 560 | * references in the heirarchy. |
---|
| 561 | * @param string $child_type |
---|
| 562 | * @param string $child_id |
---|
| 563 | * @param bool $go_linear ? |
---|
| 564 | * @param int $_return_flag An internal value that counts up as |
---|
| 565 | * recursion progesses. When the value |
---|
| 566 | * drops back to 0, we return the output. |
---|
| 567 | * @return array Array of serialized node identifiers. |
---|
| 568 | */ |
---|
| 569 | function getAllAncestors($child_type, $child_id, $go_linear=false, $_return_flag=true) |
---|
| 570 | { |
---|
| 571 | static $output = array(); |
---|
| 572 | static $return_flag; |
---|
[42] | 573 | |
---|
[1] | 574 | $qid = DB::query(" |
---|
| 575 | SELECT parent_type, parent_id, child_type, child_id, title, subnode_quantity |
---|
| 576 | FROM node_tbl |
---|
| 577 | WHERE child_type = '" . addslashes($child_type) . "' |
---|
| 578 | AND child_id = '" . addslashes($child_id) . "' |
---|
| 579 | "); |
---|
| 580 | while ($row = mysql_fetch_assoc($qid)) { |
---|
| 581 | // Preventing circular references. |
---|
| 582 | if ($row['parent_type'] == $child_type && $row['parent_id'] == $child_id) { |
---|
| 583 | continue; |
---|
| 584 | } |
---|
[42] | 585 | |
---|
[1] | 586 | // Build a linear path to root...no wormholes. |
---|
| 587 | if ($enough_already && $go_linear) { |
---|
| 588 | continue; |
---|
| 589 | } |
---|
| 590 | $enough_already = true; |
---|
[42] | 591 | |
---|
[1] | 592 | // To prevent duplicates, only add the new found node |
---|
| 593 | // if not already in the array of ancestors. |
---|
| 594 | $curr_items = sizeof($output) > 0 ? $this->toStringID($output, -1) : array(); |
---|
| 595 | if (!in_array($this->toStringID($row['child_type'], $row['child_id']), $curr_items)) { |
---|
| 596 | $output[] = $row; |
---|
| 597 | } |
---|
[42] | 598 | |
---|
[1] | 599 | $this->getAllAncestors($row['parent_type'], $row['parent_id'], $go_linear, false); |
---|
| 600 | } |
---|
| 601 | if ($_return_flag) { |
---|
| 602 | // We must reset the static $output variable so that it does |
---|
| 603 | // not fill up during subsequent function calls. |
---|
| 604 | $ret = $output; |
---|
| 605 | $output = array(); |
---|
| 606 | return $ret; |
---|
| 607 | } |
---|
| 608 | } |
---|
| 609 | |
---|
| 610 | /** |
---|
| 611 | * Tests if the specified node exists. If only the child type and id is |
---|
| 612 | * specified, check if any node exists like that. If the parent type and id |
---|
| 613 | * is also provided, test for an exact node match. |
---|
| 614 | * @param string $child_type |
---|
| 615 | * @param string $child_id |
---|
| 616 | * @param string $parent_type |
---|
| 617 | * @param string $parent_id |
---|
| 618 | * @param string $relationship_type |
---|
| 619 | * @return bool true if a leaf, or false if not or an error |
---|
| 620 | */ |
---|
| 621 | function nodeExists($child_type=null, $child_id=null, $parent_type=null, $parent_id=null, $relationship_type=null) |
---|
| 622 | { |
---|
| 623 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 624 | if ($this->node_init) { |
---|
| 625 | $child_type =& $this->child_type; |
---|
| 626 | $child_id =& $this->child_id; |
---|
| 627 | } else { |
---|
| 628 | App::logMsg(_("nodeExists failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 629 | return false; |
---|
| 630 | } |
---|
| 631 | } |
---|
[42] | 632 | |
---|
[1] | 633 | if (isset($parent_type) && isset($parent_id)) { |
---|
| 634 | $qid = DB::query(" |
---|
| 635 | SELECT 1 FROM node_tbl |
---|
| 636 | WHERE parent_type = '" . addslashes($parent_type) . "' |
---|
| 637 | AND parent_id = '" . addslashes($parent_id) . "' |
---|
| 638 | AND child_type = '" . addslashes($child_type) . "' |
---|
| 639 | AND child_id = '" . addslashes($child_id) . "' |
---|
| 640 | AND relationship_type " . (is_null($relationship_type) ? "IS NULL" : "= '" . addslashes($relationship_type) . "'") . " |
---|
| 641 | "); |
---|
| 642 | } else { |
---|
| 643 | $qid = DB::query(" |
---|
| 644 | SELECT 1 FROM node_tbl |
---|
| 645 | WHERE child_type = '" . addslashes($child_type) . "' |
---|
| 646 | AND child_id = '" . addslashes($child_id) . "' |
---|
| 647 | "); |
---|
| 648 | } |
---|
| 649 | if (mysql_num_rows($qid) > 0) { |
---|
| 650 | return true; |
---|
| 651 | } else { |
---|
| 652 | return false; |
---|
| 653 | } |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | /** |
---|
| 657 | * Recursively go through the node tree, starting at a specified node, and |
---|
| 658 | * drill down, filling an array with info about the nodes: |
---|
| 659 | * |
---|
| 660 | * @param array $preselected |
---|
| 661 | * @param string $child_type |
---|
| 662 | * @param string $child_id |
---|
| 663 | * @param string $type_constraint An array of node types to restrict the search to. |
---|
| 664 | * @param bool $include_curr Do we include the specified node in the list? |
---|
| 665 | * @param string $order SQL to append to the query of the getChildren |
---|
| 666 | * call. Ex: 'ORDER BY child_id DESC' |
---|
[42] | 667 | * @return array Details of from the node table of all nodes below the |
---|
[1] | 668 | * specified node: (type, id, title, indent level, selected status) |
---|
| 669 | */ |
---|
| 670 | function &getNodeList($preselected=null, $child_type=null, $child_id=null, $type_constraint=null, $include_curr=false, $order='', $_indent=0, $_return_flag=true) |
---|
| 671 | { |
---|
| 672 | static $output = array(); |
---|
| 673 | static $is_a_leaf = array(); |
---|
[42] | 674 | |
---|
[1] | 675 | if (!isset($child_type) || !isset($child_id)) { |
---|
| 676 | if ($this->node_init) { |
---|
| 677 | $child_type =& $this->child_type; |
---|
| 678 | $child_id =& $this->child_id; |
---|
| 679 | } else { |
---|
| 680 | App::logMsg(_("getNodeList failed. Arguments not specified properly."), LOG_ERR, __FILE__, __LINE__); |
---|
| 681 | return false; |
---|
| 682 | } |
---|
| 683 | } |
---|
[42] | 684 | |
---|
[1] | 685 | if (!is_array($preselected)) { |
---|
| 686 | $preselected = array($preselected); |
---|
| 687 | } |
---|
[42] | 688 | |
---|
[1] | 689 | if ($_return_flag && $include_curr) { |
---|
| 690 | $my_children = $this->getNode($child_type, $child_id); |
---|
| 691 | } else { |
---|
| 692 | $my_children = $this->getChildren($child_type, $child_id, $type_constraint, $order); |
---|
| 693 | } |
---|
| 694 | |
---|
| 695 | if ($my_children) { |
---|
| 696 | $num_children = sizeof($my_children); |
---|
| 697 | for ($i=0; $i<$num_children; $i++) { |
---|
| 698 | |
---|
| 699 | $row = $my_children[$i]; |
---|
| 700 | // Preventing circular references. |
---|
| 701 | if ($row['child_type'] == $child_type && $row['child_id'] == $child_id) { |
---|
| 702 | App::logMsg(sprintf(_("Circular reference detected: %s has itself as a parent."), $this->toStringID($row['child_type'], $row['child_id'])), LOG_ERR, __FILE__, __LINE__); |
---|
| 703 | continue; |
---|
| 704 | } |
---|
| 705 | $row['indent'] = $_indent; |
---|
| 706 | if (in_array($this->toStringID($row['child_type'], $row['child_id']), $preselected)) { |
---|
| 707 | $row['selected'] = true; |
---|
| 708 | } |
---|
| 709 | $output[] = $row; |
---|
| 710 | unset($row); |
---|
[42] | 711 | |
---|
[1] | 712 | // This is so we test if each node is a string only once. We store the result in the is_a_leaf array statically. |
---|
| 713 | if (!isset($is_a_leaf[$this->toStringID($my_children[$i]['child_type'], $my_children[$i]['child_id'])])) { |
---|
| 714 | $is_a_leaf[$this->toStringID($my_children[$i]['child_type'], $my_children[$i]['child_id'])] = $this->isLeaf($my_children[$i]['child_type'], $my_children[$i]['child_id']); |
---|
| 715 | } |
---|
| 716 | if (!$is_a_leaf[$this->toStringID($my_children[$i]['child_type'], $my_children[$i]['child_id'])]) { |
---|
| 717 | // If this node is not a leaf, we dive into it recursivly. |
---|
| 718 | $this->getNodeList($preselected, $my_children[$i]['child_type'], $my_children[$i]['child_id'], $type_constraint, $include_curr, $order, $_indent+1, false); |
---|
| 719 | } |
---|
| 720 | } |
---|
| 721 | } |
---|
| 722 | |
---|
| 723 | if ($_return_flag) { |
---|
| 724 | // We must reset the static variables so that they do |
---|
| 725 | // not fill up during subsequent function calls. |
---|
| 726 | $ret = $output; |
---|
| 727 | $output = array(); |
---|
| 728 | $is_a_leaf = array(); |
---|
| 729 | return $ret; |
---|
| 730 | } |
---|
| 731 | |
---|
| 732 | } |
---|
[42] | 733 | |
---|
| 734 | |
---|
[1] | 735 | /** |
---|
| 736 | * Counts the number of items linked to each parent node |
---|
| 737 | * putting the result in the relevant category counters. This function is |
---|
| 738 | * called each time a product, article, etc. is inserted, updated, or deleted. |
---|
| 739 | * |
---|
| 740 | * @param string $type_constraint An array of node types to restrict the search to. |
---|
| 741 | */ |
---|
| 742 | function rebuildSubnodeQty($type_constraint=null) |
---|
| 743 | { |
---|
| 744 | // Reset all the category counters to zero. |
---|
| 745 | DB::query("UPDATE node_tbl SET subnode_quantity = 0"); |
---|
[42] | 746 | |
---|
[1] | 747 | // Get all the nodes. |
---|
| 748 | $qid = DB::query("SELECT DISTINCT child_type, child_id FROM node_tbl"); |
---|
[42] | 749 | |
---|
[1] | 750 | // For each node count the number of children... |
---|
| 751 | while (list($child_type, $child_id) = mysql_fetch_row($qid)) { |
---|
| 752 | $num_children = $this->getNumberChildren($child_type, $child_id, $type_constraint); |
---|
| 753 | // ...and add that number to the node counter for that object and all parents. |
---|
| 754 | if ($num_children > 0) { |
---|
| 755 | $this->setSubnodeQtyToParents($child_type, $child_id, $num_children); |
---|
| 756 | } |
---|
| 757 | } |
---|
| 758 | } |
---|
[42] | 759 | |
---|
[1] | 760 | /** |
---|
| 761 | * Used internally by setSubnodeQty to add the quantity of subnodes to |
---|
| 762 | * all parents recursivly. |
---|
| 763 | */ |
---|
| 764 | function setSubnodeQtyToParents($child_type, $child_id, $num_children) |
---|
| 765 | { |
---|
| 766 | DB::query(" |
---|
| 767 | UPDATE node_tbl |
---|
| 768 | SET subnode_quantity = subnode_quantity + '" . addslashes($num_children) . "' |
---|
| 769 | WHERE child_type = '" . addslashes($child_type) . "' |
---|
| 770 | AND child_id = '" . addslashes($child_id) . "' |
---|
| 771 | ",false); |
---|
| 772 | $qid = DB::query(" |
---|
| 773 | SELECT parent_type, parent_id |
---|
[42] | 774 | FROM node_tbl |
---|
[1] | 775 | WHERE child_type = '" . addslashes($child_type) . "' |
---|
[42] | 776 | AND child_id = '" . addslashes($child_id) . "' |
---|
[1] | 777 | ",false); |
---|
| 778 | while ((list($parent_type, $parent_id) = mysql_fetch_row($qid)) && $parent_id > 0) { |
---|
| 779 | $this->setSubnodeQtyToParents($parent_type, $parent_id, $num_children); |
---|
| 780 | } |
---|
| 781 | } |
---|
| 782 | |
---|
| 783 | // THE END |
---|
| 784 | } |
---|
| 785 | |
---|
| 786 | |
---|
| 787 | |
---|
| 788 | |
---|
| 789 | ?> |
---|