New: add Savant pure php template engine

This commit is contained in:
Regis Houssin 2012-04-27 15:07:12 +02:00
parent fc468e4409
commit e543bc1ac4
11 changed files with 2292 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,125 @@
<?php
/**
*
* Provides a simple error class for Savant.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*
* @version $Id: Error.php,v 1.5 2005/05/27 14:03:50 pmjones Exp $
*
*/
/**
*
* Provides a simple error class for Savant.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
*/
class Savant3_Error {
/**
*
* The error code, typically a Savant 'ERR_*' string.
*
* @access public
*
* @var string
*
*/
public $code = null;
/**
*
* An array of error-specific information.
*
* @access public
*
* @var array
*
*/
public $info = array();
/**
*
* The error severity level.
*
* @access public
*
* @var int
*
*/
public $level = E_USER_ERROR;
/**
*
* A debug backtrace for the error, if any.
*
* @access public
*
* @var array
*
*/
public $trace = null;
/**
*
* Constructor.
*
* @access public
*
* @param array $conf An associative array where the key is a
* Savant3_Error property and the value is the value for that
* property.
*
*/
public function __construct($conf = array())
{
// set public properties
foreach ($conf as $key => $val) {
$this->$key = $val;
}
// add a backtrace
if ($conf['trace'] === true) {
$this->trace = debug_backtrace();
}
}
/**
*
* Magic method for output dump.
*
* @access public
*
* @return void
*/
public function __toString()
{
ob_start();
echo get_class($this) . ': ';
print_r(get_object_vars($this));
return ob_get_clean();
}
}
?>

View File

@ -0,0 +1,29 @@
<?php
/**
*
* Throws PHP5 exceptions for Savant.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*
* @version $Id: Exception.php,v 1.1 2005/05/27 14:04:36 pmjones Exp $
*
*/
/**
*
* A simple Savant3_Exception class.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
*/
class Savant3_Exception extends Exception {
}
?>

View File

@ -0,0 +1,85 @@
<?php
/**
*
* Abstract Savant3_Filter class.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*
* @version $Id: Filter.php,v 1.5 2005/04/29 16:23:50 pmjones Exp $
*
*/
/**
*
* Abstract Savant3_Filter class.
*
* You have to extend this class for it to be useful; e.g., "class
* Savant3_Filter_example extends Savant3_Filter".
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
*/
abstract class Savant3_Filter {
/**
*
* Optional reference to the calling Savant object.
*
* @access protected
*
* @var object
*
*/
protected $Savant = null;
/**
*
* Constructor.
*
* @access public
*
* @param array $conf An array of configuration keys and values for
* this filter.
*
* @return void
*
*/
public function __construct($conf = null)
{
settype($conf, 'array');
foreach ($conf as $key => $val) {
$this->$key = $val;
}
}
/**
*
* Stub method for extended behaviors.
*
* @access public
*
* @param string $text The text buffer to filter.
*
* @return string The text buffer after it has been filtered.
*
*/
public static function filter($text)
{
return $text;
}
}
?>

View File

@ -0,0 +1,67 @@
<?php
/**
*
* Abstract Savant3_Plugin class.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*
* @version $Id: Plugin.php,v 1.5 2005/04/29 16:23:50 pmjones Exp $
*
*/
/**
*
* Abstract Savant3_Plugin class.
*
* You have to extend this class for it to be useful; e.g., "class
* Savant3_Plugin_example extends Savant2_Plugin". Be sure to add a
* method named for the plugin itself; e.g., "function example()".
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
*/
abstract class Savant3_Plugin {
/**
*
* Reference to the calling Savant object.
*
* @access protected
*
* @var object
*
*/
protected $Savant = null;
/**
*
* Constructor.
*
* @access public
*
* @param array $conf An array of configuration keys and values for
* this plugin.
*
* @return void
*
*/
public function __construct($conf = null)
{
settype($conf, 'array');
foreach ($conf as $key => $val) {
$this->$key = $val;
}
}
}
?>

View File

