automatic cast and defaults for boolean attributes

Not all attributes are implemented, as many dont play any role on server-side.
Fixes taglist-account multiple="false" returns array(<selected id>)
This commit is contained in:
Ralf Becker 2018-09-13 14:56:20 +02:00
parent 822b967fe3
commit e483ec4957
6 changed files with 96 additions and 13 deletions

View File

@ -7,8 +7,7 @@
* @subpackage etemplate
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker@outdoor-training.de>
* @copyright 2002-16 by RalfBecker@outdoor-training.de
* @version $Id$
* @copyright 2002-18 by RalfBecker@outdoor-training.de
*/
namespace EGroupware\Api\Etemplate;
@ -43,6 +42,20 @@ class Widget
*/
public $attrs = array();
/**
* Names and defaults of boolean attributes to correctly cast them from XML in set_attrs
*
* @var array
*/
public $bool_attr_default = array(
'disabled' => false,
'statustext_html' => false,
'no_lang' => false,
// strictly speeding only for input widgets, but server-side input-widgets have a validation method, but no shared parent
'readonly' => false,
'needed' => false,
);
/**
* Children
*
@ -213,6 +226,20 @@ class Widget
$this->attrs = array_merge($this->attrs,self::$request->modifications[$this->id]);
}
// cast boolean attributes to boolean and set their defaults
foreach($this->bool_attr_default as $name => $default_value)
{
if (!isset($this->attrs[$name]))
{
$this->attrs[$name] = $default_value;
}
elseif (!is_bool($this->attrs[$name]))
{
// use PHP default evaluation, with the exception of "false" --> false
$this->attrs[$name] = !(!$this->attrs[$name] || $this->attrs[$name] === 'false');
}
}
return $template;
}

View File

@ -7,8 +7,7 @@
* @subpackage etemplate
* @link http://www.egroupware.org
* @author Nathan Gray
* @copyright 2015 Nathan Gray
* @version $Id$
* @copyright 2015-18 Nathan Gray
*/
namespace EGroupware\Api\Etemplate\Widget;
@ -20,6 +19,21 @@ use EGroupware\Api\Etemplate;
*/
class AjaxSelect extends Select
{
/**
* Constructor
*
* @param string|XMLReader $xml string with xml or XMLReader positioned on the element to construct
* @throws Api\Exception\WrongParameter
*/
public function __construct($xml = '')
{
$this->bool_attr_default += array(
'link' => true,
);
parent::__construct($xml);
}
/**
* Fill type options in self::$request->sel_options to be used on the client
*

View File

@ -7,8 +7,7 @@
* @subpackage etemplate
* @link http://www.egroupware.org
* @author Nathan Gray
* @copyright 2011 Nathan Gray
* @version $Id$
* @copyright 2011-18 Nathan Gray
*/
namespace EGroupware\Api\Etemplate\Widget;
@ -42,6 +41,10 @@ class File extends Etemplate\Widget
*/
public function __construct($xml='')
{
$this->bool_attr_default += array(
'multiple' => false,
);
if($xml) parent::__construct($xml);
// Legacy multiple - id ends in []

View File

@ -7,8 +7,7 @@
* @subpackage etemplate
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker@outdoor-training.de>
* @copyright 2002-16 by RalfBecker@outdoor-training.de
* @version $Id$
* @copyright 2002-18 by RalfBecker@outdoor-training.de
*/
namespace EGroupware\Api\Etemplate\Widget;
@ -83,6 +82,14 @@ class Select extends Etemplate\Widget
*/
public function __construct($xml = '')
{
$this->bool_attr_default += array(
//'multiple' => false, // handeled in set_attrs, as we additional allow "dynamic"
'selected_first' => true,
'search' => false,
'tags' => false,
'allow_single_deselect' => true,
);
if($xml) {
parent::__construct($xml);
}
@ -102,6 +109,12 @@ class Select extends Etemplate\Widget
{
parent::set_attrs($xml, $cloned);
if ($this->attrs['multiple'] !== 'dynamic')
{
$this->attrs['multiple'] = !isset($this->attrs['multiple']) ? false :
!(!$this->attrs['multiple'] || $this->attrs['multiple'] === 'false');
}
// set attrs[multiple] from attrs[options], unset options only if it just contains number or rows
if ($this->attrs['options'] > 1)
{

View File

@ -7,8 +7,7 @@
* @subpackage etemplate
* @link http://www.egroupware.org
* @author Nathan Gray
* @copyright 2013 Nathan Gray
* @version $Id$
* @copyright 2013-18 Nathan Gray
*/
namespace EGroupware\Api\Etemplate\Widget;
@ -42,7 +41,17 @@ class Taglist extends Etemplate\Widget
*/
public function __construct($xml = '')
{
$this->attrs['allowFreeEntries'] = true;
$this->bool_attr_default = array_merge($this->bool_attr_default, array(
'allowFreeEntries' => true,
'useCommaKey' => true,
'editModeEnabled' => true,
// inherited on js-side from Selextbox
'multiple' => true,
'selected_first' => true,
'search' => false,
'tags' => false,
'allow_single_deselect' => true,
));
if($xml) {
parent::__construct($xml);

View File

@ -7,8 +7,7 @@
* @subpackage etemplate
* @link http://www.egroupware.org
* @author Nathan Gray
* @copyright 2012 Nathan Gray
* @version $Id$
* @copyright 2012-18 Nathan Gray
*/
namespace EGroupware\Api\Etemplate\Widget;
@ -111,6 +110,24 @@ class Tree extends Etemplate\Widget
*/
const NOCHECKBOX = 'nocheckbox';
/**
* Constructor
*
* @param string|XMLReader $xml string with xml or XMLReader positioned on the element to construct
* @throws Api\Exception\WrongParameter
*/
public function __construct($xml = '')
{
$this->bool_attr_default += array(
'multiple' => false,
'highlighting' => true,
);
if($xml) {
parent::__construct($xml);
}
}
/**
* Parse and set extra attributes from xml in template object
*