sfWidgetForm.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 15112 2009-01-30 17:34:34Z fabien $
  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. * * default: The default value to use when rendering the widget
  28. * * label: The label to use when the widget is rendered by a widget schema
  29. *
  30. * @param array $options An array of options
  31. * @param array $attributes An array of default HTML attributes
  32. *
  33. * @see sfWidget
  34. */
  35. public function __construct($options = array(), $attributes = array())
  36. {
  37. $this->addOption('id_format', '%s');
  38. $this->addOption('is_hidden', false);
  39. $this->addOption('needs_multipart', false);
  40. $this->addOption('default', null);
  41. $this->addOption('label', null);
  42. parent::__construct($options, $attributes);
  43. }
  44. /**
  45. * Sets the default value for the widget.
  46. *
  47. * @param string The default value
  48. */
  49. public function setDefault($value)
  50. {
  51. $this->setOption('default', $value);
  52. }
  53. /**
  54. * Returns the default value for the widget.
  55. *
  56. * @return string The default value
  57. */
  58. public function getDefault()
  59. {
  60. return $this->getOption('default');
  61. }
  62. /**
  63. * Sets the label for the widget.
  64. *
  65. * @param string The label
  66. */
  67. public function setLabel($value)
  68. {
  69. $this->setOption('label', $value);
  70. }
  71. /**
  72. * Returns the label for the widget.
  73. *
  74. * @return string The label
  75. */
  76. public function getLabel()
  77. {
  78. return $this->getOption('label');
  79. }
  80. /**
  81. * Sets the format for HTML id attributes.
  82. *
  83. * @param string $format The format string (must contain a %s for the id placeholder)
  84. */
  85. public function setIdFormat($format)
  86. {
  87. $this->setOption('id_format', $format);
  88. }
  89. /**
  90. * Gets the HTML format string for id attributes.
  91. *
  92. * @return string The format string
  93. */
  94. public function getIdFormat()
  95. {
  96. return $this->getOption('id_format');
  97. }
  98. /**
  99. * Returns true if the widget is hidden.
  100. *
  101. * @return Boolean true if the widget is hidden, false otherwise
  102. */
  103. public function isHidden()
  104. {
  105. return $this->getOption('is_hidden');
  106. }
  107. /**
  108. * Sets the hidden flag for the widget.
  109. *
  110. * @param bool $boolean true if the widget must be hidden, false otherwise
  111. */
  112. public function setHidden($boolean)
  113. {
  114. $this->setOption('is_hidden', (boolean) $boolean);
  115. }
  116. /**
  117. * Returns true if the widget needs a multipart form.
  118. *
  119. * @return bool true if the widget needs a multipart form, false otherwise
  120. */
  121. public function needsMultipartForm()
  122. {
  123. return $this->getOption('needs_multipart');
  124. }
  125. /**
  126. * Renders a HTML tag.
  127. *
  128. * The id attribute is added automatically to the array of attributes if none is specified.
  129. * If uses for "id_format" option to generate the id.
  130. *
  131. * @param string The tag name
  132. * @param array An array of HTML attributes to be merged with the default HTML attributes
  133. *
  134. * @return string An HTML tag string
  135. */
  136. public function renderTag($tag, $attributes = array())
  137. {
  138. if (empty($tag))
  139. {
  140. return '';
  141. }
  142. $attributes = $this->fixFormId($attributes);
  143. return parent::renderTag($tag, $attributes);
  144. }
  145. /**
  146. * Renders a HTML content tag.
  147. *
  148. * The id attribute is added automatically to the array of attributes if none is specified.
  149. * If uses for "id_format" option to generate the id.
  150. *
  151. * @param string $tag The tag name
  152. * @param string $content The content of the tag
  153. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  154. *
  155. * @return string An HTML tag string
  156. */
  157. public function renderContentTag($tag, $content = null, $attributes = array())
  158. {
  159. return parent::renderContentTag($tag, $content, $this->fixFormId($attributes));
  160. }
  161. /**
  162. * Adds an HTML id attributes to the array of attributes if none is given and a name attribute exists.
  163. *
  164. * @param array $attributes An array of attributes
  165. *
  166. * @return array An array of attributes with an id.
  167. */
  168. protected function fixFormId($attributes)
  169. {
  170. if (!isset($attributes['id']) && isset($attributes['name']))
  171. {
  172. $attributes['id'] = $this->generateId($attributes['name'], isset($attributes['value']) ? $attributes['value'] : null);
  173. }
  174. return $attributes;
  175. }
  176. /**
  177. * Returns a formatted id based on the field name and optionally on the field value.
  178. *
  179. * This function determines the proper form field id name based on the parameters. If a form field has an
  180. * array value as a name we need to convert them to proper and unique ids like so:
  181. *
  182. * <samp>
  183. * name[] => name (if value == null)
  184. * name[] => name_value (if value != null)
  185. * name[bob] => name_bob
  186. * name[item][total] => name_item_total
  187. * </samp>
  188. *
  189. * @param string $name The field name
  190. * @param string $value The field value
  191. *
  192. * @return string The field id or null.
  193. */
  194. public function generateId($name, $value = null)
  195. {
  196. if (false === $this->getOption('id_format'))
  197. {
  198. return null;
  199. }
  200. // check to see if we have an array variable for a field name
  201. if (strstr($name, '['))
  202. {
  203. $name = str_replace(array('[]', '][', '[', ']'), array((!is_null($value) ? '_'.$value : ''), '_', '_', ''), $name);
  204. }
  205. if (false !== strpos($this->getOption('id_format'), '%s'))
  206. {
  207. return sprintf($this->getOption('id_format'), $name);
  208. }
  209. return $name;
  210. }
  211. /**
  212. * Generates a two chars range
  213. *
  214. * @param int $start
  215. * @param int $stop
  216. * @return array
  217. */
  218. static protected function generateTwoCharsRange($start, $stop)
  219. {
  220. $results = array();
  221. for ($i = $start; $i <= $stop; $i++)
  222. {
  223. $results[$i] = sprintf('%02d', $i);
  224. }
  225. return $results;
  226. }
  227. }