2011-08-18 20:08:40 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EGroupware - eTemplate serverside template widget
|
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Ralf Becker <RalfBecker@outdoor-training.de>
|
|
|
|
* @copyright 2002-11 by RalfBecker@outdoor-training.de
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
2011-08-18 23:56:37 +02:00
|
|
|
// allow to call direct for tests (see end of class)
|
2011-08-18 20:08:40 +02:00
|
|
|
if (!isset($GLOBALS['egw_info']))
|
|
|
|
{
|
|
|
|
$GLOBALS['egw_info'] = array(
|
|
|
|
'flags' => array(
|
|
|
|
'currentapp' => 'login',
|
2011-08-19 10:22:19 +02:00
|
|
|
'debug' => 'etemplate_widget_template',
|
2011-08-18 23:56:37 +02:00
|
|
|
)
|
2011-08-18 20:08:40 +02:00
|
|
|
);
|
|
|
|
include_once '../../header.inc.php';
|
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
|
2011-08-18 20:08:40 +02:00
|
|
|
/**
|
|
|
|
* eTemplate widget baseclass
|
|
|
|
*/
|
2011-08-18 23:56:37 +02:00
|
|
|
class etemplate_widget_template extends etemplate_widget
|
2011-08-18 20:08:40 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Cache of already read templates
|
|
|
|
*
|
|
|
|
* @var array with name => template pairs
|
|
|
|
*/
|
|
|
|
protected static $cache = array();
|
|
|
|
|
|
|
|
/**
|
2011-08-18 23:56:37 +02:00
|
|
|
* Get instance of template specified by name, template(-set) and version
|
2011-08-18 20:08:40 +02:00
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $template_set='default'
|
|
|
|
* @param string $version=''
|
|
|
|
* @param string $load_via='' use given template to load $name
|
|
|
|
* @todo Reading customized templates from database
|
2011-08-18 23:56:37 +02:00
|
|
|
* @return etemplate_widget_template|boolean false if not found
|
2011-08-18 20:08:40 +02:00
|
|
|
*/
|
2011-08-18 23:56:37 +02:00
|
|
|
public static function instance($name, $template_set='default', $version='', $load_via='')
|
2011-08-18 20:08:40 +02:00
|
|
|
{
|
|
|
|
$start = microtime(true);
|
|
|
|
if (isset(self::$cache[$name]) || !($path = self::relPath($name, $template_set, $version)))
|
|
|
|
{
|
|
|
|
if ((!$path || self::read($load_via, $template_set)) && isset(self::$cache[$name]))
|
|
|
|
{
|
2011-08-18 23:56:37 +02:00
|
|
|
//error_log(__METHOD__."('$name', '$template_set', '$version', '$load_via') read from cache");
|
2011-08-18 20:08:40 +02:00
|
|
|
return self::$cache[$name];
|
|
|
|
}
|
|
|
|
error_log(__METHOD__."('$name', '$template_set', '$version', '$load_via') template NOT found!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$reader = new XMLReader();
|
|
|
|
if (!$reader->open(EGW_SERVER_ROOT.$path)) return false;
|
|
|
|
|
|
|
|
while($reader->read())
|
|
|
|
{
|
|
|
|
if ($reader->nodeType == XMLReader::ELEMENT && $reader->name == 'template')
|
|
|
|
{
|
2011-08-18 23:56:37 +02:00
|
|
|
$template = new etemplate_widget_template($reader);
|
2011-08-18 20:08:40 +02:00
|
|
|
//echo $template->id; _debug_array($template);
|
|
|
|
|
|
|
|
self::$cache[$template->id] = $template;
|
|
|
|
|
|
|
|
if ($template->id == $name)
|
|
|
|
{
|
2011-08-18 23:56:37 +02:00
|
|
|
//error_log(__METHOD__."('$name', '$template_set', '$version', '$load_via') read in ".round(1000.0*(microtime(true)-$start),2)." ms");
|
2011-08-18 20:08:40 +02:00
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// template not found in file, should never happen
|
|
|
|
error_log(__METHOD__."('$name', '$template_set', '$version', '$load_via') template NOT found in file '$path'!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get path/URL relative to EGroupware install of a template
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param string $template_set='default'
|
|
|
|
* @param string $version=''
|
|
|
|
* @return string|boolean path of template xml file or false if not found
|
|
|
|
*/
|
|
|
|
public static function relPath($name, $template_set='default', $version='')
|
|
|
|
{
|
|
|
|
list($app, $rest) = explode('.', $name, 2);
|
|
|
|
$path = '/'.$app.'/templates/'.$template_set.'/'.$rest.'.xet';
|
|
|
|
|
|
|
|
if (file_exists(EGW_SERVER_ROOT.$path)) return $path;
|
|
|
|
|
|
|
|
if ($templateSet != 'default')
|
|
|
|
{
|
|
|
|
$path = '/'.$app.'/templates/default/'.$rest.'.xet';
|
|
|
|
|
|
|
|
if (file_exists(EGW_SERVER_ROOT.$path)) return $path;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
|
|
|
|
/**
|
2011-08-20 12:27:38 +02:00
|
|
|
* Run method on all children
|
2011-08-18 23:56:37 +02:00
|
|
|
*
|
2011-08-19 08:30:06 +02:00
|
|
|
* Reimplemented because templates can have an own namespace specified in attrs[content], NOT id!
|
2011-08-18 23:56:37 +02:00
|
|
|
*
|
|
|
|
* @param array $content
|
|
|
|
* @param array &$validated=array() validated content
|
|
|
|
* @param string $cname='' current namespace
|
2011-08-21 13:55:56 +02:00
|
|
|
* @param boolean $respect_disabled=false false (default): ignore disabled, true: method is NOT run for disabled widgets AND their children
|
2011-08-19 10:22:19 +02:00
|
|
|
* @todo handle template references containing content in id, eg. id="edit.$cont[something]"
|
2011-08-18 23:56:37 +02:00
|
|
|
*/
|
2011-08-21 13:55:56 +02:00
|
|
|
public function run($method_name, $params=array(''), $respect_disabled=false)
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2011-08-20 12:27:38 +02:00
|
|
|
$cname =& $params[0];
|
2011-08-19 08:30:06 +02:00
|
|
|
if ($this->attrs['content']) $cname = self::form_name($cname, $this->attrs['content']);
|
2011-08-18 23:56:37 +02:00
|
|
|
|
2011-08-21 13:55:56 +02:00
|
|
|
parent::run($method_name, $params, $respect_disabled);
|
2011-08-18 23:56:37 +02:00
|
|
|
}
|
2011-08-18 20:08:40 +02:00
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
|
2011-08-19 10:22:19 +02:00
|
|
|
if ($GLOBALS['egw_info']['flags']['debug'] == 'etemplate_widget_template')
|
2011-08-18 23:56:37 +02:00
|
|
|
{
|
2011-08-19 10:22:19 +02:00
|
|
|
$name = isset($_GET['name']) ? $_GET['name'] : 'timesheet.edit';
|
|
|
|
if (!($template = etemplate_widget_template::instance($name)))
|
|
|
|
{
|
|
|
|
header('HTTP-Status: 404 Not Found');
|
|
|
|
echo "<html><head><title>Not Found</title><body><h1>Not Found</h1><p>The requested eTemplate '$name' was not found!</p></body></html>\n";
|
|
|
|
exit;
|
|
|
|
}
|
2011-08-18 23:56:37 +02:00
|
|
|
header('Content-Type: text/xml');
|
|
|
|
echo $template->toXml();
|
|
|
|
}
|