2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
2015-02-13 05:35:39 +01:00
|
|
|
class CheckboxSetField extends MultiSelectField {
|
2014-07-22 13:48:45 +02:00
|
|
|
|
2016-03-21 11:12:23 +01:00
|
|
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_MULTISELECT;
|
|
|
|
|
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()) {
|
2016-04-19 11:04:43 +02:00
|
|
|
Requirements::css(FRAMEWORK_DIR . '/client/dist/styles/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()
|
|
|
|
));
|
|
|
|
|
2016-07-07 20:55:37 +02:00
|
|
|
return FormField::Field($properties);
|
2014-07-22 13:48:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-13 05:35:39 +01:00
|
|
|
* Gets the list of options to render in this formfield
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2014-07-22 13:48:45 +02:00
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
public function getOptions() {
|
2015-02-13 05:35:39 +01:00
|
|
|
$selectedValues = $this->getValueArray();
|
|
|
|
$defaultItems = $this->getDefaultItems();
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
// Generate list of options to display
|
2016-08-22 17:39:11 +02:00
|
|
|
$odd = false;
|
2015-02-13 05:35:39 +01:00
|
|
|
$formID = $this->ID();
|
|
|
|
$options = new ArrayList();
|
|
|
|
foreach($this->getSource() as $itemValue => $title) {
|
|
|
|
$itemID = Convert::raw2htmlid("{$formID}_{$itemValue}");
|
2016-08-22 17:39:11 +02:00
|
|
|
$odd = !$odd;
|
2014-07-22 13:48:45 +02:00
|
|
|
$extraClass = $odd ? 'odd' : 'even';
|
2015-02-13 05:35:39 +01:00
|
|
|
$extraClass .= ' val' . preg_replace('/[^a-zA-Z0-9\-\_]/', '_', $itemValue);
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
$itemChecked = in_array($itemValue, $selectedValues) || in_array($itemValue, $defaultItems);
|
|
|
|
$itemDisabled = $this->isDisabled() || in_array($itemValue, $defaultItems);
|
2010-02-22 07:11:58 +01:00
|
|
|
|
2015-02-13 05:35:39 +01:00
|
|
|
$options->push(new ArrayData(array(
|
2014-07-22 13:48:45 +02:00
|
|
|
'ID' => $itemID,
|
|
|
|
'Class' => $extraClass,
|
2015-02-13 05:35:39 +01:00
|
|
|
'Name' => "{$this->name}[{$itemValue}]",
|
|
|
|
'Value' => $itemValue,
|
2014-07-22 13:48:45 +02:00
|
|
|
'Title' => $title,
|
2015-02-13 05:35:39 +01:00
|
|
|
'isChecked' => $itemChecked,
|
|
|
|
'isDisabled' => $itemDisabled,
|
|
|
|
)));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-07-22 13:48:45 +02:00
|
|
|
$this->extend('updateGetOptions', $options);
|
|
|
|
return $options;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-07-22 13:48:45 +02: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-03-24 04:38:57 +01:00
|
|
|
}
|