TagHelper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) 2004 David Heinemeier Hansson
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * TagHelper defines some base helpers to construct html tags.
  12. *
  13. * @package symfony
  14. * @subpackage helper
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @author David Heinemeier Hansson
  17. * @version SVN: $Id: TagHelper.php 13097 2008-11-18 06:58:24Z Kris.Wallsmith $
  18. */
  19. /**
  20. * Constructs an html tag.
  21. *
  22. * @param string $name tag name
  23. * @param array $options tag options
  24. * @param bool $open true to leave tag open
  25. * @return string
  26. */
  27. function tag($name, $options = array(), $open = false)
  28. {
  29. if (!$name)
  30. {
  31. return '';
  32. }
  33. return '<'.$name._tag_options($options).(($open) ? '>' : ' />');
  34. }
  35. function content_tag($name, $content = '', $options = array())
  36. {
  37. if (!$name)
  38. {
  39. return '';
  40. }
  41. return '<'.$name._tag_options($options).'>'.$content.'</'.$name.'>';
  42. }
  43. function cdata_section($content)
  44. {
  45. return "<![CDATA[$content]]>";
  46. }
  47. /**
  48. * Wraps the content in conditional comments.
  49. *
  50. * @param string $condition
  51. * @param string $content
  52. *
  53. * @return string
  54. *
  55. * @see http://msdn.microsoft.com/en-us/library/ms537512(VS.85).aspx
  56. */
  57. function comment_as_conditional($condition, $content)
  58. {
  59. return "<!--[if $condition]>$content<![endif]-->";
  60. }
  61. /**
  62. * Escape carrier returns and single and double quotes for Javascript segments.
  63. */
  64. function escape_javascript($javascript = '')
  65. {
  66. $javascript = preg_replace('/\r\n|\n|\r/', "\\n", $javascript);
  67. $javascript = preg_replace('/(["\'])/', '\\\\\1', $javascript);
  68. return $javascript;
  69. }
  70. /**
  71. * Escapes an HTML string.
  72. *
  73. * @param string $html HTML string to escape
  74. * @return string escaped string
  75. */
  76. function escape_once($html)
  77. {
  78. return fix_double_escape(htmlspecialchars($html, ENT_COMPAT, sfConfig::get('sf_charset')));
  79. }
  80. /**
  81. * Fixes double escaped strings.
  82. *
  83. * @param string $escaped HTML string to fix
  84. * @return string fixed escaped string
  85. */
  86. function fix_double_escape($escaped)
  87. {
  88. return preg_replace('/&amp;([a-z]+|(#\d+)|(#x[\da-f]+));/i', '&$1;', $escaped);
  89. }
  90. function _tag_options($options = array())
  91. {
  92. $options = _parse_attributes($options);
  93. $html = '';
  94. foreach ($options as $key => $value)
  95. {
  96. $html .= ' '.$key.'="'.escape_once($value).'"';
  97. }
  98. return $html;
  99. }
  100. function _parse_attributes($string)
  101. {
  102. return is_array($string) ? $string : sfToolkit::stringToArray($string);
  103. }
  104. function _get_option(&$options, $name, $default = null)
  105. {
  106. if (array_key_exists($name, $options))
  107. {
  108. $value = $options[$name];
  109. unset($options[$name]);
  110. }
  111. else
  112. {
  113. $value = $default;
  114. }
  115. return $value;
  116. }
  117. /**
  118. * Returns a formatted ID based on the <i>$name</i> parameter and optionally the <i>$value</i> parameter.
  119. *
  120. * This function determines the proper form field ID name based on the parameters. If a form field has an
  121. * array value as a name we need to convert them to proper and unique IDs like so:
  122. * <samp>
  123. * name[] => name (if value == null)
  124. * name[] => name_value (if value != null)
  125. * name[bob] => name_bob
  126. * name[item][total] => name_item_total
  127. * </samp>
  128. *
  129. * <b>Examples:</b>
  130. * <code>
  131. * echo get_id_from_name('status[]', '1');
  132. * </code>
  133. *
  134. * @param string $name field name
  135. * @param string $value field value
  136. *
  137. * @return string <select> tag populated with all the languages in the world.
  138. */
  139. function get_id_from_name($name, $value = null)
  140. {
  141. // check to see if we have an array variable for a field name
  142. if (strstr($name, '['))
  143. {
  144. $name = str_replace(array('[]', '][', '[', ']'), array((($value != null) ? '_'.$value : ''), '_', '_', ''), $name);
  145. }
  146. return $name;
  147. }
  148. /**
  149. * Converts specific <i>$options</i> to their correct HTML format
  150. *
  151. * @param array $options
  152. * @return array returns properly formatted options
  153. */
  154. function _convert_options($options)
  155. {
  156. $options = _parse_attributes($options);
  157. foreach (array('disabled', 'readonly', 'multiple') as $attribute)
  158. {
  159. if (array_key_exists($attribute, $options))
  160. {
  161. if ($options[$attribute])
  162. {
  163. $options[$attribute] = $attribute;
  164. }
  165. else
  166. {
  167. unset($options[$attribute]);
  168. }
  169. }
  170. }
  171. return $options;
  172. }