Last active
October 13, 2015 08:17
-
-
Save stojg/4166473 to your computer and use it in GitHub Desktop.
SilverStripe Hierarchy replacement - Do not traverse into grandchildren in trees.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* CustomHierarchy | |
* | |
* This hierarchy overrides the getChildrenAsUL to not cascade into every child, this makes the sitetree loading a bit | |
* snappier | |
*/ | |
class CustomHierarchy extends Hierarchy { | |
/** | |
* Returns the children of this DataObject as an XHTML UL. | |
* | |
* | |
* @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. | |
* @param string $childrenMethod The name of the method used to get children from each object | |
* @param boolean $rootCall Set to true for this first call, and then to false for calls inside the recursion. You should not change this. | |
* @param int $minNodeCount | |
* @return string | |
*/ | |
public function getChildrenAsUL($attributes = "", $titleEval = "", $extraArg = null, $limitToMarked = false, $childrenMethod = "AllChildrenIncludingDeleted", $numChildrenMethod = "numChildren", $rootCall = true, $minNodeCount = 30) { | |
if($limitToMarked && $rootCall) { | |
$this->markingFinished($numChildrenMethod); | |
} | |
if(!$titleEval) { | |
$titleEval = '"<li>" . $child->Title'; | |
} | |
$children = $this->owner->$childrenMethod($extraArg); | |
if(!$children) { | |
return ''; | |
} | |
if($attributes) { | |
$attributes = " $attributes"; | |
} | |
$output = ''; | |
foreach($children as $child) { | |
if(!$limitToMarked || $child->isMarked()) { | |
// Here is the override, only cascade into the child if it's opened. | |
if($child->isTreeOpened()) { | |
$child->markExpanded(); | |
$output .= eval("return $titleEval;") . PHP_EOL . $child->getChildrenAsUL('', $titleEval, $extraArg, $limitToMarked, $childrenMethod, $numChildrenMethod, false, $minNodeCount) . "</li>".PHP_EOL; | |
} else { | |
$output .= eval("return $titleEval;") . "</li>" . PHP_EOL; | |
} | |
} | |
} | |
return "<ul$attributes>" . PHP_EOL . $output . "</ul>" . PHP_EOL; | |
} | |
/** | |
* Mark a segment of the tree, by calling mark(). | |
* | |
* This method is overrided so it only mark the first children. | |
* | |
* @return int The actual number of nodes marked. | |
*/ | |
public function markPartialTree($minNodeCount = 1, $context = null, $childrenMethod = "AllChildrenIncludingDeleted", $numChildrenMethod = "numChildren") { | |
$minNodeCount =1; | |
$this->markedNodes = array($this->owner->ID => $this->owner); | |
$this->owner->markUnexpanded(); | |
list($id, $node) = each($this->markedNodes); | |
$this->markChildren($node, $context, $childrenMethod, $numChildrenMethod); | |
return sizeof($this->markedNodes); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment