/** * Get the whole tree of a part of the tree via an AJAX request with empty / none item prepended. * * @param SS_HTTPRequest $request * @return string * for version 2.4 and later */ public function tree(SS_HTTPRequest $request) { if ($ID = (int) $request->latestparam('ID')) { return parent::tree($request); } else { return $this->preTree() . parent::tree($request) . OptionalTreeDropdownField::$postTree; } }
/** * Get the whole tree of a part of the tree via an AJAX request. * * @param SS_HTTPRequest $request * @return string */ public function tree(SS_HTTPRequest $request) { // Array sourceObject is an explicit list of values - construct a "flat tree" if (is_array($this->sourceObject)) { $output = "<ul class=\"tree\">\n"; foreach ($this->sourceObject as $k => $v) { $output .= '<li id="selector-' . $this->name . '-' . $k . '"><a>' . $v . '</a>'; } $output .= "</ul>"; return $output; } // Regular source specification $isSubTree = false; $this->search = $request->requestVar('search'); $ID = is_numeric($request->latestparam('ID')) ? (int) $request->latestparam('ID') : (int) $request->requestVar('ID'); if ($ID && !$request->requestVar('forceFullTree')) { $obj = DataObject::get_by_id($this->sourceObject, $ID); $isSubTree = true; if (!$obj) { throw new Exception("TreeDropdownField->tree(): the object #{$ID} of type {$this->sourceObject} could not be found"); } } else { if ($this->baseID) { $obj = DataObject::get_by_id($this->sourceObject, $this->baseID); } if (!$this->baseID || !$obj) { $obj = singleton($this->sourceObject); } } // pre-process the tree - search needs to operate globally, not locally as marking filter does if ($this->search != "") { $this->populateIDs(); } if ($this->filterCallback || $this->search != "") { $obj->setMarkingFilterFunction(array($this, "filterMarking")); } $obj->markPartialTree($nodeCountThreshold = 30, $context = null, $this->childrenMethod, $this->numChildrenMethod); // allow to pass values to be selected within the ajax request if (isset($_REQUEST['forceValue']) || $this->value) { $forceValue = isset($_REQUEST['forceValue']) ? $_REQUEST['forceValue'] : $this->value; if (($values = preg_split('/,\\s*/', $forceValue)) && count($values)) { foreach ($values as $value) { if (!$value || $value == 'unchanged') { continue; } $obj->markToExpose($this->objectForKey($value)); } } } $self = $this; $titleFn = function (&$child) use(&$self) { $keyField = $self->keyField; $labelField = $self->labelField; return sprintf('<li id="selector-%s-%s" data-id="%s" class="class-%s %s %s"><a rel="%d">%s</a>', Convert::raw2xml($self->getName()), Convert::raw2xml($child->{$keyField}), Convert::raw2xml($child->{$keyField}), Convert::raw2xml($child->class), Convert::raw2xml($child->markingClasses($self->numChildrenMethod)), $self->nodeIsDisabled($child) ? 'disabled' : '', (int) $child->ID, $child->obj($labelField)->forTemplate()); }; // Limit the amount of nodes shown for performance reasons. // Skip the check if we're filtering the tree, since its not clear how many children will // match the filter criteria until they're queried (and matched up with previously marked nodes). $nodeThresholdLeaf = Config::inst()->get('Hierarchy', 'node_threshold_leaf'); if ($nodeThresholdLeaf && !$this->filterCallback && !$this->search) { $className = $this->sourceObject; $nodeCountCallback = function ($parent, $numChildren) use($className, $nodeThresholdLeaf) { if ($className == 'SiteTree' && $parent->ID && $numChildren > $nodeThresholdLeaf) { return sprintf('<ul><li><span class="item">%s</span></li></ul>', _t('LeftAndMain.TooManyPages', 'Too many pages')); } }; } else { $nodeCountCallback = null; } if ($isSubTree) { $html = $obj->getChildrenAsUL("", $titleFn, null, true, $this->childrenMethod, $this->numChildrenMethod, true, null, $nodeCountCallback); return substr(trim($html), 4, -5); } else { $html = $obj->getChildrenAsUL('class="tree"', $titleFn, null, true, $this->childrenMethod, $this->numChildrenMethod, true, null, $nodeCountCallback); return $html; } }
/** * Get the whole tree of a part of the tree via an AJAX request. * * @param SS_HTTPRequest $request * @return string */ public function tree(SS_HTTPRequest $request) { // Array sourceObject is an explicit list of values - construct a "flat tree" if (is_array($this->sourceObject)) { $output = "<ul class=\"tree\">\n"; foreach ($this->sourceObject as $k => $v) { $output .= '<li id="selector-' . $this->name . '-' . $k . '"><a>' . $v . '</a>'; } $output .= "</ul>"; return $output; } // Regular source specification $isSubTree = false; $this->search = Convert::Raw2SQL($request->requestVar('search')); $ID = is_numeric($request->latestparam('ID')) ? (int) $request->latestparam('ID') : (int) $request->requestVar('ID'); if ($ID) { $obj = DataObject::get_by_id($this->sourceObject, $ID); $isSubTree = true; if (!$obj) { throw new Exception("TreeDropdownField->tree(): the object #{$ID} of type {$this->sourceObject} could not be found"); } } else { if ($this->baseID) { $obj = DataObject::get_by_id($this->sourceObject, $this->baseID); } if (!$this->baseID || !$obj) { $obj = singleton($this->sourceObject); } } // pre-process the tree - search needs to operate globally, not locally as marking filter does if ($this->search != "") { $this->populateIDs(); } if ($this->filterCallback || $this->sourceObject == 'Folder' || $this->search != "") { $obj->setMarkingFilterFunction(array($this, "filterMarking")); } $obj->markPartialTree(); // allow to pass values to be selected within the ajax request if (isset($_REQUEST['forceValue']) || $this->value) { $forceValue = isset($_REQUEST['forceValue']) ? $_REQUEST['forceValue'] : $this->value; if (($values = preg_split('/,\\s*/', $forceValue)) && count($values)) { foreach ($values as $value) { if (!$value) { continue; } $obj->markToExpose($this->objectForKey($value)); } } } $eval = '"<li id=\\"selector-' . $this->Name() . '-{$child->' . $this->keyField . '}\\" data-id=\\"$child->' . $this->keyField . '\\" class=\\"class-$child->class"' . ' . $child->markingClasses() . "\\"><a rel=\\"$child->ID\\">" . $child->' . $this->labelField . ' . "</a>"'; if ($isSubTree) { return substr(trim($obj->getChildrenAsUL('', $eval, null, true)), 4, -5); } else { return $obj->getChildrenAsUL('class="tree"', $eval, null, true); } }