2002-02-06 10:03:11 +01:00
< ? php
/************************************************************************** \
2004-01-27 17:58:19 +01:00
* eGroupWare - EditableTemplates - Storage Objects *
* http :// www . egroupware . org *
2002-02-06 10:03:11 +01:00
* Written by Ralf Becker < RalfBecker @ outdoor - training . de > *
* -------------------------------------------- *
* This program is free software ; you can redistribute it and / or modify it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation ; either version 2 of the License , or ( at your *
* option ) any later version . *
\ **************************************************************************/
/* $Id$ */
2004-10-07 23:53:10 +02:00
/**
* Storage Objects : Everything to store and retrive and eTemplate .
*
* eTemplates are stored in the db in table 'phpgw_etemplate' and gets distributed
* through the file 'etemplates.inc.php' in the setup dir of each app . That file gets
* automatically imported in the db , whenever you show a eTemplate of the app . For
* performace reasons the timestamp of the file is stored in the db , so 'new'
* eTemplates need to have a newer file . The distribution - file is generated with the
* function dump , usually by pressing a button in the editor .
* writeLangFile writes an lang - file with all Labels , incorporating an existing one .
* Beside a name eTemplates use the following keys to find the most suitable template
* for an user ( in order of precedence ) :
* 1 ) User -/ Group - Id ( not yet implemented )
* 2 ) preferd languages of the user ( templates for all langs have $lang = '' )
* 3 ) selected template : verdilak , ... ( the default is called '' in the db , not default )
* 4 ) a version - number of the form , eg : '0.9.13.001' ( filled up with 0 same size )
*
* @ package etemplate
2005-02-18 23:44:42 +01:00
* @ subpackage api
2004-10-07 23:53:10 +02:00
* @ author RalfBecker - AT - outdoor - training . de
* @ license GPL
*/
2002-02-06 10:03:11 +01:00
class soetemplate
{
2002-09-24 12:00:58 +02:00
var $debug ; // =1 show some debug-messages, = 'app.name' show messages only for eTemplate 'app.name'
2002-02-06 10:03:11 +01:00
var $name ; // name of the template, e.g. 'infolog.edit'
var $template ; // '' = default (not 'default')
var $lang ; // '' if general template else language short, e.g. 'de'
var $group ; // 0 = not specific else groupId or if < 0 userId
var $version ; // like 0.9.13.001
var $style ; // embeded CSS style-sheet
2005-02-06 16:49:50 +01:00
var $children ; // array with children
var $data ; // depricated: first grid of the children
2005-02-08 13:29:06 +01:00
var $size ; // depricated: witdh,height,border of first grid
2005-11-09 21:50:45 +01:00
var $db , $table_name = 'egw_etemplate' ; // name of table
2002-02-14 15:06:53 +01:00
var $db_key_cols = array (
'et_name' => 'name' ,
'et_template' => 'template' ,
'et_lang' => 'lang' ,
'et_group' => 'group' ,
'et_version' => 'version'
);
var $db_data_cols = array (
'et_data' => 'data' ,
'et_size' => 'size' ,
2002-09-29 15:01:40 +02:00
'et_style' => 'style' ,
'et_modified' => 'modified'
2002-02-14 15:06:53 +01:00
);
2002-02-06 10:03:11 +01:00
var $db_cols ;
2005-02-20 22:45:24 +01:00
/**
* @ var array $widgets_with_children widgets that contain other widgets , eg . for tree_walk method
2005-02-22 22:02:26 +01:00
* widget - type is the key , the value specifys how the children are stored .
2005-02-20 22:45:24 +01:00
*/
var $widgets_with_children = array (
2005-02-22 22:02:26 +01:00
'template' => 'template' ,
'grid' => 'grid' ,
'box' => 'box' ,
'vbox' => 'box' ,
'hbox' => 'box' ,
'groupbox' => 'box' ,
'deck' => 'box' ,
2005-02-13 14:03:01 +01:00
);
2004-10-07 23:53:10 +02:00
/**
* constructor of the class
*
* calls init or read depending on a name for the template is given
*
2005-02-13 14:03:01 +01:00
* @ 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 !!!
2004-10-07 23:53:10 +02:00
*/
function soetemplate ( $name = '' , $template = '' , $lang = '' , $group = 0 , $version = '' , $rows = 1 , $cols = 1 )
2002-02-06 10:03:11 +01:00
{
2005-11-09 21:50:45 +01:00
$this -> db = clone ( $GLOBALS [ 'egw' ] -> db );
2005-05-23 10:00:54 +02:00
$this -> db -> set_app ( 'etemplate' );
2002-02-06 10:03:11 +01:00
$this -> db_cols = $this -> db_key_cols + $this -> db_data_cols ;
2002-09-24 12:00:58 +02:00
if ( empty ( $name ))
{
$this -> init ( $name , $template , $lang , $group , $version , $rows , $cols );
}
else
{
2004-10-07 23:53:10 +02:00
$this -> read ( $name , $template , $lang , $group , $version );
2002-09-24 12:00:58 +02:00
}
2002-02-06 10:03:11 +01:00
}
2004-10-07 23:53:10 +02:00
/**
* generates column - names from index : 'A' , 'B' , ... , 'AA' , 'AB' , ... , 'ZZ' ( not more ! )
*
2005-02-13 14:03:01 +01:00
* @ static
* @ param int $num numerical index to generate name from 1 => 'A'
2004-10-07 23:53:10 +02:00
* @ return string the name
*/
2002-02-06 10:03:11 +01:00
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 ;
}
2004-10-07 23:53:10 +02:00
/**
2005-02-13 14:03:01 +01:00
* constructor for a new / empty cell / widget
2004-10-07 23:53:10 +02:00
*
* nothing fancy so far
*
2005-02-08 13:29:06 +01:00
* @ static
2005-02-13 14:03:01 +01:00
* @ param string $type type of the widget
* @ param string $name name of widget
2005-10-31 10:06:24 +01:00
* @ param array $attributes = null array with further attributes
2004-10-07 23:53:10 +02:00
* @ return array the cell
*/
2005-10-31 10:06:24 +01:00
function empty_cell ( $type = 'label' , $name = '' , $attributes = null )
2002-02-06 10:03:11 +01:00
{
2005-10-31 10:06:24 +01:00
$cell = array (
2003-08-28 16:31:11 +02:00
'type' => $type ,
'name' => $name ,
);
2005-10-31 10:06:24 +01:00
if ( $attributes && is_array ( $attributes ))
{
return array_merge ( $attributes , $cell );
}
return $cell ;
2003-08-28 16:31:11 +02:00
}
2004-10-07 23:53:10 +02:00
/**
* constructs a new cell in a give row or the last row , not existing rows will be created
*
2005-02-13 14:03:01 +01:00
* @ deprecated as it uses this -> data
2004-10-07 23:53:10 +02:00
* @ 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
2005-02-13 14:03:01 +01:00
* @ return array a reference to the new cell , use $new_cell = & $tpl -> new_cell (); ( ! )
2003-08-28 16:31:11 +02:00
*/
function & new_cell ( $row = False , $type = 'label' , $label = '' , $name = '' , $attributes = False )
{
$row = $row >= 0 ? intval ( $row ) : 0 ;
if ( $row && ! isset ( $this -> data [ $row ]) || ! isset ( $this -> data [ 1 ])) // new row ?
{
if ( ! $row ) $row = 1 ;
$this -> data [ $row ] = array ();
}
if ( ! $row ) // use last row
{
$row = count ( $this -> data );
while ( ! isset ( $this -> data [ $row ]))
{
-- $row ;
}
}
$row = & $this -> data [ $row ];
$col = $this -> num2chrs ( count ( $row ));
$cell = & $row [ $col ];
$cell = $this -> empty_cell ( $type , $name );
if ( $label !== '' )
{
$attributes [ 'label' ] = $label ;
}
if ( is_array ( $attributes ))
{
foreach ( $attributes as $name => $value )
{
$cell [ $name ] = $value ;
}
}
return $cell ;
2002-02-06 10:03:11 +01:00
}
2004-10-07 23:53:10 +02:00
/**
2005-02-08 13:29:06 +01:00
* adds $cell to it ' s parent at the parent - type spezific location for childs
*
* @ static
* @ param array & $parent referenc to the parent
* @ param array & $cell cell to add ( need to be unset after the call to add_child , as it ' s a referenc ! )
2004-10-07 23:53:10 +02:00
*/
2005-02-08 13:29:06 +01:00
function add_child ( & $parent , & $cell )
{
2005-10-31 10:06:24 +01:00
if ( is_object ( $parent )) // parent is the template itself
{
$parent -> children [] = & $cell ;
return ;
}
2005-10-31 10:33:15 +01:00
switch ( $parent [ 'type' ])
2005-02-08 13:29:06 +01:00
{
case 'vbox' :
case 'hbox' :
case 'groupbox' :
case 'box' :
case 'deck' :
list ( $n , $options ) = explode ( ',' , $parent [ 'size' ], 2 );
$parent [ ++ $n ] = & $cell ;
$parent [ 'size' ] = $n . ( $options ? ',' . $options : '' );
break ;
case 'grid' :
$data = & $parent [ 'data' ];
$cols = & $parent [ 'cols' ];
$rows = & $parent [ 'rows' ];
$row = & $data [ $rows ];
$col = count ( $row );
2005-02-16 00:22:10 +01:00
if ( ! $rows || ! is_array ( $cell )) // create a new row
{
$row = & $data [ ++ $rows ];
$row = array ();
}
2005-02-08 13:29:06 +01:00
if ( is_array ( $cell )) // real cell to add
{
$row [ soetemplate :: num2chrs ( $col ++ )] = & $cell ;
list ( $spanned ) = explode ( ',' , $cell [ 'span' ]);
$spanned = $spanned == 'all' ? 1 + $cols - $col : $spanned ;
while ( -- $spanned > 0 )
{
$row [ soetemplate :: num2chrs ( $col ++ )] = soetemplate :: empty_cell ();
}
if ( $col > $cols ) $cols = $col ;
}
break ;
}
}
/**
* initialises internal vars rows & cols from the data of a grid
2005-02-13 14:03:01 +01:00
*
* @ static
* @ param array & $grid to calc rows and cols
2005-02-08 13:29:06 +01:00
*/
function set_grid_rows_cols ( & $grid )
2002-09-25 18:36:07 +02:00
{
2005-02-08 13:29:06 +01:00
$grid [ 'rows' ] = count ( $grid [ 'data' ]) - 1 ;
$grid [ 'cols' ] = 0 ;
for ( $r = 1 ; $r <= $grid [ 'rows' ]; ++ $r )
2003-11-03 16:43:55 +01:00
{
2005-02-08 13:29:06 +01:00
$cols = count ( $grid [ 'data' ][ $r ]);
if ( $grid [ 'cols' ] < $cols )
2003-11-03 16:43:55 +01:00
{
2005-02-08 13:29:06 +01:00
$grid [ 'cols' ] = $cols ;
2003-11-03 16:43:55 +01:00
}
}
2002-09-25 18:36:07 +02:00
}
2005-02-08 13:29:06 +01:00
/**
* initialises internal vars rows & cols from the data of the first ( ! ) grid
*
2005-02-13 14:03:01 +01:00
* @ deprecated as it uses this -> data
2005-02-08 13:29:06 +01:00
*/
function set_rows_cols ()
{
if ( is_null ( $this -> data )) // tmpl contains no grid
{
$this -> rows = $this -> cols = 0 ;
}
else
{
$grid [ 'data' ] = & $this -> data ;
$grid [ 'rows' ] = & $this -> rows ;
$grid [ 'cols' ] = & $this -> cols ;
$this -> set_grid_rows_cols ( $grid );
unset ( $grid );
}
}
2002-09-25 18:36:07 +02:00
2004-10-07 23:53:10 +02:00
/**
* initialises all internal data - structures of the eTemplate and sets the keys
*
2005-02-13 14:03:01 +01:00
* @ 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
2004-10-07 23:53:10 +02:00
*/
2002-02-06 10:03:11 +01:00
function init ( $name = '' , $template = '' , $lang = '' , $group = 0 , $version = '' , $rows = 1 , $cols = 1 )
{
2005-02-06 16:49:50 +01:00
// unset children and data as they are referenzes to each other
unset ( $this -> children ); unset ( $this -> data );
foreach ( $this -> db_cols as $db_col => $col )
2002-02-06 10:03:11 +01:00
{
2005-05-07 15:55:19 +02:00
if ( $col != 'data' ) $this -> $col = is_array ( $name ) ? ( string ) $name [ $col ] : $$col ;
2002-02-06 10:03:11 +01:00
}
if ( $this -> template == 'default' )
{
$this -> template = '' ;
}
if ( $this -> lang == 'default' )
{
$this -> lang = '' ;
}
2002-09-25 18:36:07 +02:00
$this -> tpls_in_file = is_array ( $name ) ? $name [ 'tpls_in_file' ] : 0 ;
2005-02-23 15:33:00 +01:00
if ( is_array ( $name ) && $name [ 'onclick_handler' ]) $this -> onclick_handler = $name [ 'onclick_handler' ];
2002-09-25 18:36:07 +02:00
2005-02-06 16:49:50 +01:00
if ( is_array ( $name ) && isset ( $name [ 'data' ]))
2002-06-09 23:31:01 +02:00
{
2005-02-06 16:49:50 +01:00
// data/children are in $name['data']
2005-02-18 23:44:42 +01:00
$this -> children = is_array ( $name [ 'data' ]) ? $name [ 'data' ] : unserialize ( $name [ 'data' ]);
2005-02-06 16:49:50 +01:00
$this -> fix_old_template_format ();
2002-06-09 23:31:01 +02:00
}
2005-02-06 16:49:50 +01:00
else
2002-02-06 10:03:11 +01:00
{
2005-02-06 16:49:50 +01:00
$this -> size = $this -> style = '' ;
$this -> data = array ( 0 => array ());
$this -> rows = $rows < 0 ? 1 : $rows ;
$this -> cols = $cols < 0 ? 1 : $cols ;
for ( $row = 1 ; $row <= $rows ; ++ $row )
2002-02-06 10:03:11 +01:00
{
2005-02-06 16:49:50 +01:00
for ( $col = 0 ; $col < $cols ; ++ $col )
{
$this -> data [ $row ][ $this -> num2chrs ( $col )] = $this -> empty_cell ();
}
2002-02-06 10:03:11 +01:00
}
2005-02-06 16:49:50 +01:00
$this -> children [ 0 ][ 'type' ] = 'grid' ;
$this -> children [ 0 ][ 'data' ] = & $this -> data ;
$this -> children [ 0 ][ 'rows' ] = & $this -> rows ;
$this -> children [ 0 ][ 'cols' ] = & $this -> cols ;
2005-02-08 13:29:06 +01:00
$this -> children [ 0 ][ 'size' ] = & $this -> size ;
2002-02-06 10:03:11 +01:00
}
}
2004-10-07 23:53:10 +02:00
/**
* reads an eTemplate from the database
*
2005-02-13 14:03:01 +01:00
* @ 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
2004-10-07 23:53:10 +02:00
* @ return boolean True if a fitting template is found , else False
*/
2002-02-06 10:03:11 +01:00
function read ( $name , $template = 'default' , $lang = 'default' , $group = 0 , $version = '' )
{
$this -> init ( $name , $template , $lang , $group , $version );
2002-09-24 12:00:58 +02:00
if ( $this -> debug == 1 || $this -> debug == $this -> name )
{
2002-09-27 18:17:39 +02:00
echo " <p>soetemplate::read(' $this->name ',' $this->template ',' $this->lang ', $this->group ,' $this->version ')</p> \n " ;
2002-09-24 12:00:58 +02:00
}
2005-11-09 21:50:45 +01:00
if (( $GLOBALS [ 'egw_info' ][ 'server' ][ 'eTemplate-source' ] == 'files' ||
$GLOBALS [ 'egw_info' ][ 'server' ][ 'eTemplate-source' ] == 'xslt' ) && $this -> readfile ())
2002-09-17 15:27:37 +02:00
{
return True ;
}
2002-02-06 10:03:11 +01:00
if ( $this -> name )
{
$this -> test_import ( $this -> name ); // import updates in setup-dir
}
2005-11-09 21:50:45 +01:00
$pref_lang = $GLOBALS [ 'egw_info' ][ 'user' ][ 'preferences' ][ 'common' ][ 'lang' ];
$pref_templ = $GLOBALS [ 'egw_info' ][ 'server' ][ 'template_set' ];
2002-02-06 10:03:11 +01:00
2005-05-07 15:55:19 +02:00
$where = array (
'et_name' => $this -> name ,
);
2002-02-06 10:03:11 +01:00
if ( is_array ( $name ))
{
$template = $name [ 'template' ];
}
if ( $template == 'default' )
{
2005-05-07 15:55:19 +02:00
$where [] = '(et_template=' . $this -> db -> quote ( $pref_templ ) . " OR et_template='') " ;
2002-02-06 10:03:11 +01:00
}
else
{
2005-05-07 15:55:19 +02:00
$where [ 'et_template' ] = $this -> template ;
2002-02-06 10:03:11 +01:00
}
if ( is_array ( $name ))
{
$lang = $name [ 'lang' ];
}
if ( $lang == 'default' || $name [ 'lang' ] == 'default' )
{
2005-05-07 15:55:19 +02:00
$where [] = '(et_lang=' . $this -> db -> quote ( $pref_lang ) . " OR et_lang='') " ;
2002-02-06 10:03:11 +01:00
}
else
{
2005-05-07 15:55:19 +02:00
$where [ 'et_lang' ] = $this -> lang ;
2002-02-06 10:03:11 +01:00
}
if ( $this -> version != '' )
{
2005-05-07 15:55:19 +02:00
$where [ 'et_version' ] = $this -> version ;
2002-09-24 12:00:58 +02:00
}
2005-05-07 15:55:19 +02:00
$this -> db -> select ( $this -> table_name , '*' , $where , __LINE__ , __FILE__ , false , 'ORDER BY et_lang DESC,et_template DESC,et_version DESC' );
2002-02-06 10:03:11 +01:00
if ( ! $this -> db -> next_record ())
{
2002-09-24 12:00:58 +02:00
$version = $this -> version ;
return $this -> readfile () && ( empty ( $version ) || $version == $this -> version );
2002-02-06 10:03:11 +01:00
}
$this -> db2obj ();
2005-02-18 23:44:42 +01:00
if ( $this -> debug == $this -> name )
{
$this -> echo_tmpl ();
}
2002-02-06 10:03:11 +01:00
return True ;
}
2004-10-07 23:53:10 +02:00
/**
* Reads an eTemplate from the filesystem , the keys are already set by init in read
2005-02-13 14:03:01 +01:00
*
2004-10-07 23:53:10 +02:00
* @ return boolean True if a template was found , else False
*/
2002-09-17 15:27:37 +02:00
function readfile ()
{
2005-02-13 14:03:01 +01:00
list ( $app , $name ) = explode ( '.' , $this -> name , 2 );
2002-09-17 15:27:37 +02:00
$template = $this -> template == '' ? 'default' : $this -> template ;
2002-09-30 21:06:29 +02:00
2002-09-17 15:27:37 +02:00
if ( $this -> lang )
{
2002-09-30 21:06:29 +02:00
$lang = '.' . $this -> lang ;
2002-09-17 15:27:37 +02:00
}
2005-11-09 21:50:45 +01:00
$first_try = $ext = $GLOBALS [ 'egw_info' ][ 'server' ][ 'eTemplate-source' ] == 'xslt' ? '.xsl' : '.xet' ;
2002-09-17 15:27:37 +02:00
2005-11-09 21:50:45 +01:00
while (( ! $lang || !@ file_exists ( $file = EGW_SERVER_ROOT . " / $app /templates/ $template / $name $lang $ext " ) &&
!@ file_exists ( $file = EGW_SERVER_ROOT . " / $app /templates/default/ $name $lang $ext " )) &&
!@ file_exists ( $file = EGW_SERVER_ROOT . " / $app /templates/ $template / $name $ext " ) &&
!@ file_exists ( $file = EGW_SERVER_ROOT . " / $app /templates/default/ $name $ext " ))
2002-09-30 21:06:29 +02:00
{
if ( $ext == $first_try )
{
$ext = $ext == '.xet' ? '.xsl' : '.xet' ;
if ( $this -> debug == 1 || $this -> name != '' && $this -> debug == $this -> name )
{
echo " <p>tried ' $file ' now trying it with extension ' $ext ' !!!</p> \n " ;
}
}
else
{
break ;
}
}
2002-09-17 17:38:34 +02:00
if ( $this -> name == '' || $app == '' || $name == '' || !@ file_exists ( $file ) || ! ( $f = @ fopen ( $file , 'r' )))
2002-09-17 15:27:37 +02:00
{
2002-09-29 15:01:40 +02:00
if ( $this -> debug == 1 || $this -> name != '' && $this -> debug == $this -> name )
2002-09-24 12:00:58 +02:00
{
2002-09-30 21:06:29 +02:00
echo " <p>Can't open template ' $this->name ' / ' $file ' !!!</p> \n " ;
2002-09-24 12:00:58 +02:00
}
2002-09-17 15:27:37 +02:00
return False ;
}
2002-09-30 21:06:29 +02:00
$xml = fread ( $f , filesize ( $file ));
2002-09-17 15:27:37 +02:00
fclose ( $f );
2002-09-30 21:06:29 +02:00
if ( $ext == '.xsl' )
2002-09-17 15:27:37 +02:00
{
2002-09-30 21:06:29 +02:00
$cell = $this -> empty_cell ();
$cell [ 'type' ] = 'xslt' ;
$cell [ 'size' ] = $this -> name ;
//$cell['xslt'] = &$xml; xslttemplate class cant use it direct at the moment
$cell [ 'name' ] = '' ;
$this -> data = array ( 0 => array (), 1 => array ( 'A' => & $cell ));
$this -> rows = $this -> cols = 1 ;
2002-09-17 15:27:37 +02:00
}
2002-09-30 21:06:29 +02:00
else
2002-09-17 15:27:37 +02:00
{
2002-09-30 21:06:29 +02:00
if ( ! is_object ( $this -> xul_io ))
{
2005-11-09 21:50:45 +01:00
$this -> xul_io =& CreateObject ( 'etemplate.xul_io' );
2002-09-30 21:06:29 +02:00
}
2004-03-09 22:46:25 +01:00
$loaded = $this -> xul_io -> import ( $this , $xml );
2002-09-17 15:27:37 +02:00
2002-09-30 21:06:29 +02:00
if ( ! is_array ( $loaded ))
{
return False ;
}
$this -> name = $app . '.' . $name ; // if template was copied or app was renamed
2002-09-25 18:36:07 +02:00
2002-09-30 21:06:29 +02:00
$this -> tpls_in_file = count ( $loaded );
}
2002-09-17 15:27:37 +02:00
return True ;
}
2005-05-07 15:55:19 +02:00
/**
* Convert the usual * , ? wildcards to the sql ones and quote % , _
*
* @ param string $pattern
* @ return string
*/
function sql_wildcards ( $pattern )
{
return str_replace ( array ( '%' , '_' , '*' , '?' ), array ( '\\%' , '\\_' , '%' , '_' ), $pattern );
}
2004-10-07 23:53:10 +02:00
/**
2005-02-13 14:03:01 +01:00
* Lists the eTemplates matching the given criteria , sql wildcards % and _ possible
2004-10-07 23:53:10 +02:00
*
2005-02-13 14:03:01 +01:00
* @ 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
2004-10-07 23:53:10 +02:00
* @ return array of arrays with the template - params
*/
2002-09-04 00:59:47 +02:00
function search ( $name , $template = 'default' , $lang = 'default' , $group = 0 , $version = '' )
{
if ( $this -> name )
{
$this -> test_import ( $this -> name ); // import updates in setup-dir
}
if ( is_array ( $name ))
{
2005-05-07 15:55:19 +02:00
$template = ( string ) $name [ 'template' ];
$lang = ( string ) $name [ 'lang' ];
$group = ( int ) $name [ 'group' ];
$version = ( string ) $name [ 'version' ];
$name = ( string ) $name [ 'name' ];
2002-09-04 00:59:47 +02:00
}
2005-05-07 15:55:19 +02:00
$where [] = 'et_name LIKE ' . $this -> db -> quote ( $this -> sql_wildcards ( $name ) . '%' );
2002-09-04 00:59:47 +02:00
if ( $template != '' && $template != 'default' )
{
2005-05-08 19:54:05 +02:00
$where [] = 'et_template LIKE ' . $this -> db -> quote ( $this -> sql_wildcards ( $template ) . '%' );
2002-09-04 00:59:47 +02:00
}
if ( $lang != '' && $lang != 'default' )
{
2005-05-08 19:54:05 +02:00
$where [] = 'et_lang LIKE ' . $this -> db -> quote ( $this -> sql_wildcards ( $lang ) . '%' );
2002-09-04 00:59:47 +02:00
}
if ( $this -> version != '' )
{
2005-05-08 19:54:05 +02:00
$where [] = 'et_version LIKE ' . $this -> db -> quote ( $this -> sql_wildcards ( $version ) . '%' );
2002-09-04 00:59:47 +02:00
}
2005-05-07 15:55:19 +02:00
$this -> db -> select ( $this -> table_name , 'et_name,et_template,et_lang,et_group,et_version' , $where , __LINE__ , __FILE__ , false , 'ORDER BY et_name DESC,et_lang DESC,et_template DESC,et_version DESC' );
2002-09-04 00:59:47 +02:00
$result = array ();
2005-05-07 15:55:19 +02:00
while (( $row = $this -> db -> row ( true , 'et_' )))
2002-09-04 00:59:47 +02:00
{
2005-05-07 15:55:19 +02:00
if ( $row [ 'lang' ] != '##' ) // exclude or import-time-stamps
2002-09-29 15:01:40 +02:00
{
2005-05-07 15:55:19 +02:00
$result [] = $row ;
2002-09-29 15:01:40 +02:00
}
2002-09-04 00:59:47 +02:00
}
2003-03-28 00:43:37 +01:00
if ( $this -> debug )
{
2005-05-07 15:55:19 +02:00
_debug_array ( $result );
2003-03-28 00:43:37 +01:00
}
2002-09-04 00:59:47 +02:00
return $result ;
}
2004-10-07 23:53:10 +02:00
/**
* copies all cols into the obj and unserializes the data - array
*/
2002-02-06 10:03:11 +01:00
function db2obj ()
{
2005-02-06 16:49:50 +01:00
// unset children and data as they are referenzes to each other
unset ( $this -> children ); unset ( $this -> data );
foreach ( $this -> db_cols as $db_col => $name )
2002-02-06 10:03:11 +01:00
{
2005-02-06 16:49:50 +01:00
if ( $name != 'data' )
{
$this -> $name = $this -> db -> f ( $db_col );
}
else
{
2005-02-18 23:44:42 +01:00
$this -> children = unserialize ( $this -> db -> f ( $db_col ));
2005-02-06 16:49:50 +01:00
}
2002-02-06 10:03:11 +01:00
}
2005-02-06 16:49:50 +01:00
$this -> fix_old_template_format ();
}
/**
* test if we have an old / original template - format and fixes it to the new format
*/
function fix_old_template_format ()
{
if ( ! is_array ( $this -> children )) $this -> children = array ();
if ( ! isset ( $this -> children [ 0 ][ 'type' ]))
2002-02-06 10:03:11 +01:00
{
2005-02-06 16:49:50 +01:00
// old templates are treated as having one children of type grid (the original template)
$this -> data = & $this -> children ;
unset ( $this -> children );
$this -> children [ 0 ][ 'type' ] = 'grid' ;
$this -> children [ 0 ][ 'data' ] = & $this -> data ;
$this -> children [ 0 ][ 'rows' ] = & $this -> rows ;
$this -> children [ 0 ][ 'cols' ] = & $this -> cols ;
2005-02-08 13:29:06 +01:00
$this -> children [ 0 ][ 'size' ] = & $this -> size ;
2005-02-06 16:49:50 +01:00
// that code fixes a bug in very old templates, not sure if it's still needed
if ( $this -> name [ 0 ] != '.' && is_array ( $this -> data ))
2002-02-06 10:03:11 +01:00
{
2005-02-06 16:49:50 +01:00
reset ( $this -> data ); each ( $this -> data );
while ( list ( $row , $cols ) = each ( $this -> data ))
2002-02-06 10:03:11 +01:00
{
2005-02-06 16:49:50 +01:00
while ( list ( $col , $cell ) = each ( $cols ))
2002-02-06 10:03:11 +01:00
{
2005-02-06 16:49:50 +01:00
if ( is_array ( $cell [ 'type' ]))
{
$this -> data [ $row ][ $col ][ 'type' ] = $cell [ 'type' ][ 0 ];
//echo "corrected in $this->name cell $col$row attribute type<br>\n";
}
if ( is_array ( $cell [ 'align' ]))
{
$this -> data [ $row ][ $col ][ 'align' ] = $cell [ 'align' ][ 0 ];
//echo "corrected in $this->name cell $col$row attribute align<br>\n";
}
2002-02-06 10:03:11 +01:00
}
}
}
}
2005-02-06 16:49:50 +01:00
else
{
2005-02-08 13:29:06 +01:00
unset ( $this -> data );
2005-02-06 16:49:50 +01:00
// for the moment we make $this->data as a referenz to the first grid
foreach ( $this -> children as $key => $child )
{
if ( $child [ 'type' ] == 'grid' )
{
$this -> data = & $this -> children [ $key ][ 'data' ];
$this -> rows = & $this -> children [ $key ][ 'rows' ];
$this -> cols = & $this -> children [ $key ][ 'cols' ];
2005-02-08 13:29:06 +01:00
if ( ! isset ( $this -> children [ $key ][ 'size' ]) && ! empty ( $this -> size ))
{
$this -> children [ $key ][ 'size' ] = & $this -> size ;
}
else
{
$this -> size = & $this -> children [ $key ][ 'size' ];
}
2005-02-06 16:49:50 +01:00
break ;
}
}
}
2002-09-25 18:36:07 +02:00
$this -> set_rows_cols ();
2002-02-06 10:03:11 +01:00
}
2004-10-07 23:53:10 +02:00
/**
2005-02-18 23:44:42 +01:00
* all empty values and objects in the array got unset ( to save space in the db )
2004-10-07 23:53:10 +02:00
*
* The never empty type field ensures a cell does not disapear completely .
* Calls it self recursivly for arrays / the rows
*
2005-02-13 14:03:01 +01:00
* @ param array $arr the array to compress
2005-02-23 15:33:00 +01:00
* @ param boolean $remove_all_objs if true unset all objs , on false use as_array to save only the data of objs
2004-10-07 23:53:10 +02:00
* @ return array
*/
2005-02-23 15:33:00 +01:00
function compress_array ( $arr , $remove_objs = false )
2002-02-06 10:03:11 +01:00
{
if ( ! is_array ( $arr ))
{
return $arr ;
}
2005-02-06 16:49:50 +01:00
foreach ( $arr as $key => $val )
2002-02-06 10:03:11 +01:00
{
2005-11-09 21:50:45 +01:00
if ( $remove_objs && $key === 'obj' ) // it can be an array too
{
unset ( $arr [ $key ]);
}
elseif ( is_array ( $val ))
2002-02-06 10:03:11 +01:00
{
2005-02-23 15:33:00 +01:00
$arr [ $key ] = $this -> compress_array ( $val , $remove_objs );
}
2005-02-24 22:02:24 +01:00
elseif ( ! $remove_objs && $key == 'obj' && is_object ( $val ) && method_exists ( $val , 'as_array' ) &&
// this test prevents an infinit recursion of templates calling itself, atm. etemplate.editor.new
2005-11-09 21:50:45 +01:00
$GLOBALS [ 'egw_info' ][ 'etemplate' ][ 'as_array' ][ $this -> name ] ++ < 2 )
2005-02-23 15:33:00 +01:00
{
$arr [ 'obj' ] = $val -> as_array ( 2 );
2002-02-06 10:03:11 +01:00
}
2005-02-18 23:44:42 +01:00
elseif ( $val == '' || is_object ( $val ))
2002-02-06 10:03:11 +01:00
{
unset ( $arr [ $key ]);
}
}
return $arr ;
}
2004-10-07 23:53:10 +02:00
/**
* returns obj - data /- vars as array
*
2005-02-06 16:49:50 +01:00
* the returned array ( $data_too > 0 ) can be used with init to recreate the template
*
2005-02-20 22:45:24 +01:00
* @ param int $data_too - 1 = only keys , 0 = no data array , 1 = data array too , 2 = serialize data array ,
* 3 = only data values and data serialized
* @ param boolean $db_keys use db - column - names or internal names , default false = internal names
2005-02-06 16:49:50 +01:00
* @ return array with template - data
2004-10-07 23:53:10 +02:00
*/
2005-02-20 22:45:24 +01:00
function as_array ( $data_too = 0 , $db_keys = false )
2002-02-06 10:03:11 +01:00
{
2005-02-24 22:02:24 +01:00
//echo "<p>soetemplate::as_array($data_too,$db_keys) name='$this->name', ver='$this->version'</p>\n";
2002-02-06 10:03:11 +01:00
$arr = array ();
2005-02-20 22:45:24 +01:00
switch ( $data_too )
{
case - 1 :
$cols = $this -> db_key_cols ;
break ;
case 3 :
$cols = $this -> db_data_cols ;
break ;
default :
$cols = $this -> db_cols ;
}
foreach ( $cols as $db_col => $col )
2002-02-06 10:03:11 +01:00
{
2005-02-18 23:44:42 +01:00
if ( $col == 'data' )
2005-02-06 16:49:50 +01:00
{
2005-02-20 22:45:24 +01:00
if ( $data_too > 0 )
2005-02-18 23:44:42 +01:00
{
2005-02-20 22:45:24 +01:00
$arr [ $db_keys ? $db_col : $col ] = $data_too < 2 ? $this -> children :
2005-02-23 15:33:00 +01:00
serialize ( $this -> compress_array ( $this -> children , $db_keys ));
2005-02-18 23:44:42 +01:00
}
2005-02-06 16:49:50 +01:00
}
else
2002-02-06 10:03:11 +01:00
{
2005-02-20 22:45:24 +01:00
$arr [ $db_keys ? $db_col : $col ] = $this -> $col ;
2002-02-06 10:03:11 +01:00
}
}
2005-02-20 22:45:24 +01:00
if ( $data_too != - 1 && $this -> tpls_in_file && ! $db_keys )
2002-02-06 10:03:11 +01:00
{
2002-09-25 18:36:07 +02:00
$arr [ 'tpls_in_file' ] = $this -> tpls_in_file ;
}
2005-02-23 15:33:00 +01:00
if ( $data_too != - 1 && $this -> onclick_handler && ! $db_keys )
{
$arr [ 'onclick_handler' ] = $this -> onclick_handler ;
}
2002-02-06 10:03:11 +01:00
return $arr ;
}
2004-10-07 23:53:10 +02:00
/**
* saves eTemplate - object to db , can be used as saveAs by giving keys as params
*
2005-02-13 14:03:01 +01:00
* @ 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
2004-10-07 23:53:10 +02:00
* @ return int number of affected rows , 1 should be ok , 0 somethings wrong
*/
2002-02-06 10:03:11 +01:00
function save ( $name = '' , $template = '.' , $lang = '.' , $group = '' , $version = '.' )
{
2002-09-27 18:17:39 +02:00
if ( is_array ( $name ))
{
$template = $name [ 'template' ];
$lang = $name [ 'lang' ];
$group = $name [ 'group' ];
$version = $name [ 'version' ];
$name = $name [ 'name' ];
}
2002-02-06 10:03:11 +01:00
if ( $name != '' )
{
$this -> name = $name ;
}
if ( $lang != '.' )
{
$this -> lang = $lang ;
}
if ( $template != '.' )
{
$this -> template = $template ;
}
if ( $group != '' )
{
$this -> group = $group ;
}
if ( $version != '.' )
{
$this -> version = $version ;
}
2002-02-14 15:06:53 +01:00
if ( $this -> name == '' ) // name need to be set !!!
2002-02-06 10:03:11 +01:00
{
return False ;
}
2002-09-27 18:17:39 +02:00
if ( $this -> debug > 0 || $this -> debug == $this -> name )
{
echo " <p>soetemplate::save(' $this->name ',' $this->template ',' $this->lang ', $this->group ,' $this->version ')</p> \n " ;
}
2005-02-18 23:44:42 +01:00
if ( $this -> name [ 0 ] != '.' && is_array ( $this -> data )) // correct old messed up templates
2002-02-06 10:03:11 +01:00
{
reset ( $this -> data ); each ( $this -> data );
while ( list ( $row , $cols ) = each ( $this -> data ))
{
while ( list ( $col , $cell ) = each ( $cols ))
{
if ( is_array ( $cell [ 'type' ])) {
$this -> data [ $row ][ $col ][ 'type' ] = $cell [ 'type' ][ 0 ];
//echo "corrected in $this->name cell $col$row attribute type<br>\n";
}
if ( is_array ( $cell [ 'align' ])) {
$this -> data [ $row ][ $col ][ 'align' ] = $cell [ 'align' ][ 0 ];
//echo "corrected in $this->name cell $col$row attribute align<br>\n";
}
}
}
}
2002-09-29 15:01:40 +02:00
if ( ! $this -> modified )
{
$this -> modified = time ();
}
2005-05-07 15:55:19 +02:00
if ( is_null ( $this -> group ) && ! is_int ( $this -> group )) $this -> group = 0 ;
2005-11-09 21:50:45 +01:00
2005-02-20 22:45:24 +01:00
$this -> db -> insert ( $this -> table_name , $this -> as_array ( 3 , true ), $this -> as_array ( - 1 , true ), __LINE__ , __FILE__ );
2002-02-06 10:03:11 +01:00
2005-02-18 23:44:42 +01:00
$rows = $this -> db -> affected_rows ();
if ( ! $rows )
{
echo " <p>soetemplate::save(' $this->name ',' $this->template ',' $this->lang ', $this->group ,' $this->version ') <b>nothing written!!!</b></p> \n " ;
function_backtrace ();
_debug_array ( $this -> db );
}
return $rows ;
2002-02-06 10:03:11 +01:00
}
2004-10-07 23:53:10 +02:00
/**
* Deletes the eTemplate from the db , object itself is unchanged
*
* @ return int number of affected rows , 1 should be ok , 0 somethings wrong
*/
2002-02-06 10:03:11 +01:00
function delete ()
{
2005-02-20 22:45:24 +01:00
$this -> db -> delete ( $this -> table_name , $this -> as_array ( - 1 , true ), __LINE__ , __FILE__ );
2002-02-06 10:03:11 +01:00
return $this -> db -> affected_rows ();
}
2004-10-07 23:53:10 +02:00
/**
* dumps all eTemplates to < app >/ setup / etemplates . inc . php for distribution
*
2005-02-13 14:03:01 +01:00
* @ param string $app app - or template - name contain app
2004-10-07 23:53:10 +02:00
* @ return string translated message with number of dumped templates or error - message ( webserver has no write access )
*/
2005-02-18 23:44:42 +01:00
function dump4setup ( $app )
2002-02-06 10:03:11 +01:00
{
list ( $app ) = explode ( '.' , $app );
2005-02-20 22:45:24 +01:00
$this -> db -> query ( " SELECT * FROM $this->table_name WHERE et_name LIKE ' $app %' " );
2002-02-06 10:03:11 +01:00
2005-11-09 21:50:45 +01:00
$dir = EGW_SERVER_ROOT . " / $app /setup " ;
2002-02-15 06:27:30 +01:00
if ( ! is_writeable ( $dir ))
{
2003-08-08 18:20:16 +02:00
return lang ( " Error: webserver is not allowed to write into '%1' !!! " , $dir );
2002-02-15 06:27:30 +01:00
}
$file = " $dir /etemplates.inc.php " ;
if ( file_exists ( $file ))
{
2003-07-21 18:09:33 +02:00
$old_file = " $dir /etemplates.old.inc.php " ;
if ( file_exists ( $old_file ))
{
unlink ( $old_file );
}
rename ( $file , $old_file );
2002-02-15 06:27:30 +01:00
}
if ( ! ( $f = fopen ( $file , 'w' )))
2002-02-06 10:03:11 +01:00
{
return 0 ;
}
2005-02-18 23:44:42 +01:00
fwrite ( $f , " <?php \n // eTemplates for Application ' $app ', generated by soetemplate::dump4setup() " . date ( 'Y-m-d H:i' ) . " \n \n " .
'/* $' . 'Id$ */' . " \n \n \$ templ_version=1; \n \n " );
2002-10-03 13:10:52 +02:00
2002-02-06 10:03:11 +01:00
for ( $n = 0 ; $this -> db -> next_record (); ++ $n )
{
$str = '$templ_data[] = array(' ;
2005-02-06 16:49:50 +01:00
foreach ( $this -> db_cols as $db_col => $name )
2002-02-06 10:03:11 +01:00
{
2005-02-18 23:44:42 +01:00
// escape only backslashes and single quotes (in that order)
$str .= " ' $name ' => ' " . str_replace ( array ( '\\' , '\'' ), array ( '\\\\' , '\\\'' ), $this -> db -> f ( $db_col )) . " ', " ;
2002-02-06 10:03:11 +01:00
}
$str .= " ); \n \n " ;
fwrite ( $f , $str );
}
fclose ( $f );
2003-08-08 18:20:16 +02:00
return lang ( " %1 eTemplates for Application '%2' dumped to '%3' " , $n , $app , $file );
2002-02-06 10:03:11 +01:00
}
2004-10-07 23:53:10 +02:00
/**
2005-02-13 14:03:01 +01:00
* extracts all texts : labels and helptexts from the cells of an eTemplate - object
2004-10-07 23:53:10 +02:00
*
* some extensions use a '|' to squezze multiple texts in a label or help field
*
* @ return array with messages as key AND value
*/
2002-02-06 10:03:11 +01:00
function getToTranslate ()
{
$to_trans = array ();
2005-02-13 14:03:01 +01:00
$this -> widget_tree_walk ( 'getToTranslateCell' , $to_trans );
//echo '<b>'.$this->name.'</b>'; _debug_array($to_trans);
2002-02-06 10:03:11 +01:00
return $to_trans ;
}
2004-10-07 23:53:10 +02:00
/**
* Read all eTemplates of an app an extracts the texts to an array
*
2005-02-13 14:03:01 +01:00
* @ param string $app name of the app
2004-10-07 23:53:10 +02:00
* @ return array with texts
*/
2002-02-06 10:03:11 +01:00
function getToTranslateApp ( $app )
{
$to_trans = array ();
2002-10-28 02:17:32 +01:00
$tpls = $this -> search ( $app );
2002-02-06 10:03:11 +01:00
2005-11-09 21:50:45 +01:00
$tpl =& new soetemplate ; // to not alter our own data
2002-10-28 02:17:32 +01:00
while ( list (, $keys ) = each ( $tpls ))
2002-02-06 10:03:11 +01:00
{
2002-10-28 02:17:32 +01:00
if (( $keys [ 'name' ] != $last [ 'name' ] || // write only newest version
$keys [ 'template' ] != $last [ 'template' ]) &&
! strstr ( $keys [ 'name' ], 'test' ))
{
$tpl -> read ( $keys );
$to_trans += $tpl -> getToTranslate ();
$last = $keys ;
}
2002-02-06 10:03:11 +01:00
}
return $to_trans ;
}
2004-10-07 23:53:10 +02:00
/**
* Write new lang - file using the existing one and all text from the eTemplates
*
2005-02-13 14:03:01 +01:00
* @ 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
2004-10-07 23:53:10 +02:00
* 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
*/
2002-02-06 10:03:11 +01:00
function writeLangFile ( $app , $lang = 'en' , $additional = '' )
{
2002-02-14 15:06:53 +01:00
if ( ! $additional )
2002-02-06 10:03:11 +01:00
{
2005-02-06 16:49:50 +01:00
$additional = array ();
2002-02-06 10:03:11 +01:00
}
list ( $app ) = explode ( '.' , $app );
2005-11-09 21:50:45 +01:00
if ( ! file_exists ( EGW_SERVER_ROOT . '/developer_tools/inc/class.solangfile.inc.php' ))
2002-10-03 18:46:18 +02:00
{
2005-11-09 21:50:45 +01:00
$solangfile =& CreateObject ( 'etemplate.solangfile' );
2002-10-28 02:17:32 +01:00
}
else
{
2005-11-09 21:50:45 +01:00
$solangfile =& CreateObject ( 'developer_tools.solangfile' );
2002-10-03 18:46:18 +02:00
}
2002-02-06 10:03:11 +01:00
$langarr = $solangfile -> load_app ( $app , $lang );
2002-10-14 02:39:47 +02:00
if ( ! is_array ( $langarr ))
{
$langarr = array ();
}
2003-08-28 16:31:11 +02:00
$commonarr = $solangfile -> load_app ( 'phpgwapi' , $lang ) + $solangfile -> load_app ( 'etemplate' , $lang );
2002-02-06 10:03:11 +01:00
$to_trans = $this -> getToTranslateApp ( $app );
if ( is_array ( $additional ))
{
2002-02-14 15:06:53 +01:00
//echo "writeLangFile: additional ="; _debug_array($additional);
2003-08-28 16:31:11 +02:00
foreach ( $additional as $msg )
2002-02-06 10:03:11 +01:00
{
2005-02-06 16:49:50 +01:00
if ( ! is_array ( $msg )) $to_trans [ trim ( strtolower ( $msg ))] = $msg ;
2002-02-06 10:03:11 +01:00
}
}
2002-10-12 18:37:44 +02:00
unset ( $to_trans [ '' ]);
2003-08-28 16:31:11 +02:00
for ( $new = $n = 0 ; list ( $message_id , $content ) = each ( $to_trans ); ++ $n )
{
if ( ! isset ( $langarr [ $message_id ]) && ! isset ( $commonarr [ $message_id ]))
2002-02-06 10:03:11 +01:00
{
2003-08-28 16:31:11 +02:00
if ( @ isset ( $langarr [ $content ])) // caused by not lowercased-message_id's
2002-10-14 02:39:47 +02:00
{
unset ( $langarr [ $content ]);
}
2002-02-14 15:06:53 +01:00
$langarr [ $message_id ] = array (
'message_id' => $message_id ,
2003-08-28 16:31:11 +02:00
'app_name' => $app ,
'content' => $content
2002-02-14 15:06:53 +01:00
);
2002-02-06 10:03:11 +01:00
++ $new ;
}
}
ksort ( $langarr );
2002-02-14 15:06:53 +01:00
2005-11-09 21:50:45 +01:00
$dir = EGW_SERVER_ROOT . " / $app /setup " ;
2002-02-14 15:06:53 +01:00
if ( ! is_writeable ( $dir ))
{
2003-08-08 18:20:16 +02:00
return lang ( " Error: webserver is not allowed to write into '%1' !!! " , $dir );
2002-02-14 15:06:53 +01:00
}
$file = " $dir /phpgw_ $lang .lang " ;
if ( file_exists ( $file ))
{
2003-07-21 18:09:33 +02:00
$old_file = " $dir /phpgw_ $lang .old.lang " ;
if ( file_exists ( $old_file ))
{
unlink ( $old_file );
}
rename ( $file , $old_file );
2002-02-14 15:06:53 +01:00
}
2002-02-06 10:03:11 +01:00
$solangfile -> write_file ( $app , $langarr , $lang );
$solangfile -> loaddb ( $app , $lang );
2003-08-08 18:20:16 +02:00
return lang ( " %1 (%2 new) Messages writen for Application '%3' and Languages '%4' " , $n , $new , $app , $lang );
2002-02-06 10:03:11 +01:00
}
2004-10-07 23:53:10 +02:00
/**
* Imports the dump - file / $app / setup / etempplates . inc . php unconditional ( ! )
*
2005-02-13 14:03:01 +01:00
* @ param string $app app name
2004-10-07 23:53:10 +02:00
* @ return string translated message with number of templates imported
*/
2002-02-06 10:03:11 +01:00
function import_dump ( $app )
{
2005-02-18 23:44:42 +01:00
$templ_version = 0 ;
2005-11-09 21:50:45 +01:00
include ( $path = EGW_SERVER_ROOT . " / $app /setup/etemplates.inc.php " );
$templ =& new etemplate ( $app );
2002-02-06 10:03:11 +01:00
2005-02-06 16:49:50 +01:00
foreach ( $templ_data as $data )
2002-02-06 10:03:11 +01:00
{
2005-02-18 23:44:42 +01:00
if (( int ) $templ_version < 1 ) // we need to stripslashes
{
$data [ 'data' ] = stripslashes ( $data [ 'data' ]);
}
2005-02-06 16:49:50 +01:00
$templ -> init ( $data );
2002-09-29 15:01:40 +02:00
if ( ! $templ -> modified )
{
$templ -> modified = filemtime ( $path );
}
2002-02-06 10:03:11 +01:00
$templ -> save ();
}
2003-08-08 18:20:16 +02:00
return lang ( " %1 new eTemplates imported for Application '%2' " , $n , $app );
2002-02-06 10:03:11 +01:00
}
2004-10-07 23:53:10 +02:00
/**
* test if new template - import necessary for app and does the import
*
* 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 .
*
2005-02-13 14:03:01 +01:00
* @ param string $app app - or template - name
2004-10-07 23:53:10 +02:00
* @ return string translated message with number of templates imported
*/
2002-02-06 10:03:11 +01:00
function test_import ( $app ) // should be done from the setup-App
{
list ( $app ) = explode ( '.' , $app );
2005-11-09 21:50:45 +01:00
if ( ! $app || $GLOBALS [ 'egw_info' ][ 'etemplate' ][ 'import_tested' ][ $app ])
2002-02-06 10:03:11 +01:00
{
return '' ; // ensure test is done only once per call and app
}
2005-11-09 21:50:45 +01:00
$GLOBALS [ 'egw_info' ][ 'etemplate' ][ 'import_tested' ][ $app ] = True ; // need to be done before new ...
2002-02-06 10:03:11 +01:00
2005-11-09 21:50:45 +01:00
$path = EGW_SERVER_ROOT . " / $app /setup/etemplates.inc.php " ;
2002-02-06 10:03:11 +01:00
2002-09-22 19:55:17 +02:00
if ( $time = @ filemtime ( $path ))
2002-02-06 10:03:11 +01:00
{
2005-11-09 21:50:45 +01:00
$templ =& new soetemplate ( " . $app " , '' , '##' );
2002-09-29 15:01:40 +02:00
if ( $templ -> lang != '##' || $templ -> modified < $time ) // need to import
2002-02-06 10:03:11 +01:00
{
$ret = $this -> import_dump ( $app );
2002-09-29 15:01:40 +02:00
$templ -> modified = $time ;
2005-02-18 23:44:42 +01:00
$templ -> save ( '.' . $app , '' , '##' );
2002-02-06 10:03:11 +01:00
}
}
return $ret ;
}
2005-02-06 16:49:50 +01:00
/**
* prints / echos the template ' s content , eg . for debuging
2005-02-13 14:03:01 +01:00
*
2005-02-06 16:49:50 +01:00
* @ param boolean $backtrace = true give a function backtrace
2005-02-08 13:29:06 +01:00
* @ param boolean $no_other_objs = true dump other objs ( db , html , ... ) too
2005-02-06 16:49:50 +01:00
*/
2005-02-08 13:29:06 +01:00
function echo_tmpl ( $backtrace = true , $no_other_objs = true )
2005-02-06 16:49:50 +01:00
{
2005-02-08 13:29:06 +01:00
static $objs = array ( 'db' , 'html' , 'xul_io' );
2005-02-06 16:49:50 +01:00
if ( $backtrace ) echo " <p> " . function_backtrace ( 1 ) . " </p> \n " ;
2005-02-08 13:29:06 +01:00
if ( $no_other_objs )
2005-02-06 16:49:50 +01:00
{
2005-02-08 13:29:06 +01:00
foreach ( $objs as $obj )
{
$$obj = & $this -> $obj ;
unset ( $this -> $obj );
}
2005-02-06 16:49:50 +01:00
}
_debug_array ( $this );
2005-02-08 13:29:06 +01:00
if ( $no_other_objs )
2005-02-06 16:49:50 +01:00
{
2005-02-08 13:29:06 +01:00
foreach ( $objs as $obj )
{
$this -> $obj = & $$obj ;
unset ( $$obj );
}
2005-02-06 16:49:50 +01:00
}
}
2005-02-13 14:03:01 +01:00
/**
* applys a function to each widget in the children tree of the template
*
2005-02-16 00:22:10 +01:00
* The function should be defined as [ & ] func ([ & ] $widget ,[ & ] $extra [, $path ])
2005-02-13 14:03:01 +01:00
* 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 ;
}
2005-02-16 00:22:10 +01:00
$path = '/' ;
2005-02-13 14:03:01 +01:00
foreach ( $this -> children as $c => $nul )
{
$child = & $this -> children [ $c ];
if ( isset ( $this -> widgets_with_children [ $child [ 'type' ]]))
{
2005-02-16 00:22:10 +01:00
$result =& $this -> tree_walk ( $child , $func , $extra , $path . $c );
2005-02-13 14:03:01 +01:00
}
else
{
2005-02-16 00:22:10 +01:00
$result =& $func ( $child , $extra , $path . $c );
2005-02-13 14:03:01 +01:00
}
2005-02-16 00:22:10 +01:00
if ( ! is_null ( $result ) || is_array ( $extra ) && isset ( $extra [ '__RETURN_NOW__' ])) break ;
2005-02-13 14:03:01 +01:00
}
return $result ;
}
/**
* applys a function to each child in the tree of a widget ( incl . the widget itself )
*
2005-02-16 00:22:10 +01:00
* The function should be defined as [ & ] func ([ & ] $widget ,[ & ] $extra [, $path ]) [] = optional
2005-02-13 14:03:01 +01:00
* 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
2005-02-16 00:22:10 +01:00
* @ param string $path path of widget in the widget - tree
2005-02-13 14:03:01 +01:00
* @ return mixed return - value of func or null if nothing returned at all
*/
2005-02-16 00:22:10 +01:00
function & tree_walk ( & $widget , $func , & $extra , $path = '' )
2005-02-13 14:03:01 +01:00
{
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 ;
}
2005-02-16 00:22:10 +01:00
$result =& $func ( $widget , $extra , $path );
if ( ! is_null ( $result ) || is_array ( $extra ) && isset ( $extra [ '__RETURN__NOW__' ]) ||
! isset ( $this -> widgets_with_children [ $widget [ 'type' ]]))
2005-02-13 14:03:01 +01:00
{
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' ]]))
{
2005-02-16 00:22:10 +01:00
$result =& $this -> tree_walk ( $child , $func , $extra , $path . '/' . $n );
2005-02-13 14:03:01 +01:00
}
else
{
2005-02-16 00:22:10 +01:00
$result =& $func ( $child , $extra , $path . '/' . $n );
2005-02-13 14:03:01 +01:00
}
2005-02-16 00:22:10 +01:00
if ( ! is_null ( $result ) || is_array ( $extra ) && isset ( $extra [ '__RETURN__NOW__' ])) return $result ;
2005-02-13 14:03:01 +01:00
}
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' ]]))
{
2005-02-16 00:22:10 +01:00
$result =& $this -> tree_walk ( $child , $func , $extra , $path . '/' . $r . $c );
2005-02-13 14:03:01 +01:00
}
else
{
2005-02-16 00:22:10 +01:00
$result =& $func ( $child , $extra , $path . '/' . $r . $c );
2005-02-13 14:03:01 +01:00
}
2005-02-16 00:22:10 +01:00
if ( ! is_null ( $result ) || is_array ( $extra ) && isset ( $extra [ '__RETURN__NOW__' ])) return $result ;
2005-02-18 23:44:42 +01:00
unset ( $child );
2005-02-13 14:03:01 +01:00
}
}
break ;
case 'template' :
if ( ! isset ( $widget [ 'obj' ]) && $widget [ 'name' ][ 0 ] != '@' )
{
2005-11-09 21:50:45 +01:00
$widget [ 'obj' ] =& new etemplate ;
2005-02-13 14:03:01 +01:00
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 ;
}
}
}
}
2005-05-23 10:00:54 +02:00