2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* DataObjects that use the Hierarchy extension can be be organised as a hierarchy, with children and parents. The most
|
|
|
|
* obvious example of this is SiteTree.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage model
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @property int ParentID
|
|
|
|
* @property DataObject owner
|
|
|
|
* @method DataObject Parent
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2011-04-15 11:35:30 +02:00
|
|
|
class Hierarchy extends DataExtension {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $markedNodes;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
protected $markingFilter;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-01 13:20:09 +01:00
|
|
|
/** @var int */
|
2009-10-26 21:56:54 +01:00
|
|
|
protected $_cache_numChildren;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-19 22:26:48 +01:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* The lower bounds for the amount of nodes to mark. If set, the logic will expand nodes until it reaches at least
|
|
|
|
* this number, and then stops. Root nodes will always show regardless of this settting. Further nodes can be
|
|
|
|
* lazy-loaded via ajax. This isn't a hard limit. Example: On a value of 10, with 20 root nodes, each having 30
|
|
|
|
* children, the actual node count will be 50 (all root nodes plus first expanded child).
|
|
|
|
*
|
2013-03-19 22:26:48 +01:00
|
|
|
* @config
|
2014-12-01 13:20:09 +01:00
|
|
|
* @var int
|
2013-03-19 22:26:48 +01:00
|
|
|
*/
|
|
|
|
private static $node_threshold_total = 50;
|
|
|
|
|
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Limit on the maximum children a specific node can display. Serves as a hard limit to avoid exceeding available
|
|
|
|
* server resources in generating the tree, and browser resources in rendering it. Nodes with children exceeding
|
|
|
|
* this value typically won't display any children, although this is configurable through the $nodeCountCallback
|
|
|
|
* parameter in {@link getChildrenAsUL()}. "Root" nodes will always show all children, regardless of this setting.
|
|
|
|
*
|
2013-03-19 22:26:48 +01:00
|
|
|
* @config
|
2014-12-01 13:20:09 +01:00
|
|
|
* @var int
|
2013-03-19 22:26:48 +01:00
|
|
|
*/
|
|
|
|
private static $node_threshold_leaf = 250;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-05-15 05:19:39 +02:00
|
|
|
/**
|
|
|
|
* A list of classnames to exclude from display in both the CMS and front end
|
|
|
|
* displays. ->Children() and ->AllChildren affected.
|
|
|
|
* Especially useful for big sets of pages like listings
|
|
|
|
* If you use this, and still need the classes to be editable
|
|
|
|
* then add a model admin for the class
|
|
|
|
* Note: Does not filter subclasses (non-inheriting)
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $hide_from_hierarchy = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of classnames to exclude from display in the page tree views of the CMS,
|
|
|
|
* unlike $hide_from_hierarchy above which effects both CMS and front end.
|
|
|
|
* Especially useful for big sets of pages like listings
|
|
|
|
* If you use this, and still need the classes to be editable
|
|
|
|
* then add a model admin for the class
|
|
|
|
* Note: Does not filter subclasses (non-inheriting)
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @config
|
|
|
|
*/
|
|
|
|
private static $hide_from_cms_tree = array();
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public static function get_extra_config($class, $extension, $args) {
|
2012-08-22 23:29:13 +02:00
|
|
|
return array(
|
|
|
|
'has_one' => array('Parent' => $class)
|
|
|
|
);
|
2010-05-27 23:29:48 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-04-13 06:19:06 +02:00
|
|
|
/**
|
|
|
|
* Validate the owner object - check for existence of infinite loops.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @param ValidationResult $validationResult
|
2012-04-13 06:19:06 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function validate(ValidationResult $validationResult) {
|
2012-09-26 23:34:00 +02:00
|
|
|
// The object is new, won't be looping.
|
2014-08-15 08:53:05 +02:00
|
|
|
if (!$this->owner->ID) return;
|
2012-09-26 23:34:00 +02:00
|
|
|
// The object has no parent, won't be looping.
|
2014-08-15 08:53:05 +02:00
|
|
|
if (!$this->owner->ParentID) return;
|
2012-09-26 23:34:00 +02:00
|
|
|
// The parent has not changed, skip the check for performance reasons.
|
2014-08-15 08:53:05 +02:00
|
|
|
if (!$this->owner->isChanged('ParentID')) return;
|
2012-04-13 06:19:06 +02:00
|
|
|
|
|
|
|
// Walk the hierarchy upwards until we reach the top, or until we reach the originating node again.
|
|
|
|
$node = $this->owner;
|
|
|
|
while($node) {
|
|
|
|
if ($node->ParentID==$this->owner->ID) {
|
|
|
|
// Hierarchy is looping.
|
|
|
|
$validationResult->error(
|
2012-05-01 21:44:54 +02:00
|
|
|
_t(
|
|
|
|
'Hierarchy.InfiniteLoopNotAllowed',
|
|
|
|
'Infinite loop found within the "{type}" hierarchy. Please change the parent to resolve this',
|
|
|
|
'First argument is the class that makes up the hierarchy.',
|
|
|
|
array('type' => $this->owner->class)
|
2012-04-13 06:19:06 +02:00
|
|
|
),
|
|
|
|
'INFINITE_LOOP'
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$node = $node->ParentID ? $node->Parent() : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point the $validationResult contains the response.
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01: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|callable $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 bool $limitToMarked Display only marked children
|
|
|
|
* @param string $childrenMethod The name of the method used to get children from each object
|
|
|
|
* @param bool $rootCall Set to true for this first call, and then to false for calls inside
|
|
|
|
* the recursion. You should not change this.
|
|
|
|
* @param int $nodeCountThreshold See {@link self::$node_threshold_total}
|
|
|
|
* @param callable $nodeCountCallback Called with the node count, which gives the callback an opportunity to
|
|
|
|
* intercept the query. Useful e.g. to avoid excessive children listings
|
|
|
|
* (Arguments: $parent, $numChildren)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-02-07 16:33:20 +01:00
|
|
|
public function getChildrenAsUL($attributes = "", $titleEval = '"<li>" . $child->Title', $extraArg = null,
|
2014-08-15 08:53:05 +02:00
|
|
|
$limitToMarked = false, $childrenMethod = "AllChildrenIncludingDeleted",
|
2013-08-21 08:54:05 +02:00
|
|
|
$numChildrenMethod = "numChildren", $rootCall = true,
|
2017-07-19 13:44:43 +02:00
|
|
|
$nodeCountThreshold = null, $nodeCountCallback = null) {
|
2013-03-19 22:26:48 +01:00
|
|
|
|
|
|
|
if(!is_numeric($nodeCountThreshold)) {
|
|
|
|
$nodeCountThreshold = Config::inst()->get('Hierarchy', 'node_threshold_total');
|
|
|
|
}
|
2012-09-26 23:34:00 +02:00
|
|
|
|
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
|
|
|
}
|
2014-07-20 01:49:17 +02:00
|
|
|
|
|
|
|
|
2013-03-19 22:26:48 +01:00
|
|
|
if($nodeCountCallback) {
|
2014-08-15 08:53:05 +02:00
|
|
|
$nodeCountWarning = $nodeCountCallback($this->owner, $this->owner->$numChildrenMethod());
|
2013-03-19 22:26:48 +01:00
|
|
|
if($nodeCountWarning) return $nodeCountWarning;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
if($this->owner->hasMethod($childrenMethod)) {
|
|
|
|
$children = $this->owner->$childrenMethod($extraArg);
|
|
|
|
} else {
|
2014-08-15 08:53:05 +02:00
|
|
|
user_error(sprintf("Can't find the method '%s' on class '%s' for getting tree children",
|
2009-05-01 05:49:34 +02:00
|
|
|
$childrenMethod, get_class($this->owner)), E_USER_ERROR);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($children) {
|
2014-07-20 01:49:17 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($attributes) {
|
|
|
|
$attributes = " $attributes";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$output = "<ul$attributes>\n";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
foreach($children as $child) {
|
|
|
|
if(!$limitToMarked || $child->isMarked()) {
|
|
|
|
$foundAChild = true;
|
2014-07-20 01:49:17 +02:00
|
|
|
if(is_callable($titleEval)) {
|
|
|
|
$output .= $titleEval($child, $numChildrenMethod);
|
|
|
|
} else {
|
|
|
|
$output .= eval("return $titleEval;");
|
|
|
|
}
|
2013-03-12 23:01:43 +01:00
|
|
|
$output .= "\n";
|
|
|
|
|
|
|
|
$numChildren = $child->$numChildrenMethod();
|
2014-07-20 01:49:17 +02:00
|
|
|
|
2013-03-12 23:01:43 +01:00
|
|
|
if(
|
|
|
|
// Always traverse into opened nodes (they might be exposed as parents of search results)
|
|
|
|
$child->isExpanded()
|
|
|
|
// Only traverse into children if we haven't reached the maximum node count already.
|
|
|
|
// Otherwise, the remaining nodes are lazy loaded via ajax.
|
|
|
|
&& $child->isMarked()
|
|
|
|
) {
|
2013-03-19 22:26:48 +01:00
|
|
|
// Additionally check if node count requirements are met
|
|
|
|
$nodeCountWarning = $nodeCountCallback ? $nodeCountCallback($child, $numChildren) : null;
|
|
|
|
if($nodeCountWarning) {
|
|
|
|
$output .= $nodeCountWarning;
|
|
|
|
$child->markClosed();
|
|
|
|
} else {
|
2013-08-21 08:54:05 +02:00
|
|
|
$output .= $child->getChildrenAsUL("", $titleEval, $extraArg, $limitToMarked,
|
2017-07-19 13:44:43 +02:00
|
|
|
$childrenMethod, $numChildrenMethod, false, $nodeCountThreshold);
|
2014-07-20 01:49:17 +02:00
|
|
|
}
|
2013-03-19 22:26:48 +01:00
|
|
|
} elseif($child->isTreeOpened()) {
|
2013-03-12 23:01:43 +01:00
|
|
|
// Since we're not loading children, don't mark it as open either
|
|
|
|
$child->markClosed();
|
|
|
|
}
|
|
|
|
$output .= "</li>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$output .= "</ul>\n";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($foundAChild) && $foundAChild) {
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Mark a segment of the tree, by calling mark().
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-01 13:20:09 +01:00
|
|
|
* 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
|
|
|
|
* {@link isExpanded()} and {@link isMarked()} on individual nodes.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-03-12 23:01:43 +01:00
|
|
|
* @param int $nodeCountThreshold See {@link getChildrenAsUL()}
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return int The actual number of nodes marked.
|
|
|
|
*/
|
2013-03-12 23:01:43 +01:00
|
|
|
public function markPartialTree($nodeCountThreshold = 30, $context = null,
|
2017-07-19 13:44:43 +02:00
|
|
|
$childrenMethod = "AllChildrenIncludingDeleted", $numChildrenMethod = "numChildren") {
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2013-03-12 23:01:43 +01:00
|
|
|
if(!is_numeric($nodeCountThreshold)) $nodeCountThreshold = 30;
|
2009-07-09 07:53:40 +02:00
|
|
|
|
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)) {
|
2017-07-19 13:44:43 +02:00
|
|
|
$children = $this->markChildren($node, $context, $childrenMethod, $numChildrenMethod);
|
2013-03-12 23:01:43 +01:00
|
|
|
if($nodeCountThreshold && sizeof($this->markedNodes) > $nodeCountThreshold) {
|
|
|
|
// Undo marking children as opened since they're lazy loaded
|
|
|
|
if($children) foreach($children as $child) $child->markClosed();
|
2007-07-19 12:40:28 +02:00
|
|
|
break;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
return sizeof($this->markedNodes);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* 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.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function setMarkingFilter($parameterName, $parameterValue) {
|
|
|
|
$this->markingFilter = array(
|
|
|
|
"parameter" => $parameterName,
|
|
|
|
"value" => $parameterValue
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* 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 name of the function to call
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function setMarkingFilterFunction($funcName) {
|
|
|
|
$this->markingFilter = array(
|
|
|
|
"func" => $funcName,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the marking filter matches on the given node.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @param DataObject $node Node to check
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
return $ret;
|
2007-07-19 12:40:28 +02:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
2014-07-20 01:49:17 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Mark all children of the given node that match the marking filter.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @param DataObject $node Parent node
|
|
|
|
* @param mixed $context
|
|
|
|
* @param string $childrenMethod The name of the instance method to call to get the object's list of children
|
|
|
|
* @param string $numChildrenMethod The name of the instance method to call to count the object's children
|
2013-03-12 23:01:43 +01:00
|
|
|
* @return DataList
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public function markChildren($node, $context = null, $childrenMethod = "AllChildrenIncludingDeleted",
|
2017-07-19 13:44:43 +02:00
|
|
|
$numChildrenMethod = "numChildren") {
|
2009-05-01 05:49:34 +02:00
|
|
|
if($node->hasMethod($childrenMethod)) {
|
|
|
|
$children = $node->$childrenMethod($context);
|
|
|
|
} else {
|
2014-08-15 08:53:05 +02:00
|
|
|
user_error(sprintf("Can't find the method '%s' on class '%s' for getting tree children",
|
2012-04-12 02:38:07 +02:00
|
|
|
$childrenMethod, get_class($node)), E_USER_ERROR);
|
2009-05-01 05:49:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$node->markExpanded();
|
|
|
|
if($children) {
|
|
|
|
foreach($children as $child) {
|
2013-03-12 23:01:43 +01:00
|
|
|
$markingMatches = $this->markingFilterMatches($child);
|
2014-04-14 06:50:51 +02:00
|
|
|
if($markingMatches) {
|
2016-04-20 01:21:14 +02:00
|
|
|
// Mark a child node as unexpanded if it has children and has not already been expanded
|
|
|
|
if($child->$numChildrenMethod() && !$child->isExpanded()) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-03-12 23:01:43 +01:00
|
|
|
|
|
|
|
return $children;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Ensure marked nodes that have children are also marked expanded. Call this after marking but before iterating
|
|
|
|
* over the tree.
|
|
|
|
*
|
|
|
|
* @param string $numChildrenMethod The name of the instance method to call to count the object's children
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Return CSS classes of 'unexpanded', 'closed', both, or neither, as well as a 'jstree-*' state depending on the
|
|
|
|
* marking of this DataObject.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-01 13:20:09 +01:00
|
|
|
* @param string $numChildrenMethod The name of the instance method to call to count the object's children
|
2014-04-14 06:50:51 +02:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2014-07-20 01:49:17 +02:00
|
|
|
public function markingClasses($numChildrenMethod="numChildren") {
|
2007-07-19 12:40:28 +02:00
|
|
|
$classes = '';
|
2009-05-01 05:49:34 +02:00
|
|
|
if(!$this->isExpanded()) {
|
2014-04-14 06:50:51 +02:00
|
|
|
$classes .= " unexpanded";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-04-14 06:50:51 +02:00
|
|
|
// Set jstree open state, or mark it as a leaf (closed) if there are no children
|
2015-05-19 23:19:01 +02:00
|
|
|
if(!$this->owner->$numChildrenMethod()) {
|
2014-04-14 06:50:51 +02:00
|
|
|
$classes .= " jstree-leaf closed";
|
|
|
|
} elseif($this->isTreeOpened()) {
|
|
|
|
$classes .= " jstree-open";
|
2011-03-01 05:38:25 +01:00
|
|
|
} else {
|
2014-04-14 06:50:51 +02:00
|
|
|
$classes .= " jstree-closed closed";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
return $classes;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Mark the children of the DataObject with the given ID.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @param int $id ID of parent node
|
|
|
|
* @param bool $open If this is true, mark the parent node as opened
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2017-07-19 13:44:43 +02:00
|
|
|
public function markById($id, $open = false) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($this->markedNodes[$id])) {
|
2017-07-19 13:44:43 +02:00
|
|
|
$this->markChildren($this->markedNodes[$id]);
|
2007-07-19 12:40:28 +02:00
|
|
|
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.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param DataObject $childObj
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2017-07-19 13:44:43 +02:00
|
|
|
public function markToExpose($childObj) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_object($childObj)){
|
|
|
|
$stack = array_reverse($childObj->parentStack());
|
|
|
|
foreach($stack as $stackItem) {
|
2017-07-19 13:44:43 +02:00
|
|
|
$this->markById($stackItem->ID, true);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-07-31 07:43:41 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Return the IDs of all the marked nodes.
|
|
|
|
*
|
|
|
|
* @return array
|
2009-07-31 07:43:41 +02:00
|
|
|
*/
|
|
|
|
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.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @return SiteTree[]
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function parentStack() {
|
|
|
|
$p = $this->owner;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
while($p) {
|
|
|
|
$stack[] = $p;
|
|
|
|
$p = $p->ParentID ? $p->Parent() : null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $stack;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Cache of DataObjects' marked statuses: [ClassName][ID] = bool
|
|
|
|
* @var array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-05-01 05:49:34 +02:00
|
|
|
protected static $marked = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Cache of DataObjects' expanded statuses: [ClassName][ID] = bool
|
|
|
|
* @var array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-05-01 05:49:34 +02:00
|
|
|
protected static $expanded = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Cache of DataObjects' opened statuses: [ClassName][ID] = bool
|
|
|
|
* @var array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-05-01 05:49:34 +02:00
|
|
|
protected static $treeOpened = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
2013-03-12 23:01:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark this DataObject's tree as closed.
|
|
|
|
*/
|
|
|
|
public function markClosed() {
|
|
|
|
if(isset(self::$treeOpened[ClassInfo::baseDataClass($this->owner->class)][$this->owner->ID])) {
|
2014-08-15 08:53:05 +02:00
|
|
|
unset(self::$treeOpened[ClassInfo::baseDataClass($this->owner->class)][$this->owner->ID]);
|
2013-03-12 23:01:43 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Check if this DataObject is marked.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Check if this DataObject is expanded.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Check if this DataObject's tree is opened.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of this DataObject's and all it's descendants IDs.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @return int[]
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function getDescendantIDList() {
|
|
|
|
$idList = array();
|
|
|
|
$this->loadDescendantIDListInto($idList);
|
|
|
|
return $idList;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Get a list of this DataObject's and all it's descendants ID, and put them in $idList.
|
|
|
|
*
|
|
|
|
* @param array $idList Array to put results in.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Get the children for this DataObject.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @return DataList
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function Children() {
|
2014-08-15 08:53:05 +02:00
|
|
|
if(!(isset($this->_cache_children) && $this->_cache_children)) {
|
|
|
|
$result = $this->owner->stageChildren(false);
|
2016-01-04 09:15:17 +01:00
|
|
|
$children = array();
|
|
|
|
foreach ($result as $record) {
|
|
|
|
if ($record->canView()) {
|
|
|
|
$children[] = $record;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->_cache_children = new ArrayList($children);
|
|
|
|
}
|
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'.
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @return DataList
|
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.
|
2014-12-01 13:20:09 +01:00
|
|
|
* - 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.
|
|
|
|
*
|
|
|
|
* @param mixed $context
|
|
|
|
* @return ArrayList
|
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);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-10 12:35:50 +01:00
|
|
|
/**
|
|
|
|
* @see AllChildrenIncludingDeleted
|
|
|
|
*
|
2014-12-01 13:20:09 +01:00
|
|
|
* @param mixed $context
|
|
|
|
* @return ArrayList
|
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');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:28:32 +02:00
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
|
|
|
if($baseClass) {
|
|
|
|
$stageChildren = $this->owner->stageChildren(true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:28:32 +02:00
|
|
|
// Add live site content that doesn't exist on the stage site, if required.
|
|
|
|
if($this->owner->hasExtension('Versioned')) {
|
2014-08-15 08:53:05 +02:00
|
|
|
// Next, go through the live children. Only some of these will be listed
|
2010-10-04 06:28:32 +02:00
|
|
|
$liveChildren = $this->owner->liveChildren(true, true);
|
|
|
|
if($liveChildren) {
|
2012-12-08 12:20:20 +01:00
|
|
|
$merged = new ArrayList();
|
|
|
|
$merged->merge($stageChildren);
|
|
|
|
$merged->merge($liveChildren);
|
|
|
|
$stageChildren = $merged;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2010-10-04 06:28:32 +02:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
$this->owner->extend("augmentAllChildrenIncludingDeleted", $stageChildren, $context);
|
|
|
|
|
2010-10-04 06:28:32 +02:00
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
user_error("Hierarchy::AllChildren() Couldn't determine base class for '{$this->owner->class}'",
|
|
|
|
E_USER_ERROR);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-04 06:28:32 +02:00
|
|
|
return $stageChildren;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-05-01 05:49:34 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Return all the children that this page had, including pages that were deleted from both stage & live.
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
* @throws Exception
|
2009-05-01 05:49:34 +02:00
|
|
|
*/
|
|
|
|
public function AllHistoricalChildren() {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!$this->owner->hasExtension('Versioned')) {
|
|
|
|
throw new Exception('Hierarchy->AllHistoricalChildren() only works with Versioned extension applied');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-09-30 23:21:30 +02:00
|
|
|
$baseClass=ClassInfo::baseDataClass($this->owner->class);
|
2014-08-15 08:53:05 +02:00
|
|
|
return Versioned::get_including_deleted($baseClass,
|
2009-09-30 23:21:30 +02:00
|
|
|
"\"ParentID\" = " . (int)$this->owner->ID, "\"$baseClass\".\"ID\" ASC");
|
2009-05-01 05:49:34 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 04:33:46 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Return the number of children that this page ever had, including pages that were deleted.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
* @throws Exception
|
2010-04-12 04:33:46 +02:00
|
|
|
*/
|
|
|
|
public function numHistoricalChildren() {
|
2012-09-26 23:34:00 +02: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
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
return Versioned::get_including_deleted(ClassInfo::baseDataClass($this->owner->class),
|
2009-11-22 06:16:38 +01:00
|
|
|
"\"ParentID\" = " . (int)$this->owner->ID)->count();
|
2010-04-12 04:33:46 +02:00
|
|
|
}
|
|
|
|
|
2009-10-23 00:18:02 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Return the number of direct children. By default, values are cached after the first invocation. Can be
|
|
|
|
* augumented by {@link augmentNumChildrenCountQuery()}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-01 13:20:09 +01:00
|
|
|
* @param bool $cache Whether to retrieve values from 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
|
|
|
}
|
|
|
|
|
2016-05-15 05:19:39 +02:00
|
|
|
/**
|
|
|
|
* Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree?
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function showingCMSTree() {
|
|
|
|
if (!Controller::has_curr()) return false;
|
|
|
|
$controller = Controller::curr();
|
|
|
|
return $controller instanceof LeftAndMain
|
|
|
|
&& in_array($controller->getAction(), array("treeview", "listview", "getsubtree"));
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Return children in the stage site.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-01 13:20:09 +01:00
|
|
|
* @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when
|
|
|
|
* extension is applied to {@link SiteTree}.
|
2013-03-12 11:26:00 +01:00
|
|
|
* @return DataList
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function stageChildren($showAll = false) {
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
2016-05-15 05:19:39 +02:00
|
|
|
$hide_from_hierarchy = $this->owner->config()->hide_from_hierarchy;
|
|
|
|
$hide_from_cms_tree = $this->owner->config()->hide_from_cms_tree;
|
2013-03-12 11:26:00 +01:00
|
|
|
$staged = $baseClass::get()
|
2016-05-15 05:19:39 +02:00
|
|
|
->filter('ParentID', (int)$this->owner->ID)
|
|
|
|
->exclude('ID', (int)$this->owner->ID);
|
|
|
|
if ($hide_from_hierarchy) {
|
|
|
|
$staged = $staged->exclude('ClassName', $hide_from_hierarchy);
|
|
|
|
}
|
|
|
|
if ($hide_from_cms_tree && $this->showingCMSTree()) {
|
|
|
|
$staged = $staged->exclude('ClassName', $hide_from_cms_tree);
|
|
|
|
}
|
2013-03-12 11:26:00 +01:00
|
|
|
if (!$showAll && $this->owner->db('ShowInMenus')) {
|
|
|
|
$staged = $staged->filter('ShowInMenus', 1);
|
2013-08-22 12:55:47 +02:00
|
|
|
}
|
2009-01-10 12:35:50 +01:00
|
|
|
$this->owner->extend("augmentStageChildren", $staged, $showAll);
|
|
|
|
return $staged;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Return children in the live site, if it exists.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-01 13:20:09 +01:00
|
|
|
* @param bool $showAll Include all of the elements, even those not shown in the menus. Only
|
|
|
|
* applicable when extension is applied to {@link SiteTree}.
|
|
|
|
* @param bool $onlyDeletedFromStage Only return items that have been deleted from stage
|
|
|
|
* @return DataList
|
|
|
|
* @throws Exception
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-07-02 00:27:18 +02:00
|
|
|
public function liveChildren($showAll = false, $onlyDeletedFromStage = false) {
|
2012-09-26 23:34:00 +02: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);
|
2016-05-15 05:19:39 +02:00
|
|
|
$hide_from_hierarchy = $this->owner->config()->hide_from_hierarchy;
|
|
|
|
$hide_from_cms_tree = $this->owner->config()->hide_from_cms_tree;
|
2013-03-12 11:26:00 +01:00
|
|
|
$children = $baseClass::get()
|
|
|
|
->filter('ParentID', (int)$this->owner->ID)
|
|
|
|
->exclude('ID', (int)$this->owner->ID)
|
2012-12-12 05:22:45 +01:00
|
|
|
->setDataQueryParam(array(
|
|
|
|
'Versioned.mode' => $onlyDeletedFromStage ? 'stage_unique' : 'stage',
|
|
|
|
'Versioned.stage' => 'Live'
|
|
|
|
));
|
2016-05-15 05:19:39 +02:00
|
|
|
if ($hide_from_hierarchy) {
|
|
|
|
$children = $children->exclude('ClassName', $hide_from_hierarchy);
|
|
|
|
}
|
|
|
|
if ($hide_from_cms_tree && $this->showingCMSTree()) {
|
|
|
|
$children = $children->exclude('ClassName', $hide_from_cms_tree);
|
|
|
|
}
|
|
|
|
if(!$showAll && $this->owner->db('ShowInMenus')) $children = $children->filter('ShowInMenus', 1);
|
2012-08-21 05:52:12 +02:00
|
|
|
|
2009-11-22 06:58:17 +01:00
|
|
|
return $children;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing
|
|
|
|
* is returned.
|
|
|
|
*
|
|
|
|
* @param string $filter
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return DataObject
|
|
|
|
*/
|
2013-06-21 00:32:08 +02:00
|
|
|
public function getParent($filter = null) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if($p = $this->owner->__get("ParentID")) {
|
2008-12-04 23:38:32 +01:00
|
|
|
$tableClasses = ClassInfo::dataClassesFor($this->owner->class);
|
|
|
|
$baseClass = array_shift($tableClasses);
|
2013-06-21 00:32:08 +02:00
|
|
|
return DataObject::get_one($this->owner->class, array(
|
|
|
|
array("\"$baseClass\".\"ID\"" => $p),
|
|
|
|
$filter
|
|
|
|
));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +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.
|
|
|
|
*
|
2014-12-01 13:20:09 +01:00
|
|
|
* @return ArrayList
|
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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:10 +02:00
|
|
|
while($object = $object->getParent()) {
|
|
|
|
$ancestors->push($object);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:10 +02:00
|
|
|
return $ancestors;
|
|
|
|
}
|
2012-03-02 17:10:47 +01:00
|
|
|
|
|
|
|
/**
|
2014-12-01 13:20:09 +01:00
|
|
|
* Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2014-12-01 13:20:09 +01:00
|
|
|
* @param string $separator
|
|
|
|
* @return string
|
2012-03-02 17:10:47 +01:00
|
|
|
*/
|
|
|
|
public function getBreadcrumbs($separator = ' » ') {
|
|
|
|
$crumbs = array();
|
|
|
|
$ancestors = array_reverse($this->owner->getAncestors()->toArray());
|
|
|
|
foreach($ancestors as $ancestor) $crumbs[] = $ancestor->Title;
|
|
|
|
$crumbs[] = $this->owner->Title;
|
|
|
|
return implode($separator, $crumbs);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-02-25 03:10:37 +01:00
|
|
|
* @todo Write!
|
2014-12-01 13:20:09 +01:00
|
|
|
*
|
|
|
|
* @param string $className Class name of the node to find
|
|
|
|
* @param DataObject $afterNode Used for recursive calls to this function
|
|
|
|
* @return DataObject
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2014-12-01 13:20:09 +01:00
|
|
|
public function naturalPrev($className, $afterNode = null ) {
|
2007-07-19 12:40:28 +02:00
|
|
|
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.
|
2014-12-01 13:20:09 +01:00
|
|
|
* @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
|
2007-07-19 12:40:28 +02:00
|
|
|
* @return DataObject
|
|
|
|
*/
|
2014-12-01 13:20:09 +01:00
|
|
|
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
|
2007-07-19 12:40:28 +02:00
|
|
|
if($afterNode && $afterNode->ID != $this->owner->ID) {
|
|
|
|
if(!$className || ($className && $this->owner->class == $className)) {
|
|
|
|
return $this->owner;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$nextNode = null;
|
|
|
|
$baseClass = ClassInfo::baseDataClass($this->owner->class);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-12 11:26:00 +01:00
|
|
|
$children = $baseClass::get()
|
|
|
|
->filter('ParentID', (int)$this->owner->ID)
|
2015-09-07 07:38:29 +02:00
|
|
|
->sort('"Sort"', 'ASC');
|
2013-03-12 11:26:00 +01:00
|
|
|
if ($afterNode) {
|
|
|
|
$children = $children->filter('Sort:GreaterThan', $afterNode->Sort);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Try all the siblings of this node after the given node
|
2014-08-15 08:53:05 +02:00
|
|
|
/*if( $siblings = DataObject::get( ClassInfo::baseDataClass($this->owner->class),
|
|
|
|
"\"ParentID\"={$this->owner->ParentID}" . ( $afterNode ) ? "\"Sort\"
|
2012-09-26 23:34:00 +02:00
|
|
|
> {$afterNode->Sort}" : "" , '\"Sort\" ASC' ) ) $searchNodes->merge( $siblings );*/
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($children) {
|
|
|
|
foreach($children as $node) {
|
|
|
|
if($nextNode = $node->naturalNext($className, $node->ID, $this->owner)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if($nextNode) {
|
|
|
|
return $nextNode;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// if this is not an instance of the root class or has the root id, search the parent
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!(is_numeric($root) && $root == $this->owner->ID || $root == $this->owner->class)
|
|
|
|
&& ($parent = $this->owner->Parent())) {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $parent->naturalNext( $className, $root, $this->owner );
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-12-01 13:20:09 +01:00
|
|
|
/**
|
|
|
|
* Flush all Hierarchy caches:
|
|
|
|
* - Children (instance)
|
|
|
|
* - NumChildren (instance)
|
|
|
|
* - Marked (global)
|
|
|
|
* - Expanded (global)
|
|
|
|
* - TreeOpened (global)
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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
|
|
|
}
|
2012-03-27 06:04:11 +02:00
|
|
|
|
2014-12-01 13:20:09 +01:00
|
|
|
/**
|
|
|
|
* Reset global Hierarchy caches:
|
|
|
|
* - Marked
|
|
|
|
* - Expanded
|
|
|
|
* - TreeOpened
|
|
|
|
*/
|
2012-03-27 06:04:11 +02:00
|
|
|
public static function reset() {
|
2009-11-30 03:24:46 +01:00
|
|
|
self::$marked = array();
|
|
|
|
self::$expanded = array();
|
|
|
|
self::$treeOpened = array();
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-03-27 06:04:11 +02:00
|
|
|
}
|