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
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
* @copyright 2002-11 by RalfBecker@outdoor-training.de
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* eTemplate checkbox widget
|
|
|
|
*
|
|
|
|
* Multiple checkbox widgets can have the same name ending in [], in which case an array with the set_val's of the checked boxes get returned.
|
|
|
|
*/
|
2011-08-21 19:13:08 +02:00
|
|
|
class etemplate_widget_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(
|
|
|
|
'checkbox' => 'set_val,unset_val,ro_true,ro_false',
|
|
|
|
'radio' => 'set_val,ro_true,ro_false',
|
|
|
|
);
|
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);
|
|
|
|
$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 !!!'),'');
|
|
|
|
}
|
|
|
|
// defaults for set and unset values
|
|
|
|
if (!$this->attrs['set_val'] && !$this->attrs['unset_val'])
|
|
|
|
{
|
2013-06-18 18:39:26 +02:00
|
|
|
$set_val = 'true';
|
|
|
|
$unset_val = 'false';
|
2011-08-21 15:47:45 +02:00
|
|
|
}
|
2012-05-08 22:25:56 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Expand any content stuff
|
|
|
|
$set_val = self::expand_name($this->attrs['set_val'], $expand['c'], $expand['row'], $expand['c_'], $expand['row_'],$expand['cont']);
|
|
|
|
$unset_val = self::expand_name($this->attrs['unset_val'], $expand['c'], $expand['row'], $expand['c_'], $expand['row_'],$expand['cont']);
|
|
|
|
}
|
|
|
|
if (in_array((string)$set_val, (array)$value))
|
2011-08-21 15:47:45 +02:00
|
|
|
{
|
|
|
|
if ($multiple)
|
|
|
|
{
|
|
|
|
if (!isset($valid)) $valid = array();
|
2012-05-08 22:25:56 +02:00
|
|
|
$valid[] = $set_val;
|
2011-08-21 15:47:45 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-08 22:25:56 +02:00
|
|
|
$valid = $set_val;
|
2011-08-21 15:47:45 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-21 16:03:52 +02:00
|
|
|
elseif ($this->type == 'radio')
|
|
|
|
{
|
|
|
|
if (!isset($valid)) $valid = ''; // do not overwrite value of an other radio-button of the same group (identical name)!
|
|
|
|
}
|
2011-08-21 15:47:45 +02:00
|
|
|
else // if checkbox is not checked, html returns nothing: eTemplate returns unset_val (default false)
|
|
|
|
{
|
|
|
|
if ($multiple)
|
|
|
|
{
|
|
|
|
if (!isset($valid)) $valid = array();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-08 22:25:56 +02:00
|
|
|
$valid = $unset_val;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-21 16:03:52 +02:00
|
|
|
etemplate_widget::registerWidget('etemplate_widget_checkbox', array('checkbox', 'radio'));
|