123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379 |
- <?PHP
- /**
- * patTemplate
- *
- * $Id: patTemplate.php 1347 2009-12-03 21:06:36Z francois $
- *
- * powerful templating engine
- *
- * @version 3.0.0
- * @package patTemplate
- * @author Stephan Schmidt <schst@php.net>
- * @license LGPL
- * @link http://www.php-tools.net
- */
- /**
- * template already exists
- */
- define( 'PATTEMPLATE_ERROR_TEMPLATE_EXISTS', 5010 );
- /**
- * template does not exist
- */
- define ( 'PATTEMPLATE_WARNING_NO_TEMPLATE', 5011 );
- /**
- * unknown type
- */
- define ( 'PATTEMPLATE_WARNING_UNKNOWN_TYPE', 5012 );
- /**
- * base class for module could not be found
- */
- define( 'PATTEMPLATE_ERROR_BASECLASS_NOT_FOUND', 5050 );
- /**
- * module could not be found
- */
- define( 'PATTEMPLATE_ERROR_MODULE_NOT_FOUND', 5051 );
- /**
- * array expected
- */
- define( 'PATTEMPLATE_ERROR_EXPECTED_ARRAY', 5052 );
- /**
- * No input
- */
- define( 'PATTEMPLATE_ERROR_NO_INPUT', 6000 );
- /**
- * patTemplate
- *
- * powerful templating engine
- *
- * @version 3.0.0
- * @package patTemplate
- * @author Stephan Schmidt <schst@php.net>
- * @license LGPL
- * @link http://www.php-tools.net
- */
- class patTemplate
- {
- /**
- * standard system vars that identify pat tools
- * @var array
- */
- var $_systemVars = array(
- 'appName' => 'patTemplate',
- 'appVersion' => '3.0.0',
- 'author' => array(
- 'Stephan Schmidt <schst@php.net>'
- )
- );
- /**
- * default attributes for new templates
- * @access private
- * @var array
- */
- var $_defaultAttributes = array(
- 'type' => 'standard',
- 'visibility' => 'visible',
- 'loop' => 1,
- 'unusedvars' => 'strip',
- 'whitespace' => 'keep',
- 'autoclear' => 'off',
- 'autoload' => 'on'
- );
- /**
- * options for patTemplate
- *
- * Currently the following options are implemented:
- * - maintainBc defines, whether patTemplate should be backwards compatible.
- * This means, that you may use 'default' and 'empty' for subtemplates.
- *
- * @access private
- * @var array
- */
- var $_options = array(
- 'startTag' => '{',
- 'endTag' => '}',
- 'root' => '.',
- 'namespace' => 'patTemplate',
- 'maintainBc' => true
- );
- /**
- * start tag
- *
- * @access private
- * @var string
- */
- var $_startTag = '{';
- /**
- * end tag
- *
- * @access private
- * @var string
- */
- var $_endTag = '}';
- /**
- * loaded modules
- *
- * Modules are:
- * - Readers
- * - Caches
- * - Variable modifiers
- * - Filters
- *
- * @access private
- * @var array
- */
- var $_modules = array();
- /**
- * directories, where modules can be stored
- * @access private
- * @var array
- */
- var $_moduleDirs = array();
- /**
- * stores all template names
- * @access private
- * @var array
- */
- var $_templateList = array();
- /**
- * stores all template data
- * @access private
- * @var array
- */
- var $_templates = array();
- /**
- * stores all global variables
- * @access private
- * @var array
- */
- var $_globals = array();
- /**
- * stores all local variables
- * @access private
- * @var array
- */
- var $_vars = array();
- /**
- * stores the name of the first template that has been
- * found
- *
- * @access private
- * @var string
- */
- var $_root;
- /**
- * output filters that should be used
- *
- * @access private
- * @var array
- */
- var $_outputFilters = array();
- /**
- * input filters that should be used
- *
- * @access private
- * @var array
- */
- var $_inputFilters = array();
- /**
- * template cache, that should be used
- *
- * @access private
- * @var array
- */
- var $_tmplCache = null;
- /**
- * Create a new patTemplate instance.
- *
- * The constructor accepts the type of the templates as sole parameter.
- * You may choose one of:
- * - html (default)
- * - tex
- *
- * The type influences the tags you are using in your templates.
- *
- * @access public
- * @param string type (either html or tex)
- */
- function patTemplate( $type = 'html' )
- {
- if ( !defined( 'PATTEMPLATE_INCLUDE_PATH' ) )
- define( 'PATTEMPLATE_INCLUDE_PATH', dirname( __FILE__ ) . '/patTemplate' );
- $this->setType( $type );
- }
- /**
- * sets an option
- *
- * Currently, the following options are supported
- * - maintainBc (true|false)
- * - namespace (string)
- *
- * @access public
- * @param string option to set
- * @param string value of the option
- */
- function setOption( $option, $value )
- {
- $this->_options[$option] = $value;
- }
- /**
- * gets an option
- *
- * @access public
- * @param string option to get
- * @return mixed value of the option
- */
- function getOption( $option )
- {
- if ( !isset( $this->_options[$option] ) )
- return null;
- return $this->_options[$option];
- }
- /**
- * sets name of directory where templates are stored
- *
- * @access public
- * @param string dir where templates are stored
- * @deprecated please use patTemplate::setRoot() instead
- */
- function setBasedir( $basedir )
- {
- $this->_options['root'] = $basedir;
- }
- /**
- * sets root base for the template
- *
- * The parameter depends on the reader you are using.
- *
- * @access public
- * @param string root base of the templates
- */
- function setRoot( $root )
- {
- $this->_options['root'] = $root;
- }
- /**
- * gets name of root base for the templates
- *
- * @access public
- * @return mixed root base
- */
- function getRoot()
- {
- return $this->_options['root'];
- }
- /**
- * sets namespace of patTemplate tags
- *
- * @access public
- * @param string namespace
- */
- function setNamespace( $ns )
- {
- $this->_options['namespace'] = $ns;
- }
- /**
- * gets namespace of patTemplate tags
- *
- * @access public
- * @return string namespace
- */
- function getNamespace()
- {
- return $this->_options['namespace'];
- }
- /**
- * set default attribute
- *
- * @access public
- * @param string attribute name
- * @param mixed attribute value
- */
- function setDefaultAttribute( $name, $value )
- {
- $this->_defaultAttributes[$name] = $value;
- }
- /**
- * set default attributes
- *
- * @access public
- * @param array attributes
- */
- function setDefaultAttributes( $attributes )
- {
- $this->_defaultAttributes = array_merge( $this->_defaultAttributes, $attributes );
- }
- /**
- * get default attributes
- *
- * @access public
- * @return return default attributes
- */
- function getDefaultAttributes()
- {
- return $this->_defaultAttributes;
- }
- /**
- * set the type for the templates
- *
- * @access public
- * @param string type (html or tex)
- * @return boolean true on success
- */
- function setType( $type )
- {
- switch( strtolower( $type ) )
- {
- case "tex":
- $this->setTags( '<{', '}>' );
- break;
- case "html":
- $this->setTags( '{', '}' );
- break;
- default:
- return patErrorManager::raiseWarning(
- PATTEMPLATE_WARNING_UNKNOWN_TYPE,
- "Unknown type '$type'. Please use 'html' or 'tex'."
- );
- }
- return true;
- }
- /**
- * set the start and end tag for variables
- *
- * @access public
- * @param string start tag
- * @param string end tag
- * @return boolean true on success
- */
- function setTags( $startTag, $endTag )
- {
- $this->_options['startTag'] = $startTag;
- $this->_options['endTag'] = $endTag;
- $this->_startTag = $startTag;
- $this->_endTag = $endTag;
- return true;
- }
- /**
- * get start tag for variables
- *
- * @access public
- * @return string start tag
- */
- function getStartTag()
- {
- return $this->_options['startTag'];
- }
- /**
- * get end tag for variables
- *
- * @access public
- * @return string end tag
- */
- function getEndTag()
- {
- return $this->_options['endTag'];
- }
- /**
- * add a directory where patTemplate should search for
- * modules.
- *
- * You may either pass a string or an array of directories.
- *
- * patTemplate will be searching for a module in the same
- * order you added them. If the module cannot be found in
- * the custom folders, it will look in
- * patTemplate/$moduleType.
- *
- * @access public
- * @param string module type
- * @param string|array directory or directories to search.
- */
- function addModuleDir( $moduleType, $dir )
- {
- if ( !isset( $this->_moduleDirs[$moduleType] ) )
- $this->_moduleDirs[$moduleType] = array();
- if ( is_array( $dir ) )
- $this->_moduleDirs[$moduleType] = array_merge( $this->_moduleDirs[$moduleType], $dir );
- else
- array_push( $this->_moduleDirs[$moduleType], $dir );
- }
- /**
- * Sets an attribute of a template
- *
- * supported attributes: visibilty, loop, parse, unusedvars
- *
- * @param string $template name of the template
- * @param string $attribute name of the attribute
- * @param mixed $value value of the attribute
- * @access public
- * @see setAttributes(),getAttribute(), clearAttribute()
- */
- function setAttribute( $template, $attribute, $value )
- {
- $template = strtolower( $template );
- if ( !isset( $this->_templates[$template] ) )
- {
- return patErrorManager::raiseWarning(
- PATTEMPLATE_WARNING_NO_TEMPLATE,
- "Template '$template' does not exist."
- );
- }
- $attribute = strtolower( $attribute );
- $this->_templates[$template]['attributes'][$attribute] = $value;
- return true;
- }
- /**
- * Sets several attribute of a template
- *
- * $attributes has to be a assotiative arrays containing attribute/value pairs
- * supported attributes: visibilty, loop, parse, unusedvars
- *
- * @param string $template name of the template
- * @param array $attributes attribute/value pairs
- * @access public
- * @see setAttribute(), getAttribute(), clearAttribute()
- */
- function setAttributes( $template, $attributes )
- {
- if ( !is_array( $attributes ) )
- {
- return patErrorManager::raiseError( PATTEMPLATE_ERROR_EXPECTED_ARRAY, 'patTemplate::setAttributes: Expected array as second parameter, '.gettype( $attributes ).' given' );
- }
- $template = strtolower( $template );
- $attributes = array_change_key_case( $attributes );
- if ( !isset( $this->_templates[$template] ) )
- {
- return patErrorManager::raiseWarning(
- PATTEMPLATE_WARNING_NO_TEMPLATE,
- "Template '$template' does not exist."
- );
- }
- $this->_templates[$template]['attributes'] = array_merge( $this->_templates[$template]['attributes'], $attributes );
- return true;
- }
- /**
- * Get all attributes of a template
- *
- * @param string name of the template
- * @return array attributes
- * @access public
- */
- function getAttributes( $template )
- {
- $template = strtolower( $template );
- if ( !isset( $this->_templates[$template] ) )
- {
- return patErrorManager::raiseWarning(
- PATTEMPLATE_WARNING_NO_TEMPLATE,
- "Template '$template' does not exist."
- );
- }
- return $this->_templates[$template]['attributes'];
- }
- /**
- * Gets an attribute of a template
- *
- * supported attributes: visibilty, loop, parse, unusedvars
- *
- * @param string $template name of the template
- * @param string $attribute name of the attribute
- * @return mixed value of the attribute
- * @access public
- * @see setAttribute(), setAttributes(), clearAttribute()
- */
- function getAttribute( $template, $attribute )
- {
- $template = strtolower( $template );
- $attribute = strtolower( $attribute );
- if ( !isset( $this->_templates[$template] ) )
- {
- return patErrorManager::raiseWarning(
- PATTEMPLATE_WARNING_NO_TEMPLATE,
- "Template '$template' does not exist."
- );
- }
- return $this->_templates[$template]['attributes'][$attribute];
- }
- /**
- * Clears an attribute of a template
- *
- * supported attributes: visibilty, loop, parse, unusedvars
- *
- * @param string $template name of the template
- * @param string $attribute name of the attribute
- * @access public
- * @see setAttribute(), setAttributes(), getAttribute()
- */
- function clearAttribute( $template, $attribute )
- {
- $template = strtolower( $template );
- $attribute = strtolower( $attribute );
- if ( !isset( $this->_templates[$template] ) )
- {
- return patErrorManager::raiseWarning(
- PATTEMPLATE_WARNING_NO_TEMPLATE,
- "Template '$template' does not exist."
- );
- }
- $this->_templates[$template]['attributes'][$attribute] = '';;
- return true;
- }
- /**
- * Prepare a template
- *
- * This can be used if you want to add variables to
- * a template, that has not been loaded yet.
- *
- * @access public
- * @param string template name
- */
- function prepareTemplate( $name )
- {
- $name = strtolower( $name );
- if ( !isset( $this->_vars[$name] ) )
- {
- $this->_vars[$name] = array(
- 'scalar' => array(),
- 'rows' => array()
- );
- }
- }
- /**
- * add a variable to a template
- *
- * A variable may also be an indexed array, but _not_
- * an associative array!
- *
- * @access public
- * @param string $template name of the template
- * @param string $varname name of the variable
- * @param mixed $value value of the variable
- */
- function addVar( $template, $varname, $value )
- {
- $template = strtolower( $template );
- $varname = strtoupper( $varname );
- if ( !is_array( $value ) )
- {
- $this->_vars[$template]['scalar'][$varname] = $value;
- return true;
- }
- $cnt = count( $value );
- for ( $i = 0; $i < $cnt; $i++ )
- {
- if ( !isset( $this->_vars[$template]['rows'][$i] ) )
- $this->_vars[$template]['rows'][$i] = array();
- $this->_vars[$template]['rows'][$i][$varname] = $value[$i];
- }
- return true;
- }
- /**
- * get the value of a variable
- *
- * @access public
- * @param string name of the template
- * @param string name of the variable
- * @return string value of the variable, null if the variable is not set
- */
- function getVar( $template, $varname )
- {
- $template = strtolower( $template );
- $varname = strtoupper( $varname );
- if ( isset( $this->_vars[$template]['scalar'][$varname] ) )
- return $this->_vars[$template]['scalar'][$varname];
- $value = array();
- $cnt = count( $this->_vars[$template]['rows'] );
- for ( $i = 0; $i < $cnt; $i++ )
- {
- if ( !isset( $this->_vars[$template]['rows'][$i][$varname] ) )
- continue;
- array_push( $value, $this->_vars[$template]['rows'][$i][$varname] );
- }
- if ( !empty( $value ) )
- return $value;
- return null;
- }
- /**
- * Adds several variables to a template
- *
- * Each Template can have an unlimited amount of its own variables
- * $variables has to be an assotiative array containing variable/value pairs
- *
- * @param string $template name of the template
- * @param array $variables assotiative array of the variables
- * @param string $prefix prefix for all variable names
- * @access public
- * @see addVar(), addRows(), addGlobalVar(), addGlobalVars()
- */
- function addVars( $template, $variables, $prefix = '' )
- {
- $template = strtolower( $template );
- $prefix = strtoupper( $prefix );
- $variables = array_change_key_case( $variables, CASE_UPPER );
- foreach ( $variables as $varname => $value )
- {
- $varname = $prefix.$varname;
- if ( !is_array( $value ) ) {
- if (!is_scalar($value)) {
- continue;
- }
- $this->_vars[$template]['scalar'][$varname] = $value;
- continue;
- }
- $cnt = count( $value );
- for ( $i = 0; $i < $cnt; $i++ )
- {
- if ( !isset( $this->_vars[$template]['rows'][$i] ) )
- $this->_vars[$template]['rows'][$i] = array();
- $this->_vars[$template]['rows'][$i][$varname] = $value[$i];
- }
- }
- }
- /**
- * Adds several rows of variables to a template
- *
- * Each Template can have an unlimited amount of its own variables
- * Can be used to add a database result as variables to a template
- *
- * @param string $template name of the template
- * @param array $rows array containing assotiative arrays with variable/value pairs
- * @param string $prefix prefix for all variable names
- * @access public
- * @see addVar(), addVars(), addGlobalVar(), addGlobalVars()
- */
- function addRows( $template, $rows, $prefix = '' )
- {
- $template = strtolower( $template );
- $prefix = strtoupper( $prefix );
- $cnt = count( $rows );
- for ( $i = 0; $i < $cnt; $i++ )
- {
- if ( !isset( $this->_vars[$template]['rows'][$i] ) )
- $this->_vars[$template]['rows'][$i] = array();
- $rows[$i] = array_change_key_case( $rows[$i], CASE_UPPER );
- foreach ( $rows[$i] as $varname => $value )
- {
- $this->_vars[$template]['rows'][$i][$prefix.$varname] = $value;
- }
- }
- }
- /**
- * Adds an object to a template
- *
- * All properties of the object will be available as template variables.
- *
- * @param string name of the template
- * @param object|array object or array of objects
- * @param string prefix for all variable names
- * @access public
- * @see addVar(), addRows(), addGlobalVar(), addGlobalVars()
- */
- function addObject( $template, $object, $prefix = '' )
- {
- if ( is_array( $object ) )
- {
- $rows = array();
- foreach ( $object as $o )
- array_push( $rows, get_object_vars( $o ) );
- $this->addRows( $template, $rows, $prefix );
- return true;
- }
- elseif ( is_object( $object ) )
- {
- $this->addVars( $template, get_object_vars( $object ), $prefix );
- return true;
- }
- return false;
- }
- /**
- * Adds a global variable
- *
- * Global variables are valid in all templates of this object.
- * A global variable has to be scalar, it will be converted to a string.
- *
- * @access public
- * @param string $varname name of the global variable
- * @param string $value value of the variable
- * @return boolean true on success
- * @see addGlobalVars(), addVar(), addVars(), addRows()
- */
- function addGlobalVar( $varname, $value )
- {
- $this->_globals[strtoupper( $varname )] = ( string )$value;
- return true;
- }
- /**
- * Adds several global variables
- *
- * Global variables are valid in all templates of this object.
- *
- * $variables is an associative array, containing name/value pairs of the variables.
- *
- * @access public
- * @param array $variables array containing the variables
- * @param string $prefix prefix for variable names
- * @return boolean true on success
- * @see addGlobalVar(), addVar(), addVars(), addRows()
- */
- function addGlobalVars( $variables, $prefix = '' )
- {
- $variables = array_change_key_case( $variables, CASE_UPPER );
- $prefix = strtoupper( $prefix );
- foreach ( $variables as $varname => $value )
- {
- $this->_globals[$prefix.$varname] = ( string )$value;
- }
- return true;
- }
- /**
- * get all global variables
- *
- * @access public
- * @return array global variables
- */
- function getGlobalVars()
- {
- return $this->_globals;
- }
- /**
- * checks wether a template exists
- *
- * @access public
- * @param string name of the template
- * @return boolean true, if the template exists, false otherwise
- */
- function exists( $name )
- {
- return in_array( strtolower( $name ), $this->_templateList );
- }
- /**
- * enable a template cache
- *
- * A template cache will improve performace, as the templates
- * do not have to be read on each request.
- *
- * @access public
- * @param string name of the template cache
- * @param array parameters for the template cache
- * @return boolean true on success, patError otherwise
- */
- function useTemplateCache( $cache, $params = array() )
- {
- if ( !is_object( $cache ) )
- {
- $cache = &$this->loadModule( 'TemplateCache', $cache, $params );
- }
- if ( patErrorManager::isError( $cache ) )
- return $cache;
- $this->_tmplCache = &$cache;
- return true;
- }
- /**
- * enable an output filter
- *
- * Output filters are used to modify the template
- * result before it is sent to the browser.
- *
- * They are applied, when displayParsedTemplate() is called.
- *
- * @access public
- * @param string name of the output filter
- * @param array parameters for the output filter
- * @return boolean true on success, patError otherwise
- */
- function applyOutputFilter( $filter, $params = array() )
- {
- if ( !is_object( $filter ) )
- {
- $filter = &$this->loadModule( 'OutputFilter', $filter, $params );
- }
- if ( patErrorManager::isError( $filter ) )
- return $filter;
- $this->_outputFilters[] = &$filter;
- return true;
- }
- /**
- * enable an input filter
- *
- * input filters are used to modify the template
- * stream before it is split into smaller templates-
- *
- * @access public
- * @param string name of the input filter
- * @param array parameters for the input filter
- * @return boolean true on success, patError otherwise
- */
- function applyInputFilter( $filter, $params = array() )
- {
- if ( !is_object( $filter ) )
- {
- $filter = &$this->loadModule( 'InputFilter', $filter, $params );
- }
- if ( patErrorManager::isError( $filter ) )
- return $filter;
- $this->_inputFilters[] = &$filter;
- return true;
- }
- /**
- * open a file and parse for patTemplate tags
- *
- * @access public
- * @param name of the file
- * @return true, if the template could be parsed
- * @deprecated Use patTemplate::readTemplatesFromInput() instead, as the method name is misleading
- * @see readTemplatesFromInput()
- */
- function readTemplatesFromFile( $filename )
- {
- return $this->readTemplatesFromInput( $filename, 'File' );
- }
- /**
- * open any input and parse for patTemplate tags
- *
- * @access public
- * @param string name of the input (filename, shm segment, etc.)
- * @param string driver that is used as reader, you may also pass a Reader object
- * @param array additional options that will only be used for this template
- * @param string name of the template that should be used as a container, should not be used by public
- * calls.
- * @return boolean true, if the template could be parsed, false otherwise
- */
- function readTemplatesFromInput( $input, $reader = 'File', $options = null, $parseInto = null )
- {
- if ($input === '') {
- return patErrorManager::raiseError(PATTEMPLATE_ERROR_NO_INPUT, 'No input to read has been passed.');
- }
- if ( is_array( $options ) )
- $options = array_merge( $this->_options, $options );
- else
- $options = $this->_options;
- if ( !is_null( $parseInto ) )
- $parseInto = strtolower( $parseInto );
- $templates = false;
- if ( $this->_tmplCache !== null )
- {
- /**
- * get the unique cache key
- */
- $key = $this->_tmplCache->getKey( $input, $options );
- $templates = $this->_loadTemplatesFromCache( $input, $reader, $options, $key );
- /**
- * check for error returned from cache
- */
- if ( patErrorManager::isError( $templates ) )
- return $templates;
- }
- /**
- * templates have not been loaded from cache
- */
- if ( $templates === false )
- {
- if ( !is_object( $reader ) )
- {
- $reader = &$this->loadModule( 'Reader', $reader );
- if ( patErrorManager::isError( $reader ) )
- return $reader;
- }
- $reader->setOptions( $options );
- /**
- * set the root attributes
- */
- if ( !is_null( $parseInto ) )
- {
- $attributes = $this->getAttributes( $parseInto );
- if ( !patErrorManager::isError( $attributes ) )
- {
- $reader->setRootAttributes( $attributes );
- }
- }
- $templates = $reader->readTemplates( $input );
- /**
- * check for error returned from reader
- */
- if ( patErrorManager::isError( $templates ) )
- return $templates;
- /**
- * store the
- */
- if ( $this->_tmplCache !== null )
- {
- $this->_tmplCache->write( $key, $templates );
- }
- }
- /**
- * traverse all templates
- */
- foreach ( $templates as $name => $spec )
- {
- /**
- * root template
- */
- if ( $name == '__ptroot' )
- {
- if ( $parseInto === false )
- {
- continue;
- }
- if ( !in_array( $parseInto, $this->_templateList ) )
- continue;
- $spec['loaded'] = true;
- $spec['attributes'] = $this->_templates[$parseInto]['attributes'];
- $name = $parseInto;
- }
- else
- {
- /**
- * store the name
- */
- array_push( $this->_templateList, $name );
- }
- /**
- * if this is the first template that has been loaded
- * set it as the root template
- */
- if ( $this->_root === null && is_null( $parseInto ) && isset( $spec['isRoot'] ) && $spec['isRoot'] == true )
- {
- $this->_root = $name;
- }
- /**
- * set some default values
- */
- $spec['iteration'] = 0;
- $spec['lastMode'] = 'w';
- $spec['result'] = '';
- $spec['modifyVars'] = array();
- $spec['copyVars'] = array();
- $spec['defaultVars'] = array();
- /**
- * store the template
- */
- $this->_templates[$name] = $spec;
- $this->prepareTemplate( $name );
- /**
- * store the default values of the variables
- */
- foreach ( $spec['varspecs'] as $varname => $varspec )
- {
- if ( isset( $varspec['modifier'] ) )
- {
- $this->_templates[$name]['modifyVars'][$varname] = $varspec['modifier'];
- }
- if ( isset( $varspec['copyfrom'] ) )
- {
- $this->_templates[$name]['copyVars'][$varname] = $varspec['copyfrom'];
- }
- if ( !isset( $varspec['default'] ) )
- continue;
- $this->_templates[$name]['defaultVars'][$varname] = $varspec['default'];
- if ( !is_null( $this->getVar( $name, $varname ) ) )
- continue;
- $this->addVar( $name, $varname, $varspec['default'] );
- }
- unset($this->_templates[$name]['varspecs']);
- /**
- * autoload the template
- *
- * Some error management is needed here...
- */
- if ( isset( $this->_templates[$name]['attributes']['src'] ) && $this->_templates[$name]['attributes']['autoload'] == 'on' )
- {
- if ( $this->_templates[$name]['loaded'] !== true )
- {
- if ( $this->_templates[$name]['attributes']['parse'] == 'on' )
- {
- $this->readTemplatesFromInput( $this->_templates[$name]['attributes']['src'], $this->_templates[$name]['attributes']['reader'], $options, $name );
- }
- else
- {
- $this->loadTemplateFromInput( $this->_templates[$name]['attributes']['src'], $this->_templates[$name]['attributes']['reader'], null, $name );
- }
- $this->_templates[$name]['loaded'] = true;
- }
- }
- }
- return true;
- }
- /**
- * load from template cache
- *
- * @access private
- * @param string name of the input (filename, shm segment, etc.)
- * @param string driver that is used as reader, you may also pass a Reader object
- * @param array options for the reader
- * @param string cache key
- * @return array|boolean either an array containing the templates, or false
- */
- function _loadTemplatesFromCache( $input, &$reader, $options, $key )
- {
- if ( is_object( $reader ) )
- $statName = $reader->getName();
- else
- $statName = $reader;
- $stat = &$this->loadModule( 'Stat', $statName );
- $stat->setOptions( $options );
- /**
- * get modification time
- */
- $modTime = $stat->getModificationTime( $input );
- $templates = $this->_tmplCache->load( $key, $modTime );
- return $templates;
- }
- /**
- * open any input and load content into template
- *
- * @access public
- * @param string name of the input (filename, shm segment, etc.)
- * @param string driver that is used as reader
- * @param string name of the template that should be used as a container,
- * @return boolean true, if the template could be parsed, false otherwise
- */
- function loadTemplateFromInput( $input, $reader = 'File', $options = null, $parseInto = false )
- {
- if ( is_array( $options ) )
- $options = array_merge( $this->_options, $options );
- else
- $options = $this->_options;
- if ( !is_null( $parseInto ) )
- $parseInto = strtolower( $parseInto );
- $reader = &$this->loadModule( 'Reader', $reader );
- if ( patErrorManager::isError( $reader ) )
- {
- return $reader;
- }
- $reader->setOptions($options);
- $result = $reader->loadTemplate( $input );
- if ( patErrorManager::isError( $result ) )
- {
- return $result;
- }
- $this->_templates[$parseInto]['content'] .= $result;
- $this->_templates[$parseInto]['loaded'] = true;
- return true;
- }
- /**
- * load a template that had autoload="off"
- *
- * This is needed, if you change the source of a template and want to
- * load it, after changing the attribute.
- *
- * @access public
- * @param string template name
- * @return boolean true, if template could be loaded
- */
- function loadTemplate( $template )
- {
- $template = strtolower( $template );
- if ( !isset( $this->_templates[$template] ) )
- {
- return patErrorManager::raiseWarning(
- PATTEMPLATE_WARNING_NO_TEMPLATE,
- "Template '$template' does not exist."
- );
- }
- if ( $this->_templates[$template]['loaded'] === true )
- return true;
- if ( $this->_templates[$template]['attributes']['parse'] == 'on' )
- {
- return $this->readTemplatesFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template );
- }
- else
- {
- return $this->loadTemplateFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template );
- }
- }
- /**
- * loads a patTemplate module
- *
- * Modules are located in the patTemplate folder and include:
- * - Readers
- * - Caches
- * - Variable Modifiers
- * - Filters
- * - Functions
- * - Stats
- *
- * @access public
- * @param string moduleType (Reader|TemplateCache|Modifier|OutputFilter|InputFilter)
- * @param string moduleName
- * @param array parameters for the module
- * @return object
- */
- function &loadModule( $moduleType, $moduleName, $params = array() )
- {
- if ( !isset( $this->_modules[$moduleType] ) )
- $this->_modules[$moduleType] = array();
- $sig = md5( $moduleName . serialize( $params ) );
- if ( isset( $this->_modules[$moduleType][$sig] ) )
- return $this->_modules[$moduleType][$sig];
- if ( !class_exists( 'patTemplate_Module' ) )
- {
- $file = sprintf( "%s/Module.php", $this->getIncludePath() );
- if ( !@include_once $file )
- return patErrorManager::raiseError( PATTEMPLATE_ERROR_BASECLASS_NOT_FOUND, 'Could not load module base class.' );
- }
- $baseClass = 'patTemplate_' . $moduleType;
- if ( !class_exists( $baseClass ) )
- {
- $baseFile = sprintf( "%s/%s.php", $this->getIncludePath(), $moduleType );
- if ( !@include_once $baseFile )
- return patErrorManager::raiseError( PATTEMPLATE_ERROR_BASECLASS_NOT_FOUND, "Could not load base class for $moduleType ($baseFile)." );
- }
- $moduleClass = 'patTemplate_' . $moduleType . '_' .$moduleName;
- if ( !class_exists( $moduleClass ) )
- {
- if ( isset( $this->_moduleDirs[$moduleType] ) )
- $dirs = $this->_moduleDirs[$moduleType];
- else
- $dirs = array();
- array_push( $dirs, $this->getIncludePath() .'/'. $moduleType );
- foreach ( $dirs as $dir )
- {
- $moduleFile = sprintf( "%s/%s.php", $dir, str_replace( '_', '/', $moduleName ) );
- if ( @include_once $moduleFile )
- break;
- return patErrorManager::raiseError( PATTEMPLATE_ERROR_MODULE_NOT_FOUND, "Could not load module $moduleClass ($moduleFile)." );
- }
- }
- if ( !class_exists( $moduleClass ) )
- {
- return patErrorManager::raiseError( PATTEMPLATE_ERROR_MODULE_NOT_FOUND, "Module file $moduleFile does not contain class $moduleClass." );
- }
- $this->_modules[$moduleType][$sig] = &new $moduleClass;
- if ( method_exists( $this->_modules[$moduleType][$sig], 'setTemplateReference' ) )
- {
- $this->_modules[$moduleType][$sig]->setTemplateReference( $this );
- }
- $this->_modules[$moduleType][$sig]->setParams( $params );
- return $this->_modules[$moduleType][$sig];
- }
- /**
- * checks whether a module exists.
- *
- * Modules are located in the patTemplate folder and include:
- * - Readers
- * - Caches
- * - Variable Modifiers
- * - Filters
- * - Functions
- * - Stats
- *
- * @access public
- * @param string moduleType (Reader|TemplateCache|Modifier|OutputFilter|InputFilter)
- * @param string moduleName
- * @return boolean
- */
- function moduleExists( $moduleType, $moduleName )
- {
- if ( isset( $this->_moduleDirs[$moduleType] ) )
- $dirs = $this->_moduleDirs[$moduleType];
- else
- $dirs = array();
- array_push( $dirs, $this->getIncludePath() .'/'. $moduleType );
- foreach ( $dirs as $dir )
- {
- $moduleFile = sprintf( "%s/%s.php", $dir, str_replace( '_', '/', $moduleName ) );
- if ( !file_exists( $moduleFile ) )
- continue;
- if ( !is_readable( $moduleFile ) )
- continue;
- return true;
- }
- return false;
- }
- /**
- * parses a template
- *
- * Parses a template and stores the parsed content.
- * mode can be "w" for write (delete already parsed content) or "a" for append (appends the
- * new parsed content to the already parsed content)
- *
- * @access public
- * @param string name of the template
- * @param string mode for the parsing
- */
- function parseTemplate( $template, $mode = 'w' )
- {
- $template = strtolower( $template );
- if ( !isset( $this->_templates[$template] ) )
- {
- return patErrorManager::raiseWarning(
- PATTEMPLATE_WARNING_NO_TEMPLATE,
- "Template '$template' does not exist."
- );
- }
- /**
- * template is not visible
- */
- if ( $this->_templates[$template]['attributes']['visibility'] == 'hidden' )
- {
- $this->_templates[$template]['result'] = '';
- $this->_templates[$template]['parsed'] = true;
- return true;
- }
- /**
- * check, if the template has been loaded
- * and load it if necessary.
- */
- if ( $this->_templates[$template]['loaded'] !== true )
- {
- if ( $this->_templates[$template]['attributes']['parse'] == 'on' )
- {
- $result = $this->readTemplatesFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template );
- }
- else
- {
- $result = $this->loadTemplateFromInput( $this->_templates[$template]['attributes']['src'], $this->_templates[$template]['attributes']['reader'], null, $template );
- }
- if ( patErrorManager::isError( $result ) )
- {
- return $result;
- }
- }
- /**
- * check for autoclear
- */
- if (
- isset( $this->_templates[$template]['attributes']['autoclear'] ) &&
- $this->_templates[$template]['attributes']['autoclear'] == 'yes' &&
- $mode === 'w' &&
- $this->_templates[$template]['lastMode'] != 'a'
- )
- {
- $this->_templates[$template]['parsed'] = false;
- }
- /**
- * template has been parsed and mode is not 'append'
- */
- if ( $this->_templates[$template]['parsed'] === true && $mode === 'w' )
- {
- return true;
- }
- $this->_templates[$template]['lastMode'] = $mode;
- $this->_initTemplate( $template );
- if ( !isset( $this->_vars[$template]['rows'] ) )
- $this->_vars[$template]['rows'] = array();
- $loop = count( $this->_vars[$template]['rows'] );
- /**
- * loop at least one times
- */
- if ( $loop < 1 )
- $loop = 1;
- if ( isset( $this->_templates[$template]['attributes']['maxloop'] ) )
- {
- $loop = ceil( $loop / $this->_templates[$template]['attributes']['maxloop'] ) * $this->_templates[$template]['attributes']['maxloop'];
- }
- $this->_templates[$template]['loop'] = max( $this->_templates[$template]['attributes']['loop'], $loop );
- $start = 0;
- if ( isset( $this->_templates[$template]['attributes']['limit'] ) )
- {
- $p = strpos( $this->_templates[$template]['attributes']['limit'], ',' );
- if ( $p === false )
- {
- $this->_templates[$template]['loop'] = min( $this->_templates[$template]['loop'], $this->_templates[$template]['attributes']['limit'] );
- $start = 0;
- }
- else
- {
- $start = substr( $this->_templates[$template]['attributes']['limit'], 0, $p );
- $end = substr( $this->_templates[$template]['attributes']['limit'], $p+1 )+$start;
- $this->_templates[$template]['loop'] = min( $this->_templates[$template]['loop'], $end );
- }
- }
- /**
- * template should be cleared before parsing
- */
- if ( $mode == 'w' )
- {
- $this->_templates[$template]['result'] = '';
- $this->_templates[$template]['iteration'] = $start;
- }
- $loopCount = 0;
- for ( $i = $start; $i < $this->_templates[$template]['loop']; $i++ )
- {
- $finished = false;
- unset( $this->_templates[$template]['vars'] );
- /**
- * fetch the variables
- */
- $this->_fetchVariables( $template );
- /**
- * fetch the template
- */
- $result = $this->_fetchTemplate( $template );
- if ( $result === false )
- {
- $this->_templates[$template]['iteration']++;
- continue;
- }
- /**
- * parse
- */
- $this->_parseVariables( $template );
- $this->_parseDependencies( $template );
- /**
- * store result
- */
- $this->_templates[$template]['result'] .= $this->_templates[$template]['work'];
- $this->_templates[$template]['iteration']++;
- ++$loopCount;
- /**
- * check for maximum loops
- */
- if ( isset( $this->_templates[$template]['attributes']['maxloop'] ) )
- {
- if ( $loopCount == $this->_templates[$template]['attributes']['maxloop'] && $i < ( $loop-1 ) )
- {
- $loopCount = 0;
- $finished = true;
- $this->_templates[$template]['parsed'] = true;
- $this->parseTemplate( $this->_templates[$template]['attributes']['parent'], 'a' );
- $this->_templates[$template]['parsed'] = false;
- $this->_templates[$template]['result'] = '';
- }
- }
- }
- if ( !$finished && isset( $this->_templates[$template]['attributes']['maxloop'] ) )
- {
- $this->_templates[$template]['parsed'] = true;
- $this->parseTemplate( $this->_templates[$template]['attributes']['parent'], 'a', false );
- $this->_templates[$template]['parsed'] = false;
- $this->_templates[$template]['result'] = '';
- $this->_templates[$this->_templates[$template]['attributes']['parent']]['work'] = '';
- }
- $this->_parseGlobals($template);
- $this->_handleUnusedVars( $template );
- $this->_templates[$template]['parsed'] = true;
- if ( isset( $this->_templates[$template]['attributes']['autoclear'] ) && $this->_templates[$template]['attributes']['autoclear'] == 'yes' )
- {
- $this->_vars[$template] = array(
- 'scalar' => array(),
- 'rows' => array()
- );
- }
- return true;
- }
- /**
- * Initialize a template
- *
- * This method checks the variable specifications and
- * copys variables from other templates.
- *
- * @access private
- * @param string name of the template
- * @return boolean true on success
- */
- function _initTemplate( $template )
- {
- foreach ( $this->_templates[$template]['copyVars'] as $dest => $src )
- {
- /**
- * copy from the same template
- */
- if ( !is_array( $src ) )
- {
- $srcTemplate = $template;
- $srcVar = $src;
- }
- else
- {
- $srcTemplate = $src[0];
- $srcVar = $src[1];
- }
- $copied = false;
- /**
- * copy from another template
- */
- if ( isset( $this->_vars[$srcTemplate] ) )
- {
- if ( isset( $this->_vars[$srcTemplate]['scalar'][$srcVar] ) )
- {
- $this->_vars[$template]['scalar'][$dest] = $this->_vars[$srcTemplate]['scalar'][$srcVar];
- continue;
- }
- $rows = count( $this->_vars[$srcTemplate]['rows'] );
- for ( $i = 0; $i < $rows; $i++ )
- {
- if ( !isset( $this->_vars[$srcTemplate]['rows'][$i][$srcVar] ) )
- continue;
- if ( !isset( $this->_vars[$template]['rows'][$i] ) )
- $this->_vars[$template]['rows'][$i] = array();
- $this->_vars[$template]['rows'][$i][$dest] = $this->_vars[$srcTemplate]['rows'][$i][$srcVar];
- $copied = true;
- }
- }
- if ( !$copied && isset( $this->_globals[$srcVar] ))
- {
- $this->_vars[$template]['scalar'][$dest] = $this->_globals[$srcVar];
- }
- }
- return true;
- }
- /**
- * parse all variables in a template
- *
- * @access private
- * @param string
- */
- function _parseVariables( $template )
- {
- /**
- * modify variables before parsing
- */
- $this->_applyModifers($template, $this->_templates[$template]['vars']);
- foreach ( $this->_templates[$template]['vars'] as $key => $value )
- {
- if ( is_array( $value ) )
- {
- if ( count( $this->_templates[$template]['currentDependencies'] ) == 1 )
- {
- $child = $this->_templates[$template]['currentDependencies'][0];
- }
- else
- {
- if ( isset( $this->_templates[$template]['attributes']['child'] ) )
- $child = $this->_templates[$template]['attributes']['child'];
- else
- continue;
- }
- $this->setAttribute( $child, 'autoclear', 'yes' );
- $this->addVar( $child, $key, $value );
- continue;
- }
- $var = $this->_startTag.$key.$this->_endTag;
- $this->_templates[$template]['work'] = @str_replace( $var, $value, $this->_templates[$template]['work'] );
- }
- return true;
- }
- /**
- * parse global variables in the template
- *
- * @access private
- * @param string name of the template
- * @return boolean
- */
- function _parseGlobals($template)
- {
- $globalVars = $this->_globals;
- $this->_applyModifers($template, $globalVars);
- foreach ( $globalVars as $key => $value )
- {
- if ( is_array( $value ) )
- {
- continue;
- }
- $var = $this->_startTag.$key.$this->_endTag;
- $this->_templates[$template]['result'] = str_replace( $var, $value, $this->_templates[$template]['result'] );
- }
- return true;
- }
- /**
- * apply variable modifiers
- *
- * The variables will be passed by reference.
- *
- * @access private
- * @param string name of the template (use modifiers from this template)
- * @param array variables to which the modifiers should be applied
- * @return boolean
- */
- function _applyModifers($template, &$vars)
- {
- foreach ( $this->_templates[$template]['modifyVars'] as $varname => $modifier )
- {
- if ( !isset( $vars[$varname] ) )
- continue;
- if ( ( $modifier['type'] === 'php' || $modifier['type'] === 'auto' ) && is_callable( $modifier['mod'] ) )
- {
- $vars[$varname] = call_user_func( $modifier['mod'], $vars[$varname] );
- continue;
- }
- if ( $modifier['type'] === 'php' )
- continue;
- $mod = &$this->loadModule( 'Modifier', ucfirst( $modifier['mod'] ) );
- $vars[$varname] = $mod->modify( $vars[$varname], $modifier['params'] );
- }
- return true;
- }
- /**
- * parse all dependencies in a template
- *
- * @access private
- * @param string
- */
- function _parseDependencies( $template )
- {
- $countDep = count( $this->_templates[$template]['currentDependencies'] );
- for ( $i = 0; $i < $countDep; $i++ )
- {
- $depTemplate = $this->_templates[$template]['currentDependencies'][$i];
- $this->parseTemplate( $depTemplate );
- $var = $this->_startTag.'TMPL:'.strtoupper( $depTemplate) .$this->_endTag;
- $this->_templates[$template]['work'] = str_replace( $var, $this->_templates[$depTemplate]['result'], $this->_templates[$template]['work'] );
- }
- return true;
- }
- /**
- * fetch plain template
- *
- * The template content will be stored in the template
- * configuration so it can be used by other
- * methods.
- *
- * @access private
- * @param string template name
- * @return boolean
- */
- function _fetchTemplate( $template )
- {
- switch( $this->_templates[$template]['attributes']['type'] )
- {
- /**
- * condition template
- */
- case 'condition':
- $value = $this->_getConditionValue( $template, $this->_templates[$template]['attributes']['conditionvar'] );
- if ( $value === false )
- {
- $this->_templates[$template]['work'] = '';
- $this->_templates[$template]['currentDependencies'] = array();
- }
- else
- {
- $this->_templates[$template]['work'] = $this->_templates[$template]['subtemplates'][$value]['data'];
- $this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['subtemplates'][$value]['dependencies'];
- }
- break;
- /**
- * condition template
- */
- case 'simplecondition':
- foreach ( $this->_templates[$template]['attributes']['requiredvars'] as $var )
- {
- if ( $var[0] !== $template )
- $this->_fetchVariables($var[0]);
- if ( isset( $this->_templates[$var[0]]['vars'][$var[1]] ) && strlen( $this->_templates[$var[0]]['vars'][$var[1]] ) > 0 )
- continue;
- if (isset($this->_templates[$template]['attributes']['useglobals']))
- {
- if (isset($this->_globals[$var[1]]) && strlen($this->_globals[$var[1]]) > 1)
- continue;
- }
- $this->_templates[$template]['work'] = '';
- $this->_templates[$template]['currentDependencies'] = array();
- break 2;
- }
- $this->_templates[$template]['work'] = $this->_templates[$template]['content'];
- $this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['dependencies'];
- break;
- /**
- * modulo template
- */
- case 'modulo':
- // check for empty template
- if ($this->_hasVariables($template)) {
- $value = ( $this->_templates[$template]['iteration'] + 1 ) % $this->_templates[$template]['attributes']['modulo'];
- } else {
- $value = '__empty';
- }
- $value = $this->_getConditionValue( $template, $value, false );
- if ( $value === false )
- {
- $this->_templates[$template]['work'] = '';
- $this->_templates[$template]['currentDependencies'] = array();
- }
- else
- {
- $this->_templates[$template]['work'] = $this->_templates[$template]['subtemplates'][$value]['data'];
- $this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['subtemplates'][$value]['dependencies'];
- }
- break;
- /**
- * standard template
- */
- default:
- $this->_templates[$template]['work'] = $this->_templates[$template]['content'];
- $this->_templates[$template]['currentDependencies'] = $this->_templates[$template]['dependencies'];
- break;
- }
- return true;
- }
- /**
- * check, whether a template contains variables
- *
- * @access private
- * @param string template name
- * @return boolean
- */
- function _hasVariables($template)
- {
- if (!empty($this->_vars[$template]['scalar'])) {
- return true;
- }
- if (isset($this->_vars[$template]['rows'][$this->_templates[$template]['iteration']])) {
- return true;
- }
- return false;
- }
- /**
- * fetch the value of a condition variable
- *
- * _fetchVariables() has to be called before this
- * method is being called.
- *
- * @access private
- * @param string template name
- * @param string condition value
- * @param boolean flag that indicates whether value is the name of the variable that should be resolved
- *
- * @todo split this method into smaller check methods that will be called according to
- * a priority list
- */
- function _getConditionValue( $template, $value, $isVar = true )
- {
- if ( $isVar === true )
- {
- if ( isset( $this->_templates[$template]['attributes']['conditiontmpl'] ) )
- {
- $_template = $this->_templates[$template]['attributes']['conditiontmpl'];
- $this->_fetchVariables( $_template );
- }
- else
- {
- $_template = $template;
- }
- /**
- * get the value from the template variables
- */
- if ( !isset( $this->_templates[$_template]['vars'][$value] ) || strlen( $this->_templates[$_template]['vars'][$value] ) === 0 )
- {
- if ( $this->_templates[$template]['attributes']['useglobals'] == 'yes' || $this->_templates[$template]['attributes']['useglobals'] == 'useglobals' )
- {
- if ( isset( $this->_globals[$value] ) && strlen( $this->_globals[$value] ) > 0 )
- {
- $value = $this->_globals[$value];
- }
- else
- {
- $value = '__empty';
- }
- }
- else
- {
- $value = '__empty';
- }
- }
- else
- {
- $value = $this->_templates[$_template]['vars'][$value];
- }
- }
- else
- {
- $_template = $template;
- }
- /**
- * is __first?
- */
- if ( $this->_templates[$_template]['iteration'] == 0 )
- {
- if ( isset( $this->_templates[$template]['subtemplates']['__first'] ) )
- {
- return '__first';
- }
- }
- /**
- * is __last?
- */
- $max = $this->_templates[$_template]['loop'] - 1;
- if ( $this->_templates[$_template]['iteration'] == $max )
- {
- if ( isset( $this->_templates[$template]['subtemplates']['__last'] ) )
- {
- return '__last';
- }
- }
- /**
- * found an exact match
- */
- if ( isset( $this->_templates[$template]['subtemplates'][$value] ) )
- {
- return $value;
- }
- /**
- * is __default?
- */
- if ( isset( $this->_templates[$template]['subtemplates']['__default'] ) )
- {
- return '__default';
- }
- return false;
- }
- /**
- * fetch variables for a template
- *
- * The variables will be stored in the template
- * configuration so they can be used by other
- * methods.
- *
- * @access private
- * @param string template name
- * @return boolean
- */
- function _fetchVariables( $template )
- {
- /**
- * variables already have been fetched
- */
- if ( isset( $this->_templates[$template]['vars'] ) )
- {
- return true;
- }
- $iteration = $this->_templates[$template]['iteration'];
- if ( isset( $this->_templates[$template]['attributes']['varscope'] ) )
- {
- $scopeTemplate = $this->_templates[$template]['attributes']['varscope'];
- if ($this->exists($scopeTemplate)) {
- $this->_fetchVariables( $scopeTemplate );
- $vars = $this->_templates[$scopeTemplate]['vars'];
- } else {
- patErrorManager::raiseWarning(PATTEMPLATE_WARNING_NO_TEMPLATE, 'Template \''.$scopeTemplate.'\' does not exist, referenced in varscope attribute of template \''.$template.'\'');
- $vars = array();
- }
- }
- else
- {
- $vars = array();
- }
- /**
- * get the scalar variables
- */
- if ( isset( $this->_vars[$template] ) && isset( $this->_vars[$template]['scalar'] ) )
- {
- $vars = array_merge( $vars, $this->_vars[$template]['scalar'] );
- }
- /**
- * get the row variables
- */
- if ( isset( $this->_vars[$template]['rows'][$iteration] ) )
- {
- $vars = array_merge( $vars, $this->_vars[$template]['rows'][$iteration] );
- }
- /**
- * add some system variables
- */
- $currentRow = $iteration + 1;
- $vars['PAT_ROW_VAR'] = $currentRow;
- if ( $this->_templates[$template]['attributes']['type'] == 'modulo' )
- {
- $vars['PAT_MODULO_REP'] = ceil( $currentRow / $this->_templates[$template]['attributes']['modulo'] );
- $vars['PAT_MODULO'] = ( $this->_templates[$template]['iteration'] + 1 ) % $this->_templates[$template]['attributes']['modulo'];
- }
- if ( $this->_templates[$template]['attributes']['addsystemvars'] !== false )
- {
- $vars['PATTEMPLATE_VERSION'] = $this->_systemVars['appVersion'];
- $vars['PAT_LOOPS'] = $this->_templates[$template]['loop'];
- switch ($this->_templates[$template]['attributes']['addsystemvars'])
- {
- case 'boolean':
- $trueValue = 'true';
- $falseValue = 'false';
- break;
- case 'integer':
- $trueValue = '1';
- $falseValue = '0';
- break;
- default:
- $trueValue = $this->_templates[$template]['attributes']['addsystemvars'];
- $falseValue = '';
- break;
- }
- $vars['PAT_IS_ODD'] = ( $currentRow % 2 == 1 ) ? $trueValue : $falseValue;
- $vars['PAT_IS_EVEN'] = ( $currentRow % 2 == 0 ) ? $trueValue : $falseValue;
- $vars['PAT_IS_FIRST'] = ( $currentRow == 1 ) ? $trueValue : $falseValue;
- $vars['PAT_IS_LAST'] = ( $currentRow == $this->_templates[$template]['loop'] ) ? $trueValue : $falseValue;
- }
- $this->_templates[$template]['vars'] = $vars;
- return true;
- }
- /**
- * handle all unused variables in a template
- *
- * This is influenced by the 'unusedvars' attribute of the
- * template
- *
- * @access private
- * @param string
- */
- function _handleUnusedVars( $template )
- {
- $regexp = '/('.$this->_startTag.'[^a-z]+'.$this->_endTag.')/U';
- switch( $this->_templates[$template]['attributes']['unusedvars'] )
- {
- case 'comment':
- $this->_templates[$template]['result'] = preg_replace( $regexp, '<!-- \\1 -->', $this->_templates[$template]['result'] );
- break;
- case 'strip':
- $this->_templates[$template]['result'] = preg_replace( $regexp, '', $this->_templates[$template]['result'] );
- break;
- case 'nbsp':
- $this->_templates[$template]['result'] = preg_replace( $regexp, ' ', $this->_templates[$template]['result'] );
- break;
- case 'ignore':
- break;
- default:
- $this->_templates[$template]['result'] = preg_replace( $regexp, $this->_templates[$template]['attributes']['unusedvars'], $this->_templates[$template]['result'] );
- break;
- }
- return true;
- }
- /**
- * returns a parsed Template
- *
- * If the template already has been parsed, it just returns the parsed template.
- * If the template has not been loaded, it will be loaded.
- *
- * @access public
- * @param string name of the template
- * @return string Content of the parsed template
- * @see displayParsedTemplate()
- */
- function getParsedTemplate( $name = null )
- {
- if ( is_null( $name ) )
- $name = $this->_root;
- $name = strtolower( $name );
- $result = $this->parseTemplate( $name );
- if ( patErrorManager::isError( $result ) )
- return $result;
- return $this->_templates[$name]['result'];
- }
- /**
- * displays a parsed Template
- *
- * If the template has not been loaded, it will be loaded.
- *
- * @see getParsedTemplate()
- * @param string name of the template
- * @return boolean true on success
- * @access public
- */
- function displayParsedTemplate( $name = null )
- {
- $result = $this->getParsedTemplate( $name );
- /**
- * error happened
- */
- if ( patErrorManager::isError( $result ) )
- return $result;
- $cnt = count( $this->_outputFilters );
- for ( $i = 0; $i < $cnt; $i++ )
- {
- $result = $this->_outputFilters[$i]->apply( $result );
- }
- echo $result;
- return true;
- }
- /**
- * parse a template and push the result into a variable of any other
- * template
- *
- * If the template already has been parsed, it will just be pushed into the variable.
- * If the template has not been loaded, it will be loaded.
- *
- * @access public
- * @param string name of the template
- * @return string Content of the parsed template
- * @param boolean if set to true, the value will be appended to the value already stored.
- * @see getParsedTemplate()
- * @see addVar()
- */
- function parseIntoVar( $srcTmpl, $destTmpl, $var, $append = false )
- {
- $srcTmpl = strtolower( $srcTmpl );
- $destTmpl = strtolower( $destTmpl );
- $var = strtoupper($var);
- $result = $this->parseTemplate( $srcTmpl );
- if ( patErrorManager::isError( $result ) )
- return $result;
- if ( $append !== true || !isset( $this->_vars[$destTmpl]['scalar'][$var] ) )
- $this->_vars[$destTmpl]['scalar'][$var] = '';
- $this->_vars[$destTmpl]['scalar'][$var] .= $this->_templates[$srcTmpl]['result'];
- return true;
- }
- /**
- * clears a parsed Template
- *
- * Parsed Content, variables and the loop attribute are cleared
- *
- * If you will not be using this template anymore, then you should
- * call freeTemplate()
- *
- * @access public
- * @param string name of the template
- * @param boolean set this to true to clear all child templates, too
- * @see clearAllTemplates()
- * @see freeTemplate()
- */
- function clearTemplate( $name, $recursive = false )
- {
- $name = strtolower( $name );
- $this->_templates[$name]['parsed'] = false;
- $this->_templates[$name]['work'] = '';
- $this->_templates[$name]['iteration'] = 0;
- $this->_templates[$name]['result'] = '';
- $this->_vars[$name] = array(
- 'scalar' => array(),
- 'rows' => array()
- );
- if (!empty($this->_templates[$name]['defaultVars'])) {
- foreach ($this->_templates[$name]['defaultVars'] as $varname => $value) {
- $this->addVar($name, $varname, $value);
- }
- }
- /**
- * clear child templates as well
- */
- if ( $recursive === true )
- {
- $deps = $this->_getDependencies( $name );
- foreach ( $deps as $dep )
- {
- $this->clearTemplate( $dep, true );
- }
- }
- return true;
- }
- /**
- * clears all templates
- *
- * @access public
- * @uses clearTemplate()
- */
- function clearAllTemplates()
- {
- $templates = array_keys( $this->_templates );
- $cnt = count( $templates );
- for ( $i = 0; $i < $cnt; $i++ )
- {
- $this->clearTemplate( $templates[$i] );
- }
- return true;
- }
- /**
- * frees a template
- *
- * All memory consumed by the template
- * will be freed.
- *
- * @access public
- * @param string name of the template
- * @param boolean clear dependencies of the template
- * @see freeAllTemplates()
- */
- function freeTemplate( $name, $recursive = false )
- {
- $name = strtolower( $name );
- $key = array_search( $name, $this->_templateList );
- if ( $key === false )
- {
- return patErrorManager::raiseWarning(
- PATTEMPLATE_WARNING_NO_TEMPLATE,
- "Template '$name' does not exist."
- );
- }
- unset( $this->_templateList[$key] );
- $this->_templateList = array_values( $this->_templateList );
- /**
- * free child templates as well
- */
- if ( $recursive === true )
- {
- $deps = $this->_getDependencies( $name );
- foreach ( $deps as $dep )
- {
- $this->freeTemplate( $dep, true );
- }
- }
- unset( $this->_templates[$name] );
- unset( $this->_vars[$name] );
- return true;
- }
- /**
- * frees all templates
- *
- * All memory consumed by the templates
- * will be freed.
- *
- * @access public
- * @see freeTemplate()
- */
- function freeAllTemplates()
- {
- $this->_templates = array();
- $this->_vars = array();
- }
- /**
- * get _all_ dependencies of a template,
- * regardless of the subtemplates
- *
- * @access private
- * @param string template name
- * @return array list of all subtemplates
- */
- function _getDependencies( $template )
- {
- $deps = array();
- if ( isset( $this->_templates[$template]['dependencies'] ) )
- $deps = $this->_templates[$template]['dependencies'];
- if ( isset( $this->_templates[$template]['subtemplates'] ) )
- {
- foreach ( $this->_templates[$template]['subtemplates'] as $sub )
- {
- if ( isset( $sub['dependencies'] ) )
- $deps = array_merge( $deps, $sub['dependencies'] );
- }
- }
- $deps = array_unique( $deps );
- return $deps;
- }
- /**
- * Displays useful information about all or named templates
- *
- * This method breaks BC, as it now awaits an array instead of
- * unlimited parameters.
- *
- * @param mixed array of templates that should be dumped, or null if you
- * want all templates to be dumped
- * @param string dumper
- * @access public
- */
- function dump( $restrict = null, $dumper = 'Html' )
- {
- if ( is_string( $restrict ) )
- $restrict = array( $restrict );
- $dumper = &$this->loadModule( 'Dump', $dumper );
- if ( patErrorManager::isError( $dumper ) )
- {
- return $dumper;
- }
- if ( is_null( $restrict ) )
- {
- $templates = $this->_templates;
- $vars = $this->_vars;
- }
- else
- {
- $restrict = array_map( 'strtolower', $restrict );
- $templates = array();
- $vars = array();
- foreach ( $this->_templates as $name => $spec )
- {
- if ( !in_array( $name, $restrict ) )
- continue;
- $templates[$name] = $spec;
- $vars[$name] = $this->_vars[$name];
- }
- }
- $dumper->displayHeader();
- $dumper->dumpGlobals( $this->_globals );
- $dumper->dumpTemplates( $templates, $vars );
- $dumper->displayFooter();
- return true;
- }
- /**
- * get the include path
- *
- * @access public
- */
- function getIncludePath()
- {
- return PATTEMPLATE_INCLUDE_PATH;
- }
- /**
- * apply input filters that have been set
- *
- * This is being called by the readers.
- *
- * @access public
- * @param string template
- * @return string filtered templeta
- */
- function applyInputFilters( $template )
- {
- $cnt = count( $this->_inputFilters );
- for ( $i = 0; $i < $cnt; $i++ )
- {
- $template = $this->_inputFilters[$i]->apply( $template );
- }
- return $template;
- }
- /**
- * Convert the template to its string representation.
- *
- * This method allows you to just echo the patTemplate
- * object in order to display the template.
- *
- * Requires PHP5
- *
- * <code>
- * $tmpl = new patTemplate();
- * $tmpl->readTemplatesFromFile( 'myfile.tmpl' );
- * echo $tmpl;
- * </code>
- *
- * @access private
- * @return string
- */
- function __toString()
- {
- return $this->getParsedTemplate();
- }
- }
|