2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Displays a set of checkboxes as a logical group.
|
|
|
|
*
|
2010-10-15 02:04:14 +02:00
|
|
|
* ASSUMPTION -> IF you pass your source as an array, you pass values as an array too. Likewise objects are handled the same.
|
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* <code php>
|
|
|
|
* new CheckboxSetField(
|
|
|
|
* $name = "topics",
|
|
|
|
* $title = "I am interested in the following topics",
|
|
|
|
* $source = array(
|
|
|
|
* "1" => "Technology",
|
|
|
|
* "2" => "Gardening",
|
|
|
|
* "3" => "Cooking",
|
|
|
|
* "4" => "Sports"
|
|
|
|
* ),
|
|
|
|
* $value = "1"
|
|
|
|
* )
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* <b>Saving</b>
|
|
|
|
* The checkbox set field will save its data in one of ways:
|
|
|
|
* * If the field name matches a many-many join on the object being edited, that many-many join will be updated to link to the objects selected on the checkboxes. In this case, the keys of your value map should be the IDs of the database records.
|
|
|
|
* * If the field name matches a database field, a comma-separated list of values will be saved to that field. The keys can be text or numbers.
|
2009-02-02 00:49:53 +01:00
|
|
|
*
|
|
|
|
* @todo Document the different source data that can be used
|
2011-10-26 08:09:04 +02:00
|
|
|
* with this form field - e.g ComponentSet, ArrayList,
|
2009-02-02 00:49:53 +01:00
|
|
|
* array. Is it also appropriate to accept so many different
|
|
|
|
* types of data when just using an array would be appropriate?
|
|
|
|
*
|
2008-01-09 05:18:36 +01:00
|
|
|
* @package forms
|
|
|
|
* @subpackage fields-basic
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class CheckboxSetField extends OptionsetField {
|
2007-09-27 22:56:55 +02:00
|
|
|
|
2010-02-22 07:11:58 +01:00
|
|
|
/**
|
2012-04-14 07:32:29 +02:00
|
|
|
* @var array
|
2010-02-22 07:11:58 +01:00
|
|
|
*/
|
|
|
|
protected $defaultItems = array();
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
|
|
|
* @todo Explain different source data that can be used with this field,
|
2011-10-26 08:09:04 +02:00
|
|
|
* e.g. SQLMap, ArrayList or an array.
|
2009-02-02 00:49:53 +01:00
|
|
|
*/
|
2011-03-23 05:12:25 +01:00
|
|
|
function Field($properties = array()) {
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/CheckboxSetField.css');
|
2008-11-12 05:31:33 +01:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
$source = $this->source;
|
2007-07-19 12:40:28 +02:00
|
|
|
$values = $this->value;
|
2011-03-23 05:12:25 +01:00
|
|
|
$items = array();
|
2010-02-22 07:11:58 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Get values from the join, if available
|
|
|
|
if(is_object($this->form)) {
|
|
|
|
$record = $this->form->getRecord();
|
2007-07-24 05:44:03 +02:00
|
|
|
if(!$values && $record && $record->hasMethod($this->name)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$funcName = $this->name;
|
|
|
|
$join = $record->$funcName();
|
2008-12-04 23:38:32 +01:00
|
|
|
if($join) {
|
|
|
|
foreach($join as $joinItem) {
|
|
|
|
$values[] = $joinItem->ID;
|
|
|
|
}
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
|
|
|
|
// Source is not an array
|
|
|
|
if(!is_array($source) && !is_a($source, 'SQLMap')) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_array($values)) {
|
|
|
|
$items = $values;
|
|
|
|
} else {
|
2008-12-04 23:38:32 +01:00
|
|
|
// Source and values are DataObject sets.
|
2011-10-26 08:09:04 +02:00
|
|
|
if($values && is_a($values, 'SS_List')) {
|
2008-12-04 23:38:32 +01:00
|
|
|
foreach($values as $object) {
|
|
|
|
if(is_a($object, 'DataObject')) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$items[] = $object->ID;
|
2008-12-04 23:38:32 +01:00
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
} elseif($values && is_string($values)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$items = explode(',', $values);
|
|
|
|
$items = str_replace('{comma}', ',', $items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2011-10-26 08:09:04 +02:00
|
|
|
// Sometimes we pass a singluar default value thats ! an array && !SS_List
|
2012-01-02 16:47:07 +01:00
|
|
|
if($values instanceof SS_List || is_array($values)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$items = $values;
|
2008-12-04 23:38:32 +01:00
|
|
|
} else {
|
|
|
|
$items = explode(',', $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
$items = str_replace('{comma}', ',', $items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
if(is_array($source)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
unset($source['']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$odd = 0;
|
2011-12-22 13:10:57 +01:00
|
|
|
$options = array();
|
2009-11-05 01:29:21 +01:00
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
if ($source == null) $source = array();
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2011-03-23 05:12:25 +01:00
|
|
|
if($source) {
|
|
|
|
foreach($source as $value => $item) {
|
|
|
|
if($item instanceof DataObject) {
|
|
|
|
$value = $item->ID;
|
|
|
|
$title = $item->Title;
|
|
|
|
} else {
|
|
|
|
$title = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
$itemID = $this->ID() . '_' . preg_replace('/[^a-zA-Z0-9]/', '', $value);
|
|
|
|
$odd = ($odd + 1) % 2;
|
|
|
|
$extraClass = $odd ? 'odd' : 'even';
|
|
|
|
$extraClass .= ' val' . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $value);
|
2010-02-22 07:11:58 +01:00
|
|
|
|
2011-03-23 05:12:25 +01:00
|
|
|
$options[] = new ArrayData(array(
|
|
|
|
'ID' => $itemID,
|
|
|
|
'Class' => $extraClass,
|
2011-12-22 13:10:57 +01:00
|
|
|
'Name' => "{$this->name}[{$value}]",
|
2011-03-23 05:12:25 +01:00
|
|
|
'Value' => $value,
|
|
|
|
'Title' => $title,
|
|
|
|
'isChecked' => in_array($value, $items) || in_array($value, $this->defaultItems),
|
|
|
|
'isDisabled' => $this->disabled || in_array($value, $this->disabledItems)
|
|
|
|
));
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
|
|
|
$properties = array_merge($properties, array('Options' => new ArrayList($options)));
|
|
|
|
|
2012-04-14 07:32:29 +02:00
|
|
|
return $this->customise($properties)->renderWith($this->getTemplates());
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2010-02-22 07:11:58 +01:00
|
|
|
/**
|
|
|
|
* Default selections, regardless of the {@link setValue()} settings.
|
|
|
|
* Note: Items marked as disabled through {@link setDisabledItems()} can still be
|
|
|
|
* selected by default through this method.
|
|
|
|
*
|
|
|
|
* @param Array $items Collection of array keys, as defined in the $source array
|
|
|
|
*/
|
|
|
|
function setDefaultItems($items) {
|
|
|
|
$this->defaultItems = $items;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2010-02-22 07:11:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
function getDefaultItems() {
|
|
|
|
return $this->defaultItems;
|
|
|
|
}
|
|
|
|
|
2008-09-18 04:50:14 +02:00
|
|
|
/**
|
|
|
|
* Load a value into this CheckboxSetField
|
|
|
|
*/
|
2008-09-18 05:16:03 +02:00
|
|
|
function setValue($value, $obj = null) {
|
2008-09-18 04:50:14 +02:00
|
|
|
// If we're not passed a value directly, we can look for it in a relation method on the object passed as a second arg
|
2008-09-18 05:16:03 +02:00
|
|
|
if(!$value && $obj && $obj instanceof DataObject && $obj->hasMethod($this->name)) {
|
2008-09-18 04:50:14 +02:00
|
|
|
$funcName = $this->name;
|
2009-11-22 06:16:38 +01:00
|
|
|
$value = $obj->$funcName()->getIDList();
|
2008-09-18 04:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parent::setValue($value, $obj);
|
2012-02-17 13:35:26 +01:00
|
|
|
|
|
|
|
return $this;
|
2008-09-18 04:50:14 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2008-06-30 04:55:51 +02:00
|
|
|
* Save the current value of this CheckboxSetField into a DataObject.
|
|
|
|
* If the field it is saving to is a has_many or many_many relationship,
|
|
|
|
* it is saved by setByIDList(), otherwise it creates a comma separated
|
|
|
|
* list for a standard DB text/varchar field.
|
|
|
|
*
|
|
|
|
* @param DataObject $record The record to save into
|
|
|
|
*/
|
2012-04-11 07:00:57 +02:00
|
|
|
function saveInto(DataObjectInterface $record) {
|
2012-03-06 23:47:09 +01:00
|
|
|
$fieldname = $this->name;
|
|
|
|
$relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null;
|
|
|
|
if($fieldname && $record && $relation && $relation instanceof RelationList) {
|
2008-08-11 05:03:52 +02:00
|
|
|
$idList = array();
|
2012-03-05 10:55:44 +01:00
|
|
|
if($this->value) foreach($this->value as $id => $bool) {
|
2012-06-11 03:39:18 +02:00
|
|
|
if($bool) {
|
2012-03-05 10:55:44 +01:00
|
|
|
$idList[] = $id;
|
|
|
|
}
|
2008-08-11 05:03:52 +02:00
|
|
|
}
|
2012-03-06 23:47:09 +01:00
|
|
|
$relation->setByIDList($idList);
|
2008-06-30 04:55:51 +02:00
|
|
|
} elseif($fieldname && $record) {
|
|
|
|
if($this->value) {
|
2009-02-02 00:49:53 +01:00
|
|
|
$this->value = str_replace(',', '{comma}', $this->value);
|
2012-06-11 03:39:18 +02:00
|
|
|
$record->$fieldname = implode(',', (array) $this->value);
|
2007-11-09 04:04:27 +01:00
|
|
|
} else {
|
|
|
|
$record->$fieldname = '';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-11 05:03:52 +02:00
|
|
|
/**
|
2010-10-19 06:53:24 +02:00
|
|
|
* Return the CheckboxSetField value as a string
|
2009-02-02 00:49:53 +01:00
|
|
|
* selected item keys.
|
|
|
|
*
|
|
|
|
* @return string
|
2008-08-11 05:03:52 +02:00
|
|
|
*/
|
|
|
|
function dataValue() {
|
2009-02-02 00:49:53 +01:00
|
|
|
if($this->value && is_array($this->value)) {
|
2008-08-11 05:03:52 +02:00
|
|
|
$filtered = array();
|
2009-02-02 00:49:53 +01:00
|
|
|
foreach($this->value as $item) {
|
|
|
|
if($item) {
|
|
|
|
$filtered[] = str_replace(",", "{comma}", $item);
|
|
|
|
}
|
|
|
|
}
|
2012-03-05 10:55:44 +01:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
return implode(',', $filtered);
|
2008-08-11 05:03:52 +02:00
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
|
|
|
return '';
|
2008-08-11 05:03:52 +02:00
|
|
|
}
|
|
|
|
|
2008-08-12 04:58:48 +02:00
|
|
|
function performDisabledTransformation() {
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone = clone $this;
|
|
|
|
$clone->setDisabled(true);
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
return $clone;
|
2008-08-12 04:58:48 +02:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2009-02-02 00:49:53 +01:00
|
|
|
* Transforms the source data for this CheckboxSetField
|
|
|
|
* into a comma separated list of values.
|
|
|
|
*
|
|
|
|
* @return ReadonlyField
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function performReadonlyTransformation() {
|
|
|
|
$values = '';
|
2009-02-02 00:49:53 +01:00
|
|
|
$data = array();
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
$items = $this->value;
|
2009-02-02 00:49:53 +01:00
|
|
|
if($this->source) {
|
|
|
|
foreach($this->source as $source) {
|
|
|
|
if(is_object($source)) {
|
|
|
|
$sourceTitles[$source->ID] = $source->Title;
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
if($items) {
|
2007-07-19 12:40:28 +02:00
|
|
|
// Items is a DO Set
|
2012-01-02 16:47:07 +01:00
|
|
|
if($items instanceof SS_List) {
|
2009-02-02 00:49:53 +01:00
|
|
|
foreach($items as $item) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$data[] = $item->Title;
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
if($data) $values = implode(', ', $data);
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Items is an array or single piece of string (including comma seperated string)
|
2009-02-02 00:49:53 +01:00
|
|
|
} else {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!is_array($items)) {
|
2009-06-18 11:34:17 +02:00
|
|
|
$items = preg_split('/ *, */', trim($items));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
|
|
|
foreach($items as $item) {
|
2007-07-19 12:40:28 +02:00
|
|
|
if(is_array($item)) {
|
|
|
|
$data[] = $item['Title'];
|
2009-02-02 00:49:53 +01:00
|
|
|
} elseif(is_array($this->source) && !empty($this->source[$item])) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$data[] = $this->source[$item];
|
2011-10-26 08:09:04 +02:00
|
|
|
} elseif(is_a($this->source, 'SS_List')) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$data[] = $sourceTitles[$item];
|
|
|
|
} else {
|
|
|
|
$data[] = $item;
|
|
|
|
}
|
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
|
|
|
|
$values = implode(', ', $data);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
$title = ($this->title) ? $this->title : '';
|
|
|
|
|
|
|
|
$field = new ReadonlyField($this->name, $title, $values);
|
2007-07-19 12:40:28 +02:00
|
|
|
$field->setForm($this->form);
|
2009-02-02 00:49:53 +01:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $field;
|
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
|
|
|
|
function Type() {
|
|
|
|
return 'optionset checkboxset';
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
function ExtraOptions() {
|
|
|
|
return FormField::ExtraOptions();
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2012-03-24 04:38:57 +01:00
|
|
|
|
|
|
|
}
|