PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/eztemplate/classes/eztemplateattributeoperator.php

http://github.com/ezsystems/ezpublish
PHP | 189 lines | 93 code | 18 blank | 78 comment | 12 complexity | 2e5b4e6a4be5b4bef37fd2922f904433 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * File containing the eZTemplateAttributeOperator class.
  4. *
  5. * @copyright Copyright (C) eZ Systems AS. All rights reserved.
  6. * @license For full copyright and license information view LICENSE file distributed with this source code.
  7. * @version //autogentag//
  8. * @package lib
  9. */
  10. /*!
  11. \class eZTemplateAttributeOperator eztemplateattributeoperator.php
  12. \ingroup eZTemplateOperators
  13. \brief Display of variable attributes using operator "attribute" or dumps a variable using the operator "dump"
  14. This class allows for displaying template variable attributes. The display
  15. is recursive and the number of levels can be maximized.
  16. The "attribute" operator can take three parameters. The first is whether to show
  17. variable values or not, default is to not show. The second is the maximum number
  18. of levels to recurse, if blank or omitted the maxium level is 2.
  19. The third is the type of display, if set to "text" the output is as pure text
  20. otherwise as html. The default output is configured in template.ini.
  21. The "dump" operator does exactly what the "attribute" operator does with following
  22. exceptions:
  23. - The default maximum number of levels to recurse is 1
  24. - By default, it shows the values of arrays and object properties
  25. - it can handle primitive variables and NULL values
  26. \code
  27. // Example template code for operator 'attribute'
  28. // Display attributes of $myvar
  29. {$myvar|attribute}
  30. // Display 2 levels of $tree
  31. {$tree|attribute(show,2)}
  32. // Display attributes and values of $item
  33. {$item|attribute(show)}
  34. // Example template code for operator 'dump'
  35. // Dumps out $myvar - can handle primitive variables, arrays and objects.
  36. // By default it shows array values or object properties.
  37. {$myvar|dump()}
  38. // Show 2 levels of $tree (default is 1)
  39. {$tree|dump(show, 2)}
  40. \endcode
  41. */
  42. class eZTemplateAttributeOperator
  43. {
  44. /**
  45. * Initializes the object with the name $attributeName, default is "attribute" and $dumpName, default is 'dump'
  46. *
  47. * @param string $attributeName
  48. * @param string $dumpName
  49. */
  50. public function __construct( $attributeName = 'attribute', $dumpName = 'dump' )
  51. {
  52. $this->AttributeName = $attributeName;
  53. $this->DumpName = $dumpName;
  54. $this->Operators = array( $attributeName, $dumpName );
  55. }
  56. /*!
  57. Returns the template operators.
  58. */
  59. function operatorList()
  60. {
  61. return $this->Operators;
  62. }
  63. function operatorTemplateHints()
  64. {
  65. return array( $this->AttributeName => array( 'input' => true,
  66. 'output' => true,
  67. 'parameters' => 3 ),
  68. $this->DumpName => array( 'input' => true,
  69. 'outpout' => true,
  70. 'parameters' => 3 ) );
  71. }
  72. /*!
  73. See eZTemplateOperator::namedParameterList()
  74. */
  75. function namedParameterList()
  76. {
  77. return array( $this->AttributeName => array( "show_values" => array( "type" => "string",
  78. "required" => false,
  79. "default" => "" ),
  80. "max_val" => array( "type" => "numerical",
  81. "required" => false,
  82. "default" => 2 ),
  83. "format" => array( "type" => "boolean",
  84. "required" => false,
  85. "default" => eZINI::instance( 'template.ini' )->variable( 'AttributeOperator', 'DefaultFormatter' ) ) ),
  86. $this->DumpName => array( "show_values" => array( "type" => "string",
  87. "required" => false,
  88. "default" => "show" ),
  89. "max_val" => array( "type" => "numerical",
  90. "required" => false,
  91. "default" => 1 ),
  92. "format" => array( "type" => "boolean",
  93. "required" => false,
  94. "default" => eZINI::instance( 'template.ini' )->variable( 'AttributeOperator', 'DefaultFormatter' ) ) ) );
  95. }
  96. /*!
  97. \return true to tell the template engine that the parameter list exists per operator type.
  98. */
  99. function namedParameterPerOperator()
  100. {
  101. return true;
  102. }
  103. /*!
  104. Display the variable.
  105. */
  106. function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters, $placement )
  107. {
  108. $showValues = $namedParameters[ 'show_values' ] == 'show';
  109. $max = $namedParameters[ 'max_val' ];
  110. $format = $namedParameters[ 'format' ];
  111. $formatter = ezpAttributeOperatorManager::getOutputFormatter( $format );
  112. // check for an object or an array that is not empty
  113. if ( is_object( $operatorValue ) || ( is_array( $operatorValue ) && !empty( $operatorValue ) ) )
  114. {
  115. $outputString = "";
  116. $this->displayVariable( $operatorValue, $formatter, $showValues, $max, 0, $outputString );
  117. if ( $formatter instanceof ezpAttributeOperatorFormatterInterface )
  118. $operatorValue = $formatter->header( $outputString, $showValues );
  119. }
  120. else if ( $formatter instanceof ezpAttributeOperatorFormatterInterface )
  121. {
  122. $operatorValue = $formatter->exportScalar( $operatorValue );
  123. }
  124. }
  125. /*!
  126. \private
  127. Helper function for recursive display of attributes.
  128. $value is the current variable, $as_html is true if display as html,
  129. $max is the maximum number of levels, $cur_level the current level
  130. and $outputString is the output text which the function adds to.
  131. */
  132. function displayVariable( &$value, ezpAttributeOperatorFormatterInterface $formatter, $showValues, $max, $level, &$outputString )
  133. {
  134. if ( $max !== false and $level >= $max )
  135. return;
  136. if ( is_array( $value ) )
  137. {
  138. foreach ( $value as $key => $item )
  139. {
  140. $outputString .= $formatter->line( $key, $item, $showValues, $level );
  141. $this->displayVariable( $item, $formatter, $showValues, $max, $level + 1, $outputString );
  142. }
  143. }
  144. else if ( is_object( $value ) )
  145. {
  146. if ( !method_exists( $value, "attributes" ) or
  147. !method_exists( $value, "attribute" ) )
  148. return;
  149. foreach ( $value->attributes() as $key )
  150. {
  151. $item = $value->attribute( $key );
  152. $outputString .= $formatter->line( $key, $item, $showValues, $level );
  153. $this->displayVariable( $item, $formatter, $showValues, $max, $level + 1, $outputString );
  154. }
  155. }
  156. }
  157. /// The array of operators, used for registering operators
  158. public $Operators;
  159. }
  160. ?>