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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-07-11 12:44:59 +02:00
|
|
|
* <b>Saving</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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}
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-07-11 12:44:59 +02:00
|
|
|
* <b>Customizing Save Behaviour</b>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* Returning false on this method will prevent treemultiselect from saving to the {@link ComponentSet} of the given
|
|
|
|
* {@link DataObject}
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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]);
|
|
|
|
* }
|
|
|
|
* }
|
2014-08-15 08:53:05 +02:00
|
|
|
* }
|
2010-07-11 12:44:59 +02:00
|
|
|
* return true;
|
|
|
|
* }
|
2014-08-15 08:53:05 +02:00
|
|
|
* </code>
|
|
|
|
*
|
2010-07-23 01:18:40 +02:00
|
|
|
* @see TreeDropdownField for the sample implementation, but only allowing single selects
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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);
|
2015-02-08 00:00:36 +01:00
|
|
|
$this->removeExtraClass('single');
|
|
|
|
$this->addExtraClass('multiple');
|
2007-07-19 12:40:28 +02:00
|
|
|
$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;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-02-04 02:07:30 +01:00
|
|
|
// 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();
|
2014-08-15 08:53:05 +02:00
|
|
|
if(is_object($record) && $record->hasMethod($fieldName))
|
2007-07-19 12:40:28 +02:00
|
|
|
return $record->$fieldName();
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* We overwrite the field attribute to add our hidden fields, as this
|
2007-07-19 12:40:28 +02:00
|
|
|
* formfield can contain multiple values.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Field($properties = array()) {
|
2016-05-09 05:28:24 +02:00
|
|
|
Requirements::add_i18n_javascript(FRAMEWORK_DIR . '/client/lang');
|
2014-08-15 08:53:05 +02: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');
|
2016-04-19 11:04:43 +02:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/client/dist/js/TreeDropdownField.js');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
|
2016-04-19 11:04:43 +02:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/client/dist/styles/TreeDropdownField.css');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-07 20:29:09 +01:00
|
|
|
$value = '';
|
2015-02-10 06:01:19 +01:00
|
|
|
$titleArray = array();
|
|
|
|
$idArray = array();
|
2010-03-04 23:51:44 +01:00
|
|
|
$items = $this->getItems();
|
2015-12-15 12:27:22 +01:00
|
|
|
$emptyTitle = _t('DropdownField.CHOOSE', '(Choose)', 'start value of a dropdown');
|
2010-03-04 23:51:44 +01:00
|
|
|
|
|
|
|
if($items && count($items)) {
|
2015-02-10 06:01:19 +01:00
|
|
|
foreach($items as $item) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$idArray[] = $item->ID;
|
2015-02-10 06:01:19 +01:00
|
|
|
$titleArray[] = ($item instanceof ViewableData)
|
|
|
|
? $item->obj($this->labelField)->forTemplate()
|
|
|
|
: Convert::raw2xml($item->{$this->labelField});
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-10 06:01:19 +01:00
|
|
|
$title = implode(", ", $titleArray);
|
|
|
|
$value = implode(",", $idArray);
|
2011-03-07 20:29:09 +01:00
|
|
|
} else {
|
2015-12-15 12:27:22 +01:00
|
|
|
$title = $emptyTitle;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2013-03-18 20:48:44 +01:00
|
|
|
$dataUrlTree = '';
|
|
|
|
if ($this->form){
|
|
|
|
$dataUrlTree = $this->Link('tree');
|
2015-02-08 00:00:36 +01:00
|
|
|
if (!empty($idArray)){
|
2013-10-02 15:42:29 +02:00
|
|
|
$dataUrlTree = Controller::join_links($dataUrlTree, '?forceValue='.implode(',',$idArray));
|
2013-03-18 20:48:44 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-08 00:00:36 +01:00
|
|
|
$properties = array_merge(
|
|
|
|
$properties,
|
|
|
|
array(
|
|
|
|
'Title' => $title,
|
2015-12-15 12:27:22 +01:00
|
|
|
'EmptyTitle' => $emptyTitle,
|
2015-02-08 00:00:36 +01:00
|
|
|
'Link' => $dataUrlTree,
|
2015-02-10 06:01:19 +01:00
|
|
|
'Value' => $value
|
2011-03-07 20:29:09 +01:00
|
|
|
)
|
|
|
|
);
|
2015-02-08 00:00:36 +01:00
|
|
|
return $this->customise($properties)->renderWith('TreeDropdownField');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the results into the form
|
2014-08-15 08:53:05 +02:00
|
|
|
* Calls function $record->onChange($items) before saving to the assummed
|
2007-07-19 12:40:28 +02:00
|
|
|
* 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();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Changes this field to the readonly field.
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2012-12-13 13:51:28 +01:00
|
|
|
$copy = $this->castedCopy('TreeMultiselectField_Readonly');
|
|
|
|
$copy->setKeyField($this->keyField);
|
|
|
|
$copy->setLabelField($this->labelField);
|
|
|
|
$copy->setSourceObject($this->sourceObject);
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2012-12-13 13:51:28 +01:00
|
|
|
return $copy;
|
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 {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
protected $readonly = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +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);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-02-04 05:29:21 +01:00
|
|
|
$valueField = new HiddenField($this->name);
|
|
|
|
$valueField->setValue($itemIDsList);
|
|
|
|
$valueField->setForm($this->form);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-02-04 05:29:21 +01:00
|
|
|
return $field->Field().$valueField->Field();
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|