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;
|
|
|
|
|
2017-10-26 02:04:30 +02:00
|
|
|
use Exception;
|
|
|
|
use InvalidArgumentException;
|
2017-03-29 06:23:49 +02:00
|
|
|
use SilverStripe\Assets\Folder;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2017-03-29 06:23:49 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2017-05-15 05:18:35 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-10-16 04:10:05 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\Hierarchy\Hierarchy;
|
2017-03-29 06:23:49 +02:00
|
|
|
use SilverStripe\ORM\Hierarchy\MarkedSet;
|
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
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class TreeDropdownField extends FormField
|
|
|
|
{
|
2017-04-11 08:06:23 +02:00
|
|
|
protected $schemaDataType = self::SCHEMA_DATA_TYPE_SINGLESELECT;
|
|
|
|
|
2017-07-03 02:21:27 +02:00
|
|
|
/** @skipUpgrade */
|
2017-04-11 08:06:23 +02:00
|
|
|
protected $schemaComponent = 'TreeDropdownField';
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
private static $url_handlers = array(
|
|
|
|
'$Action!/$ID' => '$Action'
|
|
|
|
);
|
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'tree'
|
|
|
|
);
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-08-14 17:50:17 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var int
|
|
|
|
* @see {@link Hierarchy::$node_threshold_total}.
|
|
|
|
*/
|
|
|
|
private static $node_threshold_total = 30;
|
|
|
|
|
2017-05-11 23:26:38 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-05-16 03:02:36 +02:00
|
|
|
protected $emptyString = null;
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-05-11 23:26:38 +02:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
2017-05-16 03:02:36 +02:00
|
|
|
protected $hasEmptyDefault = false;
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
2017-03-29 06:23:49 +02:00
|
|
|
* Class name for underlying object
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $sourceObject = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of key field on underlying object
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $keyField = null;
|
|
|
|
|
|
|
|
/**
|
2017-07-26 08:13:56 +02:00
|
|
|
* Name of label field on underlying object
|
2017-03-29 06:23:49 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $labelField = null;
|
|
|
|
|
2017-07-26 08:13:56 +02:00
|
|
|
/**
|
|
|
|
* Similar to labelField but for non-html equivalent of field
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $titleField = 'Title';
|
|
|
|
|
2017-03-29 06:23:49 +02:00
|
|
|
/**
|
|
|
|
* Callback for filtering records
|
|
|
|
*
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
protected $filterCallback = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback for marking record as disabled
|
|
|
|
*
|
|
|
|
* @var callable
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-03-29 06:23:49 +02:00
|
|
|
protected $disableCallback = null;
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
2017-03-29 06:23:49 +02:00
|
|
|
* Callback for searching records. This callback takes the following arguments:
|
|
|
|
* - sourceObject Object class to search
|
|
|
|
* - labelField Label field
|
|
|
|
* - search Search text
|
|
|
|
*
|
|
|
|
* @var callable
|
|
|
|
*/
|
|
|
|
protected $searchCallback = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter for base record
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $baseID = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default child method in Hierarchy->getChildrenAsUL
|
|
|
|
*
|
|
|
|
* @var string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
protected $childrenMethod = 'AllChildrenIncludingDeleted';
|
|
|
|
|
|
|
|
/**
|
2017-03-29 06:23:49 +02:00
|
|
|
* Default child counting method in Hierarchy->getChildrenAsUL
|
|
|
|
*
|
|
|
|
* @var string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
protected $numChildrenMethod = 'numChildren';
|
|
|
|
|
|
|
|
/**
|
2017-03-29 06:23:49 +02:00
|
|
|
* Current string value for search text to filter on
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $search = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of ids in current search result (keys are ids, values are true)
|
2017-07-11 06:38:55 +02:00
|
|
|
* This includes parents of search result children which may not be an actual result
|
2017-03-29 06:23:49 +02:00
|
|
|
*
|
|
|
|
* @var array
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
2017-03-29 06:23:49 +02:00
|
|
|
protected $searchIds = [];
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
/**
|
|
|
|
* List of ids which matches the search result
|
|
|
|
* This excludes parents of search result children
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $realSearchIds = [];
|
2017-03-29 06:23:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if search should be shown
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $showSearch = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of ids which have their search expanded (keys are ids, values are true)
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $searchExpanded = [];
|
2017-10-16 04:10:05 +02:00
|
|
|
|
2017-08-10 07:00:27 +02:00
|
|
|
/**
|
|
|
|
* Show full path for selected options, only applies for single select
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $showSelectedPath = false;
|
2017-10-16 04:10:05 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected static $cacheKeyCache = [];
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* CAVEAT: for search to work properly $labelField must be a database field,
|
|
|
|
* or you need to setSearchFunction.
|
|
|
|
*
|
|
|
|
* @param string $name the field name
|
|
|
|
* @param string $title the field label
|
2017-03-29 06:23:49 +02:00
|
|
|
* @param string $sourceObject A DataObject class name with the {@link Hierarchy} extension.
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param string $keyField to field on the source class to save as the
|
|
|
|
* field value (default ID).
|
|
|
|
* @param string $labelField the field name to show as the human-readable
|
|
|
|
* value on the tree (default Title).
|
|
|
|
* @param bool $showSearch enable the ability to search the tree by
|
|
|
|
* entering the text in the input field.
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$name,
|
|
|
|
$title = null,
|
2017-03-29 06:23:49 +02:00
|
|
|
$sourceObject = null,
|
2016-11-29 00:31:16 +01:00
|
|
|
$keyField = 'ID',
|
|
|
|
$labelField = 'TreeTitle',
|
|
|
|
$showSearch = true
|
|
|
|
) {
|
2017-03-29 06:23:49 +02:00
|
|
|
if (!is_a($sourceObject, DataObject::class, true)) {
|
|
|
|
throw new InvalidArgumentException("SourceObject must be a DataObject subclass");
|
|
|
|
}
|
|
|
|
if (!DataObject::has_extension($sourceObject, Hierarchy::class)) {
|
|
|
|
throw new InvalidArgumentException("SourceObject must have Hierarchy extension");
|
|
|
|
}
|
2017-07-26 08:13:56 +02:00
|
|
|
$this->setSourceObject($sourceObject);
|
|
|
|
$this->setKeyField($keyField);
|
|
|
|
$this->setLabelField($labelField);
|
|
|
|
$this->setShowSearch($showSearch);
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-03-29 06:23:49 +02:00
|
|
|
// Extra settings for Folders
|
|
|
|
if (strcasecmp($sourceObject, Folder::class) === 0) {
|
2017-07-26 08:13:56 +02:00
|
|
|
$this->setChildrenMethod('ChildFolders');
|
|
|
|
$this->setNumChildrenMethod('numChildFolders');
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->addExtraClass('single');
|
|
|
|
|
|
|
|
parent::__construct($name, $title);
|
|
|
|
}
|
|
|
|
|
2017-07-26 08:13:56 +02:00
|
|
|
/**
|
|
|
|
* Set the ID of the root node of the tree. This defaults to 0 - i.e.
|
|
|
|
* displays the whole tree.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getTreeBaseID()
|
|
|
|
{
|
|
|
|
return $this->baseID;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Set the ID of the root node of the tree. This defaults to 0 - i.e.
|
|
|
|
* displays the whole tree.
|
|
|
|
*
|
|
|
|
* @param int $ID
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setTreeBaseID($ID)
|
|
|
|
{
|
|
|
|
$this->baseID = (int) $ID;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-07-26 08:13:56 +02:00
|
|
|
/**
|
|
|
|
* Get a callback used to filter the values of the tree before
|
|
|
|
* displaying to the user.
|
|
|
|
*
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
public function getFilterFunction()
|
|
|
|
{
|
|
|
|
return $this->filterCallback;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Set a callback used to filter the values of the tree before
|
|
|
|
* displaying to the user.
|
|
|
|
*
|
2017-07-26 08:13:56 +02:00
|
|
|
* @param callable $callback
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setFilterFunction($callback)
|
|
|
|
{
|
|
|
|
if (!is_callable($callback, true)) {
|
|
|
|
throw new InvalidArgumentException('TreeDropdownField->setFilterCallback(): not passed a valid callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->filterCallback = $callback;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-07-26 08:13:56 +02:00
|
|
|
/**
|
|
|
|
* Get the callback used to disable checkboxes for some items in the tree
|
|
|
|
*
|
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
public function getDisableFunction()
|
|
|
|
{
|
|
|
|
return $this->disableCallback;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Set a callback used to disable checkboxes for some items in the tree
|
|
|
|
*
|
2017-07-26 08:13:56 +02:00
|
|
|
* @param callable $callback
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setDisableFunction($callback)
|
|
|
|
{
|
|
|
|
if (!is_callable($callback, true)) {
|
|
|
|
throw new InvalidArgumentException('TreeDropdownField->setDisableFunction(): not passed a valid callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->disableCallback = $callback;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a callback used to search the hierarchy globally, even before
|
|
|
|
* applying the filter.
|
|
|
|
*
|
2017-07-26 08:13:56 +02:00
|
|
|
* @return callable
|
|
|
|
*/
|
|
|
|
public function getSearchFunction()
|
|
|
|
{
|
|
|
|
return $this->searchCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a callback used to search the hierarchy globally, even before
|
|
|
|
* applying the filter.
|
|
|
|
*
|
|
|
|
* @param callable $callback
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setSearchFunction($callback)
|
|
|
|
{
|
|
|
|
if (!is_callable($callback, true)) {
|
|
|
|
throw new InvalidArgumentException('TreeDropdownField->setSearchFunction(): not passed a valid callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->searchCallback = $callback;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-03-29 06:23:49 +02:00
|
|
|
/**
|
|
|
|
* Check if search is shown
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
public function getShowSearch()
|
|
|
|
{
|
|
|
|
return $this->showSearch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $bool
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setShowSearch($bool)
|
|
|
|
{
|
|
|
|
$this->showSearch = $bool;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-07-26 08:13:56 +02:00
|
|
|
/**
|
|
|
|
* Get method to invoke on each node to get the child collection
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getChildrenMethod()
|
|
|
|
{
|
|
|
|
return $this->childrenMethod;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param string $method The parameter to ChildrenMethod to use when calling Hierarchy->getChildrenAsUL in
|
|
|
|
* {@link Hierarchy}. The method specified determines the structure of the returned list. Use "ChildFolders"
|
|
|
|
* in place of the default to get a drop-down listing with only folders, i.e. not including the child elements in
|
|
|
|
* the currently selected folder. setNumChildrenMethod() should be used as well for proper functioning.
|
|
|
|
*
|
|
|
|
* See {@link Hierarchy} for a complete list of possible methods.
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setChildrenMethod($method)
|
|
|
|
{
|
|
|
|
$this->childrenMethod = $method;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-07-26 08:13:56 +02:00
|
|
|
/**
|
|
|
|
* Get method to invoke on nodes to count children
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNumChildrenMethod()
|
|
|
|
{
|
|
|
|
return $this->numChildrenMethod;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param string $method The parameter to numChildrenMethod to use when calling Hierarchy->getChildrenAsUL in
|
|
|
|
* {@link Hierarchy}. Should be used in conjunction with setChildrenMethod().
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setNumChildrenMethod($method)
|
|
|
|
{
|
|
|
|
$this->numChildrenMethod = $method;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function extraClass()
|
|
|
|
{
|
2017-07-26 08:13:56 +02:00
|
|
|
return implode(' ', array(parent::extraClass(), ($this->getShowSearch() ? "searchable" : null)));
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the whole tree of a part of the tree via an AJAX request.
|
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
2017-03-29 06:23:49 +02:00
|
|
|
* @return HTTPResponse
|
2016-11-29 00:31:16 +01:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function tree(HTTPRequest $request)
|
|
|
|
{
|
|
|
|
// Regular source specification
|
|
|
|
$isSubTree = false;
|
|
|
|
|
|
|
|
$this->search = $request->requestVar('search');
|
|
|
|
$id = (is_numeric($request->latestParam('ID')))
|
|
|
|
? (int)$request->latestParam('ID')
|
|
|
|
: (int)$request->requestVar('ID');
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
// pre-process the tree - search needs to operate globally, not locally as marking filter does
|
|
|
|
if ($this->search) {
|
|
|
|
$this->populateIDs();
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/** @var DataObject|Hierarchy $obj */
|
|
|
|
$obj = null;
|
2017-07-26 08:13:56 +02:00
|
|
|
$sourceObject = $this->getSourceObject();
|
2018-09-26 07:24:06 +02:00
|
|
|
|
2018-10-01 00:29:51 +02:00
|
|
|
// Precache numChildren count if possible.
|
|
|
|
if ($this->getNumChildrenMethod() == 'numChildren') {
|
|
|
|
// We're not calling `Hierarchy::prepopulateTreeDataCache()` because we're not customising results based
|
|
|
|
// on version or Fluent locales. So there would be no performance gain from additional caching.
|
|
|
|
Hierarchy::prepopulate_numchildren_cache($sourceObject);
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($id && !$request->requestVar('forceFullTree')) {
|
2017-07-26 08:13:56 +02:00
|
|
|
$obj = DataObject::get_by_id($sourceObject, $id);
|
2016-11-29 00:31:16 +01:00
|
|
|
$isSubTree = true;
|
|
|
|
if (!$obj) {
|
|
|
|
throw new Exception(
|
2017-07-26 08:13:56 +02:00
|
|
|
"TreeDropdownField->tree(): the object #$id of type $sourceObject could not be found"
|
2016-11-29 00:31:16 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2017-07-26 08:13:56 +02:00
|
|
|
if ($this->getTreeBaseID()) {
|
|
|
|
$obj = DataObject::get_by_id($sourceObject, $this->getTreeBaseID());
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-07-26 08:13:56 +02:00
|
|
|
if (!$this->getTreeBaseID() || !$obj) {
|
|
|
|
$obj = DataObject::singleton($sourceObject);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 06:23:49 +02:00
|
|
|
// Create marking set
|
2017-08-14 17:50:17 +02:00
|
|
|
$markingSet = MarkedSet::create(
|
|
|
|
$obj,
|
|
|
|
$this->getChildrenMethod(),
|
|
|
|
$this->getNumChildrenMethod(),
|
|
|
|
$this->config()->get('node_threshold_total')
|
|
|
|
);
|
2017-03-29 06:23:49 +02:00
|
|
|
|
|
|
|
// Set filter on searched nodes
|
2017-07-26 08:13:56 +02:00
|
|
|
if ($this->getFilterFunction() || $this->search) {
|
2017-03-29 06:23:49 +02:00
|
|
|
// Rely on filtering to limit tree
|
|
|
|
$markingSet->setMarkingFilterFunction(function ($node) {
|
|
|
|
return $this->filterMarking($node);
|
|
|
|
});
|
|
|
|
$markingSet->setLimitingEnabled(false);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
|
2017-03-29 06:23:49 +02:00
|
|
|
// Begin marking
|
|
|
|
$markingSet->markPartialTree();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-03-29 06:23:49 +02:00
|
|
|
// Allow to pass values to be selected within the ajax request
|
|
|
|
$value = $request->requestVar('forceValue') ?: $this->value;
|
|
|
|
if ($value && ($values = preg_split('/,\s*/', $value))) {
|
|
|
|
foreach ($values as $value) {
|
|
|
|
if (!$value || $value == 'unchanged') {
|
|
|
|
continue;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-03-29 06:23:49 +02:00
|
|
|
|
2017-05-19 02:15:36 +02:00
|
|
|
$object = $this->objectForKey($value);
|
|
|
|
if (!$object) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$markingSet->markToExpose($object);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-29 06:23:49 +02:00
|
|
|
// Set title formatter
|
|
|
|
$customised = function (DataObject $child) use ($isSubTree) {
|
|
|
|
return [
|
|
|
|
'name' => $this->getName(),
|
2017-07-26 08:13:56 +02:00
|
|
|
'id' => $child->obj($this->getKeyField()),
|
|
|
|
'title' => $child->obj($this->getTitleField()),
|
|
|
|
'treetitle' => $child->obj($this->getLabelField()),
|
2017-03-29 06:23:49 +02:00
|
|
|
'disabled' => $this->nodeIsDisabled($child),
|
|
|
|
'isSubTree' => $isSubTree
|
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
};
|
|
|
|
|
2017-03-29 06:23:49 +02:00
|
|
|
// Determine output format
|
|
|
|
if ($request->requestVar('format') === 'json') {
|
|
|
|
// Format JSON output
|
|
|
|
$json = $markingSet
|
|
|
|
->getChildrenAsArray($customised);
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2017-07-21 00:20:17 +02:00
|
|
|
if ($request->requestVar('flatList')) {
|
2017-07-11 06:38:55 +02:00
|
|
|
// format and filter $json here
|
|
|
|
$json['children'] = $this->flattenChildrenArray($json['children']);
|
|
|
|
}
|
2017-03-29 06:23:49 +02:00
|
|
|
return HTTPResponse::create()
|
|
|
|
->addHeader('Content-Type', 'application/json')
|
|
|
|
->setBody(json_encode($json));
|
2016-11-29 00:31:16 +01:00
|
|
|
} else {
|
2017-03-29 06:23:49 +02:00
|
|
|
// Return basic html
|
|
|
|
$html = $markingSet->renderChildren(
|
|
|
|
[self::class . '_HTML', 'type' => 'Includes'],
|
|
|
|
$customised
|
2016-11-29 00:31:16 +01:00
|
|
|
);
|
2017-03-29 06:23:49 +02:00
|
|
|
return HTTPResponse::create()
|
|
|
|
->addHeader('Content-Type', 'text/html')
|
|
|
|
->setBody($html);
|
2016-11-29 00:31:16 +01: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,
|
|
|
|
* filter on that too. Return true if all applicable conditions are true, false otherwise.
|
|
|
|
*
|
2017-03-29 06:23:49 +02:00
|
|
|
* @param DataObject $node
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function filterMarking($node)
|
|
|
|
{
|
2017-07-26 08:13:56 +02:00
|
|
|
$callback = $this->getFilterFunction();
|
|
|
|
if ($callback && !call_user_func($callback, $node)) {
|
2016-11-29 00:31:16 +01:00
|
|
|
return false;
|
|
|
|
}
|
2017-03-29 06:23:49 +02:00
|
|
|
|
|
|
|
if ($this->search) {
|
2016-11-29 00:31:16 +01:00
|
|
|
return isset($this->searchIds[$node->ID]) && $this->searchIds[$node->ID] ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marking a specific node in the tree as disabled
|
|
|
|
* @param $node
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function nodeIsDisabled($node)
|
|
|
|
{
|
2017-07-26 08:13:56 +02:00
|
|
|
$callback = $this->getDisableFunction();
|
|
|
|
return $callback && call_user_func($callback, $node);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-10-16 04:10:05 +02:00
|
|
|
|
2017-08-30 12:41:56 +02:00
|
|
|
/**
|
|
|
|
* Attributes to be given for this field type
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAttributes()
|
|
|
|
{
|
|
|
|
$attributes = array(
|
|
|
|
'class' => $this->extraClass(),
|
|
|
|
'id' => $this->ID(),
|
|
|
|
'data-schema' => json_encode($this->getSchemaData()),
|
|
|
|
'data-state' => json_encode($this->getSchemaState()),
|
|
|
|
);
|
2017-10-16 04:10:05 +02:00
|
|
|
|
2017-08-30 12:41:56 +02:00
|
|
|
$attributes = array_merge($attributes, $this->attributes);
|
2017-10-16 04:10:05 +02:00
|
|
|
|
2017-08-30 12:41:56 +02:00
|
|
|
$this->extend('updateAttributes', $attributes);
|
2017-10-16 04:10:05 +02:00
|
|
|
|
2017-08-30 12:41:56 +02:00
|
|
|
return $attributes;
|
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
2017-10-26 02:04:30 +02:00
|
|
|
* HTML-encoded label for this node, including css classes and other markup.
|
|
|
|
*
|
2018-09-28 10:46:36 +02:00
|
|
|
* @deprecated 4.0.0:5.0.0 Use setTitleField()
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param string $field
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setLabelField($field)
|
|
|
|
{
|
|
|
|
$this->labelField = $field;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-26 02:04:30 +02:00
|
|
|
* HTML-encoded label for this node, including css classes and other markup.
|
|
|
|
*
|
2018-09-28 10:46:36 +02:00
|
|
|
* @deprecated 4.0.0:5.0.0 Use getTitleField()
|
2017-07-26 08:13:56 +02:00
|
|
|
* @return string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function getLabelField()
|
|
|
|
{
|
|
|
|
return $this->labelField;
|
|
|
|
}
|
|
|
|
|
2017-07-26 08:13:56 +02:00
|
|
|
/**
|
2017-10-26 02:04:30 +02:00
|
|
|
* Field to use for plain text item titles.
|
2017-07-26 08:13:56 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitleField()
|
|
|
|
{
|
|
|
|
return $this->titleField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set field to use for item title
|
|
|
|
*
|
|
|
|
* @param string $field
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setTitleField($field)
|
|
|
|
{
|
|
|
|
$this->titleField = $field;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param string $field
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setKeyField($field)
|
|
|
|
{
|
|
|
|
$this->keyField = $field;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-01 22:47:17 +02:00
|
|
|
* @return string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function getKeyField()
|
|
|
|
{
|
|
|
|
return $this->keyField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setSourceObject($class)
|
|
|
|
{
|
|
|
|
$this->sourceObject = $class;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-07-26 08:13:56 +02:00
|
|
|
* Get class of source object
|
|
|
|
*
|
|
|
|
* @return string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function getSourceObject()
|
|
|
|
{
|
|
|
|
return $this->sourceObject;
|
|
|
|
}
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
/**
|
|
|
|
* Flattens a given list of children array items, so the data is no longer
|
|
|
|
* structured in a hierarchy
|
|
|
|
*
|
|
|
|
* NOTE: uses {@link TreeDropdownField::$realSearchIds} to filter items by if there is a search
|
|
|
|
*
|
|
|
|
* @param array $children - the list of children, which could contain their own children
|
|
|
|
* @param array $parentTitles - a list of parent titles, which we use to construct the contextString
|
|
|
|
* @return array - flattened list of children
|
|
|
|
*/
|
|
|
|
protected function flattenChildrenArray($children, $parentTitles = [])
|
|
|
|
{
|
|
|
|
$output = [];
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
foreach ($children as $child) {
|
|
|
|
$childTitles = array_merge($parentTitles, [$child['title']]);
|
|
|
|
$grandChildren = $child['children'];
|
|
|
|
$contextString = implode('/', $parentTitles);
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2018-01-16 19:39:30 +01:00
|
|
|
$child['contextString'] = ($contextString !== '') ? $contextString . '/' : '';
|
2017-07-21 00:20:17 +02:00
|
|
|
unset($child['children']);
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
if (!$this->search || in_array($child['id'], $this->realSearchIds)) {
|
|
|
|
$output[] = $child;
|
|
|
|
}
|
|
|
|
$output = array_merge($output, $this->flattenChildrenArray($grandChildren, $childTitles));
|
|
|
|
}
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
return $output;
|
|
|
|
}
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Populate $this->searchIds with the IDs of the pages matching the searched parameter and their parents.
|
|
|
|
* Reverse-constructs the tree starting from the leaves. Initially taken from CMSSiteTreeFilter, but modified
|
|
|
|
* with pluggable search function.
|
|
|
|
*/
|
|
|
|
protected function populateIDs()
|
|
|
|
{
|
|
|
|
// get all the leaves to be displayed
|
2017-07-11 06:38:55 +02:00
|
|
|
$res = $this->getSearchResults();
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
if (!$res) {
|
|
|
|
return;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
// iteratively fetch the parents in bulk, until all the leaves can be accessed using the tree control
|
|
|
|
foreach ($res as $row) {
|
|
|
|
if ($row->ParentID) {
|
|
|
|
$parents[$row->ParentID] = true;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-07-11 06:38:55 +02:00
|
|
|
$this->searchIds[$row->ID] = true;
|
|
|
|
}
|
|
|
|
$this->realSearchIds = $res->column();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-07-26 08:13:56 +02:00
|
|
|
$sourceObject = $this->getSourceObject();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
while (!empty($parents)) {
|
|
|
|
$items = DataObject::get($sourceObject)
|
|
|
|
->filter("ID", array_keys($parents));
|
|
|
|
$parents = array();
|
2016-11-29 00:31:16 +01:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
foreach ($items as $item) {
|
|
|
|
if ($item->ParentID) {
|
|
|
|
$parents[$item->ParentID] = true;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-07-11 06:38:55 +02:00
|
|
|
$this->searchIds[$item->ID] = true;
|
|
|
|
$this->searchExpanded[$item->ID] = true;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-26 08:13:56 +02:00
|
|
|
|
2017-07-11 06:38:55 +02:00
|
|
|
/**
|
|
|
|
* Get the DataObjects that matches the searched parameter.
|
|
|
|
*
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
protected function getSearchResults()
|
|
|
|
{
|
2017-07-26 08:13:56 +02:00
|
|
|
$callback = $this->getSearchFunction();
|
|
|
|
if ($callback) {
|
|
|
|
return call_user_func($callback, $this->getSourceObject(), $this->getLabelField(), $this->search);
|
2017-07-11 06:38:55 +02:00
|
|
|
}
|
2017-07-26 08:13:56 +02:00
|
|
|
|
|
|
|
$sourceObject = $this->getSourceObject();
|
2017-07-11 06:38:55 +02:00
|
|
|
$filters = array();
|
2017-10-26 02:04:30 +02:00
|
|
|
$sourceObjectInstance = DataObject::singleton($sourceObject);
|
|
|
|
$candidates = array_unique([
|
|
|
|
$this->getLabelField(),
|
|
|
|
$this->getTitleField(),
|
|
|
|
'Title',
|
|
|
|
'Name'
|
|
|
|
]);
|
|
|
|
foreach ($candidates as $candidate) {
|
|
|
|
if ($sourceObjectInstance->hasDatabaseField($candidate)) {
|
|
|
|
$filters["{$candidate}:PartialMatch"] = $this->search;
|
2017-07-11 06:38:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($filters)) {
|
|
|
|
throw new InvalidArgumentException(sprintf(
|
|
|
|
'Cannot query by %s.%s, not a valid database column',
|
|
|
|
$sourceObject,
|
2017-10-26 02:04:30 +02:00
|
|
|
$this->getTitleField()
|
2017-07-11 06:38:55 +02:00
|
|
|
));
|
|
|
|
}
|
2017-07-26 08:13:56 +02:00
|
|
|
return DataObject::get($this->getSourceObject())->filterAny($filters);
|
2017-07-11 06:38:55 +02:00
|
|
|
}
|
2016-11-29 00:31:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the object where the $keyField is equal to a certain value
|
|
|
|
*
|
|
|
|
* @param string|int $key
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
|
|
|
protected function objectForKey($key)
|
|
|
|
{
|
2017-07-26 08:13:56 +02:00
|
|
|
return DataObject::get($this->getSourceObject())
|
|
|
|
->filter($this->getKeyField(), $key)
|
2016-11-29 00:31:16 +01:00
|
|
|
->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes this field to the readonly field.
|
|
|
|
*/
|
|
|
|
public function performReadonlyTransformation()
|
|
|
|
{
|
|
|
|
/** @var TreeDropdownField_Readonly $copy */
|
2017-03-29 06:23:49 +02:00
|
|
|
$copy = $this->castedCopy(TreeDropdownField_Readonly::class);
|
2017-07-26 08:13:56 +02:00
|
|
|
$copy->setKeyField($this->getKeyField());
|
|
|
|
$copy->setLabelField($this->getLabelField());
|
|
|
|
$this->setTitleField($this->getTitleField());
|
|
|
|
$copy->setSourceObject($this->getSourceObject());
|
2016-11-29 00:31:16 +01:00
|
|
|
return $copy;
|
|
|
|
}
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-05-17 06:51:30 +02:00
|
|
|
/**
|
|
|
|
* @param string|FormField $classOrCopy
|
|
|
|
* @return FormField
|
|
|
|
*/
|
|
|
|
public function castedCopy($classOrCopy)
|
|
|
|
{
|
|
|
|
$field = $classOrCopy;
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-05-17 06:51:30 +02:00
|
|
|
if (!is_object($field)) {
|
2017-07-26 08:13:56 +02:00
|
|
|
$field = new $classOrCopy($this->name, $this->title, $this->getSourceObject());
|
2017-05-17 06:51:30 +02:00
|
|
|
}
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-05-17 06:51:30 +02:00
|
|
|
return parent::castedCopy($field);
|
|
|
|
}
|
2017-04-11 08:06:23 +02:00
|
|
|
|
|
|
|
public function getSchemaStateDefaults()
|
|
|
|
{
|
2017-05-15 05:18:35 +02:00
|
|
|
$data = parent::getSchemaStateDefaults();
|
2017-08-10 07:00:27 +02:00
|
|
|
/** @var Hierarchy|DataObject $record */
|
2017-04-11 08:06:23 +02:00
|
|
|
$record = $this->Value() ? $this->objectForKey($this->Value()) : null;
|
|
|
|
|
2017-10-16 04:10:05 +02:00
|
|
|
$data['data']['cacheKey'] = $this->getCacheKey();
|
2017-08-10 07:00:27 +02:00
|
|
|
$data['data']['showSelectedPath'] = $this->getShowSelectedPath();
|
2017-04-11 08:06:23 +02:00
|
|
|
if ($record) {
|
2017-08-10 07:00:27 +02:00
|
|
|
$titlePath = '';
|
2017-10-16 04:10:05 +02:00
|
|
|
|
2017-08-10 07:00:27 +02:00
|
|
|
if ($this->getShowSelectedPath()) {
|
|
|
|
$ancestors = $record->getAncestors(true)->reverse();
|
2017-10-16 04:10:05 +02:00
|
|
|
|
2017-08-10 07:00:27 +02:00
|
|
|
foreach ($ancestors as $parent) {
|
|
|
|
$title = $parent->obj($this->getTitleField())->getValue();
|
2018-01-16 19:39:30 +01:00
|
|
|
$titlePath .= $title . '/';
|
2017-08-10 07:00:27 +02:00
|
|
|
}
|
|
|
|
}
|
2017-04-11 08:06:23 +02:00
|
|
|
$data['data']['valueObject'] = [
|
2017-07-26 08:13:56 +02:00
|
|
|
'id' => $record->obj($this->getKeyField())->getValue(),
|
|
|
|
'title' => $record->obj($this->getTitleField())->getValue(),
|
|
|
|
'treetitle' => $record->obj($this->getLabelField())->getSchemaValue(),
|
2017-08-10 07:00:27 +02:00
|
|
|
'titlePath' => $titlePath,
|
2017-04-11 08:06:23 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2017-10-16 04:10:05 +02:00
|
|
|
/**
|
|
|
|
* Ensure cache is keyed by last modified datetime of the underlying list.
|
|
|
|
* Caches the key for the respective underlying list types, since it doesn't need to query again.
|
|
|
|
*
|
|
|
|
* @return DBDatetime
|
|
|
|
*/
|
|
|
|
protected function getCacheKey()
|
|
|
|
{
|
|
|
|
$target = $this->getSourceObject();
|
|
|
|
if (!isset(self::$cacheKeyCache[$target])) {
|
|
|
|
self::$cacheKeyCache[$target] = DataList::create($target)->max('LastEdited');
|
|
|
|
}
|
|
|
|
return self::$cacheKeyCache[$target];
|
|
|
|
}
|
|
|
|
|
2017-04-11 08:06:23 +02:00
|
|
|
public function getSchemaDataDefaults()
|
|
|
|
{
|
|
|
|
$data = parent::getSchemaDataDefaults();
|
2017-07-11 06:38:55 +02:00
|
|
|
$data['data'] = array_merge($data['data'], [
|
|
|
|
'urlTree' => $this->Link('tree'),
|
2017-07-26 08:13:56 +02:00
|
|
|
'showSearch' => $this->getShowSearch(),
|
2017-07-11 06:38:55 +02:00
|
|
|
'emptyString' => $this->getEmptyString(),
|
|
|
|
'hasEmptyDefault' => $this->getHasEmptyDefault(),
|
2017-07-21 00:20:17 +02:00
|
|
|
'multiple' => false,
|
2017-07-11 06:38:55 +02:00
|
|
|
]);
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-04-11 08:06:23 +02:00
|
|
|
return $data;
|
|
|
|
}
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-05-11 23:26:38 +02:00
|
|
|
/**
|
2017-05-16 03:02:36 +02:00
|
|
|
* @param boolean $bool
|
|
|
|
* @return self Self reference
|
2017-05-11 23:26:38 +02:00
|
|
|
*/
|
2017-05-16 03:02:36 +02:00
|
|
|
public function setHasEmptyDefault($bool)
|
2017-05-11 23:26:38 +02:00
|
|
|
{
|
2017-05-16 03:02:36 +02:00
|
|
|
$this->hasEmptyDefault = $bool;
|
2017-05-11 23:26:38 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-04-11 08:06:23 +02:00
|
|
|
/**
|
2017-05-11 23:26:38 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2017-05-16 03:02:36 +02:00
|
|
|
public function getHasEmptyDefault()
|
2017-05-11 23:26:38 +02:00
|
|
|
{
|
2017-05-16 03:02:36 +02:00
|
|
|
return $this->hasEmptyDefault;
|
2017-05-11 23:26:38 +02:00
|
|
|
}
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-05-11 23:26:38 +02:00
|
|
|
/**
|
2017-05-16 03:02:36 +02:00
|
|
|
* Set the default selection label, e.g. "select...".
|
|
|
|
* Defaults to an empty string. Automatically sets
|
|
|
|
* {@link $hasEmptyDefault} to true.
|
2017-05-11 23:26:38 +02:00
|
|
|
*
|
2017-05-16 03:02:36 +02:00
|
|
|
* @param string $string
|
2017-05-11 23:26:38 +02:00
|
|
|
* @return $this
|
|
|
|
*/
|
2017-05-16 03:02:36 +02:00
|
|
|
public function setEmptyString($string)
|
2017-05-11 23:26:38 +02:00
|
|
|
{
|
2017-05-16 03:02:36 +02:00
|
|
|
$this->setHasEmptyDefault(true);
|
|
|
|
$this->emptyString = $string;
|
2017-05-11 23:26:38 +02:00
|
|
|
return $this;
|
|
|
|
}
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-05-11 23:26:38 +02:00
|
|
|
/**
|
2017-04-11 08:06:23 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2017-05-16 03:02:36 +02:00
|
|
|
public function getEmptyString()
|
2017-04-11 08:06:23 +02:00
|
|
|
{
|
2017-05-16 03:02:36 +02:00
|
|
|
if ($this->emptyString !== null) {
|
|
|
|
return $this->emptyString;
|
2017-05-11 23:26:38 +02:00
|
|
|
}
|
2017-05-19 02:15:36 +02:00
|
|
|
|
2017-07-26 08:13:56 +02:00
|
|
|
$item = DataObject::singleton($this->getSourceObject());
|
2017-05-16 03:02:36 +02:00
|
|
|
$emptyString = _t(
|
2017-04-20 03:15:24 +02:00
|
|
|
'SilverStripe\\Forms\\DropdownField.CHOOSE_MODEL',
|
2017-04-11 08:06:23 +02:00
|
|
|
'(Choose {name})',
|
|
|
|
['name' => $item->i18n_singular_name()]
|
|
|
|
);
|
2017-05-16 03:02:36 +02:00
|
|
|
return $emptyString;
|
2017-04-11 08:06:23 +02:00
|
|
|
}
|
2017-10-16 04:10:05 +02:00
|
|
|
|
2017-08-10 07:00:27 +02:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getShowSelectedPath()
|
|
|
|
{
|
|
|
|
return $this->showSelectedPath;
|
|
|
|
}
|
2017-10-16 04:10:05 +02:00
|
|
|
|
2017-08-10 07:00:27 +02:00
|
|
|
/**
|
|
|
|
* @param bool $showSelectedPath
|
|
|
|
* @return TreeDropdownField
|
|
|
|
*/
|
|
|
|
public function setShowSelectedPath($showSelectedPath)
|
|
|
|
{
|
|
|
|
$this->showSelectedPath = $showSelectedPath;
|
|
|
|
return $this;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|