2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
2011-08-01 04:27:52 +02:00
|
|
|
* Dropdown-like field that allows you to select an item from a hierarchical
|
|
|
|
* AJAX-expandable tree.
|
2010-10-15 05:55:22 +02:00
|
|
|
*
|
2011-08-01 04:27:52 +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
|
|
|
|
* joins related to hierarchy or a hierarchy based filter.
|
2010-10-15 05:55:22 +02:00
|
|
|
*
|
2011-08-01 04:27:52 +02:00
|
|
|
* **Note:** your source object must use an implementation of hierarchy for this
|
|
|
|
* field to generate the tree correctly, e.g. {@link Group}, {@link SiteTree} etc.
|
2010-10-15 05:55:22 +02:00
|
|
|
*
|
2011-08-01 04:27:52 +02:00
|
|
|
* All operations are carried out through javascript and provides no fallback
|
|
|
|
* to non JS.
|
2010-10-15 05:55:22 +02:00
|
|
|
*
|
|
|
|
* <b>Usage</b>.
|
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* static $has_one = array(
|
|
|
|
* 'RightContent' => 'SiteTree'
|
|
|
|
* );
|
|
|
|
*
|
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>
|
|
|
|
*
|
2011-08-01 04:27:52 +02:00
|
|
|
* This will generate a tree allowing the user to expand and contract subsections
|
|
|
|
* to find the appropriate page to save to the field.
|
2010-10-19 00:50:48 +02:00
|
|
|
*
|
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.
|
2009-10-11 02:07:05 +02:00
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
2011-08-01 04:27:52 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
class TreeDropdownField extends FormField {
|
2009-10-11 02:07:05 +02:00
|
|
|
|
2011-08-01 04:27:52 +02:00
|
|
|
public static $url_handlers = array(
|
2009-10-11 02:07:05 +02:00
|
|
|
'$Action!/$ID' => '$Action'
|
|
|
|
);
|
|
|
|
|
2011-08-01 04:27:52 +02:00
|
|
|
public static $allowed_actions = array(
|
2009-10-11 02:07:05 +02:00
|
|
|
'tree'
|
|
|
|
);
|
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
|
|
|
*/
|
2010-04-12 05:33:56 +02:00
|
|
|
protected $sourceObject, $keyField, $labelField, $filterCallback, $searchCallback, $baseID = 0;
|
2012-03-07 22:07:40 +01:00
|
|
|
/**
|
|
|
|
* @var string default child method in Hierarcy->getChildrenAsUL
|
|
|
|
*/
|
|
|
|
protected $childrenMethod = 'AllChildrenIncludingDeleted';
|
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();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
2011-08-01 04:27:52 +02:00
|
|
|
* CAVEAT: for search to work properly $labelField must be a database field,
|
|
|
|
* 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
|
2011-08-01 04:27:52 +02:00
|
|
|
* @param sourceObject The object-type to list in the tree. Must be a
|
|
|
|
* {@link Hierarchy} subclass. Alternatively, you can set this to 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.
|
|
|
|
*
|
|
|
|
* @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.
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2012-09-26 23:34:00 +02:00
|
|
|
public function __construct($name, $title = null, $sourceObject = 'Group', $keyField = 'ID', $labelField = 'Title',
|
|
|
|
$showSearch = false) {
|
|
|
|
|
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;
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
parent::__construct($name, $title);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-01 04:27:52 +02:00
|
|
|
* Set the ID of the root node of the tree. This defaults to 0 - i.e.
|
|
|
|
* displays the whole tree.
|
2009-10-11 02:07:05 +02:00
|
|
|
*
|
|
|
|
* @param int $ID
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-01 04:27:52 +02:00
|
|
|
* Set a callback used to filter the values of the tree before
|
|
|
|
* displaying to the user.
|
2009-10-11 02:07:05 +02:00
|
|
|
*
|
|
|
|
* @param callback $callback
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->filterCallback = $callback;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2010-04-12 05:33:56 +02:00
|
|
|
/**
|
2011-08-01 04:27:52 +02:00
|
|
|
* Set a callback used to search the hierarchy globally, even before
|
|
|
|
* applying the filter.
|
2010-04-12 05:33:56 +02:00
|
|
|
*
|
|
|
|
* @param callback $callback
|
|
|
|
*/
|
|
|
|
public function setSearchFunction($callback) {
|
|
|
|
if(!is_callable($callback, true)) {
|
|
|
|
throw new InvalidArgumentException('TreeDropdownField->setSearchFunction(): not passed a valid callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
/**
|
|
|
|
* @param Boolean
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* @param $method The parameter to ChildrenMethod to use when calling Hierarchy->getChildrenAsUL in
|
|
|
|
* {@link Hierarchy}. The method specified determined 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.
|
|
|
|
*
|
2012-03-07 04:11:39 +01:00
|
|
|
* See {@link Hierarchy} for a complete list of possible methods.
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-05-01 07:07:41 +02:00
|
|
|
/**
|
2009-10-11 02:07:05 +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()) {
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::add_i18n_javascript(FRAMEWORK_DIR . '/javascript/lang');
|
2010-01-20 04:33:19 +01:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jstree/jquery.jstree.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/TreeDropdownField.js');
|
2010-01-20 04:33:19 +01:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
|
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/TreeDropdownField.css');
|
2010-01-20 04:33:19 +01:00
|
|
|
|
2011-05-08 13:36:13 +02:00
|
|
|
$record = $this->Value() ? $this->objectForKey($this->Value()) : null;
|
|
|
|
if($record) {
|
2009-10-11 02:07:05 +02:00
|
|
|
$title = $record->{$this->labelField};
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2012-04-14 00:16:22 +02:00
|
|
|
$title = _t('DropdownField.CHOOSE', '(Choose)', 'start value of a dropdown');
|
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(
|
|
|
|
'id' => $record ? $record->ID : null,
|
|
|
|
'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,
|
2012-06-13 16:04:14 +02:00
|
|
|
'TitleURLEncoded' => rawurlencode($title),
|
2011-12-22 13:10:57 +01:00
|
|
|
'Metadata' => ($metadata) ? Convert::raw2att(Convert::raw2json($metadata)) : null
|
2009-10-11 02:07:05 +02:00
|
|
|
)
|
|
|
|
);
|
2011-03-23 05:12:25 +01:00
|
|
|
|
|
|
|
return $this->customise($properties)->renderWith('TreeDropdownField');
|
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)));
|
|
|
|
}
|
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.
|
|
|
|
*
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* @param SS_HTTPRequest $request
|
2009-10-11 02:07:05 +02:00
|
|
|
* @return string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
public function tree(SS_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regular source specification
|
2009-10-11 02:07:05 +02:00
|
|
|
$isSubTree = false;
|
2009-11-30 00:29:31 +01:00
|
|
|
|
2011-03-07 20:29:09 +01:00
|
|
|
$this->search = Convert::Raw2SQL($request->requestVar('search'));
|
2012-09-26 23:34:00 +02:00
|
|
|
$ID = (is_numeric($request->latestparam('ID')))
|
|
|
|
? (int)$request->latestparam('ID')
|
|
|
|
: (int)$request->requestVar('ID');
|
|
|
|
|
2012-12-21 01:17:01 +01:00
|
|
|
if($ID && !$request->requestVar('forceFullTree')) {
|
2009-10-11 02:07:05 +02:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2010-04-12 05:36:56 +02:00
|
|
|
// pre-process the tree - search needs to operate globally, not locally as marking filter does
|
2010-04-12 05:33:56 +02:00
|
|
|
if ( $this->search != "" )
|
|
|
|
$this->populateIDs();
|
|
|
|
|
|
|
|
if ($this->filterCallback || $this->sourceObject == 'Folder' || $this->search != "" )
|
2009-11-30 00:29:31 +01:00
|
|
|
$obj->setMarkingFilterFunction(array($this, "filterMarking"));
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
$obj->markPartialTree();
|
|
|
|
|
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);
|
|
|
|
if(($values = preg_split('/,\s*/', $forceValue)) && count($values)) foreach($values as $value) {
|
2010-10-19 00:50:30 +02:00
|
|
|
if(!$value) continue;
|
2010-02-22 05:37:33 +01:00
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
$obj->markToExpose($this->objectForKey($value));
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-09-26 23:34:00 +02:00
|
|
|
$eval = '"<li id=\"selector-' . $this->getName() . '-{$child->' . $this->keyField . '}\" data-id=\"$child->'
|
|
|
|
. $this->keyField . '\" class=\"class-$child->class"'
|
|
|
|
. ' . $child->markingClasses() . "\"><a rel=\"$child->ID\">" . $child->' . $this->labelField . ' . "</a>"';
|
2012-03-07 04:11:39 +01:00
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
if($isSubTree) {
|
2012-03-07 04:11:39 +01:00
|
|
|
return substr(trim($obj->getChildrenAsUL('', $eval, null, true, $this->childrenMethod)), 4, -5);
|
2011-03-02 20:38:02 +01:00
|
|
|
} else {
|
2012-03-07 04:11:39 +01:00
|
|
|
return $obj->getChildrenAsUL('class="tree"', $eval, null, true, $this->childrenMethod);
|
2009-10-11 02:07:05 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-11-30 00:29:31 +01:00
|
|
|
|
|
|
|
/**
|
2012-09-26 23:34:00 +02:00
|
|
|
* Marking public function for the tree, which combines different filters sensibly. If a filter function has been
|
|
|
|
* set, that will be called. If the source is a folder, automatically filter folder. And if search text is set,
|
|
|
|
* filter on that too. Return true if all applicable conditions are true, false otherwise.
|
2009-11-30 00:29:31 +01:00
|
|
|
* @param $node
|
|
|
|
* @return unknown_type
|
|
|
|
*/
|
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;
|
|
|
|
if ($this->sourceObject == "Folder" && $node->ClassName != 'Folder') 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
|
|
|
}
|
2010-04-12 05:33:56 +02:00
|
|
|
|
2009-11-30 00:29:31 +01:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-13 13:51:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $field
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $field
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String $field
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
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
|
2010-04-12 05:33:56 +02:00
|
|
|
if ( $this->searchCallback )
|
|
|
|
$res = call_user_func($this->searchCallback, $this->sourceObject, $this->labelField, $this->search);
|
|
|
|
else
|
2010-10-13 05:56:48 +02:00
|
|
|
$res = DataObject::get($this->sourceObject, "\"$this->labelField\" LIKE '%$this->search%'");
|
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;
|
|
|
|
}
|
|
|
|
while (!empty($parents)) {
|
2012-09-26 23:34:00 +02:00
|
|
|
$res = DB::query('SELECT "ParentID", "ID" FROM "' . $this->sourceObject
|
|
|
|
. '" WHERE "ID" in ('.implode(',',array_keys($parents)).')');
|
2010-04-12 05:33:56 +02:00
|
|
|
$parents = array();
|
|
|
|
|
|
|
|
foreach($res as $row) {
|
|
|
|
if ($row['ParentID']) $parents[$row['ParentID']] = true;
|
|
|
|
$this->searchIds[$row['ID']] = true;
|
|
|
|
$this->searchExpanded[$row['ID']] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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) {
|
|
|
|
if($this->keyField == 'ID') {
|
|
|
|
return DataObject::get_by_id($this->sourceObject, $key);
|
2009-05-01 07:07:41 +02:00
|
|
|
} else {
|
2012-09-26 23:34:00 +02:00
|
|
|
return DataObject::get_one($this->sourceObject, "\"{$this->keyField}\" = '".Convert::raw2sql($key)."'");
|
2009-05-01 07:07:41 +02:00
|
|
|
}
|
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() {
|
2012-12-13 13:51:28 +01:00
|
|
|
$copy = $this->castedCopy('TreeDropdownField_Readonly');
|
|
|
|
$copy->setKeyField($this->keyField);
|
|
|
|
$copy->setLabelField($this->labelField);
|
|
|
|
$copy->setSourceObject($this->sourceObject);
|
|
|
|
|
|
|
|
return $copy;
|
2010-04-12 05:17:09 +02:00
|
|
|
}
|
2012-12-13 13:51:28 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-04-12 05:17:09 +02:00
|
|
|
|
2010-04-23 02:11:41 +02:00
|
|
|
/**
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
2010-04-12 05:17:09 +02:00
|
|
|
class TreeDropdownField_Readonly extends TreeDropdownField {
|
|
|
|
protected $readonly = true;
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2010-04-12 05:17:09 +02:00
|
|
|
$fieldName = $this->labelField;
|
|
|
|
if($this->value) {
|
2010-04-12 07:01:09 +02:00
|
|
|
$keyObj = $this->objectForKey($this->value);
|
2010-04-12 05:17:09 +02:00
|
|
|
$obj = $keyObj ? $keyObj->$fieldName : '';
|
|
|
|
} else {
|
|
|
|
$obj = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$source = array(
|
|
|
|
$this->value => $obj
|
|
|
|
);
|
|
|
|
|
|
|
|
$field = new LookupField($this->name, $this->title, $source);
|
|
|
|
$field->setValue($this->value);
|
|
|
|
$field->setForm($this->form);
|
|
|
|
return $field->Field();
|
|
|
|
}
|
2012-03-27 06:04:11 +02:00
|
|
|
}
|