2011-08-21 15:47:45 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EGroupware - eTemplate serverside checkbox widget
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
2016-03-19 14:06:07 +01:00
|
|
|
* @package api
|
|
|
|
* @subpackage etemplate
|
2011-08-21 15:47:45 +02:00
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
2016-03-19 14:06:07 +01:00
|
|
|
* @copyright 2002-16 by RalfBecker@outdoor-training.de
|
2011-08-21 15:47:45 +02:00
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
2016-03-19 14:06:07 +01:00
|
|
|
namespace EGroupware\Api\Etemplate\Widget;
|
|
|
|
|
|
|
|
use EGroupware\Api\Etemplate;
|
|
|
|
|
2011-08-21 15:47:45 +02:00
|
|
|
/**
|
|
|
|
* eTemplate checkbox widget
|
|
|
|
*
|
2013-11-29 06:49:13 +01:00
|
|
|
* Multiple checkbox widgets can have the same name ending in [], in which case an array with the selected_value's of the checked boxes get returned.
|
2011-08-21 15:47:45 +02:00
|
|
|
*/
|
2016-03-19 14:06:07 +01:00
|
|
|
class Checkbox extends Etemplate\Widget
|
2011-08-21 15:47:45 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* (Array of) comma-separated list of legacy options to automatically replace when parsing with set_attrs
|
|
|
|
*
|
|
|
|
* @var string|array
|
|
|
|
*/
|
2011-08-21 16:03:52 +02:00
|
|
|
protected $legacy_options = array(
|
2013-11-29 06:49:13 +01:00
|
|
|
'checkbox' => 'selected_value,unselected_value,ro_true,ro_false',
|
|
|
|
'radio' => 'set_value,ro_true,ro_false',
|
2011-08-21 16:03:52 +02:00
|
|
|
);
|
2011-08-21 15:47:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate input
|
|
|
|
*
|
|
|
|
* In case of multiple checkboxes using the same name ending in [], each widget only validates it's own value!
|
2011-08-21 16:03:52 +02:00
|
|
|
* Same is true for the radio buttons of a radio-group sharing the same name.
|
2011-08-21 15:47:45 +02:00
|
|
|
*
|
|
|
|
* @param string $cname current namespace
|
2012-05-03 16:17:47 +02:00
|
|
|
* @param array $expand values for keys 'c', 'row', 'c_', 'row_', 'cont'
|
2011-08-21 15:47:45 +02:00
|
|
|
* @param array $content
|
|
|
|
* @param array &$validated=array() validated content
|
|
|
|
* @return boolean true if no validation error, false otherwise
|
|
|
|
*/
|
2012-05-03 16:17:47 +02:00
|
|
|
public function validate($cname, array $expand, array $content, &$validated=array())
|
2011-08-21 15:47:45 +02:00
|
|
|
{
|
2012-05-03 16:17:47 +02:00
|
|
|
$form_name = self::form_name($cname, $this->id, $expand);
|
2011-08-21 15:47:45 +02:00
|
|
|
|
2011-08-21 16:03:52 +02:00
|
|
|
if (($multiple = substr($form_name, -2) == '[]') && $this->type == 'checkbox')
|
2011-08-21 15:47:45 +02:00
|
|
|
{
|
|
|
|
$form_name = substr($form_name, 0, -2);
|
|
|
|
}
|
|
|
|
|
2012-05-03 16:17:47 +02:00
|
|
|
if (!$this->is_readonly($cname, $form_name))
|
2011-08-21 15:47:45 +02:00
|
|
|
{
|
|
|
|
$value = self::get_array($content, $form_name);
|
2015-03-20 03:12:33 +01:00
|
|
|
if (!isset($value)) return; // value not transmitted --> nothing to validate
|
2011-08-21 15:47:45 +02:00
|
|
|
$valid =& self::get_array($validated, $form_name, true);
|
|
|
|
|
|
|
|
if (!$value && $this->attrs['needed'])
|
|
|
|
{
|
|
|
|
self::set_validation_error($form_name,lang('Field must not be empty !!!'),'');
|
|
|
|
}
|
2014-06-30 21:34:53 +02:00
|
|
|
$type = $this->type ? $this->type : $this->attrs['type'];
|
|
|
|
$value_attr = $type == 'radio' ? 'set_value' : 'selected_value';
|
2011-08-21 15:47:45 +02:00
|
|
|
// defaults for set and unset values
|
2014-08-18 19:33:43 +02:00
|
|
|
$selected_value = true;
|
|
|
|
$unselected_value = false;
|
|
|
|
if (array_key_exists($value_attr, $this->attrs) || array_key_exists('unselected_value',$this->attrs))
|
2011-08-21 15:47:45 +02:00
|
|
|
{
|
2014-08-18 19:33:43 +02:00
|
|
|
if(array_key_exists($value_attr, $this->attrs))
|
|
|
|
{
|
|
|
|
// Expand any content stuff
|
|
|
|
$selected_value = self::expand_name($this->attrs[$value_attr], $expand['c'], $expand['row'], $expand['c_'], $expand['row_'],$expand['cont']);
|
|
|
|
}
|
|
|
|
if(array_key_exists('unselected_value',$this->attrs))
|
|
|
|
{
|
|
|
|
$unselected_value = self::expand_name($this->attrs['unselected_value'], $expand['c'], $expand['row'], $expand['c_'], $expand['row_'],$expand['cont']);
|
|
|
|
}
|
2012-05-08 22:25:56 +02:00
|
|
|
}
|
2014-06-30 21:34:53 +02:00
|
|
|
if ($type == 'radio')
|
|
|
|
{
|
2016-03-19 14:06:07 +01:00
|
|
|
$options = Select::selOptions($form_name, true);
|
2014-06-30 21:34:53 +02:00
|
|
|
if (in_array($value, $options))
|
|
|
|
{
|
|
|
|
$valid = $value;
|
|
|
|
}
|
2014-07-07 18:19:37 +02:00
|
|
|
elseif ( !$options && $selected_value && $value == $selected_value)
|
|
|
|
{
|
|
|
|
// Options set in template, not in sel_options
|
|
|
|
$valid = $selected_value;
|
|
|
|
}
|
2014-06-30 21:34:53 +02:00
|
|
|
elseif (!isset($valid))
|
|
|
|
{
|
|
|
|
$valid = ''; // do not overwrite value of an other radio-button of the same group (identical name)!
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elseif (in_array((string)$selected_value, (array)$value))
|
2011-08-21 15:47:45 +02:00
|
|
|
{
|
|
|
|
if ($multiple)
|
|
|
|
{
|
|
|
|
if (!isset($valid)) $valid = array();
|
2013-11-29 06:49:13 +01:00
|
|
|
$valid[] = $selected_value;
|
2011-08-21 15:47:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-29 06:49:13 +01:00
|
|
|
$valid = $selected_value;
|
2011-08-21 15:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
2013-11-29 06:49:13 +01:00
|
|
|
else // if checkbox is not checked, html returns nothing: eTemplate returns unselected_value (default false)
|
2011-08-21 15:47:45 +02:00
|
|
|
{
|
|
|
|
if ($multiple)
|
|
|
|
{
|
|
|
|
if (!isset($valid)) $valid = array();
|
|
|
|
}
|
2013-06-25 01:03:55 +02:00
|
|
|
elseif ($value === 'true')
|
|
|
|
{
|
|
|
|
// 'true' != true
|
2013-11-29 06:49:13 +01:00
|
|
|
$valid = $selected_value;
|
2013-06-25 01:03:55 +02:00
|
|
|
}
|
2011-08-21 15:47:45 +02:00
|
|
|
else
|
|
|
|
{
|
2013-11-29 06:49:13 +01:00
|
|
|
$valid = $unselected_value;
|
2011-08-21 15:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-08 22:25:56 +02:00
|
|
|
//error_log(__METHOD__.'() '.$form_name.($multiple?'[]':'').': '.array2string($value).' --> '.array2string($valid));
|
2011-08-21 15:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-19 14:06:07 +01:00
|
|
|
Etemplate\Widget::registerWidget(__NAMESPACE__.'\\Checkbox', array('checkbox', 'radio'));
|