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
|
2010-07-11 12:44:59 +02:00
|
|
|
* which can be added to any form usually in the CMS.
|
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* This form class allows you to represent Many-Many Joins in a handy single field. The field has javascript which
|
|
|
|
* generates a AJAX tree of the site structure allowing you to save selected options to a component set on a given
|
|
|
|
* {@link DataObject}.
|
2010-07-11 12:44:59 +02:00
|
|
|
*
|
|
|
|
* <b>Saving</b>
|
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* This field saves a {@link ComponentSet} object which is present on the {@link DataObject} passed by the form,
|
|
|
|
* returned by calling a function with the same name as the field. The Join is updated by running setByIDList on the
|
|
|
|
* {@link ComponentSet}
|
2010-07-11 12:44:59 +02:00
|
|
|
*
|
|
|
|
* <b>Customizing Save Behaviour</b>
|
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* Before the data is saved, you can modify the ID list sent to the {@link ComponentSet} by specifying a function on
|
|
|
|
* the {@link DataObject} called "onChange[fieldname](&items)". This will be passed by reference the IDlist (an array
|
|
|
|
* of ID's) from the Treefield to be saved to the component set.
|
|
|
|
*
|
|
|
|
* Returning false on this method will prevent treemultiselect from saving to the {@link ComponentSet} of the given
|
|
|
|
* {@link DataObject}
|
2010-07-11 12:44:59 +02:00
|
|
|
*
|
|
|
|
* <code>
|
|
|
|
* // Called when we try and set the Parents() component set
|
|
|
|
* // by Tree Multiselect Field in the administration.
|
|
|
|
* function onChangeParents(&$items) {
|
|
|
|
* // This ensures this DataObject can never be a parent of itself
|
|
|
|
* if($items){
|
|
|
|
* foreach($items as $k => $id){
|
|
|
|
* if($id == $this->ID){
|
|
|
|
* unset($items[$k]);
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* return true;
|
|
|
|
* }
|
|
|
|
* </code>
|
2010-04-23 02:25:47 +02:00
|
|
|
*
|
2010-07-23 01:18:40 +02:00
|
|
|
* @see TreeDropdownField for the sample implementation, but only allowing single selects
|
|
|
|
*
|
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 {
|
2012-09-26 23:34:00 +02:00
|
|
|
public function __construct($name, $title=null, $sourceObject="Group", $keyField="ID", $labelField="Title") {
|
2007-07-19 12:40:28 +02:00
|
|
|
parent::__construct($name, $title, $sourceObject, $keyField, $labelField);
|
|
|
|
$this->value = 'unchanged';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return this field's linked items
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getItems() {
|
2010-02-04 02:07:30 +01:00
|
|
|
// If the value has been set, use that
|
|
|
|
if($this->value != 'unchanged' && is_array($this->sourceObject)) {
|
|
|
|
$items = array();
|
|
|
|
$values = is_array($this->value) ? $this->value : preg_split('/ *, */', trim($this->value));
|
|
|
|
foreach($values as $value) {
|
|
|
|
$item = new stdClass;
|
|
|
|
$item->ID = $value;
|
|
|
|
$item->Title = $this->sourceObject[$value];
|
|
|
|
$items[] = $item;
|
|
|
|
}
|
|
|
|
return $items;
|
|
|
|
|
|
|
|
// Otherwise, look data up from the linked relation
|
2010-03-04 23:51:44 +01:00
|
|
|
} if($this->value != 'unchanged' && is_string($this->value)) {
|
2011-05-05 12:40:24 +02:00
|
|
|
$items = new ArrayList();
|
2010-03-04 23:51:44 +01:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
return $items;
|
2010-02-04 02:07:30 +01:00
|
|
|
} else if($this->form) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$fieldName = $this->name;
|
|
|
|
$record = $this->form->getRecord();
|
|
|
|
if(is_object($record) && $record->hasMethod($fieldName))
|
|
|
|
return $record->$fieldName();
|
2010-03-04 23:51:44 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* We overwrite the field attribute to add our hidden fields, as this
|
|
|
|
* formfield can contain multiple values.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::add_i18n_javascript(FRAMEWORK_DIR . '/javascript/lang');
|
2011-03-07 20:29:09 +01:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jstree/jquery.jstree.js');
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/TreeDropdownField.js');
|
2011-03-07 20:29:09 +01:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
|
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/TreeDropdownField.css');
|
2011-03-07 20:29:09 +01:00
|
|
|
|
|
|
|
$value = '';
|
|
|
|
$itemList = '';
|
2010-03-04 23:51:44 +01:00
|
|
|
$items = $this->getItems();
|
|
|
|
|
|
|
|
if($items && count($items)) {
|
|
|
|
foreach($items as $id => $item) {
|
|
|
|
$titleArray[] = $item->Title;
|
2007-07-19 12:40:28 +02:00
|
|
|
$idArray[] = $item->ID;
|
|
|
|
}
|
2010-03-04 23:51:44 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
if(isset($titleArray)) {
|
2011-03-07 20:29:09 +01:00
|
|
|
$title = implode(", ", $titleArray);
|
2007-07-19 12:40:28 +02:00
|
|
|
$value = implode(",", $idArray);
|
|
|
|
}
|
2011-03-07 20:29:09 +01:00
|
|
|
} else {
|
2012-04-14 00:16:22 +02:00
|
|
|
$title = _t('DropdownField.CHOOSE', '(Choose)', 'start value of a dropdown');
|
2010-02-23 01:40:58 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
2012-11-15 02:19:57 +01:00
|
|
|
return FormField::create_tag(
|
2011-03-07 20:29:09 +01:00
|
|
|
'div',
|
|
|
|
array (
|
|
|
|
'id' => "TreeDropdownField_{$this->id()}",
|
2012-09-26 23:34:00 +02:00
|
|
|
'class' => 'TreeDropdownField multiple' . ($this->extraClass() ? " {$this->extraClass()}" : '')
|
|
|
|
. ($this->showSearch ? " searchable" : ''),
|
2011-03-10 04:12:46 +01:00
|
|
|
'data-url-tree' => $this->form ? $this->Link('tree') : "",
|
2011-03-07 20:29:09 +01:00
|
|
|
'data-title' => $title,
|
2012-06-13 11:57:05 +02:00
|
|
|
'title' => $this->getDescription()
|
2011-03-07 20:29:09 +01:00
|
|
|
),
|
2012-11-15 02:19:57 +01:00
|
|
|
FormField::create_tag(
|
2011-03-07 20:29:09 +01:00
|
|
|
'input',
|
|
|
|
array (
|
|
|
|
'id' => $this->id(),
|
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => $this->name,
|
|
|
|
'value' => $value
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the results into the form
|
|
|
|
* Calls function $record->onChange($items) before saving to the assummed
|
|
|
|
* Component set.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function saveInto(DataObjectInterface $record) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// 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();
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!$saveDest) {
|
|
|
|
user_error("TreeMultiselectField::saveInto() Field '$fieldName' not found on"
|
|
|
|
. " $record->class.$record->ID", E_USER_ERROR);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
if($this->value) {
|
2009-06-18 11:34:17 +02:00
|
|
|
$items = preg_split("/ *, */", trim($this->value));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2010-02-23 01:40:58 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
$saveDest->setByIDList($items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes this field to the readonly field.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2012-09-26 23:34:00 +02:00
|
|
|
$field = new TreeMultiselectField_Readonly($this->name, $this->title, $this->sourceObject,
|
|
|
|
$this->keyField, $this->labelField);
|
|
|
|
|
2010-01-05 05:48:54 +01:00
|
|
|
$field->addExtraClass($this->extraClass());
|
|
|
|
$field->setForm($this->form);
|
2010-02-04 02:09:08 +01:00
|
|
|
$field->setValue($this->value);
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2010-01-05 05:48:54 +01:00
|
|
|
return $field;
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-23 02:25:47 +02:00
|
|
|
/**
|
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-relational
|
|
|
|
*/
|
2009-06-17 13:36:49 +02:00
|
|
|
class TreeMultiselectField_Readonly extends TreeMultiselectField {
|
|
|
|
|
|
|
|
protected $readonly = true;
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2010-02-04 05:29:21 +01:00
|
|
|
$titleArray = $itemIDs = array();
|
|
|
|
$titleList = $itemIDsList = "";
|
2007-07-19 12:40:28 +02:00
|
|
|
if($items = $this->getItems()) {
|
|
|
|
foreach($items as $item) $titleArray[] = $item->Title;
|
2010-02-04 05:29:21 +01:00
|
|
|
foreach($items as $item) $itemIDs[] = $item->ID;
|
2007-07-19 12:40:28 +02:00
|
|
|
if($titleArray) $titleList = implode(", ", $titleArray);
|
2010-02-04 05:29:21 +01:00
|
|
|
if($itemIDs) $itemIDsList = implode(",", $itemIDs);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-02-04 05:29:21 +01:00
|
|
|
|
|
|
|
$field = new ReadonlyField($this->name.'_ReadonlyValue', $this->title);
|
2007-07-19 12:40:28 +02:00
|
|
|
$field->setValue($titleList);
|
|
|
|
$field->setForm($this->form);
|
2010-02-04 05:29:21 +01:00
|
|
|
|
|
|
|
$valueField = new HiddenField($this->name);
|
|
|
|
$valueField->setValue($itemIDsList);
|
|
|
|
$valueField->setForm($this->form);
|
|
|
|
|
|
|
|
return $field->Field().$valueField->Field();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|