2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Displays a set of checkboxes as a logical group.
|
|
|
|
*
|
|
|
|
* ASSUMPTION -> IF you pass your source as an array, you pass values as an array too.
|
|
|
|
* Likewise objects are handled the same.
|
2009-02-02 00:49:53 +01:00
|
|
|
*
|
|
|
|
* @todo Document the different source data that can be used
|
|
|
|
* with this form field - e.g ComponentSet, DataObjectSet,
|
|
|
|
* array. Is it also appropriate to accept so many different
|
|
|
|
* types of data when just using an array would be appropriate?
|
|
|
|
*
|
|
|
|
* @todo Make use of FormField->createTag() to generate the
|
|
|
|
* HTML tag(s) for this field.
|
|
|
|
*
|
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 {
|
|
|
|
|
|
|
|
protected $disabled = false;
|
2007-09-27 22:56:55 +02:00
|
|
|
|
2010-02-23 01:58:56 +01:00
|
|
|
/**
|
|
|
|
* @var Array
|
|
|
|
*/
|
|
|
|
protected $defaultItems = array();
|
|
|
|
|
2009-02-02 00:49:53 +01:00
|
|
|
/**
|
|
|
|
* @todo Explain different source data that can be used with this field,
|
|
|
|
* e.g. SQLMap, DataObjectSet or an array.
|
|
|
|
*
|
|
|
|
* @todo Should use CheckboxField FieldHolder rather than constructing own markup.
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function Field() {
|
2008-11-12 05:31:33 +01:00
|
|
|
Requirements::css(SAPPHIRE_DIR . '/css/CheckboxSetField.css');
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
$source = $this->source;
|
2007-07-19 12:40:28 +02:00
|
|
|
$values = $this->value;
|
2010-02-23 01:58:56 +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.
|
|
|
|
if($values && is_a($values, 'DataObjectSet')) {
|
|
|
|
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 {
|
2008-12-04 23:38:32 +01:00
|
|
|
// Sometimes we pass a singluar default value thats ! an array && !DataObjectSet
|
|
|
|
if(is_a($values, 'DataObjectSet') || 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;
|
|
|
|
$options = '';
|
2009-11-05 01:29:21 +01:00
|
|
|
|
|
|
|
if ($source == null) {
|
|
|
|
$source = array();
|
|
|
|
$options = "<li>No options available</li>";
|
|
|
|
}
|
2008-12-04 23:38:32 +01:00
|
|
|
|
2009-10-18 01:33:13 +02:00
|
|
|
if($source) foreach($source as $index => $item) {
|
2008-12-04 23:38:32 +01:00
|
|
|
if(is_a($item, 'DataObject')) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$key = $item->ID;
|
|
|
|
$value = $item->Title;
|
|
|
|
} else {
|
|
|
|
$key = $index;
|
|
|
|
$value = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
$odd = ($odd + 1) % 2;
|
2008-12-04 23:38:32 +01:00
|
|
|
$extraClass = $odd ? 'odd' : 'even';
|
|
|
|
$extraClass .= ' val' . str_replace(' ', '', $key);
|
|
|
|
$itemID = $this->id() . '_' . ereg_replace('[^a-zA-Z0-9]+', '', $key);
|
|
|
|
$checked = '';
|
2007-12-13 23:33:51 +01:00
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
if(isset($items)) {
|
2010-02-23 01:58:56 +01:00
|
|
|
$checked = (in_array($key, $items) || in_array($key, $this->defaultItems)) ? ' checked="checked"' : '';
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2010-02-23 01:58:56 +01:00
|
|
|
|
|
|
|
$disabled = ($this->disabled || in_array($key, $this->disabledItems)) ? $disabled = ' disabled="disabled"' : '';
|
2008-08-11 02:03:57 +02:00
|
|
|
$options .= "<li class=\"$extraClass\"><input id=\"$itemID\" name=\"$this->name[$key]\" type=\"checkbox\" value=\"$key\"$checked $disabled class=\"checkbox\" /> <label for=\"$itemID\">$value</label></li>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
2008-12-04 23:38:32 +01:00
|
|
|
return "<ul id=\"{$this->id()}\" class=\"optionset checkboxsetfield{$this->extraClass()}\">\n$options</ul>\n";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setDisabled($val) {
|
|
|
|
$this->disabled = $val;
|
|
|
|
}
|
|
|
|
|
2010-02-23 01:58:56 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
$selected = $obj->$funcName();
|
2009-02-02 00:49:53 +01:00
|
|
|
$value = $selected->toDropdownMap('ID', 'ID');
|
2008-09-18 04:50:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
parent::setValue($value, $obj);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function saveInto(DataObject $record) {
|
|
|
|
$fieldname = $this->name ;
|
|
|
|
if($fieldname && $record && ($record->has_many($fieldname) || $record->many_many($fieldname))) {
|
2008-08-11 05:03:52 +02:00
|
|
|
$idList = array();
|
2008-08-14 01:56:27 +02:00
|
|
|
if($this->value) foreach($this->value as $id => $bool) {
|
2009-10-16 00:24:09 +02:00
|
|
|
if($bool) {
|
|
|
|
$idList[] = $id;
|
|
|
|
}
|
2008-08-11 05:03:52 +02:00
|
|
|
}
|
|
|
|
$record->$fieldname()->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);
|
2007-07-19 12:40:28 +02:00
|
|
|
$record->$fieldname = implode(",", $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
|
|
|
/**
|
2009-02-02 00:49:53 +01:00
|
|
|
* Return the CheckboxSetField value as an array
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2009-02-02 00:49:53 +01:00
|
|
|
if(is_a($items, 'DataObjectSet')) {
|
|
|
|
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];
|
2009-09-05 02:05:02 +02:00
|
|
|
} elseif(is_a($this->source, 'DataObjectSet')) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ExtraOptions() {
|
|
|
|
return FormField::ExtraOptions();
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2009-02-02 00:49:53 +01:00
|
|
|
?>
|