@ -0,0 +1,147 @@
<?php
/**
*
* Filter to remove extra white space within the text.
*
* @package Savant3
*
* @author Monte Ohrt <monte@ispi.net>
*
* @author Contributions from Lars Noschinski <lars@usenet.noschinski.de>
*
* @author Converted to a Savant3 filter by Paul M. Jones <pmjones@ciaweb.net>
*
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*
* @version $Id: Savant3_Filter_trimwhitespace.php,v 1.4 2005/05/29 15:27:07 pmjones Exp $
*
*/
/**
*
* Filter to remove extra white space within the text.
*
* @package Savant3
*
* @author Monte Ohrt <monte@ispi.net>
*
* @author Contributions from Lars Noschinski <lars@usenet.noschinski.de>
*
* @author Converted to a Savant3 filter by Paul M. Jones <pmjones@ciaweb.net>
*
*/
class Savant3_Filter_trimwhitespace extends Savant3_Filter {
/**
*
* Removes extra white space within the text.
*
* Trim leading white space and blank lines from template source
* after it gets interpreted, cleaning up code and saving bandwidth.
* Does not affect <pre></pre>, <script></script>, or
* <textarea></textarea> blocks.
*
* @access public
*
* @param string $buffer The source text to be filtered.
*
* @return string The filtered text.
*
*/
public static function filter($buffer)
{
// Pull out the script blocks
preg_match_all("!<script[^>]+>.*?</script>!is", $buffer, $match);
$script_blocks = $match[0];
$buffer = preg_replace(
"!<script[^>]+>.*?</script>!is",
'@@@SAVANT:TRIM:SCRIPT@@@',
$buffer
);
// Pull out the pre blocks
preg_match_all("!<pre[^>]*>.*?</pre>!is", $buffer, $match);
$pre_blocks = $match[0];
$buffer = preg_replace(
"!<pre[^>]*>.*?</pre>!is",
'@@@SAVANT:TRIM:PRE@@@',
$buffer
);
// Pull out the textarea blocks
preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $buffer, $match);
$textarea_blocks = $match[0];
$buffer = preg_replace(
"!<textarea[^>]+>.*?</textarea>!is",
'@@@SAVANT:TRIM:TEXTAREA@@@',
$buffer
);
// remove all leading spaces, tabs and carriage returns NOT
// preceeded by a php close tag.
$buffer = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $buffer));
// replace script blocks
Savant3_Filter_trimwhitespace::replace(
"@@@SAVANT:TRIM:SCRIPT@@@",
$script_blocks,
$buffer
);
// replace pre blocks
Savant3_Filter_trimwhitespace::replace(
"@@@SAVANT:TRIM:PRE@@@",
$pre_blocks,
$buffer
);
// replace textarea blocks
Savant3_Filter_trimwhitespace::replace(
"@@@SAVANT:TRIM:TEXTAREA@@@",
$textarea_blocks,
$buffer
);
return $buffer;
}
/**
*
* Does a simple search-and-replace on the source text.
*
* @access protected
*
* @param string $search The string to search for.
*
* @param string $replace Replace with this text.
*
* @param string &$buffer The source text.
*
* @return string The text after search-and-replace.
*
*/
protected static function replace($search, $replace, &$buffer)
{
$len = strlen($search);
$pos = 0;
$count = count($replace);
for ($i = 0; $i < $count; $i++) {
// does the search-string exist in the buffer?
$pos = strpos($buffer, $search, $pos);
if ($pos !== false) {
// replace the search-string
$buffer = substr_replace($buffer, $replace[$i], $pos, $len);
} else {
break;
}
}
}
}
?>

View File

@ -0,0 +1,109 @@
<?php
/**
*
* Generates an <a href="">...</a> tag.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*
* @version $Id: Savant3_Plugin_ahref.php,v 1.4 2005/08/09 12:56:14 pmjones Exp $
*
*/
/**
*
* Generates an <a href="">...</a> tag.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
*/
class Savant3_Plugin_ahref extends Savant3_Plugin {
/**
*
* Generate an HTML <a href="">...</a> tag.
*
* @access public
*
* @param string|array $href A string URL for the resulting tag. May
* also be an array with any combination of the keys 'scheme',
* 'host', 'path', 'query', and 'fragment' (c.f. PHP's native
* parse_url() function).
*
* @param string $text The displayed text of the link.
*
* @param string|array $attr Any extra attributes for the <a> tag.
*
* @return string The <a href="">...</a> tag.
*
*/
public function ahref($href, $text, $attr = null)
{
$html = '<a href="';
if (is_array($href)) {
// add the HREF from an array
$tmp = '';
if (isset($href['scheme'])) {
$tmp .= $href['scheme'] . ':';
if (strtolower($href['scheme']) != 'mailto') {
$tmp .= '//';
}
}
if (isset($href['host'])) {
$tmp .= $href['host'];
}
if (isset($href['path'])) {
$tmp .= $href['path'];
}
if (isset($href['query'])) {
$tmp .= '?' . $href['query'];
}
if (isset($href['fragment'])) {
$tmp .= '#' . $href['fragment'];
}
$html .= htmlspecialchars($tmp);
} else {
// add the HREF from a scalar
$html .= htmlspecialchars($href);
}
$html .= '"';
// add attributes
if (is_array($attr)) {
// from array
foreach ($attr as $key => $val) {
$key = htmlspecialchars($key);
$val = htmlspecialchars($val);
$html .= " $key=\"$val\"";
}
} elseif (! is_null($attr)) {
// from scalar
$html .= htmlspecialchars(" $attr");
}
// set the link text, close the tag, and return
$html .= '>' . $text . '</a>';
return $html;
}
}
?>

