2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This formfield represents many-many joins using a tree selector shown in a dropdown styled element
|
|
|
|
* which can be added to any form usually in the CMS.
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class TreeMultiselectField extends TreeDropdownField {
|
|
|
|
function __construct($name, $title, $sourceObject = "Group", $keyField = "ID", $labelField = "Title") {
|
|
|
|
parent::__construct($name, $title, $sourceObject, $keyField, $labelField);
|
|
|
|
$this->value = 'unchanged';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return this field's linked items
|
|
|
|
*/
|
|
|
|
function getItems() {
|
|
|
|
if($this->form) {
|
|
|
|
$fieldName = $this->name;
|
|
|
|
$record = $this->form->getRecord();
|
|
|
|
if(is_object($record) && $record->hasMethod($fieldName))
|
|
|
|
return $record->$fieldName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* We overwrite the field attribute to add our hidden fields, as this
|
|
|
|
* formfield can contain multiple values.
|
|
|
|
*/
|
|
|
|
function Field() {
|
|
|
|
$value = '';
|
|
|
|
$itemList = '';
|
2009-03-10 23:08:52 +01:00
|
|
|
Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . "/javascript/TreeSelectorField.js");
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$items = $this->getItems();
|
|
|
|
if($items) {
|
|
|
|
foreach($items as $item) {
|
|
|
|
$titleArray[] =$item->Title;
|
|
|
|
$idArray[] = $item->ID;
|
|
|
|
}
|
|
|
|
if(isset($titleArray)) {
|
|
|
|
$itemList = implode(", ", $titleArray);
|
|
|
|
$value = implode(",", $idArray);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->id();
|
|
|
|
|
|
|
|
return <<<HTML
|
|
|
|
<div class="TreeDropdownField multiple"><input id="$id" type="hidden" name="$this->name" value="$value" /><span class="items">$itemList</span><a href="#" title="open" class="editLink"> </a></div>
|
|
|
|
HTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the results into the form
|
|
|
|
* Calls function $record->onChange($items) before saving to the assummed
|
|
|
|
* Component set.
|
|
|
|
*/
|
|
|
|
function saveInto(DataObject $record) {
|
|
|
|
// Detect whether this field has actually been updated
|
|
|
|
if($this->value !== 'unchanged') {
|
2008-11-03 14:47:40 +01:00
|
|
|
$items = array();
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$fieldName = $this->name;
|
|
|
|
$saveDest = $record->$fieldName();
|
|
|
|
if(!$saveDest) user_error("TreeMultiselectField::saveInto() Field '$fieldName' not found on $record->class.$record->ID", E_USER_ERROR);
|
|
|
|
|
|
|
|
if($this->value) {
|
|
|
|
$items = split(" *, *", trim($this->value));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allows you to modify the items on your object before save
|
|
|
|
$funcName = "onChange$fieldName";
|
|
|
|
if($record->hasMethod($funcName)){
|
|
|
|
$result = $record->$funcName($items);
|
|
|
|
if(!$result){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$saveDest->setByIDList($items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes this field to the readonly field.
|
|
|
|
*/
|
|
|
|
function performReadonlyTransformation() {
|
2009-06-17 13:36:49 +02:00
|
|
|
return new TreeMultiselectField_Readonly($this->name, $this->title, $this->sourceObject, $this->keyField, $this->labelField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TreeMultiselectField_Readonly extends TreeMultiselectField {
|
|
|
|
|
|
|
|
protected $readonly = true;
|
|
|
|
|
|
|
|
function Field() {
|
2008-11-03 14:47:40 +01:00
|
|
|
$titleArray = array();
|
|
|
|
$titleList = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
if($items = $this->getItems()) {
|
|
|
|
foreach($items as $item) $titleArray[] = $item->Title;
|
|
|
|
if($titleArray) $titleList = implode(", ", $titleArray);
|
|
|
|
}
|
2009-06-17 13:36:49 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$field = new ReadonlyField($this->name, $this->title);
|
|
|
|
$field->setValue($titleList);
|
|
|
|
$field->setForm($this->form);
|
2009-06-17 13:36:49 +02:00
|
|
|
return $field->Field();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|