* * -------------------------------------------- * * 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$ */ /*! @class soetemplate @author ralfbecker @abstract Storage Objects: Everything to store and retrive the eTemplates. @discussion eTemplates are stored in the db in table 'phpgw_etemplate' and gets distributed @discussion through the file 'etemplates.inc.php' in the setup dir of each app. That file gets @discussion automatically imported in the db, whenever you show a eTemplate of the app. For @discussion performace reasons the timestamp of the file is stored in the db, so 'new' @discussion eTemplates need to have a newer file. The distribution-file is generated with the @discussion function dump, usually by pressing a button in the editor. @discussion writeLangFile writes an lang-file with all Labels, incorporating an existing one. @discussion Beside a name eTemplates use the following keys to find the most suitable template @discussion for an user (in order of precedence): @discussion 1) User-/Group-Id (not yet implemented) @discussion 2) preferd languages of the user (templates for all langs have $lang='') @discussion 3) selected template: verdilak, ... (the default is called '' in the db, not default) @discussion 4) a version-number of the form, eg: '0.9.13.001' (filled up with 0 same size) */ class soetemplate { var $public_functions = array( 'init' => True, 'empty_cell' => True, 'new_cell' => True, 'read' => True, 'search' => True, 'save' => True, 'delete' => True, 'dump2setup' => True, 'import_dump' => True, 'writeLangFile' => True ); var $debug; // =1 show some debug-messages, = 'app.name' show messages only for eTemplate 'app.name' 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 $size; // witdh,height,border of table var $style; // embeded CSS style-sheet var $db,$db_name = 'phpgw_etemplate'; // DB name 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', 'et_style' => 'style', 'et_modified' => 'modified' ); var $db_cols; /*! @function soetemplate @abstract constructor of the class @syntax soetemplate($name='',$template='',$lang='',$group=0,$version='',$rows=2,$cols=2) @param as read */ function soetemplate($name='',$template='',$lang='',$group=0,$version='',$rows=2,$cols=2) { $this->db = $GLOBALS['phpgw']->db; $this->db_cols = $this->db_key_cols + $this->db_data_cols; if (empty($name)) { $this->init($name,$template,$lang,$group,$version,$rows,$cols); } else { $this->read($name,$template,$lang,$group,$version,$rows,$cols); } } /*! @function num2chrs @abstract generates column-names from index: 'A', 'B', ..., 'AA', 'AB', ..., 'ZZ' (not more!) @syntax num2chrs($num) @param $num index to generate name from 1 => 'A' @result the name */ 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; } /*! @function empty_cell @abstracts constructor for a new / empty cell (nothing fancy so far) @syntax empty_cell() @result the cell */ function empty_cell($type='label',$name='') { return array( 'type' => $type, 'name' => $name, ); } /*! @function new_cell @abstract constructs a new cell in a give row or the last row, not existing rows will be created @syntax new_cell( $row=False ) @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 @returns a reference to the new cell, use $new_cell = &$tpl->new_cell(); (!) */ 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; } /*! @function set_rows_cols() @abstract initialises rows & cols from the size of the data-array @syntax set_rows_cols() */ function set_rows_cols() { $this->rows = count($this->data) - 1; $this->cols = count($this->data[1]); // 1 = first row, not 0 } /*! @function init @abstract initialises all internal data-structures of the eTemplate and sets the keys @syntax init($name='',$template='',$lang='',$group=0,$version='',$rows=1,$cols=1) @param $name name of the eTemplate or array with the keys or all data @param $template,$lang,$group,$version see class @param $rows,$cols initial size of the template */ function init($name='',$template='',$lang='',$group=0,$version='',$rows=1,$cols=1) { reset($this->db_cols); while (list($db_col,$col) = each($this->db_cols)) { $this->$col = is_array($name) ? $name[$col] : $$col; } if ($this->template == 'default') { $this->template = ''; } if ($this->lang == 'default') { $this->lang = ''; } $this->tpls_in_file = is_array($name) ? $name['tpls_in_file'] : 0; if (is_array($name) && isset($name['data'])) { $this->set_rows_cols(); return; // data already set } $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) { for ($col = 0; $col < $cols; ++$col) { $this->data[$row][$this->num2chrs($col)] = $this->empty_cell(); } } } /*! @function read @abstract Reads an eTemplate from the database @syntax read($name,$template='default',$lang='default',$group=0,$version='') @param as discripted with the class, with the following exeptions @param $template as '' loads the prefered template 'default' loads the default one '' in the db @param $lang as '' loads the pref. lang 'default' loads the default one '' in the db @param $group is NOT used / implemented yet @result True if a fitting template is found, else False */ function read($name,$template='default',$lang='default',$group=0,$version='') { $this->init($name,$template,$lang,$group,$version); if ($this->debug == 1 || $this->debug == $this->name) { echo "
soetemplate::read('$this->name','$this->template','$this->lang',$this->group,'$this->version')
\n"; } if (($GLOBALS['phpgw_info']['server']['eTemplate-source'] == 'files' || $GLOBALS['phpgw_info']['server']['eTemplate-source'] == 'xslt') && $this->readfile()) { return True; } if ($this->name) { $this->test_import($this->name); // import updates in setup-dir } $pref_lang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']; $pref_templ = $GLOBALS['phpgw_info']['server']['template_set']; $sql = "SELECT * FROM $this->db_name WHERE et_name='".$this->db->db_addslashes($this->name)."' AND "; if (is_array($name)) { $template = $name['template']; } if ($template == 'default') { $sql .= "(et_template='".$this->db->db_addslashes($pref_templ)."' OR et_template='')"; } else { $sql .= "et_template='".$this->db->db_addslashes($this->template)."'"; } $sql .= ' AND '; if (is_array($name)) { $lang = $name['lang']; } if ($lang == 'default' || $name['lang'] == 'default') { $sql .= "(et_lang='".$this->db->db_addslashes($pref_lang)."' OR et_lang='')"; } else { $sql .= "et_lang='".$this->db->db_addslashes($this->lang)."'"; } if ($this->version != '') { $sql .= "AND et_version='".$this->db->db_addslashes($this->version)."'"; } $sql .= " ORDER BY et_lang DESC,et_template DESC,et_version DESC"; if ($this->debug == $this->name) { echo "soetemplate::read: sql='$sql'
\n"; } $this->db->query($sql,__LINE__,__FILE__); if (!$this->db->next_record()) { $version = $this->version; return $this->readfile() && (empty($version) || $version == $this->version); } $this->db2obj(); return True; } /*! @function readfile @abstract Reads an eTemplate from the filesystem, the keys are already set by init in read @syntax readfile() @result True if a template is found, else False */ function readfile() { list($app,$name) = split("\.",$this->name,2); $template = $this->template == '' ? 'default' : $this->template; if ($this->lang) { $lang = '.' . $this->lang; } $first_try = $ext = $GLOBALS['phpgw_info']['server']['eTemplate-source'] == 'xslt' ? '.xsl' : '.xet'; while ((!$lang || !@file_exists($file = PHPGW_SERVER_ROOT . "/$app/templates/$template/$name$lang$ext") && !@file_exists($file = PHPGW_SERVER_ROOT . "/$app/templates/default/$name$lang$ext")) && !@file_exists($file = PHPGW_SERVER_ROOT . "/$app/templates/$template/$name$ext") && !@file_exists($file = PHPGW_SERVER_ROOT . "/$app/templates/default/$name$ext")) { if ($ext == $first_try) { $ext = $ext == '.xet' ? '.xsl' : '.xet'; if ($this->debug == 1 || $this->name != '' && $this->debug == $this->name) { echo "tried '$file' now trying it with extension '$ext' !!!
\n"; } } else { break; } } if ($this->name == '' || $app == '' || $name == '' || !@file_exists($file) || !($f = @fopen($file,'r'))) { if ($this->debug == 1 || $this->name != '' && $this->debug == $this->name) { echo "Can't open template '$this->name' / '$file' !!!
\n"; } return False; } $xml = fread ($f, filesize ($file)); fclose($f); if ($ext == '.xsl') { $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; } else { if (!is_object($this->xul_io)) { $this->xul_io = CreateObject('etemplate.xul_io'); } $loaded = $this->xul_io->import(&$this,$xml); if (!is_array($loaded)) { return False; } $this->name = $app . '.' . $name; // if template was copied or app was renamed $this->tpls_in_file = count($loaded); } return True; } /*! @function search @syntax search($name,$template='default',$lang='default',$group=0,$version='') @author ralfbecker @abstract Lists the eTemplates matching the given criteria @param as discripted with the class, with the following exeptions @param $template as '' loads the prefered template 'default' loads the default one '' in the db @param $lang as '' loads the pref. lang 'default' loads the default one '' in the db @param $group is NOT used / implemented yet @result array of arrays with the template-params */ function search($name,$template='default',$lang='default',$group=0,$version='') { if ($this->name) { $this->test_import($this->name); // import updates in setup-dir } $pref_lang = $GLOBALS['phpgw_info']['user']['preferences']['common']['lang']; $pref_templ = $GLOBALS['phpgw_info']['server']['template_set']; if (is_array($name)) { $template = $name['template']; $lang = $name['lang']; $group = $name['group']; $version = $name['version']; $name = $name['name']; } $sql = "SELECT et_name,et_template,et_lang,et_group,et_version FROM $this->db_name WHERE et_name LIKE '".$this->db->db_addslashes($name)."%'"; if ($template != '' && $template != 'default') { $sql .= " AND et_template LIKE '".$this->db->db_addslashes($template)."%'"; } if ($lang != '' && $lang != 'default') { $sql .= " AND et_lang LIKE '".$this->db->db_addslashes($lang)."%'"; } if ($this->version != '') { $sql .= " AND et_version LIKE '".$this->db->db_addslashes($version)."%'"; } $sql .= " ORDER BY et_name DESC,et_lang DESC,et_template DESC,et_version DESC"; $tpl = new soetemplate; $tpl->db->query($sql,__LINE__,__FILE__); $result = array(); while ($tpl->db->next_record()) { if ($tpl->db->f('et_lang') != '##') // exclude or import-time-stamps { $tpl->db2obj(); $result[] = $tpl->as_array(); } } if ($this->debug) { echo "soetemplate::search('$name') sql='$sql'
\n\n"; print_r($result); echo "\n"; } return $result; } /*! @function db2obj @abstract copies all cols into the obj and unserializes the data-array @syntax db2obj() */ function db2obj() { for (reset($this->db_cols); list($db_col,$name) = each($this->db_cols); ) { $this->$name = $this->db->f($db_col); } $this->data = unserialize(stripslashes($this->data)); if (!is_array($this->data)) $this->data = array(); if ($this->name[0] != '.') { 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
soetemplate::save('$this->name','$this->template','$this->lang',$this->group,'$this->version')
\n"; } $this->delete(); // so we have always a new insert if ($this->name[0] != '.') // correct up old messed up templates { 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