silverstripe-framework/forms/TreeDropdownField.php
Sam Minnee 1e0c4ae8d1 Merge branch '2.4'
Conflicts:
	cache/Cache.php
	cli/CliController.php
	core/Convert.php
	core/Core.php
	core/ManifestBuilder.php
	core/Object.php
	core/SSViewer.php
	core/control/ContentController.php
	core/control/ContentNegotiator.php
	core/control/FormResponse.php
	core/control/RequestHandler.php
	core/control/SilverStripeNavigatorItem.php
	core/i18n.php
	core/i18nTextCollector.php
	core/model/DataObjectSet.php
	core/model/Hierarchy.php
	core/model/Image.php
	core/model/MySQLDatabase.php
	core/model/SiteConfig.php
	core/model/SiteTree.php
	core/model/Translatable.php
	core/model/VirtualPage.php
	dev/Debug.php
	dev/SapphireTest.php
	dev/TestRunner.php
	dev/YamlFixture.php
	dev/install/MySQLDatabaseConfigurationHelper.php
	docs/en/installation/from-source.md
	docs/en/topics/themes.md
	docs/en/tutorials/4-site-search.md
	email/Mailer.php
	filesystem/File.php
	filesystem/Folder.php
	forms/ComplexTableField.php
	forms/CurrencyField.php
	forms/DateField.php
	forms/FieldSet.php
	forms/FileField.php
	forms/FileIFrameField.php
	forms/HtmlEditorConfig.php
	forms/HtmlEditorField.php
	forms/SelectionGroup.php
	forms/SimpleImageField.php
	forms/TabSet.php
	forms/TableField.php
	forms/TableListField.php
	forms/TreeDropdownField.php
	forms/TreeMultiselectField.php
	integration/Geoip.php
	javascript/SelectionGroup.js
	javascript/TreeSelectorField.js
	javascript/UpdateURL.js
	javascript/core/jquery.ondemand.js
	javascript/tiny_mce_improvements.js
	javascript/tree/tree.js
	lang/en_US.php
	search/ContentControllerSearchExtension.php
	security/Group.php
	security/Member.php
	security/PermissionCheckboxSetField.php
	security/PermissionRole.php
	security/Security.php
	static-main.php
	templates/RelationComplexTableField.ss
	templates/TableListField.ss
	tests/ConvertTest.php
	tests/DataObjectSetTest.php
	tests/DataObjectTest.php
	tests/DataObjectTest.yml
	tests/RequestHandlingTest.php
	tests/SSViewerTest.php
	tests/SiteTreePermissionsTest.php
	tests/SiteTreeTest.php
	tests/TransactionTest.php
	tests/api/RestfulServiceTest.php
	tests/control/DirectorTest.php
	tests/control/ModelAsControllerTest.php
	tests/fieldtypes/WidgetAreaEditorTest.php
	tests/forms/CurrencyFieldTest.php
	tests/forms/FormTest.php
	tests/model/DatabaseTest.php
	tests/model/ImageTest.php
	tests/search/ContentControllerSearchExtensionTest.php
	tests/security/MemberAuthenticatorTest.php
	thirdparty/.gitignore
	thirdparty/behaviour/behaviour.js
	thirdparty/firebug-lite/firebug.js
	thirdparty/firebug-lite/firebugx.js
	thirdparty/jquery-form/jquery.form.js
	thirdparty/jquery-livequery/jquery.livequery.js
	thirdparty/jquery-livequery/test/jquery.js
	thirdparty/jquery-livequery/test/test.html
	thirdparty/jquery-livequery/test/test2.html
	thirdparty/jquery-metadata/META.json
	thirdparty/jquery-metadata/README
	thirdparty/jquery-metadata/jquery.metadata.js
	thirdparty/jquery-metadata/test/index.html
	thirdparty/jquery-metadata/test/jquery.js
	thirdparty/jquery-metadata/test/test.js
	thirdparty/jquery-metadata/test/testrunner.js
	thirdparty/jquery-metadata/test/testsuite.css
	thirdparty/jquery-ui-themes/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-bg_flat_75_ffffff_40x100.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-bg_glass_65_ffffff_1x400.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-bg_glass_75_dadada_1x400.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-icons_222222_256x240.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-icons_2e83ff_256x240.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-icons_454545_256x240.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-icons_888888_256x240.png
	thirdparty/jquery-ui-themes/smoothness/images/ui-icons_cd0a0a_256x240.png
	thirdparty/jquery-ui/i18n/jquery-ui-i18n.js
	thirdparty/jquery-ui/i18n/ui.datepicker-ar.js
	thirdparty/jquery-ui/i18n/ui.datepicker-bg.js
	thirdparty/jquery-ui/i18n/ui.datepicker-ca.js
	thirdparty/jquery-ui/i18n/ui.datepicker-cs.js
	thirdparty/jquery-ui/i18n/ui.datepicker-da.js
	thirdparty/jquery-ui/i18n/ui.datepicker-de.js
	thirdparty/jquery-ui/i18n/ui.datepicker-el.js
	thirdparty/jquery-ui/i18n/ui.datepicker-eo.js
	thirdparty/jquery-ui/i18n/ui.datepicker-es.js
	thirdparty/jquery-ui/i18n/ui.datepicker-fa.js
	thirdparty/jquery-ui/i18n/ui.datepicker-fi.js
	thirdparty/jquery-ui/i18n/ui.datepicker-fr.js
	thirdparty/jquery-ui/i18n/ui.datepicker-he.js
	thirdparty/jquery-ui/i18n/ui.datepicker-hr.js
	thirdparty/jquery-ui/i18n/ui.datepicker-hu.js
	thirdparty/jquery-ui/i18n/ui.datepicker-hy.js
	thirdparty/jquery-ui/i18n/ui.datepicker-id.js
	thirdparty/jquery-ui/i18n/ui.datepicker-is.js
	thirdparty/jquery-ui/i18n/ui.datepicker-it.js
	thirdparty/jquery-ui/i18n/ui.datepicker-ja.js
	thirdparty/jquery-ui/i18n/ui.datepicker-ko.js
	thirdparty/jquery-ui/i18n/ui.datepicker-lt.js
	thirdparty/jquery-ui/i18n/ui.datepicker-lv.js
	thirdparty/jquery-ui/i18n/ui.datepicker-ms.js
	thirdparty/jquery-ui/i18n/ui.datepicker-nl.js
	thirdparty/jquery-ui/i18n/ui.datepicker-no.js
	thirdparty/jquery-ui/i18n/ui.datepicker-pl.js
	thirdparty/jquery-ui/i18n/ui.datepicker-pt-BR.js
	thirdparty/jquery-ui/i18n/ui.datepicker-ro.js
	thirdparty/jquery-ui/i18n/ui.datepicker-ru.js
	thirdparty/jquery-ui/i18n/ui.datepicker-sk.js
	thirdparty/jquery-ui/i18n/ui.datepicker-sl.js
	thirdparty/jquery-ui/i18n/ui.datepicker-sq.js
	thirdparty/jquery-ui/i18n/ui.datepicker-sr-SR.js
	thirdparty/jquery-ui/i18n/ui.datepicker-sr.js
	thirdparty/jquery-ui/i18n/ui.datepicker-sv.js
	thirdparty/jquery-ui/i18n/ui.datepicker-th.js
	thirdparty/jquery-ui/i18n/ui.datepicker-tr.js
	thirdparty/jquery-ui/i18n/ui.datepicker-uk.js
	thirdparty/jquery-ui/i18n/ui.datepicker-zh-CN.js
	thirdparty/jquery-ui/i18n/ui.datepicker-zh-TW.js
	thirdparty/jquery/jquery.js
	thirdparty/jsmin/.piston.yml
	thirdparty/jsmin/jsmin.php
	thirdparty/prototype/prototype.js
	thirdparty/scriptaculous/dragdrop.js
	thirdparty/simplepie/.piston.yml
	thirdparty/spyc/.piston.yml
	thirdparty/spyc/README
	thirdparty/spyc/php4/spyc.php4
	thirdparty/spyc/php4/test.php4
	thirdparty/spyc/spyc.php
	thirdparty/spyc/spyc.yaml
	thirdparty/tinymce-advcode/dialog.html
	thirdparty/tinymce-advcode/editor_plugin_src.js
	thirdparty/tinymce-advcode/js/dialog.js
	thirdparty/tinymce/langs/en.js
	thirdparty/tinymce/plugins/advhr/langs/en_dlg.js
	thirdparty/tinymce/plugins/advhr/rule.htm
	thirdparty/tinymce/plugins/advimage/image.htm
	thirdparty/tinymce/plugins/advimage/langs/en_dlg.js
	thirdparty/tinymce/plugins/advlink/langs/en_dlg.js
	thirdparty/tinymce/plugins/advlink/link.htm
	thirdparty/tinymce/plugins/emotions/emotions.htm
	thirdparty/tinymce/plugins/emotions/langs/en_dlg.js
	thirdparty/tinymce/plugins/example/dialog.htm
	thirdparty/tinymce/plugins/fullpage/fullpage.htm
	thirdparty/tinymce/plugins/fullpage/langs/en_dlg.js
	thirdparty/tinymce/plugins/fullscreen/fullscreen.htm
	thirdparty/tinymce/plugins/inlinepopups/template.htm
	thirdparty/tinymce/plugins/media/langs/en_dlg.js
	thirdparty/tinymce/plugins/media/media.htm
	thirdparty/tinymce/plugins/paste/js/pasteword.js
	thirdparty/tinymce/plugins/paste/langs/en_dlg.js
	thirdparty/tinymce/plugins/paste/pastetext.htm
	thirdparty/tinymce/plugins/paste/pasteword.htm
	thirdparty/tinymce/plugins/searchreplace/langs/en_dlg.js
	thirdparty/tinymce/plugins/searchreplace/searchreplace.htm
	thirdparty/tinymce/plugins/spellchecker/editor_plugin.js
	thirdparty/tinymce/plugins/spellchecker/editor_plugin_src.js
	thirdparty/tinymce/plugins/style/langs/en_dlg.js
	thirdparty/tinymce/plugins/style/props.htm
	thirdparty/tinymce/plugins/table/cell.htm
	thirdparty/tinymce/plugins/table/langs/en_dlg.js
	thirdparty/tinymce/plugins/table/merge_cells.htm
	thirdparty/tinymce/plugins/table/row.htm
	thirdparty/tinymce/plugins/table/table.htm
	thirdparty/tinymce/plugins/template/langs/en_dlg.js
	thirdparty/tinymce/plugins/template/template.htm
	thirdparty/tinymce/plugins/xhtmlxtras/abbr.htm
	thirdparty/tinymce/plugins/xhtmlxtras/acronym.htm
	thirdparty/tinymce/plugins/xhtmlxtras/attributes.htm
	thirdparty/tinymce/plugins/xhtmlxtras/cite.htm
	thirdparty/tinymce/plugins/xhtmlxtras/del.htm
	thirdparty/tinymce/plugins/xhtmlxtras/ins.htm
	thirdparty/tinymce/plugins/xhtmlxtras/langs/en_dlg.js
	thirdparty/tinymce/themes/advanced/about.htm
	thirdparty/tinymce/themes/advanced/anchor.htm
	thirdparty/tinymce/themes/advanced/charmap.htm
	thirdparty/tinymce/themes/advanced/color_picker.htm
	thirdparty/tinymce/themes/advanced/image.htm
	thirdparty/tinymce/themes/advanced/langs/en.js
	thirdparty/tinymce/themes/advanced/langs/en_dlg.js
	thirdparty/tinymce/themes/advanced/link.htm
	thirdparty/tinymce/themes/advanced/source_editor.htm
	thirdparty/tinymce/themes/simple/langs/en.js
	thirdparty/tinymce/tiny_mce.js
	thirdparty/tinymce/tiny_mce_src.js
	widgets/Widget.php
