egroupware/phpgwapi/inc/class.xslttemplates.inc.php

319 lines
8.1 KiB
PHP
Raw Normal View History

2002-09-21 10:42:46 +02:00
<?php
2002-10-26 02:33:22 +02:00
/*******************************************************************\
* phpGroupWare API - XSLT Template class *
* Written by Dan Kuykendall <seek3r@phpgroupware.org> *
* and Bettina Gille [ceb@phpgroupware.org] *
* and Ralf Becker <ralfbecker@outdoor-training.de> *
2002-10-26 02:33:22 +02:00
* Copyright (C) 2002 Dan Kuykendall, Bettina Gille, Ralf Becker *
* ----------------------------------------------------------------- *
* This library is part of the phpGroupWare API *
* ----------------------------------------------------------------- *
* This library 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. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
\*******************************************************************/
/* $Id$ */
if (!extension_loaded('xslt'))
{
if (PHP_OS == 'Windows' || PHP_OS == 'OS/2')
{
dl('php_xslt.dll');
}
else
{
dl('xslt.so');
}
}
2002-09-21 10:42:46 +02:00
require_once('class.xmltool.inc.php');
2002-09-22 00:00:07 +02:00
class xslttemplates
2002-09-21 10:42:46 +02:00
{
var $rootdir = '';
var $prev_rootdir = '';
/* The xslfiles will be loaded up and merged into $xsldata */
var $xslfiles = Array();
var $xsldata = '';
/* Users can set $vars which will be converted into xmldata before xsl processing */
/* Or they can generate their own XML data and set it directly when they have */
/* need for a more robust schema */
var $vars = Array();
var $xmlvars = Array();
var $xmldata = '';
2002-09-22 00:00:07 +02:00
function xslttemplates($root = '.')
2002-09-21 10:42:46 +02:00
{
if(@isset($GLOBALS['phpgw_info']['flags']['printview']) && $GLOBALS['phpgw_info']['flags']['printview'] == True)
{
$this->print = True;
}
$this->set_root($root);
}
2002-09-22 00:00:07 +02:00
2002-09-23 01:03:57 +02:00
function halt($msg)
{
echo $msg;
exit;
}
2002-09-21 10:42:46 +02:00
function set_root($rootdir)
{
if (!is_dir($rootdir))
{
$this->halt('set_root: '.$rootdir.' is not a directory.');
return False;
}
$this->prev_rootdir = $this->rootdir;
$this->rootdir = $rootdir;
return True;
}
function reset_root()
{
$this->rootdir = $this->prev_rootdir;
}
2002-09-23 04:35:25 +02:00
function add_file($filename,$rootdir='',$time=1)
2002-09-21 10:42:46 +02:00
{
if (!is_array($filename))
{
if($rootdir=='')
{
$rootdir=$this->rootdir;
}
2002-09-23 04:35:25 +02:00
2002-11-15 23:31:51 +01:00
if (substr($filename, 0, 1) != SEP && substr($filename, 1, 1) != ':')
2002-09-21 10:42:46 +02:00
{
2002-11-07 00:20:02 +01:00
$new_filename = $rootdir . SEP . $filename;
2002-09-21 10:42:46 +02:00
}
else
{
$new_filename = $filename;
}
2002-09-23 04:35:25 +02:00
2002-09-21 10:42:46 +02:00
if ($this->print && $time!=2 && $time!=4)
{
$new_filename = $new_filename.'_print';
}
2002-09-23 04:35:25 +02:00
// echo 'Rootdir: '.$rootdir.'<br>'."\n".'Filename: '.$filename.'<br>'."\n".'New Filename: '.$new_filename.'<br>'."\n";
2002-09-21 10:42:46 +02:00
if (!file_exists($new_filename.'.xsl'))
{
switch($time)
{
case 2:
2002-09-23 04:35:25 +02:00
$new_root = str_replace($GLOBALS['phpgw_info']['server']['template_set'],'default',$rootdir);
$this->add_file($filename,$new_root,3);
return;
2002-09-21 10:42:46 +02:00
break;
case 3:
2002-09-23 04:35:25 +02:00
$this->add_file($filename,$rootdir,4);
return;
2002-09-21 10:42:46 +02:00
break;
case 4:
$this->halt("filename: file $new_filename.xsl does not exist.");
break;
default:
if (!$this->print)
{
2002-09-23 04:35:25 +02:00
$new_root = str_replace($GLOBALS['phpgw_info']['server']['template_set'],'default',$rootdir);
$this->add_file($filename,$new_root,4);
return;
2002-09-21 10:42:46 +02:00
}
else
{
2002-09-23 04:35:25 +02:00
$this->add_file($filename,$rootdir,2);
return;
2002-09-21 10:42:46 +02:00
}
}
}
2002-09-23 04:35:25 +02:00
else
{
$this->xslfiles[$filename] = $new_filename.'.xsl';
}
2002-09-21 10:42:46 +02:00
}
else
{
reset($filename);
while(list(,$file) = each($filename))
{
$this->add_file($file);
}
}
}
2002-10-26 02:33:22 +02:00
function set_var($name, $value, $append = True)
2002-09-21 10:42:46 +02:00
{
if($append)
2002-09-21 10:42:46 +02:00
{
if (is_array($value))
{
while(list($key,$val) = each($value))
{
2002-10-26 22:33:04 +02:00
if (!is_array($val) && !is_array($this->vars[$name][$key]))
{
$this->vars[$name][$key] .= $val;
}
else
{
$this->vars[$name][$key] = array_merge($this->vars[$name][$key],$val);
}
}
}
2002-10-25 22:34:36 +02:00
//_debug_array($this->vars);
2002-09-21 10:42:46 +02:00
}
else
{
$this->vars[$name] = $value;
}
}
function set_xml($xml, $append = False)
{
if(!$append)
{
$this->xmlvars = $xml;
}
else
{
2002-09-23 04:35:25 +02:00
$this->xmlvars .= $xml;
2002-09-21 10:42:46 +02:00
}
}
function get_var($name)
{
return $this->vars[$name];
}
function get_vars()
{
return $this->vars;
}
function get_xml()
{
return $this->xmlvars;
}
function xsl_parse()
{
if(count($this->xslfiles) > 0)
{
2002-10-19 02:17:02 +02:00
$this->xsldata = '<?xml version="1.0" encoding="' . lang('charset') . '"?>'."\n";
2002-09-21 10:42:46 +02:00
$this->xsldata .= '<!DOCTYPE xsl:stylesheet ['."\n";
$this->xsldata .= '<!ENTITY nl "&#10;">'."\n";
$this->xsldata .= '<!ENTITY nbsp "&#160;">'."\n";
$this->xsldata .= ']>'."\n";
$this->xsldata .= '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">'."\n";
2002-10-19 02:17:02 +02:00
$this->xsldata .= '<xsl:output method="html" version="1.0" encoding="' . lang('charset') . '" indent="yes" omit-xml-declaration="yes" doctype-public="-//W3C/DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" standalone="yes" media-type="text/html"/>'."\n";
2002-09-21 10:42:46 +02:00
$this->xsldata .= '<xsl:template match="/">'."\n";
$this->xsldata .= "\t".'<xsl:apply-templates select="PHPGW"/>'."\n";
$this->xsldata .= '</xsl:template>'."\n";
2002-09-23 04:35:25 +02:00
reset($this->xslfiles);
while(list($dummy,$xslfile) = each($this->xslfiles))
2002-09-21 10:42:46 +02:00
{
2002-09-23 04:35:25 +02:00
// echo 'XSLFILES: '.$dummy.'<br>'."\n".'XSL File: '.$xslfile.'<br>'."\n";
2002-09-21 10:42:46 +02:00
$fd = fopen ($xslfile, "r");
$this->xsldata .= fread($fd, filesize($xslfile));
fclose ($fd);
}
$this->xsldata .= '</xsl:stylesheet>'."\n";
}
else
{
echo 'Error: No XSL files have been selected';
exit;
}
return $this->xsldata;
}
function xml_parse()
{
$this->xmldata = '';
$xmlvars = $this->xmlvars;
$xmldata = $this->vars;
/* auto generate xml based on vars */
while(list($key,$value) = each($xmlvars))
{
$xmldata[$key] = $value;
}
//$tmpxml_object = var2xml('PHPGW',$xmldata);
//$this->xmldata = $tmpxml_object->dump_mem();
//return $this->xmldata;
$this->xmldata = var2xml('PHPGW',$xmldata);
return $this->xmldata;
}
2002-10-02 02:44:22 +02:00
function list_lineno($xml)
{
$xml = explode("\n",$xml);
echo "<pre>\n";
for ($n=1; isset($xml[$n]); ++$n)
{
echo "$n: ".htmlentities($xml[$n])."\n";
}
echo "</pre>\n";
}
2002-09-21 10:42:46 +02:00
function parse($parsexsl = True, $parsexml = True)
{
if($parsexsl)
{
$this->xsl_parse();
}
if($parsexml)
{
$this->xml_parse();
}
$xsltproc = xslt_create();
$minor = explode(".",phpversion());
if($minor[1] >= 1) // PHP 4.1.x -- preferred
{
$arguments = array('/_xml' => $this->xmldata, '/_xsl' => $this->xsldata);
$html = xslt_process($xsltproc,'arg:/_xml','arg:/_xsl',NULL,$arguments);
}
2002-11-07 00:20:02 +01:00
else /* PHP 4.0.6 -- works okay
no, it doesnt work at all :) [ceb] */
2002-09-21 10:42:46 +02:00
{
xslt_process($this->xsldata, $this->xmldata,$html);
}
2002-10-02 02:44:22 +02:00
if (!$html)
{
echo "<p>xml-data = "; $this->list_lineno($this->xmldata);
echo "<p>xsl-data = "; $this->list_lineno($this->xsldata);
die(/*$this->xsldata.*/"\n\n XSLT processing error: ".xslt_error($xsltproc));
}
2002-09-21 10:42:46 +02:00
xslt_free($xsltproc);
return $html;
}
function pparse()
{
print $this->parse();
return False;
}
function pp()
{
return $this->pparse();
}
}
?>