2011-08-18 23:56:37 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EGroupware - eTemplate serverside textbox 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 textbox widget with following sub-types:
|
2011-08-19 10:22:19 +02:00
|
|
|
* - textbox with optional multiline="true" and rows="123"
|
|
|
|
* - integer or int
|
2011-08-18 23:56:37 +02:00
|
|
|
* - float
|
|
|
|
* - hidden
|
|
|
|
* - colorpicker
|
|
|
|
* sub-types are either passed to constructor or set via 'type' attribute!
|
|
|
|
*/
|
|
|
|
class etemplate_widget_textbox extends etemplate_widget
|
|
|
|
{
|
2011-08-19 10:22:19 +02:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string|XMLReader $xml string with xml or XMLReader positioned on the element to construct
|
|
|
|
* @throws egw_exception_wrong_parameter
|
|
|
|
*/
|
|
|
|
public function __construct($xml)
|
|
|
|
{
|
|
|
|
parent::__construct($xml);
|
|
|
|
|
|
|
|
// normalize types
|
|
|
|
if ($this->type !== 'textbox')
|
|
|
|
{
|
|
|
|
if ($this->type == 'int') $this->type = 'integer';
|
|
|
|
|
|
|
|
$this->attrs['type'] = $this->type;
|
|
|
|
$this->type = 'textbox';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 15:42:23 +01:00
|
|
|
/**
|
|
|
|
* Parse and set extra attributes from xml in template object
|
|
|
|
*
|
|
|
|
* Reimplemented to handle legacy read-only by setting size < 0
|
|
|
|
*
|
|
|
|
* @param string|XMLReader $xml
|
|
|
|
* @return etemplate_widget_textbox current object or clone, if any attribute was set
|
|
|
|
*/
|
|
|
|
public function set_attrs($xml)
|
|
|
|
{
|
|
|
|
parent::set_attrs($xml);
|
|
|
|
|
|
|
|
// Legacy handling only
|
|
|
|
// A negative size triggered the HTML readonly attibute, but not etemplate readonly,
|
|
|
|
// so you got an input element, but it was not editable.
|
|
|
|
if ($this->attrs['size'] < 0)
|
|
|
|
{
|
|
|
|
$this->setElementAttribute($this->id, 'size', abs($this->attrs['size']));
|
|
|
|
self::$request->readonlys[$this->id] = false;
|
|
|
|
$this->setElementAttribute($this->id, 'readonly', true);
|
|
|
|
trigger_error("Using a negative size to set textbox readonly. " .$this, E_USER_DEPRECATED);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2011-08-18 23:56:37 +02:00
|
|
|
/**
|
|
|
|
* Validate input
|
|
|
|
*
|
|
|
|
* Following attributes get checked:
|
|
|
|
* - needed: value must NOT be empty
|
|
|
|
* - min, max: int and float widget only
|
|
|
|
* - maxlength: maximum length of string (longer strings get truncated to allowed size)
|
|
|
|
* - preg: perl regular expression incl. delimiters (set by default for int, float and colorpicker)
|
|
|
|
* - int and float get casted to their type
|
|
|
|
*
|
2011-08-20 12:27:38 +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-18 23:56:37 +02:00
|
|
|
* @param array $content
|
|
|
|
* @param array &$validated=array() validated content
|
2012-05-03 16:17:47 +02:00
|
|
|
* @param array $expand=array values for keys 'c', 'row', 'c_', 'row_', 'cont'
|
2011-08-18 23:56:37 +02:00
|
|
|
*/
|
2012-05-03 16:17:47 +02:00
|
|
|
public function validate($cname, array $expand, array $content, &$validated=array())
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2012-05-03 16:17:47 +02:00
|
|
|
$form_name = self::form_name($cname, $this->id, $expand);
|
|
|
|
|
|
|
|
if (!$this->is_readonly($cname, $form_name))
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
|
|
|
if (!isset($this->attrs['preg']))
|
|
|
|
{
|
2011-08-19 10:22:19 +02:00
|
|
|
switch($this->type)
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2011-08-19 10:22:19 +02:00
|
|
|
case 'integer':
|
2011-08-18 23:56:37 +02:00
|
|
|
$this->attrs['preg'] = '/^-?[0-9]*$/';
|
|
|
|
break;
|
|
|
|
case 'float':
|
|
|
|
$this->attrs['preg'] = '/^-?[0-9]*[,.]?[0-9]*$/';
|
|
|
|
break;
|
|
|
|
case 'colorpicker':
|
|
|
|
$this->attrs['preg'] = '/^(#[0-9a-f]{6}|)$/i';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-20 12:27:38 +02:00
|
|
|
$value = $value_in = self::get_array($content, $form_name);
|
2011-08-18 23:56:37 +02:00
|
|
|
$valid =& self::get_array($validated, $form_name, true);
|
|
|
|
|
|
|
|
if ((string)$value === '' && $this->attrs['needed'])
|
|
|
|
{
|
|
|
|
self::set_validation_error($form_name,lang('Field must not be empty !!!'),'');
|
|
|
|
}
|
|
|
|
if ((int) $this->attrs['maxlength'] > 0 && strlen($value) > (int) $this->attrs['maxlength'])
|
|
|
|
{
|
|
|
|
$value = substr($value,0,(int) $this->attrs['maxlength']);
|
|
|
|
}
|
|
|
|
if ($this->attrs['preg'] && !preg_match($this->attrs['preg'],$value))
|
|
|
|
{
|
2011-08-19 10:22:19 +02:00
|
|
|
switch($this->type)
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2011-08-19 10:22:19 +02:00
|
|
|
case 'integer':
|
2011-08-18 23:56:37 +02:00
|
|
|
self::set_validation_error($form_name,lang("'%1' is not a valid integer !!!",$value),'');
|
|
|
|
break;
|
|
|
|
case 'float':
|
|
|
|
self::set_validation_error($form_name,lang("'%1' is not a valid floatingpoint number !!!",$value),'');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
self::set_validation_error($form_name,lang("'%1' has an invalid format !!!",$value)/*." !preg_match('$this->attrs[preg]', '$value')"*/,'');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-08-19 10:22:19 +02:00
|
|
|
elseif ($this->type == 'integer' || $this->type == 'float') // cast int and float and check range
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
|
|
|
if ((string)$value !== '' || $this->attrs['needed']) // empty values are Ok if needed is not set
|
|
|
|
{
|
2011-08-19 10:22:19 +02:00
|
|
|
$value = $this->type == 'integer' ? (int) $value : (float) str_replace(',','.',$value); // allow for german (and maybe other) format
|
2011-08-18 23:56:37 +02:00
|
|
|
|
|
|
|
if (!empty($this->attrs['min']) && $value < $this->attrs['min'])
|
|
|
|
{
|
|
|
|
self::set_validation_error($form_name,lang("Value has to be at least '%1' !!!",$this->attrs['min']),'');
|
2011-08-19 10:22:19 +02:00
|
|
|
$value = $this->type == 'integer' ? (int) $this->attrs['min'] : (float) $this->attrs['min'];
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
|
|
|
if (!empty($this->attrs['max']) && $value > $this->attrs['max'])
|
|
|
|
{
|
|
|
|
self::set_validation_error($form_name,lang("Value has to be at maximum '%1' !!!",$this->attrs['max']),'');
|
2011-08-19 10:22:19 +02:00
|
|
|
$value = $this->type == 'integer' ? (int) $this->attrs['max'] : (float) $this->attrs['max'];
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$valid = $value;
|
2013-06-18 18:39:26 +02:00
|
|
|
//error_log(__METHOD__."() $form_name: ".array2string($value_in).' --> '.array2string($value));
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-19 20:11:30 +02:00
|
|
|
etemplate_widget::registerWidget('etemplate_widget_textbox', array('textbox','text','int','integer','float','passwd','hidden','colorpicker'));
|