View File

@ -0,0 +1,123 @@
<?php
/**
*
* Plugin to generate a formatted date using strftime() conventions.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*
* @version $Id: Savant3_Plugin_date.php,v 1.3 2005/03/07 14:40:16 pmjones Exp $
*
*/
/**
*
* Plugin to generate a formatted date using strftime() conventions.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
*/
class Savant3_Plugin_date extends Savant3_Plugin {
/**
*
* The default strftime() format string.
*
* @access public
*
* @var array
*
*/
public $default = '%c';
/**
*
* Custom strftime() format strings to use for dates.
*
* You can preset the format strings via Savant3::setPluginConf().
*
* <code>
* $conf = array(
* 'custom' => array(
* 'mydate' => '%Y-%m-%d',
* 'mytime' => '%R'
* )
* );
*
* $Savant->setPluginConf('date', $conf);
* </code>
*
* ... and in your template, to use a preset custom string by name:
*
* <code>
* echo $this->date($value, 'mydate');
* </code>
*
* @access public
*
* @var array
*
*/
public $custom = array(
'date' => '%Y-%m-%d',
'time' => '%H:%M:%S'
);
/**
*
* Outputs a formatted date using strftime() conventions.
*
* @access public
*
* @param string $datestring Any date-time string suitable for
* strtotime().
*
* @param string $format The strftime() formatting string, or a named
* custom string key from $this->custom.
*
* @return string The formatted date string.
*
*/
function date($datestring, $format = null)
{
settype($format, 'string');
if (is_null($format)) {
$format = $this->default;
}
// does the format string have a % sign in it?
if (strpos($format, '%') === false) {
// no, look for a custom format string
if (! empty($this->custom[$format])) {
// found a custom format string
$format = $this->custom[$format];
} else {
// did not find the custom format, revert to default
$format = $this->default;
}
}
// convert the date string to the specified format
if (trim($datestring != '')) {
return strftime($format, strtotime($datestring));
} else {
// no datestring, return VOID
return;
}
}
}
?>

View File

@ -0,0 +1,63 @@
<?php
/**
*
* Plugin to convert an associative array to a string of tag attributes.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*
* @version $Id: Savant3_Plugin_htmlAttribs.php,v 1.3 2005/09/12 17:49:27 pmjones Exp $
*
*/
/**
*
* Plugin to convert an associative array to a string of tag attributes.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
*/
class Savant3_Plugin_htmlAttribs extends Savant3_Plugin {
/**
*
* Converts an associative array to a string of tag attributes.
*
* @access public
*
* @param array $attribs From this array, each key-value pair is
* converted to an attribute name and value.
*
* @return string The XHTML for the attributes.
*
*/
public function htmlAttribs($attribs)
{
$xhtml = '';
foreach ((array) $attribs as $key => $val) {
if ($val === null) {
continue;
}
if (is_array($val)) {
$val = implode(' ', $val);
}
$key = htmlspecialchars($key);
$val = htmlspecialchars($val);
$xhtml .= " $key=\"$val\"";
}
return $xhtml;
}
}
?>

View File

