2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Displays a set of checkboxes as a logical group.
|
|
|
|
*
|
2012-09-26 23:34:00 +02:00
|
|
|
* ASSUMPTION -> IF you pass your source as an array, you pass values as an array too. Likewise objects are handled
|
|
|
|
* the same.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 02:04:14 +02:00
|
|
|
* Example:
|
2015-02-16 21:26:36 +01:00
|
|
|
* <code>
|
2010-10-15 02:04:14 +02:00
|
|
|
* new CheckboxSetField(
|
2015-02-16 21:26:36 +01:00
|
|
|
* $name = "topics",
|
|
|
|
* $title = "I am interested in the following topics",
|
|
|
|
* $source = array(
|
|
|
|
* "1" => "Technology",
|
|
|
|
* "2" => "Gardening",
|
|
|
|
* "3" => "Cooking",
|
|
|
|
* "4" => "Sports"
|
|
|
|
* ),
|
|
|
|
* $value = "1"
|
|
|
|
* );
|
2010-10-15 02:04:14 +02:00
|
|
|
* </code>
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-10-15 02:04:14 +02:00
|
|
|
* <b>Saving</b>
|
|
|
|
* The checkbox set field will save its data in one of ways:
|
2012-09-26 23:34:00 +02:00
|
|
|
* - 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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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?
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
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 {
|
2014-08-15 08:53:05 +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();
|
2014-07-22 13:48:45 +02:00
|
|
|
|
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
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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
|
|
|
|
2014-07-22 13:48:45 +02:00
|
|
|
$properties = array_merge($properties, array(
|
|
|
|
'Options' => $this->getOptions()
|
|
|
|
));
|
|
|
|
|
|
|
|
return $this->customise($properties)->renderWith(
|
|
|
|
$this->getTemplates()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function getOptions() {
|
|
|
|
$odd = 0;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +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
|
|
|
}
|
2012-12-08 12:20:20 +01: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 {
|
2014-01-12 05:28:09 +01:00
|
|
|
if($values === null) {
|
|
|
|
$items = array();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$items = explode(',', $values);
|
|
|
|
$items = str_replace('{comma}', ',', $items);
|
|
|
|
}
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-22 13:48:45 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
if(is_array($source)) {
|
2007-07-19 12:40:28 +02:00
|
|
|
unset($source['']);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-22 13:10:57 +01:00
|
|
|
$options = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-22 13:48:45 +02:00
|
|
|
if ($source == null) {
|
|
|
|
$source = array();
|
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2014-07-22 13:48:45 +02:00
|
|
|
foreach($source as $value => $item) {
|
|
|
|
if($item instanceof DataObject) {
|
|
|
|
$value = $item->ID;
|
|
|
|
$title = $item->Title;
|
|
|
|
} else {
|
|
|
|
$title = $item;
|
|
|
|
}
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2014-07-22 13:48:45 +02:00
|
|
|
$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
|
|
|
|
2014-07-22 13:48:45 +02:00
|
|
|
$options[] = new ArrayData(array(
|
|
|
|
'ID' => $itemID,
|
|
|
|
'Class' => $extraClass,
|
|
|
|
'Name' => "{$this->name}[{$value}]",
|
|
|
|
'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
|
|
|
|
2014-07-22 13:48:45 +02:00
|
|
|
$options = new ArrayList($options);
|
|
|
|
|
|
|
|
$this->extend('updateGetOptions', $options);
|
2011-03-23 05:12:25 +01:00
|
|
|
|
2014-07-22 13:48:45 +02:00
|
|
|
return $options;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-07-22 13:48:45 +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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-02-22 07:11:58 +01:00
|
|
|
* @param Array $items Collection of array keys, as defined in the $source array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setDefaultItems($items) {
|
2010-02-22 07:11:58 +01:00
|
|
|
$this->defaultItems = $items;
|
2012-02-17 13:35:26 +01:00
|
|
|
return $this;
|
2010-02-22 07:11:58 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-02-22 07:11:58 +01:00
|
|
|
/**
|
|
|
|
* @return Array
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getDefaultItems() {
|
2010-02-22 07:11:58 +01:00
|
|
|
return $this->defaultItems;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-09-18 04:50:14 +02:00
|
|
|
/**
|
|
|
|
* Load a value into this CheckboxSetField
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setValue($value, $obj = null) {
|
2012-09-26 23:34:00 +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
|
|
|
}
|
2014-08-15 08:53:05 +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-09-19 12:07:39 +02:00
|
|
|
public function saveInto(DataObjectInterface $record) {
|
2012-03-06 23:47:09 +01:00
|
|
|
$fieldname = $this->name;
|
|
|
|
$relation = ($fieldname && $record && $record->hasMethod($fieldname)) ? $record->$fieldname() : null;
|
2013-08-21 08:54:05 +02:00
|
|
|
if($fieldname && $record && $relation &&
|
|
|
|
($relation instanceof RelationList || $relation instanceof UnsavedRelationList)) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 05:03:52 +02:00
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Return the CheckboxSetField value as a string
|
2009-02-02 00:49:53 +01:00
|
|
|
* selected item keys.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-02-02 00:49:53 +01:00
|
|
|
* @return string
|
2008-08-11 05:03:52 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public 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);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
return implode(',', $filtered);
|
2008-08-11 05:03:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
return '';
|
2008-08-11 05:03:52 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performDisabledTransformation() {
|
2008-12-04 23:38:32 +01:00
|
|
|
$clone = clone $this;
|
|
|
|
$clone->setDisabled(true);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
return $clone;
|
2008-08-12 04:58:48 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-02-02 00:49:53 +01:00
|
|
|
* @return ReadonlyField
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function performReadonlyTransformation() {
|
2007-07-19 12:40:28 +02:00
|
|
|
$values = '';
|
2009-02-02 00:49:53 +01:00
|
|
|
$data = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +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);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
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
|
|
|
}
|
2014-08-15 08:53:05 +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;
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
$values = implode(', ', $data);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-13 13:51:28 +01:00
|
|
|
$field = $this->castedCopy('ReadonlyField');
|
|
|
|
$field->setValue($values);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
return $field;
|
|
|
|
}
|
2011-12-22 13:10:57 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Type() {
|
2011-12-22 13:10:57 +01:00
|
|
|
return 'optionset checkboxset';
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function ExtraOptions() {
|
2007-07-19 12:40:28 +02:00
|
|
|
return FormField::ExtraOptions();
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-11-12 03:19:12 +01:00
|
|
|
/**
|
|
|
|
* Validate this field
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-05-11 02:32:00 +02:00
|
|
|
public function validate($validator) {
|
2014-11-12 03:19:12 +01:00
|
|
|
$values = $this->value;
|
|
|
|
if (!$values) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$sourceArray = $this->getSourceAsArray();
|
|
|
|
if (is_array($values)) {
|
|
|
|
if (!array_intersect_key($sourceArray, $values)) {
|
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
|
|
|
_t(
|
|
|
|
'CheckboxSetField.SOURCE_VALIDATION',
|
|
|
|
"Please select a value within the list provided. '{value}' is not a valid option",
|
|
|
|
array('value' => implode(' and ', array_diff($sourceArray, $values)))
|
|
|
|
),
|
|
|
|
"validation"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!in_array($this->value, $sourceArray)) {
|
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
|
|
|
_t(
|
|
|
|
'CheckboxSetField.SOURCE_VALIDATION',
|
|
|
|
"Please select a value within the list provided. '{value}' is not a valid option",
|
|
|
|
array('value' => $this->value)
|
|
|
|
),
|
|
|
|
"validation"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
}
|