TagHelper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 9967 2008-06-29 07:58:50Z fabien $
  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. * Escape carrier returns and single and double quotes for Javascript segments.
  49. */
  50. function escape_javascript($javascript = '')
  51. {
  52. $javascript = preg_replace('/\r\n|\n|\r/', "\\n", $javascript);
  53. $javascript = preg_replace('/(["\'])/', '\\\\\1', $javascript);
  54. return $javascript;
  55. }
  56. /**
  57. * Escapes an HTML string.
  58. *
  59. * @param string $html HTML string to escape
  60. * @return string escaped string
  61. */
  62. function escape_once($html)
  63. {
  64. return fix_double_escape(htmlspecialchars($html, ENT_COMPAT, sfConfig::get('sf_charset')));
  65. }
  66. /**
  67. * Fixes double escaped strings.
  68. *
  69. * @param string $escaped HTML string to fix
  70. * @return string fixed escaped string
  71. */
  72. function fix_double_escape($escaped)
  73. {
  74. return preg_replace('/&amp;([a-z]+|(#\d+)|(#x[\da-f]+));/i', '&$1;', $escaped);
  75. }
  76. function _tag_options($options = array())
  77. {
  78. $options = _parse_attributes($options);
  79. $html = '';
  80. foreach ($options as $key => $value)
  81. {
  82. $html .= ' '.$key.'="'.escape_once($value).'"';
  83. }
  84. return $html;
  85. }
  86. function _parse_attributes($string)
  87. {
  88. return is_array($string) ? $string : sfToolkit::stringToArray($string);
  89. }
  90. function _get_option(&$options, $name, $default = null)
  91. {
  92. if (array_key_exists($name, $options))
  93. {
  94. $value = $options[$name];
  95. unset($options[$name]);
  96. }
  97. else
  98. {
  99. $value = $default;
  100. }
  101. return $value;
  102. }
  103. /**
  104. * Returns a formatted ID based on the <i>$name</i> parameter and optionally the <i>$value</i> parameter.
  105. *
  106. * This function determines the proper form field ID name based on the parameters. If a form field has an
  107. * array value as a name we need to convert them to proper and unique IDs like so:
  108. * <samp>
  109. * name[] => name (if value == null)
  110. * name[] => name_value (if value != null)
  111. * name[bob] => name_bob
  112. * name[item][total] => name_item_total
  113. * </samp>
  114. *
  115. * <b>Examples:</b>
  116. * <code>
  117. * echo get_id_from_name('status[]', '1');
  118. * </code>
  119. *
  120. * @param string $name field name
  121. * @param string $value field value
  122. *
  123. * @return string <select> tag populated with all the languages in the world.
  124. */
  125. function get_id_from_name($name, $value = null)
  126. {
  127. // check to see if we have an array variable for a field name
  128. if (strstr($name, '['))
  129. {
  130. $name = str_replace(array('[]', '][', '[', ']'), array((($value != null) ? '_'.$value : ''), '_', '_', ''), $name);
  131. }
  132. return $name;
  133. }
  134. /**
  135. * Converts specific <i>$options</i> to their correct HTML format
  136. *
  137. * @param array $options
  138. * @return array returns properly formatted options
  139. */
  140. function _convert_options($options)
  141. {
  142. $options = _parse_attributes($options);
  143. foreach (array('disabled', 'readonly', 'multiple') as $attribute)
  144. {
  145. if (array_key_exists($attribute, $options))
  146. {
  147. if ($options[$attribute])
  148. {
  149. $options[$attribute] = $attribute;
  150. }
  151. else
  152. {
  153. unset($options[$attribute]);
  154. }
  155. }
  156. }
  157. return $options;
  158. }