2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
|
|
|
use SilverStripe\Core\Config\Config;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\Hierarchy\Hierarchy;
|
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
use SilverStripe\View\ViewableData;
|
|
|
|
use Exception;
|
|
|
|
use InvalidArgumentException;
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Dropdown-like field that allows you to select an item from a hierarchical
|
2011-08-01 04:27:52 +02:00
|
|
|
* AJAX-expandable tree.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* Creates a field which opens a dropdown (actually a div via javascript
|
|
|
|
* included for you) which contains a tree with the ability to select a singular
|
|
|
|
* item for the value of the field. This field has the ability to store one-to-one
|
2011-08-01 04:27:52 +02:00
|
|
|
* joins related to hierarchy or a hierarchy based filter.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* **Note:** your source object must use an implementation of hierarchy for this
|
2011-08-01 04:27:52 +02:00
|
|
|
* field to generate the tree correctly, e.g. {@link Group}, {@link SiteTree} etc.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-01 04:27:52 +02:00
|
|
|
* All operations are carried out through javascript and provides no fallback
|
|
|
|
* to non JS.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <b>Usage</b>.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 05:55:22 +02:00
|
|
|
* <code>
|
|
|
|
* static $has_one = array(
|
|
|
|
* 'RightContent' => 'SiteTree'
|
|
|
|
* );
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-01 04:27:52 +02:00
|
|
|
* function getCMSFields() {
|
|
|
|
* ...
|
2010-10-15 05:55:22 +02:00
|
|
|
* $treedropdownfield = new TreeDropdownField("RightContentID", "Choose a page to show on the right:", "SiteTree");
|
2011-08-01 04:27:52 +02:00
|
|
|
* ..
|
|
|
|
* }
|
2010-10-15 05:55:22 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* This will generate a tree allowing the user to expand and contract subsections
|
2011-08-01 04:27:52 +02:00
|
|
|
* to find the appropriate page to save to the field.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2016-09-15 04:48:38 +02:00
|
|
|
* Caution: The form field does not include any JavaScript or CSS when used outside of the CMS context,
|
|
|
|
* since the required frontend dependencies are included through CMS bundling.
|
|
|
|
*
|
2012-06-20 23:59:16 +02:00
|
|
|
* @see TreeMultiselectField for the same implementation allowing multiple selections
|
|
|
|
* @see DropdownField for a simple dropdown field.
|
|
|
|
* @see CheckboxSetField for multiple selections through checkboxes.
|
|
|
|
* @see OptionsetField for single selections via radiobuttons.
|
2008-01-09 05:18:36 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class TreeDropdownField extends FormField {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2009-10-11 02:07:05 +02:00
|
|
|
'$Action!/$ID' => '$Action'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2009-10-11 02:07:05 +02:00
|
|
|
'tree'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:07:05 +02:00
|
|
|
* @ignore
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2013-05-30 13:45:10 +02:00
|
|
|
protected $sourceObject, $keyField, $labelField, $filterCallback,
|
|
|
|
$disableCallback, $searchCallback, $baseID = 0;
|
2012-03-07 22:07:40 +01:00
|
|
|
/**
|
2015-05-19 23:19:01 +02:00
|
|
|
* @var string default child method in Hierarchy->getChildrenAsUL
|
2012-03-07 22:07:40 +01:00
|
|
|
*/
|
|
|
|
protected $childrenMethod = 'AllChildrenIncludingDeleted';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-19 23:19:01 +02:00
|
|
|
/**
|
|
|
|
* @var string default child counting method in Hierarchy->getChildrenAsUL
|
|
|
|
*/
|
|
|
|
protected $numChildrenMethod = 'numChildren';
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2010-04-12 05:33:56 +02:00
|
|
|
/**
|
|
|
|
* Used by field search to leave only the relevant entries
|
|
|
|
*/
|
2011-08-01 04:27:52 +02:00
|
|
|
protected $searchIds = null, $showSearch, $searchExpanded = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* CAVEAT: for search to work properly $labelField must be a database field,
|
2011-08-01 04:27:52 +02:00
|
|
|
* or you need to setSearchFunction.
|
2010-04-12 05:36:56 +02:00
|
|
|
*
|
2009-10-11 02:07:05 +02:00
|
|
|
* @param string $name the field name
|
|
|
|
* @param string $title the field label
|
2015-02-10 06:01:19 +01:00
|
|
|
* @param string|array $sourceObject The object-type to list in the tree. This could
|
|
|
|
* be one of the following:
|
|
|
|
* - A DataObject class name with the {@link Hierarchy} extension.
|
|
|
|
* - An array of key/value pairs, like a {@link DropdownField} source. In
|
|
|
|
* this case, the field will act like show a flat list of tree items,
|
|
|
|
* without any hierarchy. This is most useful in conjunction with
|
|
|
|
* {@link TreeMultiselectField}, for presenting a set of checkboxes in
|
|
|
|
* a compact view. Note, that all value strings must be XML encoded
|
|
|
|
* safely prior to being passed in.
|
2011-08-01 04:27:52 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $keyField to field on the source class to save as the
|
2011-08-01 04:27:52 +02:00
|
|
|
* field value (default ID).
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $labelField the field name to show as the human-readable
|
2011-08-01 04:27:52 +02:00
|
|
|
* value on the tree (default Title).
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param bool $showSearch enable the ability to search the tree by
|
2011-08-01 04:27:52 +02:00
|
|
|
* entering the text in the input field.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-06-23 01:37:22 +02:00
|
|
|
public function __construct($name, $title = null, $sourceObject = 'SilverStripe\\Security\\Group', $keyField = 'ID',
|
2013-08-29 06:21:04 +02:00
|
|
|
$labelField = 'TreeTitle', $showSearch = true
|
2013-01-11 09:59:33 +01:00
|
|
|
) {
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
$this->sourceObject = $sourceObject;
|
|
|
|
$this->keyField = $keyField;
|
|
|
|
$this->labelField = $labelField;
|
2010-04-12 05:33:56 +02:00
|
|
|
$this->showSearch = $showSearch;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-05-19 23:19:01 +02:00
|
|
|
//Extra settings for Folders
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($sourceObject == 'SilverStripe\\Assets\\Folder') {
|
2015-05-19 23:19:01 +02:00
|
|
|
$this->childrenMethod = 'ChildFolders';
|
|
|
|
$this->numChildrenMethod = 'numChildFolders';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-08 00:00:36 +01:00
|
|
|
$this->addExtraClass('single');
|
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
parent::__construct($name, $title);
|
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-08-15 08:53:05 +02:00
|
|
|
* Set the ID of the root node of the tree. This defaults to 0 - i.e.
|
2011-08-01 04:27:52 +02:00
|
|
|
* displays the whole tree.
|
2009-10-11 02:07:05 +02:00
|
|
|
*
|
|
|
|
* @param int $ID
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:07:05 +02:00
|
|
|
public function setTreeBaseID($ID) {
|
|
|
|
$this->baseID = (int) $ID;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
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-08-15 08:53:05 +02:00
|
|
|
* Set a callback used to filter the values of the tree before
|
2011-08-01 04:27:52 +02:00
|
|
|
* displaying to the user.
|
2009-10-11 02:07:05 +02:00
|
|
|
*
|
|
|
|
* @param callback $callback
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return $this
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2009-10-11 02:07:05 +02:00
|
|
|
public function setFilterFunction($callback) {
|
|
|
|
if(!is_callable($callback, true)) {
|
|
|
|
throw new InvalidArgumentException('TreeDropdownField->setFilterCallback(): not passed a valid callback');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
$this->filterCallback = $callback;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-05-30 13:45:10 +02:00
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Set a callback used to disable checkboxes for some items in the tree
|
2013-05-30 13:45:10 +02:00
|
|
|
*
|
|
|
|
* @param callback $callback
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return $this
|
2013-05-30 13:45:10 +02:00
|
|
|
*/
|
|
|
|
public function setDisableFunction($callback) {
|
|
|
|
if(!is_callable($callback, true)) {
|
|
|
|
throw new InvalidArgumentException('TreeDropdownField->setDisableFunction(): not passed a valid callback');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-05-30 13:45:10 +02:00
|
|
|
$this->disableCallback = $callback;
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:33:56 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Set a callback used to search the hierarchy globally, even before
|
2011-08-01 04:27:52 +02:00
|
|
|
* applying the filter.
|
2010-04-12 05:33:56 +02:00
|
|
|
*
|
|
|
|
* @param callback $callback
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return $this
|
2010-04-12 05:33:56 +02:00
|
|
|
*/
|
|
|
|
public function setSearchFunction($callback) {
|
|
|
|
if(!is_callable($callback, true)) {
|
|
|
|
throw new InvalidArgumentException('TreeDropdownField->setSearchFunction(): not passed a valid callback');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:33:56 +02:00
|
|
|
$this->searchCallback = $callback;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2010-04-12 05:33:56 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
|
|
|
public function getShowSearch() {
|
|
|
|
return $this->showSearch;
|
|
|
|
}
|
|
|
|
|
2012-01-05 13:39:40 +01:00
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param bool $bool
|
|
|
|
* @return $this
|
2012-01-05 13:39:40 +01:00
|
|
|
*/
|
|
|
|
public function setShowSearch($bool) {
|
|
|
|
$this->showSearch = $bool;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2012-01-05 13:39:40 +01:00
|
|
|
}
|
|
|
|
|
2012-03-07 04:11:39 +01:00
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $method The parameter to ChildrenMethod to use when calling Hierarchy->getChildrenAsUL in
|
2015-05-19 23:19:01 +02:00
|
|
|
* {@link Hierarchy}. The method specified determines the structure of the returned list. Use "ChildFolders"
|
2012-09-26 23:34:00 +02:00
|
|
|
* in place of the default to get a drop-down listing with only folders, i.e. not including the child elements in
|
2015-05-19 23:19:01 +02:00
|
|
|
* the currently selected folder. setNumChildrenMethod() should be used as well for proper functioning.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-03-07 04:11:39 +01:00
|
|
|
* See {@link Hierarchy} for a complete list of possible methods.
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return $this
|
2012-03-07 04:11:39 +01:00
|
|
|
*/
|
|
|
|
public function setChildrenMethod($method) {
|
|
|
|
$this->childrenMethod = $method;
|
2013-01-08 18:32:40 +01:00
|
|
|
return $this;
|
2012-03-07 04:11:39 +01:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-05-19 23:19:01 +02:00
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $method The parameter to numChildrenMethod to use when calling Hierarchy->getChildrenAsUL in
|
2015-05-19 23:19:01 +02:00
|
|
|
* {@link Hierarchy}. Should be used in conjunction with setChildrenMethod().
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2016-06-23 01:37:22 +02:00
|
|
|
* @return $this
|
2015-05-19 23:19:01 +02:00
|
|
|
*/
|
|
|
|
public function setNumChildrenMethod($method) {
|
|
|
|
$this->numChildrenMethod = $method;
|
|
|
|
return $this;
|
|
|
|
}
|
2012-03-07 04:11:39 +01:00
|
|
|
|
2009-05-01 07:07:41 +02:00
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param array $properties
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return string
|
2009-05-01 07:07:41 +02:00
|
|
|
*/
|
2011-03-23 05:12:25 +01:00
|
|
|
public function Field($properties = array()) {
|
2016-06-30 08:00:41 +02:00
|
|
|
$item = DataObject::singleton($this->sourceObject);
|
|
|
|
$emptyTitle = _t(
|
|
|
|
'DropdownField.CHOOSE_MODEL',
|
|
|
|
'(Choose {name})',
|
|
|
|
['name' => $item->i18n_singular_name()]
|
|
|
|
);
|
2015-06-11 17:07:52 +02:00
|
|
|
|
2011-05-08 13:36:13 +02:00
|
|
|
$record = $this->Value() ? $this->objectForKey($this->Value()) : null;
|
2015-02-10 06:01:19 +01:00
|
|
|
if($record instanceof ViewableData) {
|
|
|
|
$title = $record->obj($this->labelField)->forTemplate();
|
|
|
|
} elseif($record) {
|
|
|
|
$title = Convert::raw2xml($record->{$this->labelField});
|
2015-06-11 17:07:52 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$title = $emptyTitle;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2011-05-08 13:36:13 +02:00
|
|
|
// TODO Implement for TreeMultiSelectField
|
2011-10-29 21:56:00 +02:00
|
|
|
$metadata = array(
|
2014-08-15 08:53:05 +02:00
|
|
|
'id' => $record ? $record->ID : null,
|
2011-10-29 21:56:00 +02:00
|
|
|
'ClassName' => $record ? $record->ClassName : $this->sourceObject
|
|
|
|
);
|
2011-05-08 13:36:13 +02:00
|
|
|
|
2011-03-23 05:12:25 +01:00
|
|
|
$properties = array_merge(
|
|
|
|
$properties,
|
|
|
|
array(
|
|
|
|
'Title' => $title,
|
2015-06-11 17:07:52 +02:00
|
|
|
'EmptyTitle' => $emptyTitle,
|
2015-01-27 12:37:10 +01:00
|
|
|
'Metadata' => ($metadata) ? Convert::raw2json($metadata) : null,
|
2009-10-11 02:07:05 +02:00
|
|
|
)
|
|
|
|
);
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2016-07-07 20:55:37 +02:00
|
|
|
return parent::Field($properties);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function extraClass() {
|
2011-12-22 13:10:57 +01:00
|
|
|
return implode(' ', array(parent::extraClass(), ($this->showSearch ? "searchable" : null)));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-10-11 02:07:05 +02:00
|
|
|
* Get the whole tree of a part of the tree via an AJAX request.
|
|
|
|
*
|
2016-09-09 08:43:05 +02:00
|
|
|
* @param HTTPRequest $request
|
2009-10-11 02:07:05 +02:00
|
|
|
* @return string
|
2016-06-23 01:37:22 +02:00
|
|
|
* @throws Exception
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2016-09-09 08:43:05 +02:00
|
|
|
public function tree(HTTPRequest $request) {
|
2010-04-13 04:09:32 +02:00
|
|
|
// 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;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 04:09:32 +02:00
|
|
|
// Regular source specification
|
2009-10-11 02:07:05 +02:00
|
|
|
$isSubTree = false;
|
2009-11-30 00:29:31 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$this->search = $request->requestVar('search');
|
2016-08-19 00:51:35 +02:00
|
|
|
$id = (is_numeric($request->latestParam('ID')))
|
|
|
|
? (int)$request->latestParam('ID')
|
2012-09-26 23:34:00 +02:00
|
|
|
: (int)$request->requestVar('ID');
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var DataObject|Hierarchy $obj */
|
|
|
|
$obj = null;
|
|
|
|
if($id && !$request->requestVar('forceFullTree')) {
|
|
|
|
$obj = DataObject::get_by_id($this->sourceObject, $id);
|
2009-10-11 02:07:05 +02:00
|
|
|
$isSubTree = true;
|
|
|
|
if(!$obj) {
|
|
|
|
throw new Exception (
|
2016-08-19 00:51:35 +02:00
|
|
|
"TreeDropdownField->tree(): the object #$id of type $this->sourceObject could not be found"
|
2009-10-11 02:07:05 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if($this->baseID) {
|
|
|
|
$obj = DataObject::get_by_id($this->sourceObject, $this->baseID);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!$this->baseID || !$obj) {
|
|
|
|
$obj = DataObject::singleton($this->sourceObject);
|
|
|
|
}
|
2009-10-11 02:07:05 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:36:56 +02:00
|
|
|
// pre-process the tree - search needs to operate globally, not locally as marking filter does
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($this->search) {
|
2010-04-12 05:33:56 +02:00
|
|
|
$this->populateIDs();
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($this->filterCallback || $this->search) {
|
2009-11-30 00:29:31 +01:00
|
|
|
$obj->setMarkingFilterFunction(array($this, "filterMarking"));
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|
|
|
|
$obj->markPartialTree(
|
|
|
|
$nodeCountThreshold = 30,
|
|
|
|
$context = null,
|
|
|
|
$this->childrenMethod,
|
|
|
|
$this->numChildrenMethod
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-13 03:55:24 +02:00
|
|
|
// 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);
|
2016-08-19 00:51:35 +02:00
|
|
|
$values = preg_split('/,\s*/', $forceValue);
|
|
|
|
if ($values) {
|
|
|
|
foreach($values as $value) {
|
|
|
|
if(!$value || $value == 'unchanged') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj->markToExpose($this->objectForKey($value));
|
|
|
|
}
|
2009-10-11 02:07:05 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-09-24 12:59:05 +02:00
|
|
|
|
|
|
|
$self = $this;
|
2015-02-10 06:01:19 +01:00
|
|
|
$titleFn = function(&$child) use(&$self) {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var DataObject|Hierarchy $child */
|
2013-09-24 12:59:05 +02:00
|
|
|
$keyField = $self->keyField;
|
|
|
|
$labelField = $self->labelField;
|
|
|
|
return sprintf(
|
2013-05-30 13:45:10 +02:00
|
|
|
'<li id="selector-%s-%s" data-id="%s" class="class-%s %s %s"><a rel="%d">%s</a>',
|
2013-09-24 12:59:05 +02:00
|
|
|
Convert::raw2xml($self->getName()),
|
|
|
|
Convert::raw2xml($child->$keyField),
|
|
|
|
Convert::raw2xml($child->$keyField),
|
|
|
|
Convert::raw2xml($child->class),
|
2015-05-19 23:19:01 +02:00
|
|
|
Convert::raw2xml($child->markingClasses($self->numChildrenMethod)),
|
2013-05-30 13:45:10 +02:00
|
|
|
($self->nodeIsDisabled($child)) ? 'disabled' : '',
|
2013-09-24 12:59:05 +02:00
|
|
|
(int)$child->ID,
|
2015-02-10 06:01:19 +01:00
|
|
|
$child->obj($labelField)->forTemplate()
|
2013-09-24 12:59:05 +02:00
|
|
|
);
|
|
|
|
};
|
2012-03-07 04:11:39 +01:00
|
|
|
|
2013-03-19 22:26:48 +01:00
|
|
|
// 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).
|
2016-06-15 06:03:16 +02:00
|
|
|
$nodeThresholdLeaf = Config::inst()->get('SilverStripe\\ORM\\Hierarchy\\Hierarchy', 'node_threshold_leaf');
|
2013-03-19 22:26:48 +01:00
|
|
|
if($nodeThresholdLeaf && !$this->filterCallback && !$this->search) {
|
|
|
|
$className = $this->sourceObject;
|
|
|
|
$nodeCountCallback = function($parent, $numChildren) use($className, $nodeThresholdLeaf) {
|
2016-08-19 00:51:35 +02:00
|
|
|
if( $className === 'SilverStripe\\CMS\\Model\\SiteTree'
|
|
|
|
&& $parent->ID
|
|
|
|
&& $numChildren > $nodeThresholdLeaf
|
|
|
|
) {
|
2013-03-19 22:26:48 +01:00
|
|
|
return sprintf(
|
|
|
|
'<ul><li><span class="item">%s</span></li></ul>',
|
|
|
|
_t('LeftAndMain.TooManyPages', 'Too many pages')
|
|
|
|
);
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
return null;
|
2014-08-15 08:53:05 +02:00
|
|
|
};
|
2013-03-19 22:26:48 +01:00
|
|
|
} else {
|
|
|
|
$nodeCountCallback = null;
|
|
|
|
}
|
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
if($isSubTree) {
|
2013-03-19 22:26:48 +01:00
|
|
|
$html = $obj->getChildrenAsUL(
|
|
|
|
"",
|
2013-09-24 12:59:05 +02:00
|
|
|
$titleFn,
|
2013-03-19 22:26:48 +01:00
|
|
|
null,
|
2014-08-15 08:53:05 +02:00
|
|
|
true,
|
2013-03-19 22:26:48 +01:00
|
|
|
$this->childrenMethod,
|
2015-05-19 23:19:01 +02:00
|
|
|
$this->numChildrenMethod,
|
2013-03-19 22:26:48 +01:00
|
|
|
true, // root call
|
|
|
|
null,
|
|
|
|
$nodeCountCallback
|
|
|
|
);
|
|
|
|
return substr(trim($html), 4, -5);
|
2011-03-02 20:38:02 +01:00
|
|
|
} else {
|
2013-03-19 22:26:48 +01:00
|
|
|
$html = $obj->getChildrenAsUL(
|
|
|
|
'class="tree"',
|
2013-09-24 12:59:05 +02:00
|
|
|
$titleFn,
|
2013-03-19 22:26:48 +01:00
|
|
|
null,
|
2014-08-15 08:53:05 +02:00
|
|
|
true,
|
2013-03-19 22:26:48 +01:00
|
|
|
$this->childrenMethod,
|
2015-05-19 23:19:01 +02:00
|
|
|
$this->numChildrenMethod,
|
2013-03-19 22:26:48 +01:00
|
|
|
true, // root call
|
|
|
|
null,
|
|
|
|
$nodeCountCallback
|
|
|
|
);
|
|
|
|
return $html;
|
2009-10-11 02:07:05 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-11-30 00:29:31 +01:00
|
|
|
|
|
|
|
/**
|
2015-05-19 23:19:01 +02:00
|
|
|
* Marking public function for the tree, which combines different filters sensibly.
|
|
|
|
* If a filter function has been set, that will be called. And if search text is set,
|
2012-09-26 23:34:00 +02:00
|
|
|
* filter on that too. Return true if all applicable conditions are true, false otherwise.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
2016-06-03 10:51:02 +02:00
|
|
|
* @param mixed $node
|
|
|
|
* @return bool
|
2009-11-30 00:29:31 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function filterMarking($node) {
|
2009-11-30 00:29:31 +01:00
|
|
|
if ($this->filterCallback && !call_user_func($this->filterCallback, $node)) return false;
|
2010-04-12 05:33:56 +02:00
|
|
|
if ($this->search != "") {
|
|
|
|
return isset($this->searchIds[$node->ID]) && $this->searchIds[$node->ID] ? true : false;
|
2009-11-30 00:29:31 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-30 00:29:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-13 13:51:28 +01:00
|
|
|
|
2013-05-30 13:45:10 +02:00
|
|
|
/**
|
|
|
|
* Marking a specific node in the tree as disabled
|
|
|
|
* @param $node
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function nodeIsDisabled($node) {
|
|
|
|
return ($this->disableCallback && call_user_func($this->disableCallback, $node));
|
|
|
|
}
|
|
|
|
|
2012-12-13 13:51:28 +01:00
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $field
|
|
|
|
* @return $this
|
2012-12-13 13:51:28 +01:00
|
|
|
*/
|
|
|
|
public function setLabelField($field) {
|
|
|
|
$this->labelField = $field;
|
2013-01-08 18:32:40 +01:00
|
|
|
return $this;
|
2012-12-13 13:51:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function getLabelField() {
|
|
|
|
return $this->labelField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $field
|
|
|
|
* @return $this
|
2012-12-13 13:51:28 +01:00
|
|
|
*/
|
|
|
|
public function setKeyField($field) {
|
|
|
|
$this->keyField = $field;
|
2013-01-08 18:32:40 +01:00
|
|
|
return $this;
|
2012-12-13 13:51:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function getKeyField() {
|
|
|
|
return $this->keyField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-23 01:37:22 +02:00
|
|
|
* @param string $class
|
|
|
|
* @return $this
|
2012-12-13 13:51:28 +01:00
|
|
|
*/
|
|
|
|
public function setSourceObject($class) {
|
|
|
|
$this->sourceObject = $class;
|
2013-01-08 18:32:40 +01:00
|
|
|
return $this;
|
2012-12-13 13:51:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function getSourceObject() {
|
|
|
|
return $this->sourceObject;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:33:56 +02:00
|
|
|
/**
|
|
|
|
* Populate $this->searchIds with the IDs of the pages matching the searched parameter and their parents.
|
2010-04-12 05:36:56 +02:00
|
|
|
* Reverse-constructs the tree starting from the leaves. Initially taken from CMSSiteTreeFilter, but modified
|
|
|
|
* with pluggable search function.
|
2010-04-12 05:33:56 +02:00
|
|
|
*/
|
|
|
|
protected function populateIDs() {
|
2010-04-12 05:36:56 +02:00
|
|
|
// get all the leaves to be displayed
|
2013-08-29 17:00:30 +02:00
|
|
|
if ($this->searchCallback) {
|
2010-04-12 05:33:56 +02:00
|
|
|
$res = call_user_func($this->searchCallback, $this->sourceObject, $this->labelField, $this->search);
|
2013-08-29 17:00:30 +02:00
|
|
|
} else {
|
|
|
|
$sourceObject = $this->sourceObject;
|
2013-06-21 00:32:08 +02:00
|
|
|
$filters = array();
|
2013-08-29 17:00:30 +02:00
|
|
|
if(singleton($sourceObject)->hasDatabaseField($this->labelField)) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$filters["{$this->labelField}:PartialMatch"] = $this->search;
|
2013-08-29 17:00:30 +02:00
|
|
|
} else {
|
|
|
|
if(singleton($sourceObject)->hasDatabaseField('Title')) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$filters["Title:PartialMatch"] = $this->search;
|
2013-08-29 17:00:30 +02:00
|
|
|
}
|
|
|
|
if(singleton($sourceObject)->hasDatabaseField('Name')) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$filters["Name:PartialMatch"] = $this->search;
|
2013-08-29 17:00:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
if(empty($filters)) {
|
2013-08-29 17:00:30 +02:00
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Cannot query by %s.%s, not a valid database column',
|
|
|
|
$sourceObject,
|
|
|
|
$this->labelField
|
|
|
|
));
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
$res = DataObject::get($this->sourceObject)->filterAny($filters);
|
2013-08-29 17:00:30 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 05:33:56 +02:00
|
|
|
if( $res ) {
|
2010-04-12 05:36:56 +02:00
|
|
|
// iteratively fetch the parents in bulk, until all the leaves can be accessed using the tree control
|
2010-04-12 05:33:56 +02:00
|
|
|
foreach($res as $row) {
|
|
|
|
if ($row->ParentID) $parents[$row->ParentID] = true;
|
|
|
|
$this->searchIds[$row->ID] = true;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-02-09 22:38:24 +01:00
|
|
|
$sourceObject = $this->sourceObject;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2010-04-12 05:33:56 +02:00
|
|
|
while (!empty($parents)) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$items = DataObject::get($sourceObject)
|
2015-02-09 22:38:24 +01:00
|
|
|
->filter("ID",array_keys($parents));
|
2010-04-12 05:33:56 +02:00
|
|
|
$parents = array();
|
|
|
|
|
2015-02-09 22:38:24 +01:00
|
|
|
foreach($items as $item) {
|
|
|
|
if ($item->ParentID) $parents[$item->ParentID] = true;
|
|
|
|
$this->searchIds[$item->ID] = true;
|
|
|
|
$this->searchExpanded[$item->ID] = true;
|
2010-04-12 05:33:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-11-30 00:29:31 +01:00
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
/**
|
|
|
|
* Get the object where the $keyField is equal to a certain value
|
|
|
|
*
|
|
|
|
* @param string|int $key
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
protected function objectForKey($key) {
|
2013-06-21 00:32:08 +02:00
|
|
|
return DataObject::get($this->sourceObject)
|
|
|
|
->filter($this->keyField, $key)
|
|
|
|
->first();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-04-12 05:17:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes this field to the readonly field.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var TreeDropdownField_Readonly $copy */
|
|
|
|
$copy = $this->castedCopy('SilverStripe\\Forms\\TreeDropdownField_Readonly');
|
2012-12-13 13:51:28 +01:00
|
|
|
$copy->setKeyField($this->keyField);
|
|
|
|
$copy->setLabelField($this->labelField);
|
|
|
|
$copy->setSourceObject($this->sourceObject);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-13 13:51:28 +01:00
|
|
|
return $copy;
|
2010-04-12 05:17:09 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|