PageRenderTime 46ms CodeModel.GetById 15ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/eztemplate/classes/eztemplate.php

https://github.com/aurelienRT1/ezpublish
PHP | 2702 lines | 1817 code | 229 blank | 656 comment | 258 complexity | c66ecac8902c70f99ed364aa4b2d2e3e MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2//
   3// Definition of eZTemplate class
   4//
   5// Created on: <01-Mar-2002 13:49:57 amos>
   6//
   7// ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
   8// SOFTWARE NAME: eZ Publish
   9// SOFTWARE RELEASE: 4.1.x
  10// COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  11// SOFTWARE LICENSE: GNU General Public License v2.0
  12// NOTICE: >
  13//   This program is free software; you can redistribute it and/or
  14//   modify it under the terms of version 2.0  of the GNU General
  15//   Public License as published by the Free Software Foundation.
  16//
  17//   This program is distributed in the hope that it will be useful,
  18//   but WITHOUT ANY WARRANTY; without even the implied warranty of
  19//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20//   GNU General Public License for more details.
  21//
  22//   You should have received a copy of version 2.0 of the GNU General
  23//   Public License along with this program; if not, write to the Free
  24//   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25//   MA 02110-1301, USA.
  26//
  27//
  28// ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29//
  30
  31/*! \file
  32 Template system manager.
  33*/
  34
  35/*! \defgroup eZTemplate Template system */
  36
  37/*!
  38  \class eZTemplate eztemplate.php
  39  \ingroup eZTemplate
  40  \brief The main manager for templates
  41
  42  The template systems allows for separation of code and
  43  layout by moving the layout part into template files. These
  44  template files are parsed and processed with template variables set
  45  by the PHP code.
  46
  47  The template system in itself is does not do much, it parses template files
  48  according to a rule set sets up a tree hierarchy and process the data
  49  using functions and operators. The standard template system comes with only
  50  a few functions and no operators, it is meant for these functions and operators
  51  to be specified by the users of the template system. But for simplicity a few
  52  help classes is available which can be easily enabled.
  53
  54  The classes are:
  55  - eZTemplateDelimitFunction - Inserts the left and right delimiter which are normally parsed.
  56  - eZTemplateSectionFunction - Allows for conditional blocks and loops.
  57  - eZTemplateIncludeFunction - Includes external templates
  58  - eZTemplateSequenceFunction - Creates sequences arrays
  59  - eZTemplateSwitchFunction - Conditional output of template
  60
  61  - eZTemplatePHPOperator - Allows for easy redirection of operator names to PHP functions.
  62  - eZTemplateLocaleOperator - Allows for locale conversions.
  63  - eZTemplateArrayOperator - Creates arrays
  64  - eZTemplateAttributeOperator - Displays contents of template variables, useful for debugging
  65  - eZTemplateImageOperator - Converts text to image
  66  - eZTemplateLogicOperator - Various logical operators for boolean handling
  67  - eZTemplateUnitOperator - Unit conversion and display
  68
  69  To enable these functions and operator use registerFunction and registerOperator.
  70
  71  In keeping with the spirit of being simple the template system does not know how
  72  to get the template files itself. Instead it relies on resource handlers, these
  73  handlers fetches the template files using different kind of transport mechanism.
  74  For simplicity a default resource class is available, eZTemplateFileResource fetches
  75  templates from the filesystem.
  76
  77  The parser process consists of three passes, each pass adds a new level of complexity.
  78  The first pass strips text from template blocks which starts with a left delimiter and
  79  ends with a right delimiter (default is { and } ), and places them in an array.
  80  The second pass iterates the text and block elements and removes newlines from
  81  text before function blocks and text after function blocks.
  82  The third pass builds the tree according the function rules.
  83
  84  Processing is done by iterating over the root of the tree, if a text block is found
  85  the text is appended to the result text. If a variable or contant is it's data is extracted
  86  and any operators found are run on it before fetching the result and appending it to
  87  the result text. If a function is found the function is called with the parameters
  88  and it's up to the function handle children if any.
  89
  90  Constants and template variables will usually be called variables since there's little
  91  difference. A template variable expression will start with a $ and consists of a
  92  namespace (optional) a name and attribues(optional). The variable expression
  93  \verbatim $root:var.attr1 \endverbatim exists in the "root" namespace, has the name "var" and uses the
  94  attribute "attr1". Some functions will create variables on demand, to avoid name conflicts
  95  namespaces were introduced, each function will place the new variables in a namespace
  96  specified in the template file. Attribues are used for fetching parts of the variable,
  97  for instance an element in an array or data in an object. Since the syntax is the
  98  same for arrays and objects the PHP code can use simple arrays when speed is required,
  99  the template code will not care.
 100  A different syntax is also available when you want to access an attribute using a variable.
 101  For instance \verbatim $root:var[$attr_var] \endverbatim, if the variable $attr_var contains "attr1" it would
 102  access the same attribute as in the first example.
 103
 104  The syntax for operators is a | and a name, optionally parameters can be specified with
 105  ( and ) delimited with ,. Valid operators are \verbatim |upcase, |l10n(date) \endverbatim.
 106
 107  Functions look a lot like HTML/XML tags. The function consists of a name and parameters
 108  which are assigned using the param=value syntax. Some parameters may be required while
 109  others may be optionally, the exact behaviour is specified by each function.
 110  Valid functions are \verbatim "section name=abc loop=4" \endverbatim
 111
 112  Example of usage:
 113\code
 114// Init template
 115$tpl = eZTemplate::instance();
 116
 117$tpl->registerOperators( new eZTemplatePHPOperator( array( "upcase" => "strtoupper",
 118                                                           "reverse" => "strrev" ) ) );
 119$tpl->registerOperators( new eZTemplateLocaleOperator() );
 120$tpl->registerFunction( "section", new eZTemplateSectionFunction( "section" ) );
 121$tpl->registerFunctions( new eZTemplateDelimitFunction() );
 122
 123$tpl->setVariable( "my_var", "{this value set by variable}", "test" );
 124$tpl->setVariable( "my_arr", array( "1st", "2nd", "third", "fjerde" ) );
 125$tpl->setVariable( "multidim", array( array( "a", "b" ),
 126                                      array( "c", "d" ),
 127                                      array( "e", "f" ),
 128                                      array( "g", "h" ) ) );
 129
 130class mytest
 131{
 132    function mytest( $n, $s )
 133    {
 134        $this->n = $n;
 135        $this->s = $s;
 136    }
 137
 138    function hasAttribute( $attr )
 139    {
 140        return ( $attr == "name" || $attr == "size" );
 141    }
 142
 143    function attribute( $attr )
 144    {
 145        switch ( $attr )
 146        {
 147            case "name";
 148                return $this->n;
 149            case "size";
 150                return $this->s;
 151            default:
 152                $retAttr = null;
 153                return $retAttr;
 154        }
 155    }
 156
 157};
 158
 159$tpl->setVariable( "multidim_obj", array( new mytest( "jan", 200 ),
 160                                          new mytest( "feb", 200 ),
 161                                          new mytest( "john", 200 ),
 162                                          new mytest( "doe", 50 ) ) );
 163$tpl->setVariable( "curdate", time() );
 164
 165$tpl->display( "lib/eztemplate/example/test.tpl" );
 166
 167// test.tpl
 168
 169{section name=outer loop=4}
 170123
 171{delimit}::{/delimit}
 172{/section}
 173
 174{literal test=1} This is some {blah arg1="" arg2="abc" /} {/literal}
 175
 176<title>This is a test</title>
 177<table border="1">
 178<tr><th>{$test:my_var}
 179{"some text!!!"|upcase|reverse}</th></tr>
 180{section name=abc loop=$my_arr}
 181<tr><td>{$abc:item}</td></tr>
 182{/section}
 183</table>
 184
 185<table border="1">
 186{section name=outer loop=$multidim}
 187<tr>
 188{section name=inner loop=$outer:item}
 189<td>{$inner:item}</td>
 190{/section}
 191</tr>
 192{/section}
 193</table>
 194
 195<table border="1">
 196{section name=outer loop=$multidim_obj}
 197<tr>
 198<td>{$outer:item.name}</td>
 199<td>{$outer:item.size}</td>
 200</tr>
 201{/section}
 202</table>
 203
 204{section name=outer loop=$nonexistingvar}
 205<b><i>Dette skal ikke vises</b></i>
 206{section-else}
 207<b><i>This is shown when the {ldelim}$loop{rdelim} variable is non-existant</b></i>
 208{/section}
 209
 210
 211Denne koster {1.4|l10n(currency)}<br>
 212{-123456789|l10n(number)}<br>
 213{$curdate|l10n(date)}<br>
 214{$curdate|l10n(shortdate)}<br>
 215{$curdate|l10n(time)}<br>
 216{$curdate|l10n(shorttime)}<br>
 217{include file="test2.tpl"/}
 218
 219\endcode
 220*/
 221
 222class eZTemplate
 223{
 224    const RESOURCE_FETCH = 1;
 225    const RESOURCE_QUERY = 2;
 226
 227    const ELEMENT_TEXT = 1;
 228    const ELEMENT_SINGLE_TAG = 2;
 229    const ELEMENT_NORMAL_TAG = 3;
 230    const ELEMENT_END_TAG = 4;
 231    const ELEMENT_VARIABLE = 5;
 232    const ELEMENT_COMMENT = 6;
 233
 234    const NODE_ROOT = 1;
 235    const NODE_TEXT = 2;
 236    const NODE_VARIABLE = 3;
 237    const NODE_FUNCTION = 4;
 238    const NODE_OPERATOR = 5;
 239
 240
 241    const NODE_INTERNAL = 100;
 242    const NODE_INTERNAL_CODE_PIECE = 101;
 243
 244    const NODE_INTERNAL_VARIABLE_SET = 105;
 245    const NODE_INTERNAL_VARIABLE_UNSET = 102;
 246
 247    const NODE_INTERNAL_NAMESPACE_CHANGE = 103;
 248    const NODE_INTERNAL_NAMESPACE_RESTORE = 104;
 249
 250    const NODE_INTERNAL_WARNING = 120;
 251    const NODE_INTERNAL_ERROR = 121;
 252
 253    const NODE_INTERNAL_RESOURCE_ACQUISITION = 140;
 254    const NODE_OPTIMIZED_RESOURCE_ACQUISITION = 141;
 255
 256    const NODE_INTERNAL_OUTPUT_ASSIGN = 150;
 257    const NODE_INTERNAL_OUTPUT_READ = 151;
 258    const NODE_INTERNAL_OUTPUT_INCREASE = 152;
 259    const NODE_INTERNAL_OUTPUT_DECREASE = 153;
 260
 261    const NODE_INTERNAL_OUTPUT_SPACING_INCREASE = 160;
 262    const NODE_INTERNAL_SPACING_DECREASE = 161;
 263
 264    const NODE_OPTIMIZED_INIT = 201;
 265
 266
 267    const NODE_USER_CUSTOM = 1000;
 268
 269
 270    const TYPE_VOID = 0;
 271    const TYPE_STRING = 1;
 272    const TYPE_NUMERIC = 2;
 273    const TYPE_IDENTIFIER = 3;
 274    const TYPE_VARIABLE = 4;
 275    const TYPE_ATTRIBUTE = 5;
 276    const TYPE_OPERATOR = 6;
 277    const TYPE_BOOLEAN = 7;
 278    const TYPE_ARRAY = 8;
 279    const TYPE_DYNAMIC_ARRAY = 9;
 280
 281    const TYPE_INTERNAL = 100;
 282    const TYPE_INTERNAL_CODE_PIECE = 101;
 283    const TYPE_PHP_VARIABLE = 102;
 284
 285    const TYPE_OPTIMIZED_NODE = 201;
 286    const TYPE_OPTIMIZED_ARRAY_LOOKUP = 202;
 287    const TYPE_OPTIMIZED_CONTENT_CALL = 203;
 288    const TYPE_OPTIMIZED_ATTRIBUTE_LOOKUP = 204;
 289
 290    const TYPE_INTERNAL_STOP = 999;
 291
 292
 293    const TYPE_STRING_BIT = 1;
 294    const TYPE_NUMERIC_BIT = 2;
 295    const TYPE_IDENTIFIER_BIT = 4;
 296    const TYPE_VARIABLE_BIT = 8;
 297    const TYPE_ATTRIBUTE_BIT = 16;
 298    const TYPE_OPERATOR_BIT = 32;
 299
 300    const TYPE_NONE = 0;
 301
 302    const TYPE_ALL = 63;
 303
 304    const TYPE_BASIC = 47;
 305
 306    const TYPE_MODIFIER_MASK = 48;
 307
 308    const NAMESPACE_SCOPE_GLOBAL = 1;
 309    const NAMESPACE_SCOPE_LOCAL = 2;
 310    const NAMESPACE_SCOPE_RELATIVE = 3;
 311
 312    const DEBUG_INTERNALS = false;
 313
 314    const FILE_ERRORS = 1;
 315
 316    /*!
 317     Intializes the template with left and right delimiters being { and },
 318     and a file resource. The literal tag "literal" is also registered.
 319    */
 320    function eZTemplate()
 321    {
 322        $this->Tree = array( eZTemplate::NODE_ROOT, false );
 323        $this->LDelim = "{";
 324        $this->RDelim = "}";
 325
 326        $this->IncludeText = array();
 327        $this->IncludeOutput = array();
 328
 329        $this->registerLiteral( "literal" );
 330
 331        $res = new eZTemplateFileResource();
 332        $this->DefaultResource = $res;
 333        $this->registerResource( $res );
 334
 335        $this->Resources = array();
 336        $this->Text = null;
 337
 338        $this->IsCachingAllowed = true;
 339
 340        $this->resetErrorLog();
 341
 342        $this->AutoloadPathList = array( 'lib/eztemplate/classes/' );
 343        $this->Variables = array();
 344        $this->LocalVariablesNamesStack = array();
 345        $this->CurrentLocalVariablesNames = null;
 346        $this->Functions = array();
 347        $this->FunctionAttributes = array();
 348
 349        $this->TestCompile = false;
 350
 351        $ini = eZINI::instance( 'template.ini' );
 352        if ( $ini->hasVariable( 'ControlSettings', 'MaxLevel' ) )
 353             $this->MaxLevel = $ini->variable( 'ControlSettings', 'MaxLevel' );
 354
 355        $this->MaxLevelWarning = ezpI18n::tr( 'lib/template',
 356                                         'The maximum nesting level of %max has been reached. The execution is stopped to avoid infinite recursion.',
 357                                         '',
 358                                         array( '%max' => $this->MaxLevel ) );
 359        eZDebug::createAccumulatorGroup( 'template_total', 'Template Total' );
 360
 361        $this->TemplatesUsageStatistics = array();
 362        // Array of templates which are used in a single fetch()
 363        $this->TemplateFetchList = array();
 364
 365        $this->ForeachCounter = 0;
 366        $this->ForCounter     = 0;
 367        $this->WhileCounter   = 0;
 368        $this->DoCounter      = 0;
 369        $this->ElseifCounter  = 0;
 370    }
 371
 372    /*!
 373     Returns the left delimiter being used.
 374    */
 375    function leftDelimiter()
 376    {
 377        return $this->LDelim;
 378    }
 379
 380    /*!
 381     Returns the right delimiter being used.
 382    */
 383    function rightDelimiter()
 384    {
 385        return $this->RDelim;
 386    }
 387
 388    /*!
 389     Sets the left delimiter.
 390    */
 391    function setLeftDelimiter( $delim )
 392    {
 393        $this->LDelim = $delim;
 394    }
 395
 396    /*!
 397     Sets the right delimiter.
 398    */
 399    function setRightDelimiter( $delim )
 400    {
 401        $this->RDelim = $delim;
 402    }
 403
 404    /*!
 405     Fetches the result of the template file and displays it.
 406     If $template is supplied it will load this template file first.
 407    */
 408    function display( $template = false, $extraParameters = false )
 409    {
 410        $output = $this->fetch( $template, $extraParameters );
 411        if ( $this->ShowDetails )
 412        {
 413            echo '<h1>Result:</h1>' . "\n";
 414            echo '<hr/>' . "\n";
 415        }
 416        echo "$output";
 417        if ( $this->ShowDetails )
 418        {
 419            echo '<hr/>' . "\n";
 420        }
 421        if ( $this->ShowDetails )
 422        {
 423            echo "<h1>Template data:</h1>";
 424            echo "<p class=\"filename\">" . $template . "</p>";
 425            echo "<pre class=\"example\">" . htmlspecialchars( $this->Text ) . "</pre>";
 426            reset( $this->IncludeText );
 427            while ( ( $key = key( $this->IncludeText ) ) !== null )
 428            {
 429                $item = $this->IncludeText[$key];
 430                echo "<p class=\"filename\">" . $key . "</p>";
 431                echo "<pre class=\"example\">" . htmlspecialchars( $item ) . "</pre>";
 432                next( $this->IncludeText );
 433            }
 434            echo "<h1>Result text:</h1>";
 435            echo "<p class=\"filename\">" . $template . "</p>";
 436            echo "<pre class=\"example\">" . htmlspecialchars( $output ) . "</pre>";
 437            reset( $this->IncludeOutput );
 438            while ( ( $key = key( $this->IncludeOutput ) ) !== null )
 439            {
 440                $item = $this->IncludeOutput[$key];
 441                echo "<p class=\"filename\">" . $key . "</p>";
 442                echo "<pre class=\"example\">" . htmlspecialchars( $item ) . "</pre>";
 443                next( $this->IncludeOutput );
 444            }
 445        }
 446    }
 447
 448    /*!
 449     * Initialize list of local variables for the current template.
 450     * The list contains only names of variables.
 451     */
 452    function createLocalVariablesList()
 453    {
 454        $this->LocalVariablesNamesStack[] = array();
 455        $this->CurrentLocalVariablesNames =& $this->LocalVariablesNamesStack[ count( $this->LocalVariablesNamesStack ) - 1];
 456    }
 457
 458    /*!
 459     * Check if the given local variable exists.
 460     */
 461    function hasLocalVariable( $varName, $rootNamespace )
 462    {
 463        return ( array_key_exists( $rootNamespace, $this->CurrentLocalVariablesNames ) &&
 464                 array_key_exists( $varName, $this->CurrentLocalVariablesNames[$rootNamespace] ) );
 465    }
 466
 467    /*!
 468     * Create a local variable.
 469     */
 470    function setLocalVariable( $varName, $varValue, $rootNamespace )
 471    {
 472        $this->CurrentLocalVariablesNames[$rootNamespace][$varName] = 1;
 473        $this->setVariable( $varName, $varValue, $rootNamespace );
 474    }
 475
 476    /*!
 477     * Destroy a local variable.
 478     */
 479    function unsetLocalVariable( $varName, $rootNamespace )
 480    {
 481        if ( !$this->hasLocalVariable( $varName, $rootNamespace ) )
 482            return;
 483
 484        $this->unsetVariable( $varName, $rootNamespace );
 485        unset( $this->CurrentLocalVariablesNames[$rootNamespace][$varName] );
 486    }
 487
 488    /*!
 489     * Destroy all local variables defined in the current template.
 490     */
 491    function unsetLocalVariables()
 492    {
 493        foreach ( $this->CurrentLocalVariablesNames as $ns => $vars )
 494        {
 495            foreach ( $vars as $var => $val )
 496                $this->unsetLocalVariable( $var, $ns );
 497        }
 498    }
 499
 500    /*!
 501     * Destroy list of local variables defined in the current (innermost) template.
 502     */
 503    function destroyLocalVariablesList()
 504    {
 505        array_pop( $this->LocalVariablesNamesStack );
 506
 507        if ( $this->LocalVariablesNamesStack )
 508            $this->CurrentLocalVariablesNames =& $this->LocalVariablesNamesStack[ count( $this->LocalVariablesNamesStack ) - 1];
 509        else
 510            unset( $this->CurrentLocalVariablesNames );
 511    }
 512
 513    /*!
 514     Tries to fetch the result of the template file and returns it.
 515     If $template is supplied it will load this template file first.
 516    */
 517    function fetch( $template = false, $extraParameters = false, $returnResourceData = false )
 518    {
 519        $this->resetErrorLog();
 520        // Reset fetch list when a new fetch is started
 521        $this->TemplateFetchList = array();
 522
 523        eZDebug::accumulatorStart( 'template_total' );
 524        eZDebug::accumulatorStart( 'template_load', 'template_total', 'Template load' );
 525        $root = null;
 526        if ( is_string( $template ) )
 527        {
 528            $resourceData = $this->loadURIRoot( $template, true, $extraParameters );
 529            if ( $resourceData and
 530                 $resourceData['root-node'] !== null )
 531                $root =& $resourceData['root-node'];
 532        }
 533        eZDebug::accumulatorStop( 'template_load' );
 534        if ( $resourceData['locales'] && !empty( $resourceData['locales'] ) )
 535        {
 536            $savedLocale = setlocale( LC_CTYPE, null );
 537            setlocale( LC_CTYPE, $resourceData['locales'] );
 538        }
 539
 540        $text = "";
 541
 542        if ( $root !== null or
 543             $resourceData['compiled-template'] )
 544        {
 545            if ( $this->ShowDetails )
 546                eZDebug::addTimingPoint( "Process" );
 547            eZDebug::accumulatorStart( 'template_processing', 'template_total', 'Template processing' );
 548
 549            $templateCompilationUsed = false;
 550            if ( $resourceData['compiled-template'] )
 551            {
 552                $textElements = array();
 553                if ( $this->executeCompiledTemplate( $resourceData, $textElements, "", "", $extraParameters ) )
 554                {
 555                    $text = implode( '', $textElements );
 556                    $templateCompilationUsed = true;
 557                }
 558            }
 559            if ( !$templateCompilationUsed )
 560            {
 561                if ( eZTemplate::isDebugEnabled() )
 562                {
 563                    $fname = $resourceData['template-filename'];
 564                    eZDebug::writeDebug( "FETCH START URI: $template, $fname" );
 565                }
 566                $this->process( $root, $text, "", "" );
 567                if ( eZTemplate::isDebugEnabled() )
 568                    eZDebug::writeDebug( "FETCH END URI: $template, $fname" );
 569            }
 570
 571            eZDebug::accumulatorStop( 'template_processing' );
 572            if ( $this->ShowDetails )
 573                eZDebug::addTimingPoint( "Process done" );
 574        }
 575
 576        eZDebug::accumulatorStop( 'template_total' );
 577
 578        if ( $resourceData['locales'] && !empty( $resourceData['locales'] ) )
 579        {
 580            setlocale( LC_CTYPE, $savedLocale );
 581        }
 582
 583        if ( $returnResourceData )
 584        {
 585            $resourceData['result_text'] = $text;
 586            return $resourceData;
 587        }
 588        return $text;
 589    }
 590
 591    function process( $root, &$text, $rootNamespace, $currentNamespace )
 592    {
 593        $this->createLocalVariablesList();
 594
 595        $textElements = array();
 596        $this->processNode( $root, $textElements, $rootNamespace, $currentNamespace );
 597        if ( is_array( $textElements ) )
 598            $text = implode( '', $textElements );
 599        else
 600            $text = $textElements;
 601
 602        $this->unsetLocalVariables();
 603        $this->destroyLocalVariablesList();
 604    }
 605
 606    function processNode( $node, &$textElements, $rootNamespace, $currentNamespace )
 607    {
 608        $rslt = null;
 609        $nodeType = $node[0];
 610        if ( $nodeType == eZTemplate::NODE_ROOT )
 611        {
 612            $children = $node[1];
 613            if ( $children )
 614            {
 615                foreach ( $children as $child )
 616                {
 617                    $this->processNode( $child, $textElements, $rootNamespace, $currentNamespace );
 618                    if ( !is_array( $textElements ) )
 619                        eZDebug::writeError( "Textelements is no longer array: '$textElements'",
 620                                             'eztemplate::processNode::root' );
 621                }
 622            }
 623        }
 624        else if ( $nodeType == eZTemplate::NODE_TEXT )
 625        {
 626            $textElements[] = $node[2];
 627        }
 628        else if ( $nodeType == eZTemplate::NODE_VARIABLE )
 629        {
 630            $variableData = $node[2];
 631            $variablePlacement = $node[3];
 632            $rslt = $this->processVariable( $textElements, $variableData, $variablePlacement, $rootNamespace, $currentNamespace );
 633            if ( !is_array( $textElements ) )
 634                eZDebug::writeError( "Textelements is no longer array: '$textElements'",
 635                                     'eztemplate::processNode::variable' );
 636        }
 637        else if ( $nodeType == eZTemplate::NODE_FUNCTION )
 638        {
 639            $functionChildren = $node[1];
 640            $functionName = $node[2];
 641            $functionParameters = $node[3];
 642            $functionPlacement = $node[4];
 643            $rslt = $this->processFunction( $functionName, $textElements, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace );
 644            if ( !is_array( $textElements ) )
 645                eZDebug::writeError( "Textelements is no longer array: '$textElements'",
 646                                     "eztemplate::processNode::function( '$functionName' )" );
 647        }
 648
 649        return $rslt;
 650    }
 651
 652    function processVariable( &$textElements, $variableData, $variablePlacement, $rootNamespace, $currentNamespace )
 653    {
 654        $value = $this->elementValue( $variableData, $rootNamespace, $currentNamespace, $variablePlacement );
 655        $this->appendElementText( $textElements, $value, $rootNamespace, $currentNamespace );
 656    }
 657
 658    function processFunction( $functionName, &$textElements, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace )
 659    {
 660        // Note: This code piece is replicated in the eZTemplateCompiler,
 661        //       if this code is changed the replicated code must be updated as well.
 662        $func = $this->Functions[$functionName];
 663        if ( is_array( $func ) )
 664        {
 665            $this->loadAndRegisterFunctions( $this->Functions[$functionName] );
 666            $func = $this->Functions[$functionName];
 667        }
 668        if ( isset( $func ) and
 669             is_object( $func ) )
 670        {
 671            if ( eZTemplate::isMethodDebugEnabled() )
 672                eZDebug::writeDebug( "START FUNCTION: $functionName" );
 673            $value = $func->process( $this, $textElements, $functionName, $functionChildren, $functionParameters, $functionPlacement, $rootNamespace, $currentNamespace );
 674            if ( eZTemplate::isMethodDebugEnabled() )
 675                eZDebug::writeDebug( "END FUNCTION: $functionName" );
 676            return $value;
 677        }
 678        else
 679        {
 680            $this->warning( "", "Function \"$functionName\" is not registered" );
 681        }
 682    }
 683
 684    function fetchFunctionObject( $functionName )
 685    {
 686        $func = $this->Functions[$functionName];
 687        if ( is_array( $func ) )
 688        {
 689            $this->loadAndRegisterFunctions( $this->Functions[$functionName] );
 690            $func = $this->Functions[$functionName];
 691        }
 692        return $func;
 693    }
 694
 695    /*!
 696     Loads the template using the URI $uri and parses it.
 697     \return The root node of the tree if \a $returnResourceData is false,
 698             if \c true the entire resource data structure.
 699    */
 700    function load( $uri, $extraParameters = false, $returnResourceData = false )
 701    {
 702        $resourceData = $this->loadURIRoot( $uri, true, $extraParameters );
 703        if ( !$resourceData or
 704             $resourceData['root-node'] === null )
 705        {
 706            $retValue = null;
 707            return $retValue;
 708        }
 709        else
 710            return $resourceData['root-node'];
 711    }
 712
 713    function parse( $sourceText, &$rootElement, $rootNamespace, &$resourceData )
 714    {
 715        $parser = eZTemplateMultiPassParser::instance();
 716        $parser->parse( $this, $sourceText, $rootElement, $rootNamespace, $resourceData );
 717    }
 718
 719    function loadURIData( $resourceObject, $uri, $resourceName, $template, &$extraParameters, $displayErrors = true )
 720    {
 721        $resourceData = $this->resourceData( $resourceObject, $uri, $resourceName, $template );
 722
 723        $resourceData['text'] = null;
 724        $resourceData['root-node'] = null;
 725        $resourceData['compiled-template'] = false;
 726        $resourceData['time-stamp'] = null;
 727        $resourceData['key-data'] = null;
 728        $resourceData['locales'] = null;
 729
 730        if ( !$resourceObject->handleResource( $this, $resourceData, eZTemplate::RESOURCE_FETCH, $extraParameters ) )
 731        {
 732            $resourceData = null;
 733            if ( $displayErrors )
 734                $this->warning( "", "No template could be loaded for \"$template\" using resource \"$resourceName\"" );
 735        }
 736        return $resourceData;
 737    }
 738
 739    /*!
 740     \static
 741     Creates a resource data structure of the parameters and returns it.
 742     This structure is passed to various parts of the template system.
 743
 744     \note If you only have the URI you should call resourceFor() first to
 745           figure out the resource handler.
 746    */
 747    function resourceData( $resourceObject, $uri, $resourceName, $templateName )
 748    {
 749        $resourceData = array();
 750        $resourceData['uri'] = $uri;
 751        $resourceData['resource'] = $resourceName;
 752        $resourceData['template-name'] = $templateName;
 753        $resourceData['template-filename'] = $templateName;
 754        $resourceData['handler'] = $resourceObject;
 755        $resourceData['test-compile'] = $this->TestCompile;
 756        return $resourceData;
 757    }
 758
 759    /*!
 760     Loads the template using the URI $uri and returns a structure with the text and timestamp,
 761     false otherwise.
 762     The structure keys are:
 763     - "text", the text.
 764     - "time-stamp", the timestamp.
 765    */
 766    function loadURIRoot( $uri, $displayErrors = true, &$extraParameters )
 767    {
 768        $res = "";
 769        $template = "";
 770        $resobj = $this->resourceFor( $uri, $res, $template );
 771
 772        if ( !is_object( $resobj ) )
 773        {
 774            if ( $displayErrors )
 775                $this->warning( "", "No resource handler for \"$res\" and no default resource handler, aborting." );
 776            return null;
 777        }
 778
 779        $canCache = true;
 780        if ( !$resobj->servesStaticData() )
 781            $canCache = false;
 782        if ( !$this->isCachingAllowed() )
 783            $canCache = false;
 784
 785        $resourceData = $this->loadURIData( $resobj, $uri, $res, $template, $extraParameters, $displayErrors );
 786
 787        if ( $resourceData )
 788        {
 789            $root = null;
 790            eZTemplate::appendTemplateToStatisticsIfNeeded( $resourceData['template-name'], $resourceData['template-filename'] );
 791            $this->appendTemplateFetch( $resourceData['template-filename'] );
 792
 793            if ( !$resourceData['compiled-template'] and
 794                 $resourceData['root-node'] === null )
 795            {
 796                $resourceData['root-node'] = array( eZTemplate::NODE_ROOT, false );
 797                $templateText = $resourceData["text"];
 798                $keyData = $resourceData['key-data'];
 799                $this->setIncludeText( $uri, $templateText );
 800                $rootNamespace = '';
 801                $this->parse( $templateText, $resourceData['root-node'], $rootNamespace, $resourceData );
 802
 803                if ( eZTemplate::isDebugEnabled() )
 804                {
 805                    $this->appendDebugNodes( $resourceData['root-node'], $resourceData );
 806                }
 807
 808                if ( $canCache )
 809                    $resobj->setCachedTemplateTree( $keyData, $uri, $res, $template, $extraParameters, $resourceData['root-node'] );
 810            }
 811            if ( !$resourceData['compiled-template'] and
 812                 $canCache and
 813                 $this->canCompileTemplate( $resourceData, $extraParameters ) )
 814            {
 815                $generateStatus = $this->compileTemplate( $resourceData, $extraParameters );
 816                if ( $generateStatus )
 817                    $resourceData['compiled-template'] = true;
 818            }
 819        }
 820
 821        return $resourceData;
 822    }
 823
 824    function processURI( $uri, $displayErrors = true, &$extraParameters,
 825                         &$textElements, $rootNamespace, $currentNamespace )
 826    {
 827        $this->Level++;
 828        if ( $this->Level > $this->MaxLevel )
 829        {
 830            eZDebug::writeError( $this->MaxLevelWarning,  "eZTemplate:processURI Level: $this->Level @ $uri" );
 831            $textElements[] = $this->MaxLevelWarning;
 832            $this->Level--;
 833            return;
 834        }
 835        $resourceData = $this->loadURIRoot( $uri, $displayErrors, $extraParameters );
 836        if ( !$resourceData or
 837             ( !$resourceData['compiled-template'] and
 838               $resourceData['root-node'] === null ) )
 839        {
 840            $this->Level--;
 841            return;
 842        }
 843
 844        $templateCompilationUsed = false;
 845
 846        if ( $resourceData['locales'] && !empty( $resourceData['locales'] ) )
 847        {
 848            $savedLocale = setlocale( LC_CTYPE, null );
 849            setlocale( LC_CTYPE, $resourceData['locales'] );
 850        }
 851
 852        if ( $resourceData['compiled-template'] )
 853        {
 854            if ( $this->executeCompiledTemplate( $resourceData, $textElements, $rootNamespace, $currentNamespace, $extraParameters ) )
 855                $templateCompilationUsed = true;
 856        }
 857        if ( !$templateCompilationUsed )
 858        {
 859            $text = null;
 860            if ( eZTemplate::isDebugEnabled() )
 861            {
 862                $fname = $resourceData['template-filename'];
 863                eZDebug::writeDebug( "START URI: $uri, $fname" );
 864            }
 865            $this->process( $resourceData['root-node'], $text, $rootNamespace, $currentNamespace );
 866            if ( eZTemplate::isDebugEnabled() )
 867                eZDebug::writeDebug( "END URI: $uri, $fname" );
 868            $this->setIncludeOutput( $uri, $text );
 869            $textElements[] = $text;
 870        }
 871
 872        if ( $resourceData['locales'] && !empty( $resourceData['locales'] ) )
 873        {
 874            setlocale( LC_CTYPE, $savedLocale );
 875        }
 876        $this->Level--;
 877
 878    }
 879
 880    function canCompileTemplate( $resourceData, &$extraParameters )
 881    {
 882        $resourceObject = $resourceData['handler'];
 883        if ( !$resourceObject )
 884            return false;
 885        $canGenerate = $resourceObject->canCompileTemplate( $this, $resourceData, $extraParameters );
 886        return $canGenerate;
 887    }
 888
 889    /*!
 890     Validates the template file \a $file and returns \c true if the file has correct syntax.
 891     \param $returnResourceData If \c true then the returned value will be the resourcedata structure
 892     \sa compileTemplateFile(), fetch()
 893    */
 894    function validateTemplateFile( $file, $returnResourceData = false )
 895    {
 896        $this->resetErrorLog();
 897
 898        if ( !file_exists( $file ) )
 899            return false;
 900        $resourceHandler = $this->resourceFor( $file, $resourceName, $templateName );
 901        if ( !$resourceHandler )
 902            return false;
 903        $resourceData = $this->resourceData( $resourceHandler, $file, $resourceName, $templateName );
 904        $resourceData['key-data'] = "file:" . $file;
 905        $key = md5( $resourceData['key-data'] );
 906        $extraParameters = array();
 907
 908        // Disable caching/compiling while fetchin the resource
 909        // It will be restored afterwards
 910        $isCachingAllowed = $this->IsCachingAllowed;
 911        $this->IsCachingAllowed = false;
 912
 913        $resourceHandler->handleResource( $this, $resourceData, eZTemplate::RESOURCE_FETCH, $extraParameters );
 914
 915        // Restore previous caching flag
 916        $this->IsCachingAllowed = $isCachingAllowed;
 917
 918        $root =& $resourceData['root-node'];
 919        $root = array( eZTemplate::NODE_ROOT, false );
 920        $templateText = $resourceData["text"];
 921        $rootNamespace = '';
 922        $this->parse( $templateText, $root, $rootNamespace, $resourceData );
 923        if ( eZTemplate::isDebugEnabled() )
 924        {
 925            $this->appendDebugNodes( $root, $resourceData );
 926        }
 927
 928        $result = !$this->hasErrors() and !$this->hasWarnings();
 929        if ( $returnResourceData )
 930        {
 931            $resourceData['result'] = $result;
 932            return $resourceData;
 933        }
 934        return $result;
 935    }
 936
 937    /*!
 938     Compiles the template file \a $file and returns \c true if the compilation was OK.
 939     \param $returnResourceData If \c true then the returned value will be the resourcedata structure
 940     \sa validateTemplateFile(), fetch()
 941    */
 942    function compileTemplateFile( $file, $returnResourceData = false )
 943    {
 944        $this->resetErrorLog();
 945
 946        if ( !file_exists( $file ) )
 947            return false;
 948        $resourceHandler = $this->resourceFor( $file, $resourceName, $templateName );
 949        if ( !$resourceHandler )
 950            return false;
 951        $resourceData = $this->resourceData( $resourceHandler, $file, $resourceName, $templateName );
 952        $resourceData['key-data'] = "file:" . $file;
 953        $key = md5( $resourceData['key-data'] );
 954        $extraParameters = array();
 955        $resourceHandler->handleResource( $this, $resourceData, eZTemplate::RESOURCE_FETCH, $extraParameters );
 956
 957        $isCompiled = false;
 958        if ( isset( $resourceData['compiled-template'] ) )
 959            $isCompiled = $resourceData['compiled-template'];
 960
 961        if ( !$isCompiled )
 962        {
 963            $root =& $resourceData['root-node'];
 964            $root = array( eZTemplate::NODE_ROOT, false );
 965            $templateText = $resourceData["text"];
 966            $rootNamespace = '';
 967            $this->parse( $templateText, $root, $rootNamespace, $resourceData );
 968            if ( eZTemplate::isDebugEnabled() )
 969            {
 970                $this->appendDebugNodes( $root, $resourceData );
 971            }
 972
 973            $result = eZTemplateCompiler::compileTemplate( $this, $key, $resourceData );
 974        }
 975        else
 976        {
 977            $result = true;
 978        }
 979
 980        if ( $returnResourceData )
 981        {
 982            $resourceData['result'] = $result;
 983            return $resourceData;
 984        }
 985        return $result;
 986    }
 987
 988    function compileTemplate( &$resourceData, &$extraParameters )
 989    {
 990        $resourceObject = $resourceData['handler'];
 991        if ( !$resourceObject )
 992            return false;
 993        $keyData = $resourceData['key-data'];
 994        $uri = $resourceData['uri'];
 995        $resourceName = $resourceData['resource'];
 996        $templatePath = $resourceData['template-name'];
 997        return $resourceObject->compileTemplate( $this, $keyData, $uri, $resourceName, $templatePath, $extraParameters, $resourceData );
 998    }
 999
1000    function executeCompiledTemplate( &$resourceData, &$textElements, $rootNamespace, $currentNamespace, &$extraParameters )
1001    {
1002        $resourceObject = $resourceData['handler'];
1003        if ( !$resourceObject )
1004            return false;
1005        $keyData = $resourceData['key-data'];
1006        $uri = $resourceData['uri'];
1007        $resourceName = $resourceData['resource'];
1008        $templatePath = $resourceData['template-name'];
1009        $timestamp = $resourceData['time-stamp'];
1010        return $resourceObject->executeCompiledTemplate( $this, $textElements,
1011                                                         $keyData, $uri, $resourceData, $templatePath,
1012                                                         $extraParameters, $timestamp,
1013                                                         $rootNamespace, $currentNamespace );
1014    }
1015
1016    /*!
1017     Returns the resource object for URI $uri. If a resource type is specified
1018     in the URI it is extracted and set in $res. The template name is set in $template
1019     without any resource specifier. To specify a resource the name and a ":" is
1020     prepended to the URI, for instance file:my.tpl.
1021     If no resource type is found the URI the default resource handler is used.
1022    */
1023    function resourceFor( $uri, &$res, &$template )
1024    {
1025        $args = explode( ":", $uri );
1026        if ( isset( $args[1] ) )
1027        {
1028            $res = $args[0];
1029            $template = $args[1];
1030        }
1031        else
1032            $template = $uri;
1033        if ( eZTemplate::isDebugEnabled() )
1034        {
1035            eZDebug::writeNotice( "eZTemplate: Loading template \"$template\" with resource \"$res\"" );
1036        }
1037        if ( isset( $this->Resources[$res] ) and is_object( $this->Resources[$res] ) )
1038        {
1039            return $this->Resources[$res];
1040        }
1041        return $this->DefaultResource;
1042    }
1043
1044    /*!
1045     \return The resource handler object for resource name \a $resourceName.
1046     \sa resourceFor
1047    */
1048    function resourceHandler( $resourceName )
1049    {
1050        if ( isset( $this->Resources[$resourceName] ) &&
1051             is_object( $this->Resources[$resourceName] ) )
1052        {
1053            return $this->Resources[$resourceName];
1054        }
1055        return $this->DefaultResource;
1056    }
1057
1058    function hasChildren( &$function, $functionName )
1059    {
1060        $hasChildren = $function->hasChildren();
1061        if ( is_array( $hasChildren ) )
1062            return $hasChildren[$functionName];
1063        else
1064            return $hasChildren;
1065     }
1066
1067    /*!
1068     Returns the empty variable type.
1069    */
1070    function emptyVariable()
1071    {
1072        return array( "type" => "null" );
1073    }
1074
1075    /*!
1076     \static
1077    */
1078    function mergeNamespace( $rootNamespace, $additionalNamespace )
1079    {
1080        $namespace = $rootNamespace;
1081        if ( $namespace == '' )
1082            $namespace = $additionalNamespace;
1083        else if ( $additionalNamespace != '' )
1084            $namespace = "$namespace:$additionalNamespace";
1085        return $namespace;
1086    }
1087
1088    /*!
1089     Returns the actual value of a template type or null if an unknown type.
1090    */
1091    function elementValue( &$dataElements, $rootNamespace, $currentNamespace, $placement = false,
1092                           $checkExistance = false, $checkForProxy = false )
1093    {
1094        /*
1095         * We use a small dirty hack in this function...
1096         * To help the caller to determine if the value was a proxy object,
1097         * we store boolean true to $dataElements['proxy-object-found'] in this case.
1098         * (it's up to caller to remove this garbage from $dataElements...)
1099         * This behaviour is enabled by $checkForProxy parameter.
1100         */
1101
1102        $value = null;
1103        if ( !is_array( $dataElements ) )
1104        {
1105            $this->error( "elementValue",
1106                          "Missing array data structure, got " . gettype( $dataElements ) );
1107            return null;
1108        }
1109        foreach ( $dataElements as $dataElement )
1110        {
1111            if ( $dataElement === null )
1112            {
1113                return null;
1114            }
1115            $dataType = $dataElement[0];
1116            switch ( $dataType )
1117            {
1118                case eZTemplate::TYPE_VOID:
1119                {
1120                    if ( !$checkExistance )
1121                        $this->warning( 'elementValue',
1122                                        'Found void datatype, should not be used' );
1123                    else
1124                    {
1125                        return null;
1126                    }
1127                } break;
1128                case eZTemplate::TYPE_STRING:
1129                case eZTemplate::TYPE_NUMERIC:
1130                case eZTemplate::TYPE_IDENTIFIER:
1131                case eZTemplate::TYPE_BOOLEAN:
1132                case eZTemplate::TYPE_ARRAY:
1133                {
1134                    $value = $dataElement[1];
1135                } break;
1136                case eZTemplate::TYPE_VARIABLE:
1137                {
1138                    $variableData = $dataElement[1];
1139                    $variableNamespace = $variableData[0];
1140                    $variableNamespaceScope = $variableData[1];
1141                    $variableName = $variableData[2];
1142                    if ( $variableNamespaceScope == eZTemplate::NAMESPACE_SCOPE_GLOBAL )
1143                        $namespace = $variableNamespace;
1144                    else if ( $variableNamespaceScope == eZTemplate::NAMESPACE_SCOPE_LOCAL )
1145                        $namespace = $this->mergeNamespace( $rootNamespace, $variableNamespace );
1146                    else if ( $variableNamespaceScope == eZTemplate::NAMESPACE_SCOPE_RELATIVE )
1147                        $namespace = $this->mergeNamespace( $currentNamespace, $variableNamespace );
1148                    else
1149                        $namespace = false;
1150                    if ( $this->hasVariable( $variableName, $namespace ) )
1151                    {
1152                        $value = $this->variable( $variableName, $namespace );
1153                    }
1154                    else
1155                    {
1156                        if ( !$checkExistance )
1157                            $this->error( '', "Unknown template variable '$variableName' in namespace '$namespace'", $placement );
1158                        {
1159                            return null;
1160                        }
1161                    }
1162                } break;
1163                case eZTemplate::TYPE_ATTRIBUTE:
1164                {
1165                    $attributeData = $dataElement[1];
1166                    $attributeValue = $this->elementValue( $attributeData, $rootNamespace, $currentNamespace, false, $checkExistance );
1167
1168                    if ( $attributeValue !== null )
1169                    {
1170                        if ( !is_numeric( $attributeValue ) and
1171                             !is_string( $attributeValue ) and
1172                             !is_bool( $attributeValue ) )
1173                        {
1174                            if ( !$checkExistance )
1175                                $this->error( "",
1176                                              "Cannot use type " . gettype( $attributeValue ) . " for attribute lookup", $placement );
1177                            {
1178                                return null;
1179                            }
1180                        }
1181                        if ( is_array( $value ) )
1182                        {
1183                            if ( array_key_exists( $attributeValue, $value ) )
1184                            {
1185                                $value = $value[$attributeValue];
1186                            }
1187                            else
1188                            {
1189                                if ( !$checkExistance )
1190                                {
1191                                    $arrayAttributeList = array_keys( $value );
1192                                    $arrayCount = count( $arrayAttributeList );
1193                                    $errorMessage = "No such attribute for array($arrayCount): $attributeValue";
1194                                    $chooseText = "Choose one of following: ";
1195                                    $errorMessage .= "\n$chooseText";
1196                                    $errorMessage .= $this->expandAttributes( $arrayAttributeList, $chooseText, 25 );
1197                                    $this->error( "",
1198                                                  $errorMessage, $placement );
1199                                }
1200                                return null;
1201                            }
1202                        }
1203                        else if ( is_object( $value ) )
1204                        {
1205                            if ( method_exists( $value, "attribute" ) and
1206                                 method_exists( $value, "hasattribute" ) )
1207                            {
1208                                if ( $value->hasAttribute( $attributeValue ) )
1209                                {
1210                                    $value = $value->attribute( $attributeValue );
1211                                }
1212                                else
1213                                {
1214                                    if ( !$checkExistance )
1215                                    {
1216                                        $objectAttributeList = array();
1217                                        if ( method_exists( $value, 'attributes' ) )
1218                                            $objectAttributeList = $value->attributes();
1219                                        $objectClass= get_class( $value );
1220                                        $errorMessage = "No such attribute for object($objectClass): $attributeValue";
1221                                        $chooseText = "Choose one of following: ";
1222                                        $errorMessage .= "\n$chooseText";
1223                                        $errorMessage .= $this->expandAttributes( $objectAttributeList, $chooseText, 25 );
1224                                        $this->error( "",
1225                                                      $errorMessage, $placement );
1226                                    }
1227                                    return null;
1228                                }
1229                            }
1230                            else
1231                            {
1232                                if ( !$checkExistance )
1233                                    $this->error( "",
1234                                                  "Cannot retrieve attribute of object(" . get_class( $value ) .
1235                                                  "), no attribute functions available",
1236                                                  $placement );
1237                                return null;
1238                            }
1239                        }
1240                        else
1241                        {
1242                            if ( !$checkExistance )
1243                                $this->error( "",
1244                                              "Cannot retrieve attribute of a " . gettype( $value ),
1245                                              $placement );
1246                            return null;
1247                        }
1248                    }
1249                    else
1250                    {
1251                        if ( !$checkExistance )
1252                            $this->error( '',
1253                                          'Attribute value was null, cannot get attribute',
1254                                          $placement );
1255                        return null;
1256                    }
1257                } break;
1258                case eZTemplate::TYPE_OPERATOR:
1259                {
1260                    $operatorParameters = $dataElement[1];
1261                    $operatorName = $operatorParameters[0];
1262                    $operatorParameters = array_splice( $operatorParameters, 1 );
1263                    if ( is_object( $value ) and
1264                         method_exists( $value, 'templateValue' ) )
1265                    {
1266                        if ( $checkForProxy )
1267                            $dataElements['proxy-object-found'] = true;
1268                        $value = $value->templateValue();
1269                    }
1270                    $valueData = array( 'value' => $value );
1271                    $this->processOperator( $operatorName, $operatorParameters, $rootNamespace, $currentNamespace,
1272                                            $valueData, $placement, $checkExistance );
1273                    $value = $valueData['value'];
1274                } break;
1275                default:
1276                {
1277                    if ( !$checkExistance )
1278                        $this->error( "elementValue",
1279                                      "Unknown data type: '$dataType'" );
1280                    return null;
1281                }
1282            }
1283        }
1284        if ( is_object( $value ) and
1285             method_exists( $value, 'templateValue' ) )
1286        {
1287            if ( $checkForProxy )
1288                $dataElements['proxy-object-found'] = true;
1289            return $value->templateValue();
1290        }
1291        return $value;
1292    }
1293
1294    function expandAttributes( $attributeList, $chooseText, $maxThreshold, $minThreshold = 1 )
1295    {
1296        $errorMessage = '';
1297        $attributeCount = count( $attributeList );
1298        if ( $attributeCount < $minThreshold )
1299            return $errorMessage;
1300        if ( $attributeCount < $maxThreshold )
1301        {
1302            $chooseLength = strlen( $chooseText );
1303            $attributeText = '';
1304            $i = 0;
1305            foreach ( $attributeList as $attributeName )
1306            {
1307                if ( $i > 0 )
1308                    $attributeText .= ",";
1309                if ( strlen( $attributeText ) > 40 )
1310                {
1311                    $attributeText .= "\n";
1312                    $errorMessage .= $attributeText;
1313                    $errorMessage .= str_repeat( ' ', $chooseLength );
1314                    $attributeText = '';
1315                }
1316                else if ( $i > 0 )
1317                    $attributeText .= " ";
1318                $attributeText .= $attributeName;
1319                ++$i;
1320            }
1321            $errorMessage .= $attributeText;
1322        }
1323

Large files files are truncated, but you can click here to view the full file