mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT: Refactored TreeDropdownField to generate and manage the tree using Hierachy's ParentID data, rather than relying on the client.
From: Andrew Short <andrewjshort@gmail.com> git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@88482 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
4a054c0cc5
commit
264b484e82
@ -1,148 +1,181 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Dropdown-like field that gives you a tree of items, using ajax.
|
* Dropdown-like field that allows you to select an item from a hierachical AJAX-expandable tree
|
||||||
|
*
|
||||||
* @package forms
|
* @package forms
|
||||||
* @subpackage fields-relational
|
* @subpackage fields-relational
|
||||||
*/
|
*/
|
||||||
class TreeDropdownField extends FormField {
|
class TreeDropdownField extends FormField {
|
||||||
protected $sourceObject, $keyField, $labelField, $filterFunc;
|
|
||||||
protected $treeBaseID = 0;
|
public static $url_handlers = array (
|
||||||
|
'$Action!/$ID' => '$Action'
|
||||||
|
);
|
||||||
|
|
||||||
|
public static $allowed_actions = array (
|
||||||
|
'tree'
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new tree dropdown field.
|
* @ignore
|
||||||
* @param name The name of the field.
|
|
||||||
* @param title The label of the field.
|
|
||||||
* @param sourceObject The object-type to list in the tree. Must be a 'hierachy' object.
|
|
||||||
* @param keyField The column of that object-type to return as the field value. Defaults to ID
|
|
||||||
* @param labelField The column to show as the human-readable value in the tree. Defaults to Title
|
|
||||||
*/
|
*/
|
||||||
function __construct($name, $title, $sourceObject = "Group", $keyField = "ID", $labelField = "Title") {
|
protected $sourceObject, $keyField, $labelField, $filterCallback, $baseID = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name the field name
|
||||||
|
* @param string $title the field label
|
||||||
|
* @param string $souceClass the class to display in the tree, must have the "Hierachy" extension.
|
||||||
|
* @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).
|
||||||
|
*/
|
||||||
|
public function __construct($name, $title = null, $sourceObject = 'Group', $keyField = 'ID', $labelField = 'Title') {
|
||||||
$this->sourceObject = $sourceObject;
|
$this->sourceObject = $sourceObject;
|
||||||
$this->keyField = $keyField;
|
$this->keyField = $keyField;
|
||||||
$this->labelField = $labelField;
|
$this->labelField = $labelField;
|
||||||
|
|
||||||
|
if(!Object::has_extension($this->sourceObject, 'Hierarchy')) {
|
||||||
|
throw new Exception (
|
||||||
|
"TreeDropdownField: the source class '$this->sourceObject' must have the Hierarchy extension applied"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
|
||||||
|
|
||||||
|
Requirements::javascript(THIRDPARTY_DIR . '/behaviour.js');
|
||||||
|
Requirements::javascript(THIRDPARTY_DIR . '/tree/tree.js');
|
||||||
|
Requirements::javascript(SAPPHIRE_DIR . '/javascript/TreeSelectorField.js');
|
||||||
|
|
||||||
|
Requirements::css(THIRDPARTY_DIR . '/tree/tree.css');
|
||||||
|
Requirements::css(SAPPHIRE_DIR . '/css/TreeDropdownField.css');
|
||||||
|
|
||||||
parent::__construct($name, $title);
|
parent::__construct($name, $title);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setFilterFunction($filterFunc) {
|
|
||||||
$this->filterFunc = $filterFunc;
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Set the root node of the tree. Defaults to 0, ie, the whole tree
|
* Set the ID of the root node of the tree. This defaults to 0 - i.e. displays the whole tree.
|
||||||
|
*
|
||||||
|
* @param int $ID
|
||||||
*/
|
*/
|
||||||
function setTreeBaseID($treeBaseID) {
|
public function setTreeBaseID($ID) {
|
||||||
$this->treeBaseID = $treeBaseID;
|
$this->baseID = (int) $ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Field() {
|
/**
|
||||||
Requirements::css(SAPPHIRE_DIR . '/css/TreeDropdownField.css');
|
* Set a callback used to filter the values of the tree before displaying to the user.
|
||||||
Requirements::javascript(THIRDPARTY_DIR . "/tree/tree.js");
|
*
|
||||||
Requirements::css(THIRDPARTY_DIR . "/tree/tree.css");
|
* @param callback $callback
|
||||||
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
|
*/
|
||||||
Requirements::javascript(SAPPHIRE_DIR . "/javascript/TreeSelectorField.js");
|
public function setFilterFunction($callback) {
|
||||||
|
if(!is_callable($callback, true)) {
|
||||||
|
throw new InvalidArgumentException('TreeDropdownField->setFilterCallback(): not passed a valid callback');
|
||||||
|
}
|
||||||
|
|
||||||
if($this->value) {
|
$this->filterCallback = $callback;
|
||||||
$record = $this->getByKey($this->value);
|
}
|
||||||
$title = ($record) ? $record->Title : _t('DropdownField.CHOOSE', "(Choose)", PR_MEDIUM, 'Start-value of a dropdown');
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function Field() {
|
||||||
|
if($this->Value() && $record = $this->objectForKey($this->Value())) {
|
||||||
|
$title = $record->{$this->labelField};
|
||||||
} else {
|
} else {
|
||||||
$title = _t('DropdownField.CHOOSE', "(Choose)", PR_MEDIUM, 'Start-value of a dropdown');
|
$title = _t('DropdownField.CHOOSE', '(Choose)', PR_MEDIUM, 'start value of a dropdown');
|
||||||
}
|
}
|
||||||
|
|
||||||
$id = $this->id();
|
return $this->createTag (
|
||||||
|
'div',
|
||||||
$classes = "TreeDropdownField single";
|
array (
|
||||||
if($this->extraClass()) $classes .= ' ' . $this->extraClass();
|
'id' => "TreeDropdownField_{$this->id()}",
|
||||||
|
'class' => 'TreeDropdownField single' . ($this->extraClass() ? " {$this->extraClass()}" : '')
|
||||||
return <<<HTML
|
),
|
||||||
<div id="TreeDropdownField_$id" class="$classes"><input id="$id" type="hidden" name="$this->name" value="$this->value" /><span class="items">$title</span><a href="#" title="open" class="editLink"> </a></div>
|
$this->createTag (
|
||||||
HTML;
|
'input',
|
||||||
|
array (
|
||||||
|
'id' => $this->id(),
|
||||||
|
'type' => 'hidden',
|
||||||
|
'name' => $this->name,
|
||||||
|
'value' => $this->value
|
||||||
|
)
|
||||||
|
) . $this->createTag (
|
||||||
|
'span',
|
||||||
|
array (
|
||||||
|
'class' => 'items'
|
||||||
|
),
|
||||||
|
$title
|
||||||
|
) . $this->createTag (
|
||||||
|
'a',
|
||||||
|
array (
|
||||||
|
'href' => '#',
|
||||||
|
'title' => 'open',
|
||||||
|
'class' => 'editLink'
|
||||||
|
),
|
||||||
|
' '
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the site tree
|
* Get the whole tree of a part of the tree via an AJAX request.
|
||||||
|
*
|
||||||
|
* @param HTTPRequest $request
|
||||||
|
* @return string
|
||||||
*/
|
*/
|
||||||
function gettree() {
|
public function tree(HTTPRequest $request) {
|
||||||
if($this->treeBaseID) $obj = DataObject::get_by_id($this->sourceObject, $this->treeBaseID);
|
$isSubTree = false;
|
||||||
else $obj = singleton($this->sourceObject);
|
|
||||||
|
|
||||||
if($this->filterFunc) $obj->setMarkingFilterFunction($this->filterFunc);
|
if($ID = (int) $request->param('ID')) {
|
||||||
else if($this->sourceObject == 'Folder') $obj->setMarkingFilter('ClassName', 'Folder');
|
$obj = DataObject::get_by_id($this->sourceObject, $ID);
|
||||||
$obj->markPartialTree();
|
$isSubTree = true;
|
||||||
|
|
||||||
// If we've already got values selected, make sure that we've got them in our tree
|
if(!$obj) {
|
||||||
if($_REQUEST['forceValues']) {
|
throw new Exception (
|
||||||
$forceValues = preg_split("/ *, */", trim($_REQUEST['forceValues']));
|
"TreeDropdownField->tree(): the object #$ID of type $this->sourceObject could not be found"
|
||||||
foreach($forceValues as $value) {
|
);
|
||||||
$obj->markToExpose($this->getByKey($value));
|
}
|
||||||
}
|
} else {
|
||||||
|
if($this->baseID) {
|
||||||
|
$obj = DataObject::get_by_id($this->sourceObject, $this->baseID);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!$this->baseID || !$obj) $obj = singleton($this->sourceObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($this->filterCallback) {
|
||||||
|
$obj->setMarkingFilterFunction($this->filterCallback);
|
||||||
|
} elseif($this->sourceObject == 'Folder') {
|
||||||
|
$obj->setMarkingFilter('ClassName', 'Folder');
|
||||||
}
|
}
|
||||||
|
|
||||||
$eval = '"<li id=\"selector-' . $this->name . '-$child->' . $this->keyField . '\" class=\"$child->class" . $child->markingClasses() . "\"><a>" . $child->' . $this->labelField . ' . "</a>"';
|
|
||||||
echo $obj->getChildrenAsUL("class=\"tree\"", $eval, null, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a subtree via Ajax
|
|
||||||
*/
|
|
||||||
public function getsubtree() {
|
|
||||||
$obj = $this->getByKey($_REQUEST['SubtreeRootID']);
|
|
||||||
|
|
||||||
if(!$obj) user_error("Can't find database record $this->sourceObject with $this->keyField = $_REQUEST[SubtreeRootID]", E_USER_ERROR);
|
|
||||||
|
|
||||||
if($this->filterFunc) $obj->setMarkingFilterFunction($this->filterFunc);
|
|
||||||
else if($this->sourceObject == 'Folder') $obj->setMarkingFilter('ClassName', 'Folder');
|
|
||||||
$obj->markPartialTree();
|
$obj->markPartialTree();
|
||||||
|
|
||||||
$eval = '"<li id=\"selector-' . $this->name . '-$child->' . $this->keyField . '\" class=\"$child->class" . $child->markingClasses() . "\"><a>" . $child->' . $this->labelField . ' . "</a>"';
|
if($forceValues = $this->value) {
|
||||||
$tree = $obj->getChildrenAsUL("", $eval, null, true);
|
if(($values = preg_split('/,\s*/', $forceValues)) && count($values)) foreach($values as $value) {
|
||||||
echo substr(trim($tree), 4,-5);
|
$obj->markToExpose($this->objectForKey($value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$eval = '"<li id=\"selector-' . $this->Name() . '-{$child->' . $this->keyField . '}\" 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $obj->getChildrenAsUL('class="tree"', $eval, null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Get the object where the $keyField is equal to a certain value
|
||||||
|
*
|
||||||
|
* @param string|int $key
|
||||||
* @return DataObject
|
* @return DataObject
|
||||||
*/
|
*/
|
||||||
public function getByKey($key) {
|
protected function objectForKey($key) {
|
||||||
if($this->keyField == 'ID') {
|
if($this->keyField == 'ID') {
|
||||||
return DataObject::get_by_id($this->sourceObject, $key);
|
return DataObject::get_by_id($this->sourceObject, $key);
|
||||||
} else {
|
} else {
|
||||||
$SQL_key = Convert::raw2sql($key);
|
return DataObject::get_one($this->sourceObject, "\"{$this->keyField}\" = '" . Convert::raw2sql($key) . "'");
|
||||||
return DataObject::get_one($this->sourceObject, "\"$this->keyField\" = '$SQL_key'");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the stack of values to be traversed to find the given key in the database
|
|
||||||
*/
|
|
||||||
public function getstack() {
|
|
||||||
$page = $this->getByKey($_REQUEST['SubtreeRootID']);
|
|
||||||
|
|
||||||
while($page->ParentID) {
|
|
||||||
echo $ids[] = $page->ID;
|
|
||||||
$page = $page->Parent;
|
|
||||||
}
|
|
||||||
$ids[] = $page->ID;
|
|
||||||
echo implode(",", array_reverse($ids));
|
|
||||||
}
|
|
||||||
|
|
||||||
function performReadonlyTransformation() {
|
|
||||||
$fieldName = $this->labelField;
|
|
||||||
if($this->value) {
|
|
||||||
$obj = ($this->getByKey($this->value)) ? $this->getByKey($this->value)->$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);
|
|
||||||
$field->setReadonly(true);
|
|
||||||
return $field;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
|
@ -142,10 +142,9 @@ TreeDropdownField.prototype = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
ajaxGetTree: function(after) {
|
ajaxGetTree: function(after) {
|
||||||
var ajaxURL = this.helperURLBase() + 'gettree?forceValues=' + this.inputTag.value;
|
var ajaxURL = this.helperURLBase() + 'tree/';
|
||||||
ajaxURL += $('SecurityID') ? '&SecurityID=' + $('SecurityID').value : '';
|
ajaxURL += $('SecurityID') ? '&SecurityID=' + $('SecurityID').value : '';
|
||||||
if($('Form_EditForm_Locale')) ajaxURL += "&locale=" + $('Form_EditForm_Locale').value;
|
if($('Form_EditForm_Locale')) ajaxURL += "&locale=" + $('Form_EditForm_Locale').value;
|
||||||
|
|
||||||
new Ajax.Request(ajaxURL, {
|
new Ajax.Request(ajaxURL, {
|
||||||
method : 'get',
|
method : 'get',
|
||||||
onSuccess : after,
|
onSuccess : after,
|
||||||
@ -165,7 +164,7 @@ TreeDropdownField.prototype = {
|
|||||||
this.tree = Tree.create(this.itemTree.getElementsByTagName('ul')[0], {
|
this.tree = Tree.create(this.itemTree.getElementsByTagName('ul')[0], {
|
||||||
ajaxExpansion: this.ajaxExpansion,
|
ajaxExpansion: this.ajaxExpansion,
|
||||||
getIdx: function() {
|
getIdx: function() {
|
||||||
return this.id.replace(this.options.idxBase,'');
|
return this.getElementsByTagName('a')[0].getAttribute('rel');
|
||||||
},
|
},
|
||||||
idxBase : 'selector-' + this.inputTag.name + '-',
|
idxBase : 'selector-' + this.inputTag.name + '-',
|
||||||
dropdownField : this,
|
dropdownField : this,
|
||||||
@ -190,7 +189,7 @@ TreeDropdownField.prototype = {
|
|||||||
var ul = this.treeNodeHolder();
|
var ul = this.treeNodeHolder();
|
||||||
ul.innerHTML = ss.i18n._t('LOADING', 'Loading...');
|
ul.innerHTML = ss.i18n._t('LOADING', 'Loading...');
|
||||||
|
|
||||||
var ajaxURL = this.options.dropdownField.helperURLBase() + 'getsubtree?&SubtreeRootID=' + this.getIdx();
|
var ajaxURL = this.options.dropdownField.helperURLBase() + 'tree/' + this.getIdx();
|
||||||
ajaxURL += $('SecurityID') ? '&SecurityID=' + $('SecurityID').value : '';
|
ajaxURL += $('SecurityID') ? '&SecurityID=' + $('SecurityID').value : '';
|
||||||
if($('Form_EditForm_Locale')) ajaxURL += "&locale=" + $('Form_EditForm_Locale').value;
|
if($('Form_EditForm_Locale')) ajaxURL += "&locale=" + $('Form_EditForm_Locale').value;
|
||||||
|
|
||||||
@ -230,12 +229,6 @@ TreeDropdownField.prototype = {
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
this.humanItems.innerHTML = this.inputTag.value ? this.inputTag.value : '(Choose)';
|
this.humanItems.innerHTML = this.inputTag.value ? this.inputTag.value : '(Choose)';
|
||||||
/*
|
|
||||||
new Ajax.Request(this.options.dropdownField.helperURLBase() + '&methodName=findsubtreefor&ID=' + this.getIdx(), {
|
|
||||||
onSuccess : this.installSubtree.bind(this),
|
|
||||||
onFailure : function(response) { errorMessage('error loading subtree', response); }
|
|
||||||
});
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setValueFromTree: function(treeID, title) {
|
setValueFromTree: function(treeID, title) {
|
||||||
@ -302,4 +295,4 @@ TreeMultiselectField.prototype = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TreeMultiselectField.applyTo('div.TreeDropdownField.multiple');
|
TreeMultiselectField.applyTo('div.TreeDropdownField.multiple');
|
||||||
TreeDropdownField.applyTo('div.TreeDropdownField.single');
|
TreeDropdownField.applyTo('div.TreeDropdownField.single');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user