2007-01-15 23:12:22 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-15 23:12:22 +03:00
|
|
|
/**
|
|
|
|
* Doctrine_Tree_NestedSet
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @subpackage Tree
|
2007-01-15 23:12:22 +03:00
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2007-01-15 23:12:22 +03:00
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
* @author Joe Simms <joe.simms@websites4.com>
|
|
|
|
*/
|
|
|
|
class Doctrine_Tree_NestedSet extends Doctrine_Tree implements Doctrine_Tree_Interface
|
|
|
|
{
|
2007-07-08 16:57:52 +04:00
|
|
|
private $_baseQuery;
|
2007-08-04 18:33:15 +04:00
|
|
|
private $_baseAlias = "base";
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-15 23:12:22 +03:00
|
|
|
/**
|
|
|
|
* constructor, creates tree with reference to table and sets default root options
|
|
|
|
*
|
|
|
|
* @param object $table instance of Doctrine_Table
|
|
|
|
* @param array $options options
|
|
|
|
*/
|
2008-02-04 00:29:57 +03:00
|
|
|
public function __construct($table, $options)
|
2007-01-15 23:12:22 +03:00
|
|
|
{
|
|
|
|
// set default many root attributes
|
2007-02-08 15:53:32 +03:00
|
|
|
$options['hasManyRoots'] = isset($options['hasManyRoots']) ? $options['hasManyRoots'] : false;
|
2007-11-08 21:50:20 +03:00
|
|
|
if ($options['hasManyRoots']) {
|
2007-02-08 15:53:32 +03:00
|
|
|
$options['rootColumnName'] = isset($options['rootColumnName']) ? $options['rootColumnName'] : 'root_id';
|
2007-11-08 21:50:20 +03:00
|
|
|
}
|
|
|
|
|
2007-01-15 23:12:22 +03:00
|
|
|
parent::__construct($table, $options);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-01-15 23:12:22 +03:00
|
|
|
/**
|
|
|
|
* used to define table attributes required for the NestetSet implementation
|
|
|
|
* adds lft and rgt columns for corresponding left and right values
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function setTableDefinition()
|
|
|
|
{
|
2007-01-21 21:31:51 +03:00
|
|
|
if ($root = $this->getAttribute('rootColumnName')) {
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->table->setColumn($root, 'integer', 4);
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->table->setColumn('lft', 'integer', 4);
|
|
|
|
$this->table->setColumn('rgt', 'integer', 4);
|
|
|
|
$this->table->setColumn('level', 'integer', 2);
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates root node from given record or from a new record
|
|
|
|
*
|
2008-05-14 01:20:34 +04:00
|
|
|
* @param object $record instance of Doctrine_Entity
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
2008-05-14 01:20:34 +04:00
|
|
|
public function createRoot(Doctrine_Entity $record = null)
|
2007-01-15 23:12:22 +03:00
|
|
|
{
|
|
|
|
if ( ! $record) {
|
|
|
|
$record = $this->table->create();
|
|
|
|
}
|
|
|
|
|
2007-05-07 20:05:14 +04:00
|
|
|
// if tree is many roots, and no root id has been set, then get next root id
|
|
|
|
if ($root = $this->getAttribute('hasManyRoots') && $record->getNode()->getRootValue() <= 0) {
|
2007-01-15 23:12:22 +03:00
|
|
|
$record->getNode()->setRootValue($this->getNextRootId());
|
|
|
|
}
|
|
|
|
|
|
|
|
$record->set('lft', '1');
|
|
|
|
$record->set('rgt', '2');
|
2007-07-08 16:57:52 +04:00
|
|
|
$record->set('level', 0);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
$record->save();
|
|
|
|
|
|
|
|
return $record;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns root node
|
|
|
|
*
|
2008-05-14 01:20:34 +04:00
|
|
|
* @return object $record instance of Doctrine_Entity
|
2007-07-08 16:57:52 +04:00
|
|
|
* @deprecated Use fetchRoot()
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function findRoot($rootId = 1)
|
|
|
|
{
|
2007-07-08 16:57:52 +04:00
|
|
|
return $this->fetchRoot($rootId);
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
/**
|
|
|
|
* Fetches a/the root node.
|
|
|
|
*
|
|
|
|
* @param integer $rootId
|
|
|
|
*/
|
|
|
|
public function fetchRoot($rootId = 1)
|
|
|
|
{
|
|
|
|
$q = $this->getBaseQuery();
|
2007-08-04 18:33:15 +04:00
|
|
|
$q = $q->addWhere($this->_baseAlias . '.lft = ?', 1);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
// if tree has many roots, then specify root id
|
|
|
|
$q = $this->returnQueryWithRootId($q, $rootId);
|
2007-07-08 16:57:52 +04:00
|
|
|
$data = $q->execute();
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
if (count($data) <= 0) {
|
|
|
|
return false;
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($data instanceof Doctrine_Collection) {
|
|
|
|
$root = $data->getFirst();
|
|
|
|
$root['level'] = 0;
|
|
|
|
} else if (is_array($data)) {
|
|
|
|
$root = array_shift($data);
|
|
|
|
$root['level'] = 0;
|
|
|
|
} else {
|
|
|
|
throw new Doctrine_Tree_Exception("Unexpected data structure returned.");
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
return $root;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-07-08 16:57:52 +04:00
|
|
|
* Fetches a tree.
|
2007-01-15 23:12:22 +03:00
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @param array $options Options
|
|
|
|
* @return mixed The tree or FALSE if the tree could not be found.
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function fetchTree($options = array())
|
|
|
|
{
|
|
|
|
// fetch tree
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->getBaseQuery();
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-08-04 18:33:15 +04:00
|
|
|
$q = $q->addWhere($this->_baseAlias . ".lft >= ?", 1);
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
// if tree has many roots, then specify root id
|
|
|
|
$rootId = isset($options['root_id']) ? $options['root_id'] : '1';
|
2007-07-08 16:57:52 +04:00
|
|
|
if (is_array($rootId)) {
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addOrderBy($this->_baseAlias . "." . $this->getAttribute('rootColumnName') .
|
|
|
|
", " . $this->_baseAlias . ".lft ASC");
|
2007-07-08 16:57:52 +04:00
|
|
|
} else {
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addOrderBy($this->_baseAlias . ".lft ASC");
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->returnQueryWithRootId($q, $rootId);
|
2007-01-15 23:12:22 +03:00
|
|
|
$tree = $q->execute();
|
2007-07-08 16:57:52 +04:00
|
|
|
|
|
|
|
if (count($tree) <= 0) {
|
|
|
|
return false;
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
2007-07-08 16:57:52 +04:00
|
|
|
|
|
|
|
return $tree;
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-07-08 16:57:52 +04:00
|
|
|
* Fetches a branch of a tree.
|
2007-01-15 23:12:22 +03:00
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @param mixed $pk primary key as used by table::find() to locate node to traverse tree from
|
|
|
|
* @param array $options Options.
|
|
|
|
* @return mixed The branch or FALSE if the branch could not be found.
|
|
|
|
* @todo Only fetch the lft and rgt values of the initial record. more is not needed.
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function fetchBranch($pk, $options = array())
|
|
|
|
{
|
|
|
|
$record = $this->table->find($pk);
|
2008-05-14 01:20:34 +04:00
|
|
|
if ( ! ($record instanceof Doctrine_Entity) || !$record->exists()) {
|
2007-06-07 23:17:56 +04:00
|
|
|
// TODO: if record doesn't exist, throw exception or similar?
|
|
|
|
return false;
|
|
|
|
}
|
2007-07-08 16:57:52 +04:00
|
|
|
//$depth = isset($options['depth']) ? $options['depth'] : null;
|
2007-06-07 23:17:56 +04:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->getBaseQuery();
|
|
|
|
$params = array($record->get('lft'), $record->get('rgt'));
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->addWhere($this->_baseAlias . ".lft >= ? AND " . $this->_baseAlias . ".rgt <= ?", $params)
|
|
|
|
->addOrderBy($this->_baseAlias . ".lft asc");
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->returnQueryWithRootId($q, $record->getNode()->getRootValue());
|
|
|
|
return $q->execute();
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-07-08 16:57:52 +04:00
|
|
|
* Fetches all root nodes. If the tree has only one root this is the same as
|
|
|
|
* fetchRoot().
|
2007-01-15 23:12:22 +03:00
|
|
|
*
|
2007-07-08 16:57:52 +04:00
|
|
|
* @return mixed The root nodes.
|
2007-01-15 23:12:22 +03:00
|
|
|
*/
|
|
|
|
public function fetchRoots()
|
|
|
|
{
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = $this->getBaseQuery();
|
2007-08-04 18:33:15 +04:00
|
|
|
$q = $q->addWhere($this->_baseAlias . '.lft = ?', 1);
|
|
|
|
return $q->execute();
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* calculates the next available root id
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getNextRootId()
|
|
|
|
{
|
|
|
|
return $this->getMaxRootId() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* calculates the current max root id
|
|
|
|
*
|
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
public function getMaxRootId()
|
|
|
|
{
|
|
|
|
$component = $this->table->getComponentName();
|
2007-01-21 21:31:51 +03:00
|
|
|
$column = $this->getAttribute('rootColumnName');
|
2007-01-15 23:12:22 +03:00
|
|
|
|
|
|
|
// cannot get this dql to work, cannot retrieve result using $coll[0]->max
|
|
|
|
//$dql = "SELECT MAX(c.$column) FROM $component c";
|
|
|
|
|
|
|
|
$dql = 'SELECT c.' . $column . ' FROM ' . $component . ' c ORDER BY c.' . $column . ' DESC LIMIT 1';
|
|
|
|
|
|
|
|
$coll = $this->table->getConnection()->query($dql);
|
|
|
|
|
|
|
|
$max = $coll[0]->get($column);
|
|
|
|
|
|
|
|
$max = !is_null($max) ? $max : 0;
|
|
|
|
|
|
|
|
return $max;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns parsed query with root id where clause added if applicable
|
|
|
|
*
|
|
|
|
* @param object $query Doctrine_Query
|
|
|
|
* @param integer $root_id id of destination root
|
|
|
|
* @return object Doctrine_Query
|
2007-01-21 21:31:51 +03:00
|
|
|
*/
|
2007-01-15 23:12:22 +03:00
|
|
|
public function returnQueryWithRootId($query, $rootId = 1)
|
|
|
|
{
|
2007-01-21 21:31:51 +03:00
|
|
|
if ($root = $this->getAttribute('rootColumnName')) {
|
2007-07-08 16:57:52 +04:00
|
|
|
if (is_array($rootId)) {
|
|
|
|
$query->addWhere($root . ' IN (' . implode(',', array_fill(0, count($rootId), '?')) . ')',
|
|
|
|
$rootId);
|
|
|
|
} else {
|
|
|
|
$query->addWhere($root . ' = ?', $rootId);
|
|
|
|
}
|
2007-01-15 23:12:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @return unknown
|
|
|
|
*/
|
|
|
|
public function getBaseQuery()
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! isset($this->_baseQuery)) {
|
2007-07-08 16:57:52 +04:00
|
|
|
$this->_baseQuery = $this->_createBaseQuery();
|
|
|
|
}
|
2007-08-14 20:37:48 +04:00
|
|
|
return $this->_baseQuery->copy();
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-08-04 18:33:15 +04:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function getBaseAlias()
|
|
|
|
{
|
|
|
|
return $this->_baseAlias;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
private function _createBaseQuery()
|
|
|
|
{
|
2007-08-04 18:33:15 +04:00
|
|
|
$this->_baseAlias = "base";
|
2007-07-08 16:57:52 +04:00
|
|
|
$q = new Doctrine_Query();
|
2007-08-04 18:33:15 +04:00
|
|
|
$q->select($this->_baseAlias . ".*")->from($this->getBaseComponent() . " " . $this->_baseAlias);
|
2007-07-08 16:57:52 +04:00
|
|
|
return $q;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
* @param Doctrine_Query $query
|
|
|
|
*/
|
|
|
|
public function setBaseQuery(Doctrine_Query $query)
|
|
|
|
{
|
2007-08-04 18:33:15 +04:00
|
|
|
$this->_baseAlias = $query->getRootAlias();
|
|
|
|
$query->addSelect($this->_baseAlias . ".lft, " . $this->_baseAlias . ".rgt, ". $this->_baseAlias . ".level");
|
2007-07-08 16:57:52 +04:00
|
|
|
if ($this->getAttribute('rootColumnName')) {
|
2007-08-04 18:33:15 +04:00
|
|
|
$query->addSelect($this->_baseAlias . "." . $this->getAttribute('rootColumnName'));
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
|
|
|
$this->_baseQuery = $query;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-07-08 16:57:52 +04:00
|
|
|
/**
|
|
|
|
* Enter description here...
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function resetBaseQuery()
|
|
|
|
{
|
2007-11-13 18:52:45 +03:00
|
|
|
$this->_baseQuery = $this->_createBaseQuery();
|
2007-07-08 16:57:52 +04:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-10-04 01:43:22 +04:00
|
|
|
}
|