2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2011-10-19 00:32:52 +02:00
|
|
|
* DataObjects that use the Hierarchy extension can be be organised as a hierarchy, with children and parents.
|
2007-07-19 12:40:28 +02:00
|
|
|
* The most obvious example of this is SiteTree.
|
2008-02-25 03:10:37 +01:00
|
|
|
* @package sapphire
|
|
|
|
* @subpackage model
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2011-04-15 11:35:30 +02:00
|
|
|
class Hierarchy extends DataExtension {
|
2009-10-26 21:56:54 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $markedNodes;
|
2009-10-26 21:56:54 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $markingFilter;
|
|
|
|
|
2009-10-26 21:56:54 +01:00
|
|
|
/**
|
|
|
|
* @var Int
|
|
|
|
*/
|
|
|
|
protected $_cache_numChildren;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
function augmentSQL(SQLQuery &$query) {
|
|
|
|
}
|
|
|
|
|
|
|
|
function augmentDatabase() {
|
|
|
|
}
|
|
|
|
|
|
|
|
function augmentWrite(&$manipulation) {
|
|
|
|
}
|
2010-05-27 23:29:48 +02:00
|
|
|
|
2011-10-30 23:17:37 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param string $extension
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function extraStatics($class=null, $extension=null) {
|
2010-05-27 23:29:48 +02:00
|
|
|
return array(
|
|
|
|
'has_one' => array(
|
|
|
|
// TODO this method is called *both* statically and on an instance
|
|
|
|
"Parent" => ($class) ? $class : $this->owner->class
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the children of this DataObject as an XHTML UL. This will be called recursively on each child,
|
|
|
|
* so if they have children they will be displayed as a UL inside a LI.
|
|
|
|
* @param string $attributes Attributes to add to the UL.
|
|
|
|
* @param string $titleEval PHP code to evaluate to start each child - this should include '<li>'
|
|
|
|
* @param string $extraArg Extra arguments that will be passed on to children, for if they overload this function.
|
|
|
|
* @param boolean $limitToMarked Display only marked children.
|
2009-05-01 05:49:34 +02:00
|
|
|
* @param string $childrenMethod The name of the method used to get children from each object
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param boolean $rootCall Set to true for this first call, and then to false for calls inside the recursion. You should not change this.
|
2009-07-09 07:53:40 +02:00
|
|
|
* @param int $minNodeCount
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2010-04-12 04:33:46 +02:00
|
|
|
public function getChildrenAsUL($attributes = "", $titleEval = '"<li>" . $child->Title', $extraArg = null, $limitToMarked = false, $childrenMethod = "AllChildrenIncludingDeleted", $numChildrenMethod = "numChildren", $rootCall = true, $minNodeCount = 30) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($limitToMarked && $rootCall) {
|
2010-04-12 04:33:46 +02:00
|
|
|
$this->markingFinished($numChildrenMethod);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
if($this->owner->hasMethod($childrenMethod)) {
|
|
|
|
$children = $this->owner->$childrenMethod($extraArg);
|
|
|
|
} else {
|
|
|
|
user_error(sprintf("Can't find the method '%s' on class '%s' for getting tree children",
|
|
|
|
$childrenMethod, get_class($this->owner)), E_USER_ERROR);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($children) {
|
|
|
|
if($attributes) {
|
|
|
|
$attributes = " $attributes";
|
|
|
|
}
|
|
|
|
|
|
|
|
$output = "<ul$attributes>\n";
|
|
|
|
|
|
|
|
foreach($children as $child) {
|
|
|
|
if(!$limitToMarked || $child->isMarked()) {
|
|
|
|
$foundAChild = true;
|
|
|
|
$output .= eval("return $titleEval;") . "\n" .
|
2010-04-12 04:33:46 +02:00
|
|
|
$child->getChildrenAsUL("", $titleEval, $extraArg, $limitToMarked, $childrenMethod, $numChildrenMethod, false, $minNodeCount) . "</li>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$output .= "</ul>\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(isset($foundAChild) && $foundAChild) {
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark a segment of the tree, by calling mark().
|
|
|
|
* The method performs a breadth-first traversal until the number of nodes is more than minCount.
|
|
|
|
* This is used to get a limited number of tree nodes to show in the CMS initially.
|
|
|
|
*
|
|
|
|
* This method returns the number of nodes marked. After this method is called other methods
|
|
|
|
* can check isExpanded() and isMarked() on individual nodes.
|
|
|
|
*
|
2009-07-09 07:53:40 +02:00
|
|
|
* @param int $minNodeCount The minimum amount of nodes to mark.
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return int The actual number of nodes marked.
|
|
|
|
*/
|
2010-04-12 04:33:46 +02:00
|
|
|
public function markPartialTree($minNodeCount = 30, $context = null, $childrenMethod = "AllChildrenIncludingDeleted", $numChildrenMethod = "numChildren") {
|
2009-07-09 07:53:40 +02:00
|
|
|
if(!is_numeric($minNodeCount)) $minNodeCount = 30;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->markedNodes = array($this->owner->ID => $this->owner);
|
|
|
|
$this->owner->markUnexpanded();
|
|
|
|
|
|
|
|
// foreach can't handle an ever-growing $nodes list
|
|
|
|
while(list($id, $node) = each($this->markedNodes)) {
|
2010-11-24 07:23:48 +01:00
|
|
|
// Inspect the number of potential nodes to mark - if its more than the minimum count,
|
|
|
|
// don't query them in the first place to inspect further marking state in-memory
|
|
|
|
if((sizeof($this->markedNodes) + $node->$numChildrenMethod()) >= $minNodeCount) break;
|
|
|
|
|
2010-04-12 04:33:46 +02:00
|
|
|
$this->markChildren($node, $context, $childrenMethod, $numChildrenMethod);
|
2009-07-09 07:53:40 +02:00
|
|
|
if($minNodeCount && sizeof($this->markedNodes) >= $minNodeCount) {
|
2007-07-19 12:40:28 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sizeof($this->markedNodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the marking to only those object with $node->$parameterName = $parameterValue
|
|
|
|
* @param string $parameterName The parameter on each node to check when marking.
|
|
|
|
* @param mixed $parameterValue The value the parameter must be to be marked.
|
|
|
|
*/
|
|
|
|
public function setMarkingFilter($parameterName, $parameterValue) {
|
|
|
|
$this->markingFilter = array(
|
|
|
|
"parameter" => $parameterName,
|
|
|
|
"value" => $parameterValue
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the marking to only those where the function returns true.
|
|
|
|
* The node in question will be passed to the function.
|
|
|
|
* @param string $funcName The function name.
|
|
|
|
*/
|
|
|
|
public function setMarkingFilterFunction($funcName) {
|
|
|
|
$this->markingFilter = array(
|
|
|
|
"func" => $funcName,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the marking filter matches on the given node.
|
|
|
|
* @param DataObject $node Node to check.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function markingFilterMatches($node) {
|
|
|
|
if(!$this->markingFilter) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-09-14 19:40:29 +02:00
|
|
|
if(isset($this->markingFilter['parameter']) && $parameterName = $this->markingFilter['parameter']) {
|
|
|
|
if(is_array($this->markingFilter['value'])){
|
2007-07-19 12:40:28 +02:00
|
|
|
$ret = false;
|
|
|
|
foreach($this->markingFilter['value'] as $value) {
|
|
|
|
$ret = $ret||$node->$parameterName==$value;
|
|
|
|
if($ret == true) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
} else {
|
|
|
|
return ($node->$parameterName == $this->markingFilter['value']);
|
|
|
|
}
|
2009-05-18 01:15:31 +02:00
|
|
|
} else if ($func = $this->markingFilter['func']) {
|
|
|
|
return call_user_func($func, $node);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark all children of the given node that match the marking filter.
|
|
|
|
* @param DataObject $node Parent node.
|
|
|
|
*/
|
2010-04-12 04:33:46 +02:00
|
|
|
public function markChildren($node, $context = null, $childrenMethod = "AllChildrenIncludingDeleted", $numChildrenMethod = "numChildren") {
|
2009-05-01 05:49:34 +02:00
|
|
|
if($node->hasMethod($childrenMethod)) {
|
|
|
|
$children = $node->$childrenMethod($context);
|
|
|
|
} else {
|
|
|
|
user_error(sprintf("Can't find the method '%s' on class '%s' for getting tree children",
|
|
|
|
$childrenMethod, get_class($this->owner)), E_USER_ERROR);
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$node->markExpanded();
|
|
|
|
if($children) {
|
|
|
|
foreach($children as $child) {
|
|
|
|
if(!$this->markingFilter || $this->markingFilterMatches($child)) {
|
2010-04-12 04:33:46 +02:00
|
|
|
if($child->$numChildrenMethod()) {
|
2009-07-09 07:55:34 +02:00
|
|
|
$child->markUnexpanded();
|
|
|
|
} else {
|
|
|
|
$child->markExpanded();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
$this->markedNodes[$child->ID] = $child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure marked nodes that have children are also marked expanded.
|
|
|
|
* Call this after marking but before iterating over the tree.
|
|
|
|
*/
|
2010-04-12 04:33:46 +02:00
|
|
|
protected function markingFinished($numChildrenMethod = "numChildren") {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Mark childless nodes as expanded.
|
2009-07-16 05:44:15 +02:00
|
|
|
if($this->markedNodes) {
|
|
|
|
foreach($this->markedNodes as $id => $node) {
|
2010-04-12 04:33:46 +02:00
|
|
|
if(!$node->isExpanded() && !$node->$numChildrenMethod()) {
|
2009-07-16 05:44:15 +02:00
|
|
|
$node->markExpanded();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return CSS classes of 'unexpanded', 'closed', both, or neither, depending on
|
|
|
|
* the marking of this DataObject.
|
|
|
|
*/
|
|
|
|
public function markingClasses() {
|
|
|
|
$classes = '';
|
2009-05-01 05:49:34 +02:00
|
|
|
if(!$this->isExpanded()) {
|
2011-03-01 05:38:25 +01:00
|
|
|
$classes .= " unexpanded jstree-closed";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-01 05:38:25 +01:00
|
|
|
if($this->isTreeOpened()) {
|
|
|
|
if($this->numChildren() > 0) $classes .= " jstree-open";
|
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
$classes .= " closed";
|
|
|
|
}
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark the children of the DataObject with the given ID.
|
|
|
|
* @param int $id ID of parent node.
|
|
|
|
* @param boolean $open If this is true, mark the parent node as opened.
|
|
|
|
*/
|
|
|
|
public function markById($id, $open = false) {
|
|
|
|
if(isset($this->markedNodes[$id])) {
|
|
|
|
$this->markChildren($this->markedNodes[$id]);
|
|
|
|
if($open) {
|
|
|
|
$this->markedNodes[$id]->markOpened();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expose the given object in the tree, by marking this page and all it ancestors.
|
|
|
|
* @param DataObject $childObj
|
|
|
|
*/
|
|
|
|
public function markToExpose($childObj) {
|
|
|
|
if(is_object($childObj)){
|
|
|
|
$stack = array_reverse($childObj->parentStack());
|
|
|
|
foreach($stack as $stackItem) {
|
|
|
|
$this->markById($stackItem->ID, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-31 07:43:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the IDs of all the marked nodes
|
|
|
|
*/
|
|
|
|
public function markedNodeIDs() {
|
|
|
|
return array_keys($this->markedNodes);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an array of this page and its ancestors, ordered item -> root.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function parentStack() {
|
|
|
|
$p = $this->owner;
|
|
|
|
|
|
|
|
while($p) {
|
|
|
|
$stack[] = $p;
|
|
|
|
$p = $p->ParentID ? $p->Parent() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $stack;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if this DataObject is marked.
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2009-05-01 05:49:34 +02:00
|
|
|
protected static $marked = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* True if this DataObject is expanded.
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2009-05-01 05:49:34 +02:00
|
|
|
protected static $expanded = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* True if this DataObject is opened.
|
|
|
|
* @var boolean
|
|
|
|
*/
|
2009-05-01 05:49:34 +02:00
|
|
|
protected static $treeOpened = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark this DataObject as expanded.
|
|
|
|
*/
|
|
|
|
public function markExpanded() {
|
2009-05-01 05:49:34 +02:00
|
|
|
self::$marked[ClassInfo::baseDataClass($this->owner->class)][$this->owner->ID] = true;
|
|
|
|
self::$expanded[ClassInfo::baseDataClass($this->owner->class)][$this->owner->ID] = true;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark this DataObject as unexpanded.
|
|
|
|
*/
|
|
|
|
public function markUnexpanded() {
|
2009-05-01 05:49:34 +02:00
|
|
|
self::$marked[ClassInfo::baseDataClass($this->owner->class)][$this->owner->ID] = true;
|
|
|
|
self::$expanded[ClassInfo::baseDataClass($this->owner->class)][$this->owner->ID] = false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark this DataObject's tree as opened.
|
|
|
|
*/
|
|
|
|
public function markOpened() {
|
2009-05-01 05:49:34 +02:00
|
|
|
self::$marked[ClassInfo::baseDataClass($this->owner->class)][$this->owner->ID] = true;
|
|
|
|
self::$treeOpened[ClassInfo::baseDataClass($this->owner->class)][$this->owner->ID] = true;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this DataObject is marked.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isMarked() {
|
2009-05-01 05:49:34 +02:00
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
|
|
|
$id = $this->owner->ID;
|
|
|
|
return isset(self::$marked[$baseClass][$id]) ? self::$marked[$baseClass][$id] : false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this DataObject is expanded.
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function isExpanded() {
|
2009-05-01 05:49:34 +02:00
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
|
|
|
$id = $this->owner->ID;
|
|
|
|
return isset(self::$expanded[$baseClass][$id]) ? self::$expanded[$baseClass][$id] : false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if this DataObject's tree is opened.
|
|
|
|
*/
|
|
|
|
public function isTreeOpened() {
|
2009-05-01 05:49:34 +02:00
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
|
|
|
$id = $this->owner->ID;
|
|
|
|
return isset(self::$treeOpened[$baseClass][$id]) ? self::$treeOpened[$baseClass][$id] : false;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* Return a partial tree as an HTML UL.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function partialTreeAsUL($minCount = 50) {
|
|
|
|
$children = $this->owner->AllChildren();
|
|
|
|
if($children) {
|
|
|
|
if($attributes) $attributes = " $attributes";
|
|
|
|
$output = "<ul$attributes>\n";
|
|
|
|
|
|
|
|
foreach($children as $child) {
|
|
|
|
$output .= eval("return $titleEval;") . "\n" .
|
|
|
|
$child->getChildrenAsUL("", $titleEval, $extraArg) . "</li>\n";
|
|
|
|
}
|
|
|
|
$output .= "</ul>\n";
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of this DataObject's and all it's descendants IDs.
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getDescendantIDList() {
|
|
|
|
$idList = array();
|
|
|
|
$this->loadDescendantIDListInto($idList);
|
|
|
|
return $idList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of this DataObject's and all it's descendants ID, and put it in $idList.
|
|
|
|
* @var array $idList Array to put results in.
|
|
|
|
*/
|
|
|
|
public function loadDescendantIDListInto(&$idList) {
|
2010-04-13 04:13:27 +02:00
|
|
|
if($children = $this->AllChildren()) {
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($children as $child) {
|
|
|
|
if(in_array($child->ID, $idList)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$idList[] = $child->ID;
|
2010-04-13 05:23:06 +02:00
|
|
|
$ext = $child->getExtensionInstance('Hierarchy');
|
|
|
|
$ext->setOwner($child);
|
|
|
|
$ext->loadDescendantIDListInto($idList);
|
|
|
|
$ext->clearOwner();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the children for this DataObject.
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function Children() {
|
2009-02-04 02:36:12 +01:00
|
|
|
if(!(isset($this->_cache_children) && $this->_cache_children)) {
|
2009-02-02 00:49:53 +01:00
|
|
|
$result = $this->owner->stageChildren(false);
|
|
|
|
if(isset($result)) {
|
2011-05-05 12:40:24 +02:00
|
|
|
$this->_cache_children = new ArrayList();
|
2009-02-02 00:49:53 +01:00
|
|
|
foreach($result as $child) {
|
|
|
|
if($child->canView()) {
|
2009-02-04 02:36:12 +01:00
|
|
|
$this->_cache_children->push($child);
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-04 02:36:12 +01:00
|
|
|
return $this->_cache_children;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all children, including those 'not in menus'.
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function AllChildren() {
|
2010-10-04 06:28:32 +02:00
|
|
|
return $this->owner->stageChildren(true);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all children, including those that have been deleted but are still in live.
|
|
|
|
* Deleted children will be marked as "DeletedFromStage"
|
|
|
|
* Added children will be marked as "AddedToStage"
|
|
|
|
* Modified children will be marked as "ModifiedOnStage"
|
|
|
|
* Everything else has "SameOnStage" set, as an indicator that this information has been looked up.
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-01-10 12:35:50 +01:00
|
|
|
public function AllChildrenIncludingDeleted($context = null) {
|
|
|
|
return $this->doAllChildrenIncludingDeleted($context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see AllChildrenIncludingDeleted
|
|
|
|
*
|
|
|
|
* @param unknown_type $context
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List
|
2009-01-10 12:35:50 +01:00
|
|
|
*/
|
|
|
|
public function doAllChildrenIncludingDeleted($context = null) {
|
2009-06-04 08:48:44 +02:00
|
|
|
if(!$this->owner) user_error('Hierarchy::doAllChildrenIncludingDeleted() called without $this->owner');
|
|
|
|
|
2009-01-16 05:04:13 +01:00
|
|
|
$idxStageChildren = array();
|
|
|
|
$idxLiveChildren = array();
|
|
|
|
|
2010-10-04 06:28:32 +02:00
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
|
|
|
if($baseClass) {
|
|
|
|
$stageChildren = $this->owner->stageChildren(true);
|
|
|
|
|
|
|
|
// Add live site content that doesn't exist on the stage site, if required.
|
|
|
|
if($this->owner->hasExtension('Versioned')) {
|
|
|
|
// Next, go through the live children. Only some of these will be listed
|
|
|
|
$liveChildren = $this->owner->liveChildren(true, true);
|
|
|
|
if($liveChildren) {
|
2011-05-05 12:40:24 +02:00
|
|
|
$merged = new ArrayList();
|
2009-11-22 06:58:17 +01:00
|
|
|
$merged->merge($stageChildren);
|
|
|
|
$merged->merge($liveChildren);
|
|
|
|
$stageChildren = $merged;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2010-10-04 06:28:32 +02:00
|
|
|
|
|
|
|
$this->owner->extend("augmentAllChildrenIncludingDeleted", $stageChildren, $context);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
user_error("Hierarchy::AllChildren() Couldn't determine base class for '{$this->owner->class}'", E_USER_ERROR);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2010-10-04 06:28:32 +02:00
|
|
|
return $stageChildren;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-05-01 05:49:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all the children that this page had, including pages that were deleted
|
|
|
|
* from both stage & live.
|
|
|
|
*/
|
|
|
|
public function AllHistoricalChildren() {
|
2011-03-23 04:32:24 +01:00
|
|
|
if(!$this->owner->hasExtension('Versioned')) throw new Exception('Hierarchy->AllHistoricalChildren() only works with Versioned extension applied');
|
|
|
|
|
2009-09-30 23:21:30 +02:00
|
|
|
$baseClass=ClassInfo::baseDataClass($this->owner->class);
|
|
|
|
return Versioned::get_including_deleted($baseClass,
|
|
|
|
"\"ParentID\" = " . (int)$this->owner->ID, "\"$baseClass\".\"ID\" ASC");
|
2009-05-01 05:49:34 +02:00
|
|
|
}
|
2009-10-23 00:18:02 +02:00
|
|
|
|
2010-04-12 04:33:46 +02:00
|
|
|
/**
|
|
|
|
* Return the number of children that this page ever had, including pages that were deleted
|
|
|
|
*/
|
|
|
|
public function numHistoricalChildren() {
|
2011-03-23 04:32:24 +01:00
|
|
|
if(!$this->owner->hasExtension('Versioned')) throw new Exception('Hierarchy->AllHistoricalChildren() only works with Versioned extension applied');
|
2009-11-22 06:16:38 +01:00
|
|
|
|
|
|
|
return Versioned::get_including_deleted(ClassInfo::baseDataClass($this->owner->class),
|
|
|
|
"\"ParentID\" = " . (int)$this->owner->ID)->count();
|
2010-04-12 04:33:46 +02:00
|
|
|
}
|
|
|
|
|
2009-10-23 00:18:02 +02:00
|
|
|
/**
|
2009-10-26 21:56:54 +01:00
|
|
|
* Return the number of direct children.
|
|
|
|
* By default, values are cached after the first invocation.
|
|
|
|
* Can be augumented by {@link augmentNumChildrenCountQuery()}.
|
|
|
|
*
|
|
|
|
* @param Boolean $cache
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
2009-10-26 21:56:54 +01:00
|
|
|
public function numChildren($cache = true) {
|
2009-10-23 00:18:02 +02:00
|
|
|
// Build the cache for this class if it doesn't exist.
|
2009-10-26 21:56:54 +01:00
|
|
|
if(!$cache || !is_numeric($this->_cache_numChildren)) {
|
2009-11-22 06:16:38 +01:00
|
|
|
// Hey, this is efficient now!
|
|
|
|
// We call stageChildren(), because Children() has canView() filtering
|
|
|
|
$this->_cache_numChildren = (int)$this->owner->stageChildren(true)->Count();
|
2009-10-23 00:18:02 +02:00
|
|
|
}
|
2009-10-26 21:56:54 +01:00
|
|
|
|
2009-10-23 00:18:02 +02:00
|
|
|
// If theres no value in the cache, it just means that it doesn't have any children.
|
2009-10-26 21:56:54 +01:00
|
|
|
return $this->_cache_numChildren;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return children from the stage site
|
2010-10-12 23:58:49 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param showAll Inlcude all of the elements, even those not shown in the menus.
|
2010-10-12 23:58:49 +02:00
|
|
|
* (only applicable when extension is applied to {@link SiteTree}).
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function stageChildren($showAll = false) {
|
2010-10-12 23:58:49 +02:00
|
|
|
if($this->owner->db('ShowInMenus')) {
|
|
|
|
$extraFilter = ($showAll) ? '' : " AND \"ShowInMenus\"=1";
|
|
|
|
} else {
|
|
|
|
$extraFilter = '';
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
2009-01-10 12:35:50 +01:00
|
|
|
|
2009-07-02 00:27:18 +02:00
|
|
|
$staged = DataObject::get($baseClass, "\"{$baseClass}\".\"ParentID\" = "
|
|
|
|
. (int)$this->owner->ID . " AND \"{$baseClass}\".\"ID\" != " . (int)$this->owner->ID
|
|
|
|
. $extraFilter, "");
|
|
|
|
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->owner->extend("augmentStageChildren", $staged, $showAll);
|
|
|
|
return $staged;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return children from the live site, if it exists.
|
2010-10-12 23:58:49 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @param boolean $showAll Include all of the elements, even those not shown in the menus.
|
2010-10-12 23:58:49 +02:00
|
|
|
* (only applicable when extension is applied to {@link SiteTree}).
|
2009-07-02 00:27:18 +02:00
|
|
|
* @param boolean $onlyDeletedFromStage Only return items that have been deleted from stage
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-07-02 00:27:18 +02:00
|
|
|
public function liveChildren($showAll = false, $onlyDeletedFromStage = false) {
|
2011-03-23 04:32:24 +01:00
|
|
|
if(!$this->owner->hasExtension('Versioned')) throw new Exception('Hierarchy->liveChildren() only works with Versioned extension applied');
|
2009-07-02 00:27:18 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
2009-11-22 06:58:17 +01:00
|
|
|
$id = $this->owner->ID;
|
|
|
|
|
2011-04-05 13:01:57 +02:00
|
|
|
$children = DataObject::get($baseClass)->where("\"{$baseClass}\".\"ParentID\" = $id AND \"{$baseClass}\".\"ID\" != $id");
|
|
|
|
if(!$showAll) $children = $children->where('"ShowInMenus" = 1');
|
2009-09-24 07:22:12 +02:00
|
|
|
|
2009-11-22 06:58:17 +01:00
|
|
|
// Query the live site
|
|
|
|
$children->dataQuery()->setQueryParam('Versioned.mode', 'stage');
|
|
|
|
$children->dataQuery()->setQueryParam('Versioned.stage', 'Live');
|
2009-07-02 00:27:18 +02:00
|
|
|
|
|
|
|
if($onlyDeletedFromStage) {
|
2009-11-22 06:58:17 +01:00
|
|
|
// Note that this makes a second query, and could be optimised to be a joi;
|
|
|
|
$stageChildren = DataObject::get($baseClass)
|
2011-04-05 13:01:57 +02:00
|
|
|
->where("\"{$baseClass}\".\"ParentID\" = $id AND \"{$baseClass}\".\"ID\" != $id");
|
2009-11-22 06:58:17 +01:00
|
|
|
$stageChildren->dataQuery()->setQueryParam('Versioned.mode', 'stage');
|
|
|
|
$stageChildren->dataQuery()->setQueryParam('Versioned.stage', '');
|
|
|
|
|
|
|
|
$ids = $stageChildren->column("ID");
|
|
|
|
if($ids) {
|
2011-04-05 13:01:57 +02:00
|
|
|
$children->where("\"$baseClass\".\"ID\" NOT IN (" . implode(',',$ids) . ")");
|
2009-11-22 06:58:17 +01:00
|
|
|
}
|
2009-07-02 00:27:18 +02:00
|
|
|
}
|
|
|
|
|
2009-11-22 06:58:17 +01:00
|
|
|
return $children;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the parent of this class.
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
public function getParent($filter = '') {
|
|
|
|
if($p = $this->owner->__get("ParentID")) {
|
2008-12-04 23:38:32 +01:00
|
|
|
$tableClasses = ClassInfo::dataClassesFor($this->owner->class);
|
|
|
|
$baseClass = array_shift($tableClasses);
|
|
|
|
$filter .= ($filter) ? " AND " : ""."\"$baseClass\".\"ID\" = $p";
|
|
|
|
return DataObject::get_one($this->owner->class, $filter);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2009-10-11 02:07:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return all the parents of this class in a set ordered from the lowest to highest parent.
|
|
|
|
*
|
2011-10-26 08:09:04 +02:00
|
|
|
* @return SS_List
|
2009-10-11 02:07:10 +02:00
|
|
|
*/
|
|
|
|
public function getAncestors() {
|
2011-05-05 12:40:24 +02:00
|
|
|
$ancestors = new ArrayList();
|
2009-10-11 02:07:10 +02:00
|
|
|
$object = $this->owner;
|
|
|
|
|
|
|
|
while($object = $object->getParent()) {
|
|
|
|
$ancestors->push($object);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ancestors;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the next node in the tree of the type. If there is no instance of the className descended from this node,
|
|
|
|
* then search the parents.
|
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @todo Write!
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function naturalPrev( $className, $afterNode = null ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the next node in the tree of the type. If there is no instance of the className descended from this node,
|
|
|
|
* then search the parents.
|
|
|
|
* @param string $className Class name of the node to find.
|
|
|
|
* @param string|int $root ID/ClassName of the node to limit the search to
|
|
|
|
* @param DataObject afterNode Used for recursive calls to this function
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
public function naturalNext( $className = null, $root = 0, $afterNode = null ) {
|
|
|
|
// If this node is not the node we are searching from, then we can possibly return this
|
|
|
|
// node as a solution
|
|
|
|
if($afterNode && $afterNode->ID != $this->owner->ID) {
|
|
|
|
if(!$className || ($className && $this->owner->class == $className)) {
|
|
|
|
return $this->owner;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$nextNode = null;
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
|
|
|
|
2010-10-04 06:28:32 +02:00
|
|
|
$children = DataObject::get(ClassInfo::baseDataClass($this->owner->class), "\"$baseClass\".\"ParentID\"={$this->owner->ID}" . ( ( $afterNode ) ? " AND \"Sort\" > " . sprintf( '%d', $afterNode->Sort ) : "" ), '"Sort" ASC');
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Try all the siblings of this node after the given node
|
2008-11-23 01:31:06 +01:00
|
|
|
/*if( $siblings = DataObject::get( ClassInfo::baseDataClass($this->owner->class), "\"ParentID\"={$this->owner->ParentID}" . ( $afterNode ) ? "\"Sort\" > {$afterNode->Sort}" : "" , '\"Sort\" ASC' ) )
|
2007-07-19 12:40:28 +02:00
|
|
|
$searchNodes->merge( $siblings );*/
|
|
|
|
|
|
|
|
if($children) {
|
|
|
|
foreach($children as $node) {
|
|
|
|
if($nextNode = $node->naturalNext($className, $node->ID, $this->owner)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if($nextNode) {
|
|
|
|
return $nextNode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if this is not an instance of the root class or has the root id, search the parent
|
|
|
|
if(!(is_numeric($root) && $root == $this->owner->ID || $root == $this->owner->class) && ($parent = $this->owner->Parent())) {
|
|
|
|
return $parent->naturalNext( $className, $root, $this->owner );
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2009-01-15 06:54:10 +01:00
|
|
|
|
|
|
|
function flushCache() {
|
2009-02-04 02:36:12 +01:00
|
|
|
$this->_cache_children = null;
|
2009-10-26 21:56:54 +01:00
|
|
|
$this->_cache_numChildren = null;
|
2010-12-05 09:26:40 +01:00
|
|
|
self::$marked = array();
|
|
|
|
self::$expanded = array();
|
|
|
|
self::$treeOpened = array();
|
2009-01-15 06:54:10 +01:00
|
|
|
}
|
2009-11-30 03:24:46 +01:00
|
|
|
|
|
|
|
function reset() {
|
|
|
|
self::$marked = array();
|
|
|
|
self::$expanded = array();
|
|
|
|
self::$treeOpened = array();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-06-25 05:58:21 +02:00
|
|
|
?>
|