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 = '';
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');
// needed for errorMessage()
Requirements::javascript(SAPPHIRE_DIR . '/javascript/LeftAndMain.js');
Requirements::javascript(SAPPHIRE_DIR . "/javascript/TreeSelectorField.js");
// Any field values have priority over the relation getters
if($this->value) {
$items = new DataObjectSet();
$ids = explode(',', $this->value);
foreach($ids as $id) {
if(!is_numeric($id)) continue;
$item = DataObject::get_by_id($this->sourceObject, $id);
if($item) $items->push($item);
}
} else {
$items = $this->getItems();
}
if($items && $items->Count()) {
foreach($items as $item) {
$titleArray[] =$item->Title;
$idArray[] = $item->ID;
}
if(isset($titleArray)) {
$itemList = implode(", ", $titleArray);
$value = implode(",", $idArray);
}
}
$id = $this->id();
return <<$itemList
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') {
$items = array();
$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 = preg_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() {
return new TreeMultiselectField_Readonly($this->name, $this->title, $this->sourceObject, $this->keyField, $this->labelField);
}
}
class TreeMultiselectField_Readonly extends TreeMultiselectField {
protected $readonly = true;
function Field() {
$titleArray = array();
$titleList = array();
if($items = $this->getItems()) {
foreach($items as $item) $titleArray[] = $item->Title;
if($titleArray) $titleList = implode(", ", $titleArray);
}
$field = new ReadonlyField($this->name, $this->title);
$field->setValue($titleList);
$field->setForm($this->form);
return $field->Field();
}
}