2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-09 05:18:36 +01:00
|
|
|
/**
|
2009-10-11 02:07:05 +02:00
|
|
|
* Dropdown-like field that allows you to select an item from a hierachical AJAX-expandable tree
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class TreeDropdownField extends FormField {
|
2009-10-11 02:07:05 +02:00
|
|
|
|
|
|
|
public static $url_handlers = array (
|
|
|
|
'$Action!/$ID' => '$Action'
|
|
|
|
);
|
|
|
|
|
|
|
|
public static $allowed_actions = array (
|
|
|
|
'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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by field search to leave only the relevant entries
|
|
|
|
*/
|
|
|
|
protected $searchIds = null, $searchExpanded = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
/**
|
2009-10-11 02:07:05 +02:00
|
|
|
* @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).
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2010-04-12 05:33:56 +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
|
|
|
if(!Object::has_extension($this->sourceObject, 'Hierarchy')) {
|
|
|
|
throw new Exception (
|
|
|
|
"TreeDropdownField: the source class '$this->sourceObject' must have the Hierarchy extension applied"
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-10-11 02:07:05 +02:00
|
|
|
* Set the ID of the root node of the tree. This defaults to 0 - i.e. displays the whole tree.
|
|
|
|
*
|
|
|
|
* @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;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-10-11 02:07:05 +02:00
|
|
|
* Set a callback used to filter the values of the tree before displaying to the user.
|
|
|
|
*
|
|
|
|
* @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;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2010-04-12 05:33:56 +02:00
|
|
|
/**
|
|
|
|
* Set a callback used to search the hierarchy globally, before even applying the filter.
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2009-10-11 02:07:05 +02:00
|
|
|
public function Field() {
|
2009-11-21 03:29:59 +01:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/prototype.js');
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/behaviour.js');
|
|
|
|
Requirements::css(SAPPHIRE_DIR . '/css/TreeDropdownField.css');
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/tree/tree.js");
|
|
|
|
Requirements::css(SAPPHIRE_DIR . "/javascript/tree/tree.css");
|
|
|
|
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
|
2010-02-12 03:39:07 +01:00
|
|
|
// needed for errorMessage()
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/javascript/LeftAndMain.js');
|
2009-11-21 03:29:59 +01:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/TreeSelectorField.js");
|
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
if($this->Value() && $record = $this->objectForKey($this->Value())) {
|
|
|
|
$title = $record->{$this->labelField};
|
2007-07-19 12:40:28 +02:00
|
|
|
} else {
|
2009-10-11 02:07:05 +02:00
|
|
|
$title = _t('DropdownField.CHOOSE', '(Choose)', PR_MEDIUM, 'start value of a dropdown');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-10-11 02:07:05 +02:00
|
|
|
|
|
|
|
return $this->createTag (
|
|
|
|
'div',
|
|
|
|
array (
|
|
|
|
'id' => "TreeDropdownField_{$this->id()}",
|
|
|
|
'class' => 'TreeDropdownField single' . ($this->extraClass() ? " {$this->extraClass()}" : '')
|
|
|
|
),
|
|
|
|
$this->createTag (
|
|
|
|
'input',
|
|
|
|
array (
|
|
|
|
'id' => $this->id(),
|
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => $this->name,
|
|
|
|
'value' => $this->value
|
|
|
|
)
|
2010-04-12 05:33:56 +02:00
|
|
|
) . ($this->showSearch ?
|
2009-11-30 00:29:31 +01:00
|
|
|
$this->createTag(
|
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
'class' => 'items',
|
2010-04-12 05:33:56 +02:00
|
|
|
'value' => '(Choose or type search)'
|
2009-11-30 00:29:31 +01:00
|
|
|
)
|
|
|
|
) :
|
|
|
|
$this->createTag (
|
|
|
|
'span',
|
|
|
|
array (
|
|
|
|
'class' => 'items'
|
|
|
|
),
|
|
|
|
$title
|
|
|
|
)
|
2009-10-11 02:07:05 +02:00
|
|
|
) . $this->createTag (
|
|
|
|
'a',
|
|
|
|
array (
|
|
|
|
'href' => '#',
|
|
|
|
'title' => 'open',
|
|
|
|
'class' => 'editLink'
|
|
|
|
),
|
|
|
|
' '
|
|
|
|
)
|
|
|
|
);
|
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) {
|
2009-10-11 02:07:05 +02:00
|
|
|
$isSubTree = false;
|
2009-11-30 00:29:31 +01:00
|
|
|
|
2010-04-12 05:33:56 +02:00
|
|
|
$this->search = Convert::Raw2SQL($request->getVar('search'));
|
2009-11-30 00:29:31 +01:00
|
|
|
|
2009-11-21 02:44:32 +01:00
|
|
|
if($ID = (int) $request->latestparam('ID')) {
|
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: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();
|
|
|
|
|
|
|
|
if($forceValues = $this->value) {
|
|
|
|
if(($values = preg_split('/,\s*/', $forceValues)) && count($values)) foreach($values as $value) {
|
2010-02-22 05:37:33 +01:00
|
|
|
if(!is_numeric($value)) continue;
|
|
|
|
|
2009-10-11 02:07:05 +02:00
|
|
|
$obj->markToExpose($this->objectForKey($value));
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-10-11 02:07:05 +02:00
|
|
|
|
|
|
|
$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);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-11-30 00:29:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Marking function for the tree, which combines different filters sensibly. If a filter function has been set,
|
2010-04-12 05:33:56 +02:00
|
|
|
* that will be called. If the source is a folder, automatically filter folder. And if search text is set, filter on that
|
2009-11-30 00:29:31 +01:00
|
|
|
* too. Return true if all applicable conditions are true, false otherwise.
|
|
|
|
* @param $node
|
|
|
|
* @return unknown_type
|
|
|
|
*/
|
|
|
|
function filterMarking($node) {
|
|
|
|
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;
|
|
|
|
}
|
2010-04-12 05:33:56 +02: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.
|
|
|
|
*/
|
|
|
|
protected function populateIDs() {
|
|
|
|
if ( $this->searchCallback )
|
|
|
|
$res = call_user_func($this->searchCallback, $this->sourceObject, $this->labelField, $this->search);
|
|
|
|
else
|
|
|
|
$res = DataObject::get($this->sourceObject, "$this->labelField LIKE '%$this->search%'");
|
|
|
|
|
|
|
|
if( $res ) {
|
|
|
|
/* And keep a record of parents we don't need to get parents of themselves, as well as IDs to mark */
|
|
|
|
foreach($res as $row) {
|
|
|
|
if ($row->ParentID) $parents[$row->ParentID] = true;
|
|
|
|
$this->searchIds[$row->ID] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!empty($parents)) {
|
|
|
|
$res = DB::query('SELECT "ParentID", "ID" FROM '.$this->sourceObject.' WHERE "ID" in ('.implode(',',array_keys($parents)).')');
|
|
|
|
$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 {
|
2009-10-11 02:07:05 +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.
|
|
|
|
*/
|
|
|
|
function performReadonlyTransformation() {
|
|
|
|
return new TreeDropdownField_Readonly($this->name, $this->title, $this->sourceObject, $this->keyField, $this->labelField);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-04-12 05:17:09 +02:00
|
|
|
|
|
|
|
class TreeDropdownField_Readonly extends TreeDropdownField {
|
|
|
|
protected $readonly = true;
|
|
|
|
|
|
|
|
function Field() {
|
|
|
|
$fieldName = $this->labelField;
|
|
|
|
if($this->value) {
|
|
|
|
$keyObj = $this->getByKey($this->value);
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
}
|