PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/eztemplate/classes/eztemplateattributeoperator.php

https://github.com/aurelienRT1/ezpublish
PHP | 219 lines | 134 code | 11 blank | 74 comment | 32 complexity | 8ae7bf82c446b17dcd06044f6c20fbc3 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. //
  3. // Definition of eZTemplateAttributeOperator class
  4. //
  5. // Created on: <01-Mar-2002 13:50:09 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. \class eZTemplateAttributeOperator eztemplateattributeoperator.php
  32. \ingroup eZTemplateOperators
  33. \brief Display of variable attributes using operator "attribute"
  34. This class allows for displaying template variable attributes. The display
  35. is recursive and the number of levels can be maximized.
  36. The operator can take three parameters. The first is the maximum number of
  37. levels to recurse, if blank or omitted the maxium level is infinity.
  38. The second is the type of display, if set to "text" the output is as pure text
  39. otherwise as html.
  40. The third is whether to show variable values or not, default is to not show.
  41. \code
  42. // Example template code
  43. // Display attributes of $myvar
  44. {$myvar|attribute}
  45. // Display 2 levels of $tree
  46. {$tree|attribute(2)}
  47. // Display attributes and values of $item
  48. {$item|attribute(,,show)}
  49. \endcode
  50. */
  51. class eZTemplateAttributeOperator
  52. {
  53. /*!
  54. Initializes the object with the name $name, default is "attribute".
  55. */
  56. function eZTemplateAttributeOperator( $name = "attribute" )
  57. {
  58. $this->AttributeName = $name;
  59. $this->Operators = array( $name );
  60. }
  61. /*!
  62. Returns the template operators.
  63. */
  64. function operatorList()
  65. {
  66. return $this->Operators;
  67. }
  68. function operatorTemplateHints()
  69. {
  70. return array( $this->AttributeName => array( 'input' => true,
  71. 'output' => true,
  72. 'parameters' => 3 ) );
  73. }
  74. /*!
  75. See eZTemplateOperator::namedParameterList()
  76. */
  77. function namedParameterList()
  78. {
  79. return array( "show_values" => array( "type" => "string",
  80. "required" => false,
  81. "default" => "" ),
  82. "max_val" => array( "type" => "numerical",
  83. "required" => false,
  84. "default" => 2 ),
  85. "as_html" => array( "type" => "boolean",
  86. "required" => false,
  87. "default" => true ) );
  88. }
  89. /*!
  90. Display the variable.
  91. */
  92. function modify( $tpl, $operatorName, $operatorParameters, $rootNamespace, $currentNamespace, &$operatorValue, $namedParameters )
  93. {
  94. $max = $namedParameters["max_val"];
  95. $as_html = $namedParameters["as_html"];
  96. $show_values = $namedParameters["show_values"] == "show";
  97. $txt = "";
  98. $this->displayVariable( $operatorValue, $as_html, $show_values, $max, 0, $txt );
  99. if ( $as_html )
  100. {
  101. $headers = "<th align=\"left\">Attribute</th>\n<th align=\"left\">Type</th>\n";
  102. if ( $show_values )
  103. $headers .= "<th align=\"left\">Value</th>\n";
  104. $operatorValue = "<table><tr>$headers</tr>\n$txt</table>\n";
  105. }
  106. else
  107. $operatorValue = $txt;
  108. }
  109. /*!
  110. \private
  111. Helper function for recursive display of attributes.
  112. $value is the current variable, $as_html is true if display as html,
  113. $max is the maximum number of levels, $cur_level the current level
  114. and $txt is the output text which the function adds to.
  115. */
  116. function displayVariable( &$value, $as_html, $show_values, $max, $cur_level, &$txt )
  117. {
  118. if ( $max !== false and $cur_level >= $max )
  119. return;
  120. if ( is_array( $value ) )
  121. {
  122. foreach( $value as $key => $item )
  123. {
  124. $type = gettype( $item );
  125. if ( is_object( $item ) )
  126. $type .= "[" . get_class( $item ) . "]";
  127. if ( is_bool( $item ) )
  128. $itemValue = $item ? "true" : "false";
  129. else if ( is_array( $item ) )
  130. $itemValue = 'Array(' . count( $item ) . ')';
  131. else if ( is_string( $item ) )
  132. $itemValue = "'" . $item . "'";
  133. else if ( is_object( $item ) )
  134. $itemValue = method_exists( $item, '__toString' ) ? (string)$item : 'Object';
  135. else
  136. $itemValue = $item;
  137. if ( $as_html )
  138. {
  139. $spacing = str_repeat( "&gt;", $cur_level );
  140. if ( $show_values )
  141. $txt .= "<tr><td>$spacing$key</td>\n<td>$type</td>\n<td>$itemValue</td>\n</tr>\n";
  142. else
  143. $txt .= "<tr><td>$spacing$key</td>\n<td>$type</td>\n</tr>\n";
  144. }
  145. else
  146. {
  147. $spacing = str_repeat( " ", $cur_level*4 );
  148. if ( $show_values )
  149. $txt .= "$spacing$key ($type=$itemValue)\n";
  150. else
  151. $txt .= "$spacing$key ($type)\n";
  152. }
  153. $this->displayVariable( $item, $as_html, $show_values, $max, $cur_level + 1, $txt );
  154. }
  155. }
  156. else if ( is_object( $value ) )
  157. {
  158. if ( !method_exists( $value, "attributes" ) or
  159. !method_exists( $value, "attribute" ) )
  160. return;
  161. $attrs = $value->attributes();
  162. foreach ( $attrs as $key )
  163. {
  164. $item = $value->attribute( $key );
  165. $type = gettype( $item );
  166. if ( is_object( $item ) )
  167. $type .= "[" . get_class( $item ) . "]";
  168. if ( is_bool( $item ) )
  169. $itemValue = $item ? "true" : "false";
  170. else if ( is_array( $item ) )
  171. $itemValue = 'Array(' . count( $item ) . ')';
  172. else if ( is_numeric( $item ) )
  173. $itemValue = $item;
  174. else if ( is_string( $item ) )
  175. $itemValue = "'" . $item . "'";
  176. else if ( is_object( $item ) )
  177. $itemValue = method_exists( $item, '__toString' ) ? (string)$item : 'Object';
  178. else
  179. $itemValue = $item;
  180. if ( $as_html )
  181. {
  182. $spacing = str_repeat( "&gt;", $cur_level );
  183. if ( $show_values )
  184. $txt .= "<tr><td>$spacing$key</td>\n<td>$type</td>\n<td>$itemValue</td>\n</tr>\n";
  185. else
  186. $txt .= "<tr><td>$spacing$key</td>\n<td>$type</td>\n</tr>\n";
  187. }
  188. else
  189. {
  190. $spacing = str_repeat( " ", $cur_level*4 );
  191. if ( $show_values )
  192. $txt .= "$spacing$key ($type=$itemValue)\n";
  193. else
  194. $txt .= "$spacing$key ($type)\n";
  195. }
  196. $this->displayVariable( $item, $as_html, $show_values, $max, $cur_level + 1, $txt );
  197. }
  198. }
  199. }
  200. /// The array of operators, used for registering operators
  201. public $Operators;
  202. }
  203. ?>