@ -0,0 +1,199 @@
<?php
/**
*
* Plugin to generate an <img ... /> tag.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*
* @version $Id: Savant3_Plugin_image.php,v 1.7 2005/08/12 14:34:09 pmjones Exp $
*
*/
/**
*
* Plugin to generate an <img ... /> tag.
*
* Support for alpha transparency of PNG files in Microsoft IE added by
* Edward Ritter; thanks, Edward.
*
* @package Savant3
*
* @author Paul M. Jones <pmjones@ciaweb.net>
*
*/
class Savant3_Plugin_image extends Savant3_Plugin {
/**
*
* The document root.
*
* @access public
*
* @var string
*
*/
protected $documentRoot = null;
/**
*
* The base directory for images within the document root.
*
* @access public
*
* @var string
*
*/
protected $imageDir = null;
/**
*
* Outputs an <img ... /> tag.
*
* Microsoft IE alpha PNG support added by Edward Ritter.
*
* @access public
*
* @param string $file The path to the image on the local file system
* relative to $this->imageDir.
*
* @param string $alt Alternative descriptive text for the image;
* defaults to the filename of the image.
*
* @param int $border The border width for the image; defaults to zero.
*
* @param int $width The displayed image width in pixels; defaults to
* the width of the image.
*
* @param int $height The displayed image height in pixels; defaults to
* the height of the image.
*
* @return string An <img ... /> tag.
*
*/
public function image($file, $alt = null, $height = null, $width = null,
$attr = null)
{
// is the document root set?
if (is_null($this->documentRoot) && isset($_SERVER['DOCUMENT_ROOT'])) {
// no, so set it
$this->documentRoot = $_SERVER['DOCUMENT_ROOT'];
}
// make sure there's a DIRECTORY_SEPARATOR between the docroot
// and the image dir
if (substr($this->documentRoot, -1) != DIRECTORY_SEPARATOR &&
substr($this->imageDir, 0, 1) != DIRECTORY_SEPARATOR) {
$this->documentRoot .= DIRECTORY_SEPARATOR;
}
// make sure there's a separator between the imageDir and the
// file name
if (substr($this->imageDir, -1) != DIRECTORY_SEPARATOR &&
substr($file, 0, 1) != DIRECTORY_SEPARATOR) {
$this->imageDir .= DIRECTORY_SEPARATOR;
}
// the image file type code (PNG = 3)
$type = null;
// get the file information
$info = false;
if (strpos($file, '://') === false) {
// no "://" in the file, so it's local
$file = $this->imageDir . $file;
$tmp = $this->documentRoot . $file;
$info = @getimagesize($tmp);
} else {
// don't attempt to get file info from streams, it takes
// way too long.
$info = false;
}
// did we find the file info?
if (is_array($info)) {
// capture type info regardless
$type = $info[2];
// capture size info where both not specified
if (is_null($width) && is_null($height)) {
$width = $info[0];
$height = $info[1];
}
}
// clean up
unset($info);
// is the file a PNG? if so, check user agent, we will need to
// make special allowances for Microsoft IE.
if (stristr($_SERVER['HTTP_USER_AGENT'], 'MSIE') && $type === 3) {
// support alpha transparency for PNG files in MSIE
$html = '<span style="position: relative;';
if ($height) {
$html .= ' height: ' . $height . 'px;';
}
if ($width) {
$html .= ' width: ' . $width . 'px;';
}
$html .= ' filter:progid:DXImageTransform.Microsoft.AlphaImageLoader';
$html .= "(src='" . htmlspecialchars($file) . "',sizingMethod='scale');\"";
$html .= ' title="' . htmlspecialchars($alt) . '"';
$html .= $this->Savant->htmlAttribs($attr);
// done
$html .= '></span>';
} else {
// not IE, so build a normal image tag.
$html = '<img';
$html .= ' src="' . htmlspecialchars($file) . '"';
// add the alt attribute
if (is_null($alt)) {
$alt = basename($file);
}
$html .= ' alt="' . htmlspecialchars($alt) . '"';
// add the height attribute
if ($height) {
$html .= ' height="' . htmlspecialchars($height) . '"';
}
// add the width attribute
if ($width) {
$html .= ' width="' . htmlspecialchars($width) . '"';
}
$html .= $this->Savant->htmlAttribs($attr);
// done
$html .= ' />';
}
// done!
return $html;
}
}
?>

View File

@ -665,6 +665,14 @@ if (! defined('NOREQUIRETRAN'))
}
}
// Use php template engine
if ($conf->global->MAIN_USE_TEMPLATE_ENGINE && ! defined('NOTEMPLATEENGINE'))
{
require_once(DOL_DOCUMENT_ROOT.'/includes/savant/Savant3.php');
$tpl = new Savant3();
}
// Case forcing style from url
if (GETPOST('theme'))
{