2011-02-14 18:47:53 +13:00

341 lines
12 KiB
PHP
Executable File

<?php
/**
* Dropdown-like field that allows you to select an item from a hierachical AJAX-expandable tree.
*
* 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.
*
* **Note:** your source object must use an implementation of hierarchy for this field to generate the tree correctly, e.g. groups, sitetree etc.
*
* All operations are carried out through behaviour and javascript.
*
* <b>Usage</b>.
*
* treedropdownfield is used on {@link VirtualPage} a class which creates another instance of a page, with exactly the same fields that can be represented on another part of the site. The code below is taken from an example of this.
*
* <code>
* // Put this at the top of the class that defines your model (e.g. the class that extends DataObject).
* static $has_one = array(
* 'RightContent' => 'SiteTree'
* );
*
* // Setup the linking to the original page. (Put this in your getCMSFields() method or similar)
* $treedropdownfield = new TreeDropdownField("RightContentID", "Choose a page to show on the right:", "SiteTree");
* </code>
*
* This will generate a tree allowing the user to expand and contract subsections to find the appropriate page to save to the field.
*
* @see TreeMultiselectField for the same implementation allowing multiple selections
* @see DropdownField for a simple <select> field with a single element.
* @see CheckboxSetField for multiple selections through checkboxes.
* @see OptionsetField for single selections via radiobuttons.
*
* @package forms
* @subpackage fields-relational
*/
class TreeDropdownField extends FormField {
public static $url_handlers = array (
'$Action!/$ID' => '$Action'
);
public static $allowed_actions = array (
'tree'
);
/**
* @ignore
*/
protected $sourceObject, $keyField, $labelField, $filterCallback, $searchCallback, $baseID = 0;
/**
* Used by field search to leave only the relevant entries
*/
protected $searchIds = null, $searchExpanded = array();
/**
* 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
* @param sourceObject The object-type to list in the tree. Must be a 'hierachy' object. Alternatively,
* you can set this to an array of key/value pairs, like a dropdown source. In this case, the field
* will act like show a flat list of tree items, without any hierachy. This is most useful in
* conjunction with 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 string $showSearch enable the ability to search the tree by entering the text in the input field.
*/
public function __construct($name, $title = null, $sourceObject = 'Group', $keyField = 'ID', $labelField = 'Title', $showSearch = false) {
$this->sourceObject = $sourceObject;
$this->keyField = $keyField;
$this->labelField = $labelField;
$this->showSearch = $showSearch;
parent::__construct($name, $title);
}
/**
* Set the ID of the root node of the tree. This defaults to 0 - i.e. displays the whole tree.
*
* @param int $ID
*/
public function setTreeBaseID($ID) {
$this->baseID = (int) $ID;
}
/**
* Set a callback used to filter the values of the tree before displaying to the user.
*
* @param callback $callback
*/
public function setFilterFunction($callback) {
if(!is_callable($callback, true)) {
throw new InvalidArgumentException('TreeDropdownField->setFilterCallback(): not passed a valid callback');
}
$this->filterCallback = $callback;
}
/**
* Set a callback used to search the hierarchy globally, even before 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;
}
/**
* @return string
*/
public function Field() {
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/prototype/prototype.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/behaviour/behaviour.js');
Requirements::javascript(SAPPHIRE_DIR . '/thirdparty/jquery/jquery.js');
Requirements::javascript(SAPPHIRE_DIR . '/javascript/jquery_improvements.js');
Requirements::javascript(SAPPHIRE_DIR . '/javascript/tree/tree.js');
// needed for errorMessage()
Requirements::javascript(SAPPHIRE_DIR . '/javascript/LeftAndMain.js');
Requirements::javascript(SAPPHIRE_DIR . '/javascript/TreeSelectorField.js');
Requirements::css(SAPPHIRE_DIR . '/javascript/tree/tree.css');
Requirements::css(SAPPHIRE_DIR . '/css/TreeDropdownField.css');
if($this->Value() && $record = $this->objectForKey($this->Value())) {
$title = $record->{$this->labelField};
} else {
$title = _t('DropdownField.CHOOSE', '(Choose)', PR_MEDIUM, 'start value of a dropdown');
}
return $this->createTag (
'div',
array (
'id' => "TreeDropdownField_{$this->id()}",
'class' => 'TreeDropdownField single' . ($this->extraClass() ? " {$this->extraClass()}" : ''),
'href' => $this->form ? $this->Link() : "",
),
$this->createTag (
'input',
array (
'id' => $this->id(),
'type' => 'hidden',
'name' => $this->name,
'value' => $this->value
)
) . ($this->showSearch ?
$this->createTag(
'input',
array(
'class' => 'items',
'value' => '(Choose or type search)'
)
) :
$this->createTag (
'span',
array (
'class' => 'items'
),
$title
)
) . $this->createTag (
'a',
array (
'href' => '#',
'title' => 'open',
'class' => 'editLink'
),
'&nbsp;'
)
);
}
/**
* Get the whole tree of a part of the tree via an AJAX request.
*
* @param SS_HTTPRequest $request
* @return string
*/
public function tree(SS_HTTPRequest $request) {
// 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
$isSubTree = false;
$this->search = Convert::Raw2SQL($request->getVar('search'));
if($ID = (int) $request->latestparam('ID')) {
$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);
}
// pre-process the tree - search needs to operate globally, not locally as marking filter does
if ( $this->search != "" )
$this->populateIDs();
if ($this->filterCallback || $this->sourceObject == 'Folder' || $this->search != "" )
$obj->setMarkingFilterFunction(array($this, "filterMarking"));
$obj->markPartialTree();
// 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) {
if(!$value) continue;
$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);
}
/**
* Marking 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.
* @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;
if ($this->search != "") {
return isset($this->searchIds[$node->ID]) && $this->searchIds[$node->ID] ? true : false;
}
return true;
}
/**
* 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
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 ) {
// 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;
$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;
}
}
}
}
/**
* 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);
} else {
return DataObject::get_one($this->sourceObject, "\"{$this->keyField}\" = '" . Convert::raw2sql($key) . "'");
}
}
/**
* Changes this field to the readonly field.
*/
function performReadonlyTransformation() {
return new TreeDropdownField_Readonly($this->name, $this->title, $this->sourceObject, $this->keyField, $this->labelField);
}
}
/**
* @package forms
* @subpackage fields-relational
*/
class TreeDropdownField_Readonly extends TreeDropdownField {
protected $readonly = true;
function Field() {
$fieldName = $this->labelField;
if($this->value) {
$keyObj = $this->objectForKey($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();
}
}