2011-08-18 20:08:40 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EGroupware - eTemplate widget baseclass
|
|
|
|
*
|
|
|
|
* @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>
|
2014-03-28 18:32:32 +01:00
|
|
|
* @copyright 2002-14 by RalfBecker@outdoor-training.de
|
2011-08-18 20:08:40 +02:00
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* eTemplate widget baseclass
|
2011-08-18 23:56:37 +02:00
|
|
|
*
|
|
|
|
* @todo text content, eg. the styles of a template are not parsed, thought they are not used here either
|
2011-08-18 20:08:40 +02:00
|
|
|
*/
|
|
|
|
class etemplate_widget
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Widget type
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Widget id
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public $id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Widget attributes
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $attrs = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Children
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $children = array();
|
|
|
|
|
2011-08-19 10:22:19 +02:00
|
|
|
/**
|
|
|
|
* (Array of) comma-separated list of legacy options to automatically replace when parsing with set_attrs
|
|
|
|
*
|
|
|
|
* @var string|array
|
|
|
|
*/
|
|
|
|
protected $legacy_options;
|
|
|
|
|
2011-08-18 23:56:37 +02:00
|
|
|
/**
|
|
|
|
* Request object of the currently created request
|
|
|
|
*
|
|
|
|
* It's a static variable as etemplates can contain further etemplates (rendered by a different object)
|
|
|
|
*
|
|
|
|
* @var etemplate_request
|
|
|
|
*/
|
|
|
|
static protected $request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JSON response object, if we run via a JSON request
|
|
|
|
*
|
|
|
|
* @var egw_json_response
|
|
|
|
*/
|
|
|
|
static protected $response;
|
|
|
|
|
2013-06-18 18:38:27 +02:00
|
|
|
/**
|
|
|
|
* Namespaced content array, used when trying to initialize
|
|
|
|
*
|
|
|
|
* This is pretty much a global static variable, used when reading
|
|
|
|
* a template with the content set. This allows variable expansion
|
|
|
|
* in the constructor.
|
|
|
|
*
|
|
|
|
* @protected $cont
|
|
|
|
*/
|
|
|
|
static protected $cont = null;
|
|
|
|
|
2011-08-18 20:08:40 +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)
|
2011-08-19 08:30:06 +02:00
|
|
|
{
|
|
|
|
$reader = self::get_reader($xml);
|
|
|
|
$this->type = $reader->name;
|
|
|
|
$depth = $reader->depth;
|
|
|
|
|
|
|
|
$this->id = $reader->getAttribute('id');
|
|
|
|
|
2013-06-18 18:38:27 +02:00
|
|
|
// Update content?
|
|
|
|
if(self::$cont == null)
|
|
|
|
self::$cont = is_array(self::$request->content) ? self::$request->content : array();
|
|
|
|
if($this->id && is_array(self::$cont[$this->id]))
|
|
|
|
{
|
|
|
|
$old_cont = self::$cont;
|
|
|
|
self::$cont = self::$cont[$this->id];
|
|
|
|
}
|
|
|
|
|
2011-08-19 08:30:06 +02:00
|
|
|
// read all attributes
|
|
|
|
$this->set_attrs($reader);
|
|
|
|
|
|
|
|
while($reader->read() && $reader->depth > $depth)
|
|
|
|
{
|
|
|
|
if ($reader->nodeType == XMLReader::ELEMENT && $reader->depth > $depth)
|
|
|
|
{
|
|
|
|
$this->children[] = self::factory($reader->name, $reader, $reader->getAttribute('id'));
|
|
|
|
}
|
|
|
|
}
|
2013-06-18 18:38:27 +02:00
|
|
|
|
|
|
|
// Reset content as we leave
|
|
|
|
if($old_cont) {
|
|
|
|
self::$cont = $old_cont;
|
|
|
|
}
|
2011-08-19 08:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get XMLReader for given xml string
|
|
|
|
*
|
|
|
|
* @param string|XMLReader $xml string with xml or XMLReader positioned on an element
|
|
|
|
* @throws egw_exception_wrong_parameter
|
|
|
|
*/
|
|
|
|
protected static function get_reader($xml)
|
2011-08-18 20:08:40 +02:00
|
|
|
{
|
|
|
|
if (is_a($xml, 'XMLReader'))
|
|
|
|
{
|
|
|
|
$reader = $xml;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$reader = new XMLReader();
|
|
|
|
if (!$reader->XML($xml))
|
|
|
|
{
|
|
|
|
throw new egw_exception_wrong_parameter("Can't parse xml:\n$xml");
|
|
|
|
}
|
|
|
|
}
|
2011-08-19 08:30:06 +02:00
|
|
|
return $reader;
|
|
|
|
}
|
2011-08-18 20:08:40 +02:00
|
|
|
|
2011-08-19 08:30:06 +02:00
|
|
|
/**
|
|
|
|
* Parse and set extra attributes from xml in template object
|
|
|
|
*
|
|
|
|
* Returns a cloned template object, if any attribute needs to be set.
|
|
|
|
* This is necessary as templates can be used multiple time, so we can not alter the cached template!
|
|
|
|
*
|
|
|
|
* @param string|XMLReader $xml
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param boolean $cloned =true true: object does NOT need to be cloned, false: to set attribute, set them in cloned object
|
2011-08-19 08:30:06 +02:00
|
|
|
* @return etemplate_widget_template current object or clone, if any attribute was set
|
|
|
|
*/
|
|
|
|
public function set_attrs($xml, $cloned=true)
|
|
|
|
{
|
|
|
|
$reader = self::get_reader($xml);
|
2011-08-18 20:08:40 +02:00
|
|
|
|
2011-08-19 10:22:19 +02:00
|
|
|
// check if we have to split legacy options (can be by type)
|
|
|
|
$legacy_options = $this->legacy_options;
|
|
|
|
if (is_array($legacy_options))
|
|
|
|
{
|
|
|
|
if (!($type = $reader->getAttribute('type')))
|
|
|
|
{
|
|
|
|
$type = $this->type;
|
|
|
|
}
|
|
|
|
$legacy_options = $legacy_options[$type];
|
|
|
|
}
|
|
|
|
|
2011-08-19 08:30:06 +02:00
|
|
|
// read and set all attributes
|
|
|
|
$template = $this;
|
|
|
|
while($reader->moveToNextAttribute())
|
2011-08-18 20:08:40 +02:00
|
|
|
{
|
2011-08-19 08:30:06 +02:00
|
|
|
if ($reader->name != 'id' && $template->attr[$reader->name] != $reader->value)
|
2011-08-18 20:08:40 +02:00
|
|
|
{
|
2012-05-03 16:17:47 +02:00
|
|
|
if (!$cloned)
|
|
|
|
{
|
|
|
|
$template = clone($this);
|
|
|
|
$cloned = true; // only clone it once, otherwise we loose attributes!
|
|
|
|
}
|
2014-05-22 11:39:16 +02:00
|
|
|
// $reader->value is an object and therefore assigned by reference
|
|
|
|
// this is important to not loose content when validating dynamic generated tabs as in settings!
|
|
|
|
$template->attrs[$reader->name] = $value = $reader->value;
|
2014-05-21 11:55:02 +02:00
|
|
|
|
|
|
|
// expand attributes values, otherwise eg. validation can not use attrs referencing to content
|
2014-05-22 14:15:11 +02:00
|
|
|
if ($value[0] == '@' || strpos($value, '$cont') !== false)
|
2014-05-21 11:55:02 +02:00
|
|
|
{
|
|
|
|
$value = self::expand_name($value, null, null, null, null,
|
|
|
|
isset(self::$cont) ? self::$cont : self::$request->content);
|
|
|
|
}
|
2011-08-19 10:22:19 +02:00
|
|
|
|
|
|
|
// split legacy options
|
|
|
|
if ($legacy_options && $reader->name == 'options')
|
|
|
|
{
|
|
|
|
$legacy_options = explode(',', $legacy_options);
|
2014-05-21 11:55:02 +02:00
|
|
|
foreach(self::csv_split($value, count($legacy_options)) as $n => $val)
|
2011-08-19 10:22:19 +02:00
|
|
|
{
|
|
|
|
if ($legacy_options[$n] && (string)$val !== '') $template->attrs[$legacy_options[$n]] = $val;
|
|
|
|
}
|
|
|
|
}
|
2011-08-18 20:08:40 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-24 01:48:11 +02:00
|
|
|
|
|
|
|
// Add in anything in the modification array
|
|
|
|
if(is_array(self::$request->modifications[$this->id]))
|
|
|
|
{
|
|
|
|
$this->attrs = array_merge($this->attrs,self::$request->modifications[$this->id]);
|
|
|
|
}
|
2013-02-08 09:54:20 +01:00
|
|
|
|
2011-08-19 08:30:06 +02:00
|
|
|
return $template;
|
2011-08-18 20:08:40 +02:00
|
|
|
}
|
|
|
|
|
2011-08-19 10:22:19 +02:00
|
|
|
/**
|
|
|
|
* Split a $delimiter-separated options string, which can contain parts with delimiters enclosed in $enclosure
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
* - csv_split('"1,2,3",2,3') === array('1,2,3','2','3')
|
|
|
|
* - csv_split('1,2,3',2) === array('1','2,3')
|
|
|
|
* - csv_split('"1,2,3",2,3',2) === array('1,2,3','2,3')
|
|
|
|
* - csv_split('"a""b,c",d') === array('a"b,c','d') // to escape enclosures double them!
|
|
|
|
*
|
|
|
|
* @param string $str
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param int $num =null in how many parts to split maximal, parts over this number end up (unseparated) in the last part
|
|
|
|
* @param string $delimiter =','
|
|
|
|
* @param string $enclosure ='"'
|
2011-08-19 10:22:19 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function csv_split($str,$num=null,$delimiter=',',$enclosure='"')
|
|
|
|
{
|
|
|
|
if (strpos($str,$enclosure) === false)
|
|
|
|
{
|
|
|
|
return is_null($num) ? explode($delimiter,$str) : explode($delimiter,$str,$num); // no need to run this more expensive code
|
|
|
|
}
|
|
|
|
$parts = explode($delimiter,$str);
|
|
|
|
for($n = 0; isset($parts[$n]); ++$n)
|
|
|
|
{
|
|
|
|
$part =& $parts[$n];
|
|
|
|
if ($part[0] === $enclosure)
|
|
|
|
{
|
|
|
|
while (isset($parts[$n+1]) && substr($part,-1) !== $enclosure)
|
|
|
|
{
|
|
|
|
$part .= $delimiter.$parts[++$n];
|
|
|
|
unset($parts[$n]);
|
|
|
|
}
|
|
|
|
$part = substr(str_replace($enclosure.$enclosure,$enclosure,$part),1,-1);
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 09:53:17 +01:00
|
|
|
$parts_renum = array_values($parts); // renumber the parts (in case we had to concat them)
|
2011-08-19 10:22:19 +02:00
|
|
|
|
2014-11-19 09:53:17 +01:00
|
|
|
if ($num > 0 && count($parts_renum) > $num)
|
2011-08-19 10:22:19 +02:00
|
|
|
{
|
2014-11-19 09:53:17 +01:00
|
|
|
$parts_renum[$num-1] = implode($delimiter,array_slice($parts_renum,$num-1,count($parts_renum)-$num+1));
|
|
|
|
$parts_renum = array_slice($parts_renum,0,$num);
|
2011-08-19 10:22:19 +02:00
|
|
|
}
|
2014-11-19 09:53:17 +01:00
|
|
|
return $parts_renum;
|
2011-08-19 10:22:19 +02:00
|
|
|
}
|
|
|
|
|
2011-08-18 23:56:37 +02:00
|
|
|
/**
|
|
|
|
* Registry of classes implementing widgets
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
static protected $widget_registry = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a given class for certain widgets
|
|
|
|
*
|
|
|
|
* Registration is only needed if widget (base-)name is not autoloadable,
|
|
|
|
* eg. class etemplate_widget_template does NOT need to be registered.
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param string|array $widgets
|
|
|
|
*/
|
|
|
|
public static function registerWidget($class, $widgets)
|
|
|
|
{
|
2011-08-25 21:52:51 +02:00
|
|
|
if (!is_subclass_of($class, __CLASS__))
|
|
|
|
{
|
2013-11-04 18:23:58 +01:00
|
|
|
throw new egw_exception_wrong_parameter(__METHOD__."('$class', ".array2string($widgets).") $class is no subclass of ".__CLASS__.'!');
|
2011-08-25 21:52:51 +02:00
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
foreach((array)$widgets as $widget)
|
|
|
|
{
|
|
|
|
self::$widget_registry[$widget] = $class;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-18 20:08:40 +02:00
|
|
|
/**
|
|
|
|
* Factory method to construct all widgets
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @param string|XMLReader $xml
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param string $id =null
|
2011-08-18 20:08:40 +02:00
|
|
|
*/
|
|
|
|
public static function factory($type, $xml, $id=null)
|
|
|
|
{
|
2011-08-18 23:56:37 +02:00
|
|
|
$class_name =& self::$widget_registry[$type];
|
2011-08-18 20:08:40 +02:00
|
|
|
|
|
|
|
if (!isset($class_name))
|
|
|
|
{
|
|
|
|
list($basetype) = explode('-',$type);
|
2013-06-19 20:11:30 +02:00
|
|
|
if (!class_exists($class_name = 'etemplate_widget_'.str_replace('-','_',$type)) &&
|
2014-05-05 18:05:39 +02:00
|
|
|
!class_exists($class_name = 'etemplate_widget_'.$basetype) &&
|
|
|
|
// widgets supplied by application in class ${app}_widget_etemplate or ${app}_${subtype}_widget_etemplate
|
|
|
|
!(isset($GLOBALS['egw_info']['apps'][$basetype]) &&
|
|
|
|
(class_exists($class_name = str_replace('-','_',$type).'_etemplate_widget') ||
|
|
|
|
class_exists($class_name = $basetype.'_etemplate_widget'))))
|
2011-08-18 20:08:40 +02:00
|
|
|
{
|
2013-06-19 20:11:30 +02:00
|
|
|
// Try for base type, it's probably better than the root
|
|
|
|
if(self::$widget_registry[$basetype] && self::$widget_registry[$basetype] != $class_name)
|
|
|
|
{
|
|
|
|
$class_name = self::$widget_registry[$basetype];
|
|
|
|
}
|
2014-09-25 20:30:32 +02:00
|
|
|
// Look for old widgets that were adapted but not renamed
|
|
|
|
else if (class_exists($class_name = $basetype.'_widget') && in_array('etemplate_widget', class_parents($class_name)))
|
|
|
|
{
|
|
|
|
// Side-effects set $class_name
|
|
|
|
//error_log("Ported old widget: $class_name");
|
|
|
|
}
|
2013-06-19 20:11:30 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Fall back to widget class, we can not ignore it, as the widget may contain other widgets
|
|
|
|
$class_name = 'etemplate_widget';
|
|
|
|
}
|
2011-08-18 20:08:40 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-11 20:59:34 +02:00
|
|
|
|
|
|
|
if(!$xml)
|
|
|
|
{
|
2014-01-23 13:43:29 +01:00
|
|
|
if (empty($type)) $type = 'widget';
|
2013-06-11 20:59:34 +02:00
|
|
|
$xml = "<$type id='$id'/>";
|
|
|
|
}
|
2011-08-20 12:27:38 +02:00
|
|
|
//error_log(__METHOD__."('$type', ..., '$id') using $class_name");
|
|
|
|
|
2011-08-18 20:08:40 +02:00
|
|
|
// currently only overlays can contain templates, other widgets can only reference to templates via id
|
2011-08-18 23:56:37 +02:00
|
|
|
if ($type == 'template' && $id && ($template = etemplate_widget_template::instance($id)))
|
2011-08-18 20:08:40 +02:00
|
|
|
{
|
2011-08-19 08:30:06 +02:00
|
|
|
// references can set different attributes like: class, span, content (namespace)
|
|
|
|
return $template->set_attrs($xml, false); // false = need to clone template, if attributs are set!
|
2011-08-18 20:08:40 +02:00
|
|
|
}
|
|
|
|
return new $class_name($xml);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate over children to find the one with the given id and optional type
|
|
|
|
*
|
|
|
|
* @param string $id
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param string $type =null
|
2011-08-18 20:08:40 +02:00
|
|
|
* @return etemplate_widget or NULL
|
|
|
|
*/
|
|
|
|
public function getElementById($id, $type=null)
|
|
|
|
{
|
|
|
|
foreach($this->children as $child)
|
|
|
|
{
|
|
|
|
if ($child->id === $id && (is_null($type) || $child->type === $type))
|
|
|
|
{
|
|
|
|
return $child;
|
|
|
|
}
|
|
|
|
if (($element = $child->getElementById($id, $type)))
|
|
|
|
{
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-03-30 00:55:44 +02:00
|
|
|
/**
|
2012-06-08 18:28:20 +02:00
|
|
|
* Iterate over children to find the one with the given type
|
2012-03-30 00:55:44 +02:00
|
|
|
*
|
|
|
|
* @param string $type
|
2014-11-19 10:22:52 +01:00
|
|
|
* @return array of etemplate_widget or empty array
|
2012-03-30 00:55:44 +02:00
|
|
|
*/
|
|
|
|
public function getElementsByType($type)
|
|
|
|
{
|
|
|
|
$elements = array();
|
|
|
|
foreach($this->children as $child)
|
|
|
|
{
|
|
|
|
if ($child->type === $type)
|
|
|
|
{
|
|
|
|
$elements[] = $child;
|
|
|
|
}
|
2014-11-19 09:53:17 +01:00
|
|
|
$elements += $child->getElementsByType($type);
|
2012-03-30 00:55:44 +02:00
|
|
|
}
|
|
|
|
return $elements;
|
|
|
|
}
|
|
|
|
|
2011-08-18 20:08:40 +02:00
|
|
|
/**
|
2011-08-20 12:27:38 +02:00
|
|
|
* Run a given method on all children
|
2011-08-18 23:56:37 +02:00
|
|
|
*
|
2011-08-20 12:27:38 +02:00
|
|
|
* Default implementation only calls method on itself and run on all children
|
2011-08-18 20:08:40 +02:00
|
|
|
*
|
2011-08-20 12:27:38 +02:00
|
|
|
* @param string $method_name
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param array $params =array('') parameter(s) first parameter has to be the cname, second $expand!
|
|
|
|
* @param boolean $respect_disabled =false false (default): ignore disabled, true: method is NOT run for disabled widgets AND their children
|
2011-08-18 20:08:40 +02:00
|
|
|
*/
|
2011-08-21 13:55:56 +02:00
|
|
|
public function run($method_name, $params=array(''), $respect_disabled=false)
|
2011-08-18 20:08:40 +02:00
|
|
|
{
|
2012-05-03 16:17:47 +02:00
|
|
|
// maintain $expand array name-expansion
|
|
|
|
$cname = $params[0];
|
|
|
|
$expand =& $params[1];
|
2013-02-04 13:35:30 +01:00
|
|
|
if ($expand['cname'] && $expand['cname'] !== $cname)
|
2012-05-03 16:17:47 +02:00
|
|
|
{
|
|
|
|
$expand['cont'] =& self::get_array(self::$request->content, $cname);
|
|
|
|
$expand['cname'] = $cname;
|
|
|
|
}
|
2013-08-23 15:00:13 +02:00
|
|
|
if ($respect_disabled && ($disabled = $this->attrs['disabled'] && self::check_disabled($this->attrs['disabled'], $expand)))
|
2011-08-21 13:55:56 +02:00
|
|
|
{
|
2014-07-24 11:38:55 +02:00
|
|
|
//error_log(__METHOD__."('$method_name', ".array2string($params).', '.array2string($respect_disabled).") $this disabled='{$this->attrs['disabled']}'=".array2string($disabled).": NOT running");
|
2013-08-23 15:00:13 +02:00
|
|
|
return;
|
2011-08-21 13:55:56 +02:00
|
|
|
}
|
2011-08-20 12:27:38 +02:00
|
|
|
if (method_exists($this, $method_name))
|
|
|
|
{
|
2013-02-04 13:35:30 +01:00
|
|
|
// Some parameter checking to avoid fatal errors
|
|
|
|
$call = true;
|
|
|
|
$method = new ReflectionMethod($this, $method_name);
|
|
|
|
foreach($method->getParameters() as $index => $param)
|
|
|
|
{
|
|
|
|
if(!$param->isOptional() && !array_key_exists($index,$params))
|
|
|
|
{
|
|
|
|
error_log("Missing required parameter {$param->getPosition()}: {$param->getName()}");
|
|
|
|
$call = false;
|
|
|
|
}
|
|
|
|
if($param->isArray() && !is_array($params[$index]))
|
|
|
|
{
|
|
|
|
error_log("$method_name expects an array for {$param->getPosition()}: {$param->getName()}");
|
|
|
|
$params[$index] = (array)$params[$index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($call) call_user_func_array(array($this, $method_name), $params);
|
2011-08-20 12:27:38 +02:00
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
foreach($this->children as $child)
|
|
|
|
{
|
2013-06-11 20:59:34 +02:00
|
|
|
// If type has something that can be expanded, we need to expand it so the correct method is run
|
2013-07-08 20:58:07 +02:00
|
|
|
$this->expand_widget($child, $expand);
|
2011-08-21 13:55:56 +02:00
|
|
|
$child->run($method_name, $params, $respect_disabled);
|
|
|
|
}
|
|
|
|
}
|
2013-08-23 15:00:13 +02:00
|
|
|
|
2013-07-08 20:58:07 +02:00
|
|
|
/**
|
|
|
|
* If a widget's type is expandable, we need to expand it to make sure we have
|
|
|
|
* the right class before running the method on it
|
|
|
|
*
|
|
|
|
* @param etemplate_widget $child Widget to check & expand if needed
|
|
|
|
* @param Array $expand Expansion array
|
|
|
|
*/
|
|
|
|
protected function expand_widget(etemplate_widget &$child, Array &$expand)
|
|
|
|
{
|
|
|
|
if(strpos($child->attrs['type'], '@') !== false || strpos($child->attrs['type'], '$') !== false)
|
|
|
|
{
|
|
|
|
$type = self::expand_name($child->attrs['type'],$expand['c'], $expand['row'], $expand['c_'], $expand['row_'], $expand['cont']);
|
|
|
|
$id = self::expand_name($child->id,$expand['c'], $expand['row'], $expand['c_'], $expand['row_'], $expand['cont']);
|
|
|
|
$attrs = $child->attrs;
|
|
|
|
unset($attrs['type']);
|
|
|
|
$expanded_child = self::factory($type, false,$id);
|
|
|
|
$expanded_child->id = $id;
|
|
|
|
$expanded_child->type = $type;
|
|
|
|
$expanded_child->attrs = $attrs + array('type' => $type);
|
|
|
|
$child = $expanded_child;
|
|
|
|
}
|
|
|
|
}
|
2011-08-21 13:55:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a grid row or column is disabled
|
|
|
|
*
|
|
|
|
* Expression: [!][@]val[=[@]check]
|
|
|
|
* Parts in square brackets are optional, a ! negates the expression, @val evaluates to $content['val']
|
|
|
|
* if no =check is given all set non-empty and non-zero strings are true (standard php behavior)
|
|
|
|
*
|
|
|
|
* @param string $disabled expression to check, eg. "!@var" for !$content['var']
|
2012-05-03 16:17:47 +02:00
|
|
|
* @param array $expand values for keys 'c', 'row', 'c_', 'row_', 'cont'
|
2011-08-21 13:55:56 +02:00
|
|
|
* @return boolean true if the row/col is disabled or false if not
|
|
|
|
*/
|
2012-06-08 18:57:08 +02:00
|
|
|
protected static function check_disabled($disabled, array $expand)
|
2011-08-21 13:55:56 +02:00
|
|
|
{
|
2014-11-19 09:53:17 +01:00
|
|
|
if (($not = $disabled[0] == '!'))
|
2011-08-21 13:55:56 +02:00
|
|
|
{
|
|
|
|
$disabled = substr($disabled,1);
|
|
|
|
}
|
2014-11-19 09:53:17 +01:00
|
|
|
list($value,$check) = $vals = explode('=',$disabled);
|
2011-08-21 13:55:56 +02:00
|
|
|
|
|
|
|
// use expand_name to be able to use @ or $
|
2014-11-19 09:53:17 +01:00
|
|
|
$val = self::expand_name($value, $expand['c'], $expand['row'], $expand['c_'], $expand['row_'], $expand['cont']);
|
|
|
|
$check_val = self::expand_name($check, $expand['c'], $expand['row'], $expand['c_'], $expand['row_'], $expand['cont']);
|
2011-08-21 13:55:56 +02:00
|
|
|
$result = count($vals) == 1 ? $val != '' : ($check_val[0] == '/' ? preg_match($check_val,$val) : $val == $check_val);
|
|
|
|
if ($not) $result = !$result;
|
|
|
|
|
|
|
|
//error_log(__METHOD__."('".($not?'!':'')."$disabled' = '$val' ".(count($vals) == 1 ? '' : ($not?'!':'=')."= '$check_val'")." = ".($result?'True':'False'));
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regular expression matching a PHP variable in a string, eg.
|
|
|
|
*
|
|
|
|
* "replies[$row][reply_message]" should only match $row
|
|
|
|
* "delete[$row_cont[path]]" should match $row_cont[path]
|
|
|
|
*/
|
|
|
|
const PHP_VAR_PREG = '\$[A-Za-z0-9_]+(\[[A-Za-z0-9_]+\])*';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* allows a few variables (eg. row-number) to be used in field-names
|
|
|
|
*
|
|
|
|
* This is mainly used for autorepeat, but other use is possible.
|
|
|
|
* You need to be aware of the rules PHP uses to expand vars in strings, a name
|
|
|
|
* of "Row$row[length]" will expand to 'Row' as $row is scalar, you need to use
|
|
|
|
* "Row${row}[length]" instead. Only one indirection is allowd in a string by php !!!
|
|
|
|
* Out of that reason we have now the variable $row_cont, which is $cont[$row] too.
|
|
|
|
* Attention !!!
|
|
|
|
* Using only number as index in field-names causes a lot trouble, as depending
|
|
|
|
* on the variable type (which php determines itself) you used filling and later
|
|
|
|
* accessing the array it can by the index or the key of an array element.
|
|
|
|
* To make it short and clear, use "Row$row" or "$col$row" not "$row" or "$row$col" !!!
|
|
|
|
*
|
|
|
|
* @param string $name the name to expand
|
|
|
|
* @param int $c is the column index starting with 0 (if you have row-headers, data-cells start at 1)
|
|
|
|
* @param int $row is the row number starting with 0 (if you have col-headers, data-cells start at 1)
|
|
|
|
* @param int $c_ is the value of the previous template-inclusion,
|
|
|
|
* eg. the column-headers in the eTemplate-editor are templates itself,
|
|
|
|
* to show the column-name in the header you can not use $col as it will
|
|
|
|
* be constant as it is always the same col in the header-template,
|
|
|
|
* what you want is the value of the previous template-inclusion.
|
|
|
|
* @param int $row_ is the value of the previous template-inclusion,
|
|
|
|
* @param array $cont content of the template, you might use it to generate button-names with id values in it:
|
|
|
|
* "del[$cont[id]]" expands to "del[123]" if $cont = array('id' => 123)
|
|
|
|
* @return string the expanded name
|
|
|
|
*/
|
|
|
|
protected static function expand_name($name,$c,$row,$c_='',$row_='',$cont='')
|
|
|
|
{
|
|
|
|
$is_index_in_content = $name[0] == '@';
|
|
|
|
if (($pos_var=strpos($name,'$')) !== false)
|
|
|
|
{
|
|
|
|
if (!$cont)
|
|
|
|
{
|
|
|
|
$cont = array();
|
|
|
|
}
|
|
|
|
if (!is_numeric($c)) $c = boetemplate::chrs2num($c);
|
2012-05-03 16:17:47 +02:00
|
|
|
$col = self::num2chrs($c-1); // $c-1 to get: 0:'@', 1:'A', ...
|
|
|
|
$col_ = self::num2chrs($c_-1);
|
2011-08-21 13:55:56 +02:00
|
|
|
$row_cont = $cont[$row];
|
|
|
|
$col_row_cont = $cont[$col.$row];
|
|
|
|
|
|
|
|
eval('$name = "'.str_replace('"','\\"',$name).'";');
|
2014-11-19 10:22:52 +01:00
|
|
|
unset($col_, $row_, $row_cont, $col_row_cont); // quiten IDE warning about used vars, they might be used in above eval!
|
2011-08-21 13:55:56 +02:00
|
|
|
}
|
|
|
|
if ($is_index_in_content)
|
|
|
|
{
|
2012-05-09 01:17:43 +02:00
|
|
|
if ($name[1] == '@' && is_array(self::$request->content))
|
2011-08-21 13:55:56 +02:00
|
|
|
{
|
|
|
|
$name = self::get_array(self::$request->content,substr($name,2));
|
|
|
|
}
|
2012-05-09 01:17:43 +02:00
|
|
|
elseif(is_array($cont))
|
2011-08-21 13:55:56 +02:00
|
|
|
{
|
|
|
|
$name = self::get_array($cont,substr($name,1));
|
|
|
|
}
|
2012-07-10 20:06:44 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Content not set expands to ''
|
|
|
|
$name = '';
|
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
2011-08-21 13:55:56 +02:00
|
|
|
return $name;
|
2011-08-18 20:08:40 +02:00
|
|
|
}
|
|
|
|
|
2012-05-03 16:17:47 +02:00
|
|
|
/**
|
|
|
|
* generates column-names from index: 'A', 'B', ..., 'AA', 'AB', ..., 'ZZ' (not more!)
|
|
|
|
*
|
|
|
|
* @param int $num numerical index to generate name from 1 => 'A'
|
|
|
|
* @return string the name
|
|
|
|
*/
|
|
|
|
static function num2chrs($num)
|
|
|
|
{
|
|
|
|
$min = ord('A');
|
|
|
|
$max = ord('Z') - $min + 1;
|
|
|
|
if ($num >= $max)
|
|
|
|
{
|
|
|
|
$chrs = chr(($num / $max) + $min - 1);
|
|
|
|
}
|
|
|
|
$chrs .= chr(($num % $max) + $min);
|
|
|
|
|
|
|
|
return $chrs;
|
|
|
|
}
|
|
|
|
|
2011-08-18 20:08:40 +02:00
|
|
|
/**
|
|
|
|
* Convert object to string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function __toString()
|
|
|
|
{
|
2013-02-04 13:35:30 +01:00
|
|
|
return '['.get_class($this).'] ' .
|
|
|
|
$this->type.($this->attrs['type'] && $this->attrs['type'] != $this->type ? '('.$this->attrs['type'].')' : '').'#'.$this->id;
|
2011-08-18 20:08:40 +02:00
|
|
|
}
|
2013-08-23 15:00:13 +02:00
|
|
|
|
2013-07-17 11:38:37 +02:00
|
|
|
/**
|
|
|
|
* When cloning a widget, we also clone children
|
|
|
|
*/
|
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
foreach($this->children as $child_num => $child) {
|
|
|
|
$this->children[$child_num] = clone $child;
|
|
|
|
}
|
|
|
|
}
|
2011-08-18 20:08:40 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert widget (incl. children) to xml
|
|
|
|
*
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param string $indent =''
|
2011-08-18 20:08:40 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function toXml($indent='')
|
|
|
|
{
|
|
|
|
echo "$indent<$this->type";
|
|
|
|
if ($this->id) echo ' id="'.htmlspecialchars($this->id).'"';
|
|
|
|
foreach($this->attrs as $name => $value)
|
|
|
|
{
|
2011-08-19 10:22:19 +02:00
|
|
|
if ($name == 'options' && $this->legacy_options && (!is_array($this->legacy_options) ||
|
|
|
|
isset($this->legacy_options[$this->attrs['type'] ? $this->attrs['type'] : $this->type])))
|
|
|
|
{
|
|
|
|
continue; // do NOT output already converted legacy options
|
|
|
|
}
|
2011-08-18 20:08:40 +02:00
|
|
|
echo ' '.$name.'="'.htmlspecialchars($value).'"';
|
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
echo ' php-class="'.get_class($this).'"';
|
|
|
|
|
2011-08-18 20:08:40 +02:00
|
|
|
if ($this->children)
|
|
|
|
{
|
|
|
|
echo ">\n";
|
|
|
|
foreach($this->children as $child)
|
|
|
|
{
|
|
|
|
$child->toXml($indent."\t");
|
|
|
|
}
|
|
|
|
echo "$indent</$this->type>\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
echo " />\n";
|
|
|
|
}
|
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* build the name of a form-element from a basename and name
|
|
|
|
*
|
|
|
|
* name and basename can contain sub-indices in square bracets, eg. basename="base[basesub1][basesub2]"
|
|
|
|
* and name = "name[sub]" gives "base[basesub1][basesub2][name][sub]"
|
|
|
|
*
|
|
|
|
* @param string $cname basename
|
|
|
|
* @param string $name name
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param array $expand =null values for keys 'c', 'row', 'c_', 'row_', 'cont'
|
2011-08-18 23:56:37 +02:00
|
|
|
* @return string complete form-name
|
|
|
|
*/
|
2012-05-03 16:17:47 +02:00
|
|
|
static function form_name($cname,$name,array $expand=null)
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2012-05-03 16:17:47 +02:00
|
|
|
if ($expand && !empty($name))
|
|
|
|
{
|
|
|
|
$name = self::expand_name($name, $expand['c'], $expand['row'], $expand['c_'], $expand['row_'], $expand['cont']);
|
|
|
|
}
|
2011-11-14 23:57:06 +01:00
|
|
|
if (count($name_parts = explode('[', $name, 2)) > 1)
|
|
|
|
{
|
|
|
|
$name_parts = array_merge(array($name_parts[0]), explode('][', substr($name_parts[1],0,-1)));
|
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
if (!empty($cname))
|
|
|
|
{
|
|
|
|
array_unshift($name_parts,$cname);
|
|
|
|
}
|
|
|
|
$form_name = array_shift($name_parts);
|
|
|
|
if (count($name_parts))
|
|
|
|
{
|
2012-05-03 16:17:47 +02:00
|
|
|
// RB: not sure why this business with entity encoding for square brakets, it messes up validation
|
|
|
|
//$form_name .= '['.implode('][',$name_parts).']';
|
|
|
|
$form_name .= '['.implode('][',$name_parts).']';
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
|
|
|
return $form_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return a reference to $arr[$idx]
|
|
|
|
*
|
|
|
|
* This works for non-trival indexes like 'a[b][c]' too: it returns &$arr[a][b][c]
|
|
|
|
* $sub = get_array($arr,'a[b]'); $sub = 'c'; is equivalent to $arr['a']['b'] = 'c';
|
|
|
|
*
|
|
|
|
* @param array $arr the array to search, referenz as a referenz gets returned
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param string $_idx the index, may contain sub-indices like a[b], see example below
|
2011-08-18 23:56:37 +02:00
|
|
|
* @param boolean $reference_into default False, if True none-existing sub-arrays/-indices get created to be returned as referenz, else False is returned
|
|
|
|
* @param bool $skip_empty returns false if $idx is not present in $arr
|
2014-02-10 22:59:50 +01:00
|
|
|
* @return mixed reference to $arr[$idx] or null if $idx is not set and not $reference_into
|
2011-08-18 23:56:37 +02:00
|
|
|
*/
|
2014-11-19 09:53:17 +01:00
|
|
|
static function &get_array(&$arr,$_idx,$reference_into=False,$skip_empty=False)
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
|
|
|
if (!is_array($arr))
|
|
|
|
{
|
2014-11-19 09:53:17 +01:00
|
|
|
throw new egw_exception_assertion_failed(__METHOD__."(\$arr,'$_idx',$reference_into,$skip_empty) \$arr is no array!");
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
2014-11-19 09:53:17 +01:00
|
|
|
if (is_object($_idx)) return false; // given an error in php5.2
|
2011-08-18 23:56:37 +02:00
|
|
|
|
2012-03-02 19:35:49 +01:00
|
|
|
// Make sure none of these are left
|
2014-11-19 09:53:17 +01:00
|
|
|
$idx = str_replace(array('[',']'), array('[',']'), $_idx);
|
2012-03-02 19:35:49 +01:00
|
|
|
|
2012-03-30 00:55:44 +02:00
|
|
|
// Handle things expecting arrays - ends in []
|
|
|
|
if(substr($idx,-2) == "[]")
|
|
|
|
{
|
|
|
|
$idx = substr($idx,0,-2);
|
|
|
|
}
|
|
|
|
|
2011-11-14 23:57:06 +01:00
|
|
|
if (count($idxs = explode('[', $idx, 2)) > 1)
|
|
|
|
{
|
|
|
|
$idxs = array_merge(array($idxs[0]), explode('][', substr($idxs[1],0,-1)));
|
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
$pos = &$arr;
|
|
|
|
foreach($idxs as $idx)
|
|
|
|
{
|
2013-02-08 09:54:20 +01:00
|
|
|
if (!is_array($pos) && (!$reference_into || $reference_into && isset($pos)))
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2013-02-08 09:54:20 +01:00
|
|
|
//if ($reference_into) error_log(__METHOD__."(".(strlen($s=array2string($arr))>512?substr($s,0,512).'...':$s).", '$idx', ".array2string($reference_into).", ".array2string($skip_empty).") ".function_backtrace());
|
2014-02-10 22:59:50 +01:00
|
|
|
return null;
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
2014-02-10 22:59:50 +01:00
|
|
|
if($skip_empty && (!is_array($pos) || !isset($pos[$idx]))) return null;
|
2011-08-18 23:56:37 +02:00
|
|
|
$pos = &$pos[$idx];
|
|
|
|
}
|
|
|
|
return $pos;
|
|
|
|
}
|
|
|
|
|
2015-03-20 03:12:33 +01:00
|
|
|
/**
|
|
|
|
* return a reference to $arr[$idx]
|
|
|
|
*
|
|
|
|
* This works for non-trival indexes like 'a[b][c]' too: it returns &$arr[a][b][c]
|
|
|
|
* $sub = get_array($arr,'a[b]'); $sub = 'c'; is equivalent to $arr['a']['b'] = 'c';
|
|
|
|
*
|
|
|
|
* @param array& $_arr the array to search, referenz as a referenz gets returned
|
|
|
|
* @param string $_idx the index, may contain sub-indices like a[b], see example below
|
|
|
|
* @param mixed $_value value to set
|
|
|
|
*/
|
|
|
|
static function set_array(&$_arr, $_idx, $_value)
|
|
|
|
{
|
|
|
|
$ref =& self::get_array($_arr, $_idx, true);
|
|
|
|
if (true) $ref = $_value;
|
|
|
|
}
|
|
|
|
|
2011-08-18 23:56:37 +02:00
|
|
|
/**
|
|
|
|
* Checks if a widget is readonly:
|
|
|
|
* - readonly attribute set
|
|
|
|
* - $readonlys[__ALL__] set and $readonlys[$form_name] !== false
|
|
|
|
* - $readonlys[$form_name] evaluates to true
|
|
|
|
*
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param string $cname =''
|
|
|
|
* @param string $form_name =null form_name, to not calculate him again
|
2011-08-18 23:56:37 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2012-05-03 16:17:47 +02:00
|
|
|
public function is_readonly($cname='', $form_name=null)
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2012-05-03 16:17:47 +02:00
|
|
|
if (!isset($form_name))
|
|
|
|
{
|
|
|
|
$expand = array(
|
|
|
|
'cont' => self::get_array(self::$request->content, $cname),
|
|
|
|
);
|
|
|
|
$form_name = self::form_name($cname, $this->id, $expand);
|
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
$readonly = $this->attrs['readonly'] || self::$request->readonlys[$form_name] ||
|
2014-02-10 22:59:50 +01:00
|
|
|
self::get_array(self::$request->readonlys,$form_name) === true ||
|
|
|
|
isset(self::$request->readonlys['__ALL__']) && (
|
|
|
|
// Exceptions to all
|
|
|
|
self::$request->readonlys[$form_name] !== false &&
|
|
|
|
self::get_array(self::$request->readonlys,$form_name) !== false
|
|
|
|
);
|
2011-08-18 23:56:37 +02:00
|
|
|
|
2014-02-12 19:35:04 +01:00
|
|
|
//error_log(__METHOD__."('$cname') this->id='$this->id' --> form_name='$form_name': attrs[readonly]=".array2string($this->attrs['readonly']).", readonlys['$form_name']=".array2string(self::$request->readonlys[$form_name]).", readonlys[$form_name]=".array2string(self::get_array(self::$request->readonlys,$form_name)).", readonlys['__ALL__']=".array2string(self::$request->readonlys['__ALL__'])." returning ".array2string($readonly));
|
2011-08-18 23:56:37 +02:00
|
|
|
return $readonly;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Validation errors from process_show and the extensions, should be set via etemplate::set_validation_error
|
|
|
|
*
|
|
|
|
* @public array form_name => message pairs
|
|
|
|
*/
|
|
|
|
static protected $validation_errors = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a validation error, to be displayed in the next exec
|
|
|
|
*
|
|
|
|
* @param string $name (complete) name of the widget causing the error
|
2011-08-19 08:30:06 +02:00
|
|
|
* @param string|boolean $error error-message already translated or false to reset all existing error for given name
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param string $cname =null set it to '', if the name is already a form-name, defaults to self::$name_vars
|
2011-08-18 23:56:37 +02:00
|
|
|
*/
|
|
|
|
public static function set_validation_error($name,$error,$cname=null)
|
|
|
|
{
|
2011-08-19 08:30:06 +02:00
|
|
|
// not yet used: if (is_null($cname)) $cname = self::$name_vars;
|
2014-02-04 10:24:17 +01:00
|
|
|
error_log(__METHOD__."('$name','$error','$cname') ".function_backtrace());
|
2011-08-19 08:30:06 +02:00
|
|
|
|
2011-08-18 23:56:37 +02:00
|
|
|
if ($cname) $name = self::form_name($cname,$name);
|
|
|
|
|
2011-08-19 08:30:06 +02:00
|
|
|
if ($error === false)
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2011-08-19 08:30:06 +02:00
|
|
|
unset(self::$validation_errors[$name]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (self::$validation_errors[$name])
|
|
|
|
{
|
|
|
|
self::$validation_errors[$name] .= ', ';
|
|
|
|
}
|
|
|
|
self::$validation_errors[$name] .= $error;
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if we have not ignored validation errors
|
|
|
|
*
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param string $ignore_validation ='' if not empty regular expression for validation-errors to ignore
|
|
|
|
* @param string $cname =null name-prefix, which need to be ignored, default self::$name_vars
|
2011-08-18 23:56:37 +02:00
|
|
|
* @return boolean true if there are not ignored validation errors, false otherwise
|
|
|
|
*/
|
|
|
|
public static function validation_errors($ignore_validation='',$cname='')
|
|
|
|
{
|
2011-08-19 08:30:06 +02:00
|
|
|
// not yet used: if (is_null($cname)) $cname = self::$name_vars;
|
2011-08-18 23:56:37 +02:00
|
|
|
//echo "<p>uietemplate::validation_errors('$ignore_validation','$cname') validation_error="; _debug_array(self::$validation_errors);
|
|
|
|
if (!$ignore_validation) return count(self::$validation_errors) > 0;
|
|
|
|
|
2014-11-19 09:53:17 +01:00
|
|
|
foreach(array_values(self::$validation_errors) as $name)
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
|
|
|
if ($cname) $name = preg_replace('/^'.$cname.'\[([^\]]+)\](.*)$/','\\1\\2',$name);
|
|
|
|
|
|
|
|
// treat $ignoare_validation only as regular expression, if it starts with a slash
|
|
|
|
if ($ignore_validation[0] == '/' && !preg_match($ignore_validation,$name) ||
|
|
|
|
$ignore_validation[0] != '/' && $ignore_validation != $name)
|
|
|
|
{
|
|
|
|
//echo "<p>uietemplate::validation_errors('$ignore_validation','$cname') name='$name' ($error) not ignored!!!</p>\n";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//echo "<p>uietemplate::validation_errors('$ignore_validation','$cname') name='$name' ($error) ignored</p>\n";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-24 11:24:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns reference to an attribute in a named cell
|
|
|
|
*
|
|
|
|
* Currently we always return a reference to an not set value, unless it was set before.
|
|
|
|
* We do not return a reference to the actual cell, as it get's contructed on client-side!
|
|
|
|
*
|
|
|
|
* @param string $name cell-name
|
|
|
|
* @param string $attr attribute-name
|
|
|
|
* @return mixed reference to attribute, usually NULL
|
|
|
|
*/
|
|
|
|
public function &getElementAttribute($name, $attr)
|
|
|
|
{
|
2011-09-09 15:26:54 +02:00
|
|
|
//error_log(__METHOD__."('$name', '$attr')");
|
2011-08-24 11:24:55 +02:00
|
|
|
return self::$request->modifications[$name][$attr];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set an attribute in a named cell if val is not NULL else return the attribute
|
|
|
|
*
|
2014-02-27 18:43:51 +01:00
|
|
|
* Can be called static, in which case it only sets modifications.
|
|
|
|
*
|
2011-08-24 11:24:55 +02:00
|
|
|
* @param string $name cell-name
|
|
|
|
* @param string $attr attribute-name
|
|
|
|
* @param mixed $val if not NULL sets attribute else returns it
|
|
|
|
* @return reference to attribute
|
|
|
|
*/
|
2014-02-27 18:43:51 +01:00
|
|
|
public static function &setElementAttribute($name,$attr,$val)
|
2011-08-24 11:24:55 +02:00
|
|
|
{
|
2014-02-27 21:25:06 +01:00
|
|
|
//error_log(__METHOD__."('$name', '$attr', ...) request=".get_class(self::$request).", response=".get_class(self::$response).function_backtrace());
|
2011-08-25 21:52:51 +02:00
|
|
|
$ref =& self::$request->modifications[$name][$attr];
|
2014-02-27 18:43:51 +01:00
|
|
|
if(self::$request && self::$response && (!isset($this) || $val != $this->attrs[$attr]))
|
2013-11-01 22:48:22 +01:00
|
|
|
{
|
|
|
|
// In an AJAX response - automatically add
|
|
|
|
self::$response->generic('assign',array(
|
|
|
|
'etemplate_exec_id' => self::$request->id(),
|
|
|
|
'id' => $name,
|
|
|
|
'key' => $attr,
|
|
|
|
'value' => $val
|
|
|
|
));
|
2014-05-13 22:11:06 +02:00
|
|
|
// Don't delete it
|
|
|
|
self::$request->data_modified = true;
|
2014-03-12 14:44:33 +01:00
|
|
|
//error_log(__METHOD__."('$name', '$attr', ...) ".function_backtrace());
|
2013-11-01 22:48:22 +01:00
|
|
|
}
|
2014-02-27 18:43:51 +01:00
|
|
|
if (isset($this)) $this->attrs[$attr] = $val;
|
2011-08-25 21:52:51 +02:00
|
|
|
if (!is_null($val)) $ref = $val;
|
2011-08-24 11:24:55 +02:00
|
|
|
|
2011-09-09 15:26:54 +02:00
|
|
|
//error_log(__METHOD__."('$name', '$attr', ".array2string($val).')');
|
2011-08-25 21:52:51 +02:00
|
|
|
return $ref;
|
2011-08-24 11:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* disables all cells with name == $name
|
|
|
|
*
|
|
|
|
* @param sting $name cell-name
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param boolean $disabled =true disable or enable a cell, default true=disable
|
2011-08-24 11:24:55 +02:00
|
|
|
* @return reference to attribute
|
|
|
|
*/
|
|
|
|
public function disableElement($name,$disabled=True)
|
|
|
|
{
|
|
|
|
return self::setElementAttribute($name, 'disabled', $disabled);
|
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-21 13:55:56 +02:00
|
|
|
* *box widgets having an own namespace
|
2011-08-18 23:56:37 +02:00
|
|
|
*/
|
2011-08-21 13:55:56 +02:00
|
|
|
class etemplate_widget_box extends etemplate_widget
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2011-08-19 10:22:19 +02:00
|
|
|
/**
|
|
|
|
* (Array of) comma-separated list of legacy options to automatically replace when parsing with set_attrs
|
|
|
|
*
|
|
|
|
* @var string|array
|
|
|
|
*/
|
|
|
|
protected $legacy_options = array(
|
|
|
|
'box' => ',cellpadding,cellspacing,keep',
|
|
|
|
'hbox' => 'cellpadding,cellspacing,keep',
|
|
|
|
'vbox' => 'cellpadding,cellspacing,keep',
|
|
|
|
'groupbox' => 'cellpadding,cellspacing,keep',
|
|
|
|
);
|
|
|
|
|
2011-08-18 23:56:37 +02:00
|
|
|
/**
|
2011-08-20 12:27:38 +02:00
|
|
|
* Run a given method on all children
|
2011-08-18 23:56:37 +02:00
|
|
|
*
|
2015-07-07 13:37:41 +02:00
|
|
|
* Reimplemented because grids and boxes can have an own namespace.
|
|
|
|
* GroupBox has no namespace!
|
2011-08-18 23:56:37 +02:00
|
|
|
*
|
2011-08-20 12:27:38 +02:00
|
|
|
* @param string $method_name
|
2014-11-19 09:53:17 +01:00
|
|
|
* @param array $params =array('') parameter(s) first parameter has to be cname!
|
|
|
|
* @param boolean $respect_disabled =false false (default): ignore disabled, true: method is NOT run for disabled widgets AND their children
|
2011-08-18 23:56:37 +02:00
|
|
|
*/
|
2011-08-21 13:55:56 +02:00
|
|
|
public function run($method_name, $params=array(''), $respect_disabled=false)
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2011-08-20 12:27:38 +02:00
|
|
|
$cname =& $params[0];
|
2013-07-04 21:38:36 +02:00
|
|
|
$expand =& $params[1];
|
2012-04-18 00:54:40 +02:00
|
|
|
$old_cname = $params[0];
|
2013-07-04 21:38:36 +02:00
|
|
|
$old_expand = $params[1];
|
2013-08-23 15:00:13 +02:00
|
|
|
|
2015-07-07 13:37:41 +02:00
|
|
|
if ($this->id && $this->type != 'groupbox') $cname = self::form_name($cname, $this->id, $params[1]);
|
2013-07-04 21:38:36 +02:00
|
|
|
if ($expand['cname'] !== $cname && $cname)
|
|
|
|
{
|
|
|
|
$expand['cont'] =& self::get_array(self::$request->content, $cname);
|
|
|
|
$expand['cname'] = $cname;
|
|
|
|
}
|
2013-08-23 15:00:13 +02:00
|
|
|
if ($respect_disabled && ($disabled = $this->attrs['disabled'] && self::check_disabled($this->attrs['disabled'], $expand)))
|
2013-07-04 21:38:36 +02:00
|
|
|
{
|
2014-11-19 09:53:17 +01:00
|
|
|
//error_log(__METHOD__."('$method_name', ".array2string($params).', '.array2string($respect_disabled).") $this disabled='{$this->attrs['disabled']}'=".array2string($disabled).": NOT running");
|
2013-08-23 15:00:13 +02:00
|
|
|
return;
|
2013-07-04 21:38:36 +02:00
|
|
|
}
|
|
|
|
if (method_exists($this, $method_name))
|
|
|
|
{
|
|
|
|
call_user_func_array(array($this, $method_name), $params);
|
|
|
|
}
|
2013-08-23 15:00:13 +02:00
|
|
|
|
2013-07-04 21:38:36 +02:00
|
|
|
// Expand children
|
2014-11-19 09:53:17 +01:00
|
|
|
$columns_disabled = null;
|
2013-07-04 21:38:36 +02:00
|
|
|
for($n = 0; ; ++$n)
|
|
|
|
{
|
|
|
|
if (isset($this->children[$n]))
|
|
|
|
{
|
2013-07-08 20:58:07 +02:00
|
|
|
$child =& $this->children[$n];
|
|
|
|
// If type has something that can be expanded, we need to expand it so the correct method is run
|
|
|
|
$this->expand_widget($child, $expand);
|
2013-07-04 21:38:36 +02:00
|
|
|
}
|
|
|
|
// check if we need to autorepeat last row ($child)
|
2013-07-08 20:58:07 +02:00
|
|
|
elseif (isset($child) && $child->type == 'box' && $this->need_autorepeat($child, $cname, $expand))
|
2013-07-04 21:38:36 +02:00
|
|
|
{
|
2013-07-08 20:58:07 +02:00
|
|
|
// Set row for repeating
|
|
|
|
$expand['row'] = $n;
|
2013-07-04 21:38:36 +02:00
|
|
|
// not breaking repeats last row/column ($child)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//error_log('Running ' . $method_name . ' on child ' . $n . '(' . $child . ') ['.$expand['row'] . ','.$expand['c'] . ']');
|
|
|
|
$disabled = $child->run($method_name, $params, $respect_disabled, $columns_disabled) === false;
|
|
|
|
}
|
2013-08-23 15:00:13 +02:00
|
|
|
|
2012-04-18 00:54:40 +02:00
|
|
|
$params[0] = $old_cname;
|
2013-07-04 21:38:36 +02:00
|
|
|
$params[1] = $old_expand;
|
2013-08-23 15:00:13 +02:00
|
|
|
|
2013-07-04 21:38:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
2013-08-23 15:00:13 +02:00
|
|
|
|
2013-07-04 21:38:36 +02:00
|
|
|
/**
|
|
|
|
* Check if a box child needs autorepeating, because still content left
|
|
|
|
*
|
|
|
|
* We only check passed widget and direct children.
|
|
|
|
*
|
|
|
|
* @param string $cname
|
|
|
|
* @param array $expand
|
|
|
|
*/
|
|
|
|
private function need_autorepeat(etemplate_widget &$widget, $cname, array $expand)
|
|
|
|
{
|
|
|
|
foreach(array($widget) + $widget->children as $check_widget)
|
|
|
|
{
|
|
|
|
$pat = $check_widget->id;
|
2014-11-19 09:53:17 +01:00
|
|
|
while(($pattern = strstr($pat, '$')))
|
2013-07-04 21:38:36 +02:00
|
|
|
{
|
2014-11-19 09:53:17 +01:00
|
|
|
$pat = substr($pattern,$pattern[1] == '{' ? 2 : 1);
|
2013-07-04 21:38:36 +02:00
|
|
|
|
2014-03-28 18:32:32 +01:00
|
|
|
$Ok = $pat[0] == 'r' && !(substr($pat,0,2) == 'r_' ||
|
|
|
|
substr($pat,0,4) == 'row_' && substr($pat,0,8) != 'row_cont');
|
2013-07-04 21:38:36 +02:00
|
|
|
|
2014-03-28 18:32:32 +01:00
|
|
|
if ($Ok && ($fname=self::form_name($cname, $check_widget->id, $expand)) &&
|
|
|
|
// need to break if fname ends in [] as get_array() will ignore it and returns whole array
|
|
|
|
// for an id like "run[$row_cont[appname]]"
|
|
|
|
substr($fname, -2) != '[]' &&
|
|
|
|
($value = self::get_array(self::$request->content, $fname)) !== null) // null = not found (can be false!)
|
2013-07-04 21:38:36 +02:00
|
|
|
{
|
2014-03-28 18:32:32 +01:00
|
|
|
//error_log(__METHOD__."($widget,$cname) $this autorepeating row $expand[row] because of $check_widget->id = '$fname' is ".array2string($value));
|
2014-11-19 09:53:17 +01:00
|
|
|
unset($value);
|
2013-07-04 21:38:36 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-23 15:00:13 +02:00
|
|
|
|
2013-07-04 21:38:36 +02:00
|
|
|
return false;
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
2011-08-18 20:08:40 +02:00
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
// register class for layout widgets, which can have an own namespace
|
2011-08-21 13:55:56 +02:00
|
|
|
etemplate_widget::registerWidget('etemplate_widget_box', array('box', 'hbox', 'vbox', 'groupbox'));
|
2011-08-19 10:22:19 +02:00
|
|
|
|
|
|
|
/**
|
2011-08-21 15:46:21 +02:00
|
|
|
* Description widget
|
2011-08-19 10:22:19 +02:00
|
|
|
*
|
|
|
|
* Reimplemented to set legacy options
|
|
|
|
*/
|
|
|
|
class etemplate_widget_description extends etemplate_widget
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* (Array of) comma-separated list of legacy options to automatically replace when parsing with set_attrs
|
|
|
|
*
|
|
|
|
* @var string|array
|
|
|
|
*/
|
|
|
|
protected $legacy_options = 'bold-italic,link,activate_links,label_for,link_target,link_popup_size,link_title';
|
|
|
|
}
|