/library/EZ/View/Smarty/Plugin/Standard.php

https://github.com/TheOnly92/Ikariem · PHP · 216 lines · 153 code · 19 blank · 44 comment · 33 complexity · f840f41b376fa02b47462512ad4b9256 MD5 · raw file

  1. <?php
  2. class EZ_View_Smarty_Plugin_Standard extends EZ_View_Smarty_Plugin_Abstract {
  3. public static function layoutFunction($params, &$smarty) {
  4. $section = $params ['section'];
  5. $toReturn = "";
  6. try {
  7. //oppress errors
  8. @$toReturn = $smarty->_tpl_vars ['this']->placeholder ( 'Zend_Layout' )->$section;
  9. return $toReturn;
  10. } catch ( Exception $e ) {
  11. return $toReturn;
  12. }
  13. }
  14. public static function headTitleFunction($params, &$smarty) {
  15. $method = (isset ( $params ['method'] )) ? $params ['method'] : - 1;
  16. unset ( $params ['method'] );
  17. $args = (isset ( $params ['args'] )) ? self::parseArgs ( $params ['args'] ) : null;
  18. $toReturn = "";
  19. try {
  20. //oppress errors
  21. if ($method != - 1) {
  22. @$toReturn = $smarty->_tpl_vars ['this']->headTitle ()->$method ( $args );
  23. } else {
  24. $toReturn = $smarty->_tpl_vars ['this']->headTitle ();
  25. }
  26. return $toReturn;
  27. } catch ( Exception $e ) {
  28. return $toReturn;
  29. }
  30. }
  31. public static function headScriptFunction($params, &$smarty) {
  32. $method = (isset ( $params ['method'] )) ? $params ['method'] : - 1;
  33. unset ( $params ['method'] );
  34. $args = (isset ( $params ['args'] )) ? self::parseArgs ( $params ['args'] ) : null;
  35. $toReturn = "";
  36. try {
  37. if ($method != - 1) {
  38. //oppress errors
  39. if (is_array ( $args )) {
  40. //$toReturn = call_user_func('$smarty->_tpl_vars["this"]->headScript()->$method()',$args,true);
  41. //print "args src= ".$args['src'];
  42. //TODO: fix args usage
  43. $toReturn = $smarty->_tpl_vars ['this']->headScript ()->$method ( $args ['src'] );
  44. } else {
  45. @$toReturn = $smarty->_tpl_vars ['this']->headScript ()->$method ( $args );
  46. }
  47. } else {
  48. $toReturn = $smarty->_tpl_vars ['this']->headScript ();
  49. }
  50. return $toReturn;
  51. } catch ( Exception $e ) {
  52. return $toReturn;
  53. }
  54. }
  55. public static function jsLocaleFunction($params, &$smarty) {
  56. $method = (isset ( $params ['method'] )) ? $params ['method'] : - 1;
  57. unset ( $params ['method'] );
  58. $args = (isset ( $params ['args'] )) ? self::parseArgs ( $params ['args'] ) : null;
  59. $toReturn = "";
  60. try {
  61. if ($method != - 1) {
  62. //oppress errors
  63. if (is_array ( $args )) {
  64. //$toReturn = call_user_func('$smarty->_tpl_vars["this"]->headScript()->$method()',$args,true);
  65. //print "args src= ".$args['src'];
  66. //TODO: fix args usage
  67. $toReturn = $smarty->_tpl_vars ['this']->jsLocale ()->$method ( $args ['src'] );
  68. } else {
  69. @$toReturn = $smarty->_tpl_vars ['this']->jsLocale ()->$method ( $args );
  70. }
  71. } else {
  72. $toReturn = $smarty->_tpl_vars ['this']->jsLocale ();
  73. }
  74. return $toReturn;
  75. } catch ( Exception $e ) {
  76. return $toReturn;
  77. }
  78. }
  79. public static function headStyleFunction($params, &$smarty) {
  80. $method = (isset ( $params ['method'] )) ? $params ['method'] : - 1;
  81. unset ( $params ['method'] );
  82. $args = (isset ( $params ['args'] )) ? self::parseArgs ( $params ['args'] ) : null;
  83. $toReturn = "";
  84. try {
  85. if ($method != - 1) {
  86. //oppress errors
  87. if (is_array ( $args )) {
  88. //$toReturn = call_user_func('$smarty->_tpl_vars["this"]->headStyle()->$method()',$args,true);
  89. //print "args src= ".$args['src'];
  90. //TODO: fix args usage
  91. $toReturn = $smarty->_tpl_vars ['this']->headStyle ()->$method ( $args ['src'] );
  92. } else {
  93. @$toReturn = $smarty->_tpl_vars ['this']->headStyle ()->$method ( $args );
  94. }
  95. } else {
  96. $toReturn = $smarty->_tpl_vars ['this']->headStyle ();
  97. }
  98. return $toReturn;
  99. } catch ( Exception $e ) {
  100. return $toReturn;
  101. }
  102. }
  103. public static function parseArgs($args) {
  104. if (preg_match_all ( "/([^\\[^\\]]+)/", $args, $matches )) {
  105. $params = explode ( ',', $matches [1] [0] );
  106. $toReturn = array ( );
  107. foreach ( $params as $p ) {
  108. @list ( $key, $value ) = explode ( '=>', $p );
  109. $toReturn [preg_replace ( "/[\\' ]+/", "", $key )] = preg_replace ( "/[\\' ]+/", "", $value );
  110. }
  111. return $toReturn;
  112. }
  113. return $args;
  114. }
  115. /**
  116. * Smarty block function, provides gettext support for smarty.
  117. *
  118. * The block content is the text that should be translated.
  119. *
  120. * Any parameter that is sent to the function will be represented as %n in the translation text,
  121. * where n is 1 for the first parameter. The following parameters are reserved:
  122. * - escape - sets escape mode:
  123. * - 'html' for HTML escaping, this is the default.
  124. * - 'js' for javascript escaping.
  125. * - 'url' for url escaping.
  126. * - 'no'/'off'/0 - turns off escaping
  127. * - plural - The plural version of the text (2nd parameter of ngettext())
  128. * - count - The item count for plural mode (3rd parameter of ngettext())
  129. */
  130. public static function tBlock($params, $text, &$smarty) {
  131. $text = stripslashes ( $text );
  132. // set escape mode
  133. if (isset ( $params ['escape'] )) {
  134. $escape = $params ['escape'];
  135. unset ( $params ['escape'] );
  136. }
  137. // set plural version
  138. if (isset ( $params ['plural'] )) {
  139. $plural = $params ['plural'];
  140. unset ( $params ['plural'] );
  141. // set count
  142. if (isset ( $params ['count'] )) {
  143. $count = $params ['count'];
  144. unset ( $params ['count'] );
  145. }
  146. }
  147. // use plural if required parameters are set
  148. if (isset ( $count ) && isset ( $plural )) {
  149. $text = EZ_Language::getInstance ()->translatePlural ( $text, $plural, $count );
  150. } else { // use normal
  151. $text = EZ_Language::getInstance ()->translate ( $text );
  152. }
  153. // run strarg if there are parameters
  154. if (count ( $params )) {
  155. $text = self::strarg ( $text, $params );
  156. }
  157. if (! isset ( $escape ) || $escape == 'html') { // html escape, default
  158. $text = nl2br ( htmlspecialchars ( $text ) );
  159. } elseif (isset ( $escape )) {
  160. switch ( $escape) {
  161. case 'javascript' :
  162. case 'js' :
  163. // javascript escape
  164. $text = str_replace ( '\'', '\\\'', stripslashes ( $text ) );
  165. break;
  166. case 'url' :
  167. // url escape
  168. $text = urlencode ( $text );
  169. break;
  170. }
  171. }
  172. return $text;
  173. }
  174. /**
  175. * Replaces arguments in a string with their values.
  176. * Arguments are represented by % followed by their number.
  177. *
  178. * @param string Source string
  179. * @param mixed Arguments, can be passed in an array or through single variables.
  180. * @returns string Modified string
  181. */
  182. public static function strarg($str) {
  183. $tr = array();
  184. $p = 0;
  185. for ($i=1; $i < func_num_args(); $i++) {
  186. $arg = func_get_arg($i);
  187. if (is_array($arg)) {
  188. foreach ($arg as $aarg) {
  189. $tr['%'.++$p] = $aarg;
  190. }
  191. } else {
  192. $tr['%'.++$p] = $arg;
  193. }
  194. }
  195. return strtr($str, $tr);
  196. }
  197. }