sfWidgetForm.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * sfWidgetForm is the base class for all form widgets.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetForm.class.php 10235 2008-07-12 07:04:14Z Carl.Vondrick $
  16. */
  17. abstract class sfWidgetForm extends sfWidget
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * Available options:
  23. *
  24. * * id_format: The format for the generated HTML id attributes (%s by default)
  25. * * is_hidden: true if the form widget must be hidden, false otherwise (false by default)
  26. * * needs_multipart: true if the form widget needs a multipart form, false otherwise (false by default)
  27. *
  28. * @param array $options An array of options
  29. * @param array $attributes An array of default HTML attributes
  30. *
  31. * @see sfWidget
  32. */
  33. public function __construct($options = array(), $attributes = array())
  34. {
  35. $this->addOption('id_format', '%s');
  36. $this->addOption('is_hidden', false);
  37. $this->addOption('needs_multipart', false);
  38. parent::__construct($options, $attributes);
  39. }
  40. /**
  41. * Sets the format for HTML id attributes.
  42. *
  43. * @param string $format The format string (must contain a %s for the id placeholder)
  44. */
  45. public function setIdFormat($format)
  46. {
  47. $this->setOption('id_format', $format);
  48. }
  49. /**
  50. * Gets the HTML format string for id attributes.
  51. *
  52. * @return string The format string
  53. */
  54. public function getIdFormat()
  55. {
  56. return $this->getOption('id_format');
  57. }
  58. /**
  59. * Returns true if the widget is hidden.
  60. *
  61. * @return Boolean true if the widget is hidden, false otherwise
  62. */
  63. public function isHidden()
  64. {
  65. return $this->getOption('is_hidden');
  66. }
  67. /**
  68. * Sets the hidden flag for the widget.
  69. *
  70. * @param bool $boolean true if the widget must be hidden, false otherwise
  71. */
  72. public function setHidden($boolean)
  73. {
  74. $this->setOption('is_hidden', (boolean) $boolean);
  75. }
  76. /**
  77. * Returns true if the widget needs a multipart form.
  78. *
  79. * @return bool true if the widget needs a multipart form, false otherwise
  80. */
  81. public function needsMultipartForm()
  82. {
  83. return $this->getOption('needs_multipart');
  84. }
  85. /**
  86. * Renders a HTML tag.
  87. *
  88. * The id attribute is added automatically to the array of attributes if none is specified.
  89. * If uses for "id_format" option to generate the id.
  90. *
  91. * @param string The tag name
  92. * @param array An array of HTML attributes to be merged with the default HTML attributes
  93. *
  94. * @return string An HTML tag string
  95. */
  96. public function renderTag($tag, $attributes = array())
  97. {
  98. if (empty($tag))
  99. {
  100. return '';
  101. }
  102. $attributes = $this->fixFormId($attributes);
  103. return sprintf('<%s%s%s', $tag, $this->attributesToHtml($attributes), self::$xhtml ? ' />' : (strtolower($tag) == 'input' ? '>' : sprintf('></%s>', $tag)));
  104. }
  105. /**
  106. * Renders a HTML content tag.
  107. *
  108. * The id attribute is added automatically to the array of attributes if none is specified.
  109. * If uses for "id_format" option to generate the id.
  110. *
  111. * @param string $tag The tag name
  112. * @param string $content The content of the tag
  113. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  114. *
  115. * @return string An HTML tag string
  116. */
  117. public function renderContentTag($tag, $content = null, $attributes = array())
  118. {
  119. return parent::renderContentTag($tag, $content, $this->fixFormId($attributes));
  120. }
  121. /**
  122. * Adds an HTML id attributes to the array of attributes if none is given and a name attribute exists.
  123. *
  124. * @param array $attributes An array of attributes
  125. *
  126. * @return array An array of attributes with an id.
  127. */
  128. protected function fixFormId($attributes)
  129. {
  130. if (!isset($attributes['id']) && isset($attributes['name']))
  131. {
  132. $attributes['id'] = $this->generateId($attributes['name'], isset($attributes['value']) ? $attributes['value'] : null);
  133. }
  134. return $attributes;
  135. }
  136. /**
  137. * Returns a formatted id based on the field name and optionally on the field value.
  138. *
  139. * This function determines the proper form field id name based on the parameters. If a form field has an
  140. * array value as a name we need to convert them to proper and unique ids like so:
  141. *
  142. * <samp>
  143. * name[] => name (if value == null)
  144. * name[] => name_value (if value != null)
  145. * name[bob] => name_bob
  146. * name[item][total] => name_item_total
  147. * </samp>
  148. *
  149. * @param string $name The field name
  150. * @param string $value The field value
  151. *
  152. * @return string The field id or null.
  153. */
  154. public function generateId($name, $value = null)
  155. {
  156. if (false === $this->getOption('id_format'))
  157. {
  158. return null;
  159. }
  160. // check to see if we have an array variable for a field name
  161. if (strstr($name, '['))
  162. {
  163. $name = str_replace(array('[]', '][', '[', ']'), array((!is_null($value) ? '_'.$value : ''), '_', '_', ''), $name);
  164. }
  165. if (false !== strpos($this->getOption('id_format'), '%s'))
  166. {
  167. return sprintf($this->getOption('id_format'), $name);
  168. }
  169. return $name;
  170. }
  171. /**
  172. * Generates a two chars range
  173. *
  174. * @param int $start
  175. * @param int $stop
  176. * @return array
  177. */
  178. static protected function generateTwoCharsRange($start, $stop)
  179. {
  180. $results = array();
  181. for ($i = $start; $i <= $stop; $i++)
  182. {
  183. $results[$i] = sprintf('%02d', $i);
  184. }
  185. return $results;
  186. }
  187. }