- new function widget_tree_walk, which aplies a given function to all widgets or a template, used now by soetemplate::getToTranslate() and boetemplate::set_cell_attribute()

- fixed inline documentation, works very nice now
This commit is contained in:
Ralf Becker 2005-02-13 13:03:01 +00:00
parent 9034ef788c
commit f2e0fda75d
2 changed files with 375 additions and 305 deletions

View File

@ -52,13 +52,14 @@
// 'grid' => 'Grid', // tabular widget containing rows with columns of widgets
'deck' => 'Deck' // a container of elements where only one is visible, size = # of elem.
);
/**
* constructor of class
*
* Calls the constructor of soetemplate
*
* @param $name string/array name of etemplate or array with name and other keys
* @param $load_via string/array name or array with keys of other etemplate to load in order to get $name
* @param string/array $name name of etemplate or array with name and other keys
* @param string/array $load_via name or array with keys of other etemplate to load in order to get $name
*/
function boetemplate($name='',$load_via='')
{
@ -126,15 +127,16 @@
* 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 $name sring the name to expand
* @param $c int is the column index starting with 0 (if you have row-headers, data-cells start at 1)
* @param $row int is the row number starting with 0 (if you have col-headers, data-cells start at 1)
* @param $c_, $row_ int are the respective values of the previous template-inclusion,
* @param sring $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 $cont array content of the template, you might use it to generate button-names with id values in it:
* @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
*/
@ -176,12 +178,12 @@
* In general everything expand_names can generate is ok - see there.
* As you usually have col- and row-headers, data-cells start with '1' or 'A' !!!
*
* @param $cell array with data of cell: name, type, size, ...
* @param $c,$r int col/row index starting from 0
* @param &$idx string returns the index in $content and $readonlys (NOT $sel_options !!!)
* @param &$idx_cname string returns the basename for the form-name: is $idx if only one value
* @param (no ',') is given in size (name (not template-fields) are always only one value)
* @param $check_col boolean to check for col- or row-autorepeat
* @param array $cell with data of cell: name, type, size, ...
* @param int $c,$r col/row index starting from 0
* @param string &$idx returns the index in $content and $readonlys (NOT $sel_options !!!)
* @param string &$idx_cname returns the basename for the form-name: is $idx if only one value
* (no ',') is given in size (name (not template-fields) are always only one value)
* @param boolean $check_col to check for col- or row-autorepeat
* @return boolean true if cell is autorepeat (has index with vars / '$') or false otherwise
*/
function autorepeat_idx($cell,$c,$r,&$idx,&$idx_cname,$check_col=False)
@ -233,87 +235,6 @@
return $id;
}
/**
* db-sessions appsession function, not longer used !!!
*
* It was used to overcome the problem with overflowing php4-sessions, which seems to not exist any more !!!
* @depricated
*/
function appsession($location = 'default', $appname = '', $data = '##NOTHING##')
{
// use the version from the sessions-class if we use db-sessions
//
if ($GLOBALS['phpgw_info']['server']['sessions_type'] == 'db')
{
return $GLOBALS['phpgw']->session->appsession($location,$appname,$data);
}
// if not, we use or own copy of the appsessions function
// setting these class vars to be compatible with the session-class
//
$this->sessionid = $GLOBALS['phpgw']->session->sessionid;
$this->account_id = $GLOBALS['phpgw']->session->account_id;
if (! $appname)
{
$appname = $GLOBALS['phpgw_info']['flags']['currentapp'];
}
if ($data == '##NOTHING##')
{
$query = "SELECT content FROM phpgw_app_sessions WHERE"
." sessionid='".$this->sessionid."' AND loginid='".$this->account_id."'"
." AND app = '".$appname."' AND location='".$location."'";
$GLOBALS['phpgw']->db->query($query,__LINE__,__FILE__);
$GLOBALS['phpgw']->db->next_record();
// I added these into seperate steps for easier debugging
$data = $GLOBALS['phpgw']->db->f('content');
// Changed by Skeeter 2001 Mar 04 0400Z
// This was not properly decoding structures saved into session data properly
// $data = $GLOBALS['phpgw']->common->decrypt($data);
// return stripslashes($data);
// Changed by milosch 2001 Dec 20
// do not stripslashes here unless this proves to be a problem.
// Changed by milosch 2001 Dec 25
/* do not decrypt and return if no data (decrypt returning garbage) */
if($data)
{
$data = $GLOBALS['phpgw']->crypto->decrypt($data);
// echo 'appsession returning: '; _debug_array($data);
}
}
else
{
$GLOBALS['phpgw']->db->query("SELECT content FROM phpgw_app_sessions WHERE "
. "sessionid = '".$this->sessionid."' AND loginid = '".$this->account_id."'"
. " AND app = '".$appname."' AND location = '".$location."'",__LINE__,__FILE__);
$encrypteddata = $GLOBALS['phpgw']->crypto->encrypt($data);
$encrypteddata = $GLOBALS['phpgw']->db->db_addslashes($encrypteddata);
if ($GLOBALS['phpgw']->db->num_rows()==0)
{
$GLOBALS['phpgw']->db->query("INSERT INTO phpgw_app_sessions (sessionid,loginid,app,location,content,session_dla) "
. "VALUES ('".$this->sessionid."','".$this->account_id."','".$appname
. "','".$location."','".$encrypteddata."','" . time() . "')",__LINE__,__FILE__);
}
else
{
$GLOBALS['phpgw']->db->query("UPDATE phpgw_app_sessions SET content='".$encrypteddata."'"
. "WHERE sessionid = '".$this->sessionid."'"
. "AND loginid = '".$this->account_id."' AND app = '".$appname."'"
. "AND location = '".$location."'",__LINE__,__FILE__);
}
}
// we need to clean up not longer used records, else the db gets bigger and bigger
//
$GLOBALS['phpgw']->db->query("DELETE FROM phpgw_app_sessions WHERE session_dla <= '" . (time() - $GLOBALS['phpgw_info']['server']['sessions_timeout'])
. "'",__LINE__,__FILE__);
return $data;
}
/**
* saves content,readonlys,template-keys, ... via the appsession function
*
@ -322,8 +243,8 @@
* is then saved as a hidden-var in the form. The above mentions session-id has nothing to do / is different
* from the session-id which is constant for all windows opened in one session.
*
* @param $data array the data to save
* @param $id string the id to use or '' to generate a new id
* @param array $data the data to save
* @param string $id the id to use or '' to generate a new id
* @return string location-id
*/
function save_appsession($data,$id='')
@ -340,7 +261,7 @@
/**
* gets content,readonlys,template-keys, ... back from the appsession function
*
* @param $id the location-id
* @param string $id the location-id
* @return array with session-data
*/
function get_appsession($id)
@ -359,11 +280,12 @@
/**
* gets an attribute in a named cell
*
* @param $name string cell-name
* @param $attr string attribute-name
* @static
* @param string $name cell-name
* @param string $attr attribute-name
* @return mixed the attribute or False if named cell not found
*/
function get_cell_attribute($name,$attr)
function &get_cell_attribute($name,$attr)
{
return $this->set_cell_attribute($name,$attr,NULL);
}
@ -371,81 +293,30 @@
/**
* set an attribute in a named cell if val is not NULL else return the attribute
*
* @param $name sting cell-name
* @param $attr attribute-name
* @param $val mixed if not NULL sets attribute else returns it
* @static
* @param sting $name cell-name
* @param string $attr attribute-name
* @param mixed $val if not NULL sets attribute else returns it
* @return mixed number of changed cells or False, if none changed
*/
function set_cell_attribute($name,$attr,$val)
function &set_cell_attribute($name,$attr,$val)
{
//echo "<p>set_cell_attribute(tpl->name=$this->name, name='$name', attr='$attr',val='$val')</p>\n";
$n = False;
foreach($this->data as $row => $cols)
$extra = array(false,$name,$attr,$val);
$result =& $this->widget_tree_walk('set_cell_attribute_helper',$extra);
if (is_null($val))
{
if (!is_array($cols)) // should never happen
{
echo "<p>set_cell_attribute(tpl->name=$this->name, name='$name', attr='$attr',val='$val') <b>cols not set for row '$row'</b></p>\n";
$this->echo_tmpl();
}
foreach($cols as $col => $cell)
{
if ($cell['name'] == $name)
{
if (is_null($val))
{
return $cell[$attr];
}
$this->data[$row][$col][$attr] = $val;
++$n;
}
switch($cell['type'])
{
case 'template':
if (is_object($cell['obj']) || $cell['name'][0] != '@')
{
if (!is_object($cell['obj']))
{
$this->data[$row][$col]['obj'] = CreateObject('etemplate.etemplate',$cell['name']);
}
$ret = $this->data[$row][$col]['obj']->set_cell_attribute($name,$attr,$val);
if (is_int($ret))
{
$n += $ret;
}
elseif ($ret !== False && is_null($val))
{
return $ret;
}
}
break;
case 'vbox':
case 'hbox':
case 'groupbox':
case 'box':
for ($i = 0; $i < (int)$cell['size']; ++$i)
{
if ($cell[$i]['name'] == $name)
{
if (is_null($val))
{
return $cell[$attr];
}
$this->data[$row][$col][$i][$attr] = $val;
++$n;
}
}
break;
}
}
return $result;
}
return $n;
return $extra[0];
}
/**
* disables all cells with name == $name
*
* @param $name sting cell-name
* @param sting $name cell-name
* @return mixed number of changed cells or False, if none changed
*/
function disable_cells($name)
@ -456,11 +327,12 @@
/**
* set one or more attibutes for row $n
*
* @param $n int numerical row-number starting with 1 (!)
* @param $height string percent or pixel or '' for no height
* @param $class string name of css class (without the leading '.') or '' for no class
* @param $valign string alignment (top,middle,bottom) or '' for none
* @param $disabled boolean True or expression or False to disable or enable the row (Only the number 0 means dont change the attribute !!!)
* @deprecated as it uses this->data
* @param int $n numerical row-number starting with 1 (!)
* @param string $height percent or pixel or '' for no height
* @param string $class name of css class (without the leading '.') or '' for no class
* @param string $valign alignment (top,middle,bottom) or '' for none
* @param boolean $disabled True or expression or False to disable or enable the row (Only the number 0 means dont change the attribute !!!)
*/
function set_row_attributes($n,$height=0,$class=0,$valign=0,$disabled=0)
{
@ -477,8 +349,9 @@
/**
* disables row $n
*
* @param $n int numerical row-number starting with 1 (!)
* @param $enable boolean can be used to re-enable a row if set to True
* @deprecated as it uses this->data
* @param int $n numerical row-number starting with 1 (!)
* @param boolean $enable=false can be used to re-enable a row if set to True
*/
function disable_row($n,$enable=False)
{
@ -488,9 +361,10 @@
/**
* set one or more attibutes for column $c
*
* @param $c int/string numerical column-number starting with 0 (!), or the char-code starting with 'A'
* @param $width string percent or pixel or '' for no height
* @param $disabled boolean True or expression or False to disable or enable the column (Only the number 0 means dont change the attribute !!!)
* @deprecated as it uses this->data
* @param int/string $c numerical column-number starting with 0 (!), or the char-code starting with 'A'
* @param string $width percent or pixel or '' for no height
* @param mixed $disabled=0 True or expression or False to disable or enable the column (Only the number 0 means dont change the attribute !!!)
*/
function set_column_attributes($c,$width=0,$disabled=0)
{
@ -506,8 +380,10 @@
/**
* disables column $c
* @param $c int/string numerical column-number starting with 0 (!), or the char-code starting with 'A'
* @param $enable can be used to re-enable a column if set to True
*
* @deprecated as it uses this->data
* @param int/string $c numerical column-number starting with 0 (!), or the char-code starting with 'A'
* @param boolean $enable can be used to re-enable a column if set to True
*/
function disable_column($c,$enable=False)
{
@ -517,7 +393,7 @@
/**
* trys to load the Extension / Widget-class from the app or etemplate
*
* @param $name string name of the extension, the classname should be class.${name}_widget.inc.php
* @param string $name name of the extension, the classname should be class.${name}_widget.inc.php
* the $name might be "$name.$app" to give a app-name (default is the current app,or template-name)
* @return string/boolean human readable name or false if not found/loadable
*/
@ -547,28 +423,31 @@
return $GLOBALS['phpgw_info']['etemplate']['extension'][$type]->human_name;
}
function haveExtension($type,$function='')
/*
* checks if extension is loaded and load it if it isnt
/**
* checks if extension is loaded (load it if it isnt) and optional checks if it has a given method
*
* @return boolean
* @param string $name name of the extension, the classname should be class.${name}_widget.inc.php
* the $name might be "$name.$app" to give a app-name (default is the current app,or template-name)
* @param string $function 'pre_process', 'post_process' or 'render'
* @return boolean true if the extension (incl. method) exists, else false
*/
function haveExtension($type,$function='')
{
return ($GLOBALS['phpgw_info']['etemplate']['extension'][$type] || $this->loadExtension($type,$ui)) &&
($function == '' || $GLOBALS['phpgw_info']['etemplate']['extension'][$type]->public_functions[$function]);
}
function extensionPreProcess($type,$name,&$value,&$cell,&$readonlys)
/*
* executes the pre_process-function of the extension $cell[]type]
/**
* executes the pre_process-function of the extension $cell[type]
*
* @param $type string type of the extension
* @param $name string form-name of this widget/field (used as a unique index into extension_data)
* @param &$cell array table-cell on which the extension operates
* @param &$value mixed value of the extensions content(-array)
* @param &$readonlys array value of the extensions readonly-setting(-array)
* @param string $type type of the extension
* @param string $name form-name of this widget/field (used as a unique index into extension_data)
* @param mixed &$value value of the extensions content(-array)
* @param array &$cell table-cell on which the extension operates
* @param array &$readonlys value of the extensions readonly-setting(-array)
* @return mixed the return-value of the extensions preprocess function
*/
function extensionPreProcess($type,$name,&$value,&$cell,&$readonlys)
{
if (!$this->haveExtension($type))
{
@ -578,15 +457,16 @@
$GLOBALS['phpgw_info']['etemplate']['extension_data'][$name],$this);
}
function extensionPostProcess($type,$name,&$value,$value_in)
/*
/**
* executes the post_process-function of the extension $cell[type]
*
* @param $type string name of the extension
* @param $name string form-name of this widget/field (used as a unique index into extension_data)
* @param &$value mixed value of the extensions content(-array)
* @param string $type name of the extension
* @param string $name form-name of this widget/field (used as a unique index into extension_data)
* @param mixed &$value returns the value of the extensions content(-array)
* @param mixed $value_in unprocessed value, eg. as posted by the browser
* @return boolean True if a value should be returned (default for no postprocess fkt.), else False
*/
function extensionPostProcess($type,$name,&$value,$value_in)
{
if (!$this->haveExtension($type,'post_process'))
{
@ -597,11 +477,17 @@
$GLOBALS['phpgw_info']['etemplate']['loop'],$this,$value_in);
}
function extensionRender($type,$name,&$value,&$cell,$readonly)
/*
/**
* executes the render-function of the extension $cell[type]
*
* @param string $type name of the extension
* @param string $name form-name of this widget/field (used as a unique index into extension_data)
* @param mixed &$value value of the extensions content(-array)
* @param array &$cell table-cell on which the extension operates
* @param array &$readonlys value of the extensions readonly-setting(-array)
* @return mixed return-value of the render function
*/
function extensionRender($type,$name,&$value,&$cell,$readonly)
{
if (!$this->haveExtension($type,'render'))
{
@ -614,9 +500,12 @@
/**
* checks if $idx is set in array $arr
*
* @param $arr array
* @param $idx string may contain multiple subindex (eg.'x[y][z]')
* @return boolean
* for one level of subindes identical to isset($arr[$idx])
*
* @static
* @param array $arr array to check
* @param string $idx may contain multiple subindex (eg.'x[y][z]')
* @return boolean true if set, else false
*/
function isset_array($arr,$idx)
{
@ -639,9 +528,10 @@
*
* This works for non-trival indexes like 'a[b][c]' too: $arr['a']['b']['c'] = $val;
*
* @param &$arr array the array to search
* @param $idx string the index, may contain sub-indices like a[b], see example below
* @param $val mixed
* @static
* @param array &$arr the array to search
* @param string $idx the index, may contain sub-indices like a[b], see example below
* @param mixed $val value to set
*/
function set_array(&$arr,$idx,$val)
{
@ -664,9 +554,10 @@
* 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 $arr array the array to search, referenz as a referenz gets returned
* @param $idx string the index, may contain sub-indices like a[b], see example below
* @param $reference_into boolean default False, if True none-existing sub-arrays/-indices get created to be returned as referenz, else False is returned
* @static
* @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 boolean $reference_into default False, if True none-existing sub-arrays/-indices get created to be returned as referenz, else False is returned
* @return mixed reference to $arr[$idx] or false if $idx is not set and not $reference_into
*/
function &get_array(&$arr,$idx,$reference_into=False)
@ -694,8 +585,9 @@
* This works for non-trival indexes like 'a[b][c]' too
* unset_array($arr,'a[b]'); is equivalent to unset($arr['a']['b']);
*
* @param $arr array the array to search, referenz as a referenz gets returned
* @param $idx string the index, may contain sub-indices like a[b], see example below
* @static
* @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
*/
function unset_array(&$arr,$idx)
{
@ -720,8 +612,9 @@
* - array_merge, as it calls itself recursive for values which are arrays.
* - array_merge_recursive accumulates values with the same index and $new does NOT overwrite $old
*
* @param $old array
* @param $new array
* @static
* @param array $old
* @param array $new
* @return array the merged array
*/
function complete_array_merge($old,$new)
@ -747,6 +640,9 @@
/**
* generated a file-name from an eTemplates, name, template(-set) and lang
*
* @param string/array $name name of template or array('name'=>$name,'template'=>$template,'lang'=>$lang)
* @param string $template template-set
* @param string $lang language to use
* @return string
*/
function cache_name($name='',$template='default',$lang='default')
@ -774,7 +670,7 @@
}
/**
* stores the etemplate in the cache in phpgw_info
* stores the etemplate in the cache in phpgw_info
*/
function store_in_cache()
{
@ -785,6 +681,11 @@
/*
* returns true if a given eTemplate is in the cache
*
* @param string/array $name name of template or array('name'=>$name,'template'=>$template,'lang'=>$lang)
* @param string $template template-set
* @param string $lang language to use
* @param int $group to use the template for
* @param string $version of the template
* @return boolean
*/
function in_cache($name,$template='default',$lang='default',$group=0,$version='')
@ -810,6 +711,11 @@
*
* same as read but only via the cache
*
* @param string/array $name name of template or array('name'=>$name,'template'=>$template,'lang'=>$lang)
* @param string $template template-set
* @param string $lang language to use
* @param int $group to use the template for
* @param string $version of the template
* @return boolean true if the eTemplate was found in the cache
*/
function read_from_cache($name,$template='default',$lang='default',$group=0,$version='')
@ -829,12 +735,12 @@
*
* reimplementation of soetemplate::read to use and/or update the cache
*
* @param $name string name of the eTemplate or array with the values for all keys
* @param $template string template-set, '' loads the prefered template of the user, 'default' loads the default one '' in the db
* @param $lang string language, '' loads the pref. lang of the user, 'default' loads the default one '' in the db
* @param $group int id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param $version string version of the eTemplate
* @param $load_via mixed name/array of keys of etemplate to load in order to get $name (only as second try!)
* @param string $name name of the eTemplate or array with the values for all keys
* @param string $template template-set, '' loads the prefered template of the user, 'default' loads the default one '' in the db
* @param string $lang language, '' loads the pref. lang of the user, 'default' loads the default one '' in the db
* @param int $group id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param string $version version of the eTemplate
* @param mixed $load_via name/array of keys of etemplate to load in order to get $name (only as second try!)
* @return boolean True if a fitting template is found, else False
*/
function read($name,$template='default',$lang='default',$group=0,$version='',$load_via='')
@ -878,11 +784,11 @@
*
* reimplementation of soetemplate::save to update the cache
*
* @param $name string name of the eTemplate or array with the values for all keys
* @param $template string template-set or '' for the default one
* @param $lang string language or '' for the default one
* @param $group int id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param $version string version of the eTemplate
* @param string $name name of the eTemplate or array with the values for all keys
* @param string $template template-set or '' for the default one
* @param string $lang language or '' for the default one
* @param int $group id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param string $version version of the eTemplate
* @return the number of affected rows, 1 should be ok, 0 somethings wrong
*/
function save($name='',$template='.',$lang='.',$group=0,$version='.')
@ -894,3 +800,21 @@
return $result;
}
}
if (!function_exists('set_cell_attribute_helper'))
{
function &set_cell_attribute_helper(&$widget,&$extra)
{
// extra = array(0=>n,1=>name,2=>attr,3=>value)
if ($widget['name'] == $extra[1])
{
if (is_null($extra[3]))
{
$extra['__RETURN_NOW__'] = true; // wouldnt work otherwise, if attr is not yet set == null
return $widget[$extra[2]];
}
$widget[$extra[2]] = $extra[3];
++$extra[0];
}
}
}

View File

@ -60,19 +60,28 @@
'et_modified' => 'modified'
);
var $db_cols;
var $widgets_with_children = array( // widgets that contain other widgets, for tree_walk method
'box' => true,
'vbox' => true,
'hbox' => true,
'groupbox' => true,
'deck' => true,
'grid' => true,
'template' => true,
);
/**
* constructor of the class
*
* calls init or read depending on a name for the template is given
*
* @param $name string name of the eTemplate or array with the values for all keys
* @param $template string template-set, '' loads the prefered template of the user, 'default' loads the default one '' in the db
* @param $lang string language, '' loads the pref. lang of the user, 'default' loads the default one '' in the db
* @param $group int id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param $version string version of the eTemplate
* @param $rows int initial size of the template, default 1, only used if no name given !!!
* @param $cols int initial size of the template, default 1, only used if no name given !!!
* @param string $name name of the eTemplate or array with the values for all keys
* @param string $template template-set, '' loads the prefered template of the user, 'default' loads the default one '' in the db
* @param string $lang language, '' loads the pref. lang of the user, 'default' loads the default one '' in the db
* @param int $group id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param string $version version of the eTemplate
* @param int $rows initial size of the template, default 1, only used if no name given !!!
* @param int $cols initial size of the template, default 1, only used if no name given !!!
*/
function soetemplate($name='',$template='',$lang='',$group=0,$version='',$rows=1,$cols=1)
{
@ -92,7 +101,8 @@
/**
* generates column-names from index: 'A', 'B', ..., 'AA', 'AB', ..., 'ZZ' (not more!)
*
* @param $num int numerical index to generate name from 1 => 'A'
* @static
* @param int $num numerical index to generate name from 1 => 'A'
* @return string the name
*/
function num2chrs($num)
@ -109,11 +119,13 @@
}
/**
* constructor for a new / empty cell
* constructor for a new / empty cell/widget
*
* nothing fancy so far
*
* @static
* @param string $type type of the widget
* @param string $name name of widget
* @return array the cell
*/
function empty_cell($type='label',$name='')
@ -127,12 +139,13 @@
/**
* constructs a new cell in a give row or the last row, not existing rows will be created
*
* @deprecated as it uses this->data
* @param int $row row-number starting with 1 (!)
* @param string $type type of the cell
* @param string $label label for the cell
* @param string $name name of the cell (index in the content-array)
* @param array $attributes other attributes for the cell
* @return a reference to the new cell, use $new_cell = &$tpl->new_cell(); (!)
* @return array a reference to the new cell, use $new_cell = &$tpl->new_cell(); (!)
*/
function &new_cell($row=False,$type='label',$label='',$name='',$attributes=False)
{
@ -221,6 +234,9 @@
/**
* initialises internal vars rows & cols from the data of a grid
*
* @static
* @param array &$grid to calc rows and cols
*/
function set_grid_rows_cols(&$grid)
{
@ -239,7 +255,7 @@
/**
* initialises internal vars rows & cols from the data of the first (!) grid
*
* @depricated
* @deprecated as it uses this->data
*/
function set_rows_cols()
{
@ -260,13 +276,13 @@
/**
* initialises all internal data-structures of the eTemplate and sets the keys
*
* @param $name string name of the eTemplate or array with the values for all keys and possibly data
* @param $template string template-set or '' for the default one
* @param $lang string language or '' for the default one
* @param $group int id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param $version string version of the eTemplate
* @param $rows int initial size of the template, default 1
* @param $cols int initial size of the template, default 1
* @param string $name name of the eTemplate or array with the values for all keys and possibly data
* @param string $template template-set or '' for the default one
* @param string $lang language or '' for the default one
* @param int $group id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param string $version version of the eTemplate
* @param int $rows initial size of the template, default 1
* @param int $cols initial size of the template, default 1
*/
function init($name='',$template='',$lang='',$group=0,$version='',$rows=1,$cols=1)
{
@ -318,11 +334,11 @@
/**
* reads an eTemplate from the database
*
* @param $name string name of the eTemplate or array with the values for all keys
* @param $template string template-set, '' loads the prefered template of the user, 'default' loads the default one '' in the db
* @param $lang string language, '' loads the pref. lang of the user, 'default' loads the default one '' in the db
* @param $group int id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param $version string version of the eTemplate
* @param string $name name of the eTemplate or array with the values for all keys
* @param string $template template-set, '' loads the prefered template of the user, 'default' loads the default one '' in the db
* @param string $lang language, '' loads the pref. lang of the user, 'default' loads the default one '' in the db
* @param int $group id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param string $version version of the eTemplate
* @return boolean True if a fitting template is found, else False
*/
function read($name,$template='default',$lang='default',$group=0,$version='')
@ -393,11 +409,12 @@
/**
* Reads an eTemplate from the filesystem, the keys are already set by init in read
*
* @return boolean True if a template was found, else False
*/
function readfile()
{
list($app,$name) = split("\.",$this->name,2);
list($app,$name) = explode('.',$this->name,2);
$template = $this->template == '' ? 'default' : $this->template;
if ($this->lang)
@ -466,13 +483,13 @@
}
/**
* Lists the eTemplates matching the given criteria
* Lists the eTemplates matching the given criteria, sql wildcards % and _ possible
*
* @param $name string name of the eTemplate or array with the values for all keys
* @param $template string template-set, '' loads the prefered template of the user, 'default' loads the default one '' in the db
* @param $lang string language, '' loads the pref. lang of the user, 'default' loads the default one '' in the db
* @param $group int id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param $version string version of the eTemplate
* @param string $name name of the eTemplate or array with the values for all keys
* @param string $template template-set, '' loads the prefered template of the user, 'default' loads the default one '' in the db
* @param string $lang language, '' loads the pref. lang of the user, 'default' loads the default one '' in the db
* @param int $group id of the (primary) group of the user or 0 for none, not used at the moment !!!
* @param string $version version of the eTemplate
* @return array of arrays with the template-params
*/
function search($name,$template='default',$lang='default',$group=0,$version='')
@ -624,7 +641,7 @@
* The never empty type field ensures a cell does not disapear completely.
* Calls it self recursivly for arrays / the rows
*
* @param $arr the array to compress
* @param array $arr the array to compress
* @return array
*/
function compress_array($arr)
@ -652,7 +669,7 @@
*
* the returned array ($data_too > 0) can be used with init to recreate the template
*
* @param $data_too int 0 = no data array, 1 = data array too, 2 = serialize data array
* @param int $data_too 0 = no data array, 1 = data array too, 2 = serialize data array
* @return array with template-data
*/
function as_array($data_too=0)
@ -679,6 +696,11 @@
/**
* saves eTemplate-object to db, can be used as saveAs by giving keys as params
*
* @param string $name name of the eTemplate or array with the values for all keys
* @param string $template template-set
* @param string $lang language or ''
* @param int $group id of the (primary) group, not used at the moment !!!
* @param string $version version of the eTemplate
* @return int number of affected rows, 1 should be ok, 0 somethings wrong
*/
function save($name='',$template='.',$lang='.',$group='',$version='.')
@ -780,7 +802,7 @@
/**
* dumps all eTemplates to <app>/setup/etemplates.inc.php for distribution
*
* @param $app string app- or template-name contain app
* @param string $app app- or template-name contain app
* @return string translated message with number of dumped templates or error-message (webserver has no write access)
*/
function dump2setup($app)
@ -827,34 +849,8 @@
return lang("%1 eTemplates for Application '%2' dumped to '%3'",$n,$app,$file);
}
function getToTranslateCell($cell,&$to_trans)
{
$strings = explode('|',$cell['help']);
if ($cell['type'] != 'image')
{
$strings = array_merge($strings,explode('|',$cell['label']));
}
list($extra_row) = explode(',',$cell['size']);
if (substr($cell['type'],0,6) == 'select' && !empty($extra_row) && !intval($extra_row))
{
$strings[] = $extra_row;
}
if (!empty($cell['blur']))
{
$strings[] = $cell['blur'];
}
foreach($strings as $str)
{
if (strlen($str) > 1 && $str[0] != '@')
{
$to_trans[trim(strtolower($str))] = $str;
}
}
}
/**
* extracts all texts: labels and helptexts from an eTemplate-object
* extracts all texts: labels and helptexts from the cells of an eTemplate-object
*
* some extensions use a '|' to squezze multiple texts in a label or help field
*
@ -864,29 +860,16 @@
{
$to_trans = array();
reset($this->data); each($this->data); // skip width
while (list($row,$cols) = each($this->data))
{
foreach($cols as $col => $cell)
{
$this->getToTranslateCell($cell,$to_trans);
if ($cell['type'] == 'vbox' || $cell['type'] == 'hbox')
{
for ($n = 1; $n <= $cell['size']; ++$n)
{
$this->getToTranslateCell($cell[$n],$to_trans);
}
}
}
}
$this->widget_tree_walk('getToTranslateCell',$to_trans);
//echo '<b>'.$this->name.'</b>'; _debug_array($to_trans);
return $to_trans;
}
/**
* Read all eTemplates of an app an extracts the texts to an array
*
* @param $app string name of the app
* @param string $app name of the app
* @return array with texts
*/
function getToTranslateApp($app)
@ -914,9 +897,9 @@
/**
* Write new lang-file using the existing one and all text from the eTemplates
*
* @param $app string app- or template-name
* @param $lang string language the messages in the template are, defaults to 'en'
* @param $additional array extra texts to translate, if you pass here an array with all messages and
* @param string $app app- or template-name
* @param string $lang language the messages in the template are, defaults to 'en'
* @param array $additional extra texts to translate, if you pass here an array with all messages and
* select-options they get writen too (form is <unique key> => <message>)
* @return string translated message with number of messages written (total and new), or error-message
*/
@ -996,7 +979,7 @@
/**
* Imports the dump-file /$app/setup/etempplates.inc.php unconditional (!)
*
* @param $app string app name
* @param string $app app name
* @return string translated message with number of templates imported
*/
function import_dump($app)
@ -1023,7 +1006,7 @@
* Get called on every read of a eTemplate, caches the result in phpgw_info.
* The timestamp of the last import for app gets written into the db.
*
* @param $app string app- or template-name
* @param string $app app- or template-name
* @return string translated message with number of templates imported
*/
function test_import($app) // should be done from the setup-App
@ -1053,6 +1036,7 @@
/**
* prints/echos the template's content, eg. for debuging
*
* @param boolean $backtrace = true give a function backtrace
* @param boolean $no_other_objs = true dump other objs (db, html, ...) too
*/
@ -1081,4 +1065,166 @@
}
}
}
};
/**
* applys a function to each widget in the children tree of the template
*
* The function should be defined as [&]func([&]$widget,[&]$extra)
* If the function returns anything but null or sets $extra['__RETURN__NOW__'] (func has to reference $extra !!!),
* the walk stops imediatly and returns that result
*
* Only some widgets have a sub-tree of children: *box, grid, template, ...
* For them we call tree_walk($widget,$func,$extra) instead of func direct
*
* @param string/array $func function to use or array($obj,'method')
* @param mixed &$extra extra parameter passed to function
* @return mixed return-value of func or null if nothing returned at all
*/
function &widget_tree_walk($func,&$extra)
{
if (!is_callable($func))
{
echo "<p><b>boetemplate($this->name)::widget_tree_walk</b>(".print_r($func,true).", ".print_r($extra,true).", ".print_r($opts,true).") func is not callable !!!<br>".function_backtrace()."</p>";
return false;
}
foreach($this->children as $c => $nul)
{
$child = &$this->children[$c];
if (isset($this->widgets_with_children[$child['type']]))
{
$result =& $this->tree_walk($child,$func,$extra);
}
else
{
$result =& $func($child,$extra);
}
if (!is_null($result) || isset($extra['__RETURN_NOW__'])) break;
}
return $result;
}
/**
* applys a function to each child in the tree of a widget (incl. the widget itself)
*
* The function should be defined as [&]func([&]$widget,[&]$extra) [] = optional
* If the function returns anything but null or sets $extra['__RETURN__NOW__'] (func has to reference $extra !!!),
* the walk stops imediatly and returns that result
*
* Only some widgets have a sub-tree of children: *box, grid, template, ...
* For performance reasons the function use recursion only if a widget with children contains
* a further widget with children.
*
* @param array $widget the widget(-tree) the function should be applied too
* @param string/array $func function to use or array($obj,'method')
* @param mixed &$extra extra parameter passed to function
* @return mixed return-value of func or null if nothing returned at all
*/
function &tree_walk(&$widget,$func,&$extra)
{
if (!is_callable($func))
{
echo "<p><b>boetemplate::tree_walk</b>(, ".print_r($func,true).", ".print_r($extra,true).", ".print_r($opts,true).") func is not callable !!!<br>".function_backtrace()."</p>";
return false;
}
$result =& $func($widget,$extra);
if (!is_null($result) || isset($extra['__RETURN__NOW__']) || !isset($this->widgets_with_children[$widget['type']]))
{
return $result;
}
switch($widget['type'])
{
case 'box':
case 'vbox':
case 'hbox':
case 'groupbox':
case 'deck':
for($n = 1; is_array($widget[$n]); ++$n)
{
$child = &$widget[$n];
if (isset($this->widgets_with_children[$child['type']]))
{
$result =& $this->tree_walk($child,$func,$extra);
}
else
{
$result =& $func($child,$extra);
}
if (!is_null($result) || isset($extra['__RETURN__NOW__'])) return $result;
}
break;
case 'grid':
$data = &$widget['data'];
if (!is_array($data)) break; // no children
foreach($data as $r => $row)
{
if (!$r || !is_array($row)) continue;
foreach($row as $c => $col)
{
$child = &$data[$r][$c];
if (isset($this->widgets_with_children[$child['type']]))
{
$result =& $this->tree_walk($child,$func,$extra);
}
else
{
$result =& $func($child,$extra);
}
if (!is_null($result) || isset($extra['__RETURN__NOW__'])) return $result;
}
}
break;
case 'template':
if (!isset($widget['obj']) && $widget['name'][0] != '@')
{
$widget['obj'] = new etemplate;
if (!$widget['obj']->read($widget['name'])) $widget['obj'] = false;
}
if (!is_object($widget['obj'])) break; // cant descent into template
$result =& $widget['obj']->widget_tree_walk($func,$extra);
break;
}
return $result;
}
}
if (!function_exists('getToTranslateCell'))
{
/**
* extracts all translatable labels from a widget
*
* @param array $cell the widget
* @param array &$to_trans array with (lowercased) label => translation pairs
*/
function getToTranslateCell($cell,&$to_trans)
{
//echo $cell['name']; _debug_array($cell);
$strings = explode('|',$cell['help']);
if ($cell['type'] != 'image')
{
$strings = array_merge($strings,explode('|',$cell['label']));
}
list($extra_row) = explode(',',$cell['size']);
if (substr($cell['type'],0,6) == 'select' && !empty($extra_row) && !intval($extra_row))
{
$strings[] = $extra_row;
}
if (!empty($cell['blur']))
{
$strings[] = $cell['blur'];
}
foreach($strings as $str)
{
if (strlen($str) > 1 && $str[0] != '@')
{
$to_trans[trim(strtolower($str))] = $str;
}
}
}
}