1. sfWidgetForm.class.php
  2. /** * sfWidgetForm is the base class for all form widgets. * * @package symfony * @subpackage widget * @author Fabien Potencier * @version SVN: $Id: sfWidgetForm.class.php 24137 2009-11-18 13:12:40Z fabien $ */
  3. abstract class sfWidgetForm extends sfWidget
  4. {
  5. protected
  6. $parent = null;
  7. /**
  8. * Constructor.
  9. *
  10. * Available options:
  11. *
  12. * * id_format: The format for the generated HTML id attributes (%s by default)
  13. * * is_hidden: true if the form widget must be hidden, false otherwise (false by default)
  14. * * needs_multipart: true if the form widget needs a multipart form, false otherwise (false by default)
  15. * * default: The default value to use when rendering the widget
  16. * * label: The label to use when the widget is rendered by a widget schema
  17. *
  18. * @param array $options An array of options
  19. * @param array $attributes An array of default HTML attributes
  20. *
  21. * @see sfWidget
  22. */
  23. public function __construct($options = array(), $attributes = array())
  24. {
  25. $this->addOption('id_format', '%s');
  26. $this->addOption('is_hidden', false);
  27. $this->addOption('needs_multipart', false);
  28. $this->addOption('default', null);
  29. $this->addOption('label', null);
  30. parent::__construct($options, $attributes);
  31. }
  32. /**
  33. * Sets the default value for the widget.
  34. *
  35. * @param string $value The default value
  36. *
  37. * @return sfWidget The current widget instance
  38. */
  39. public function setDefault($value)
  40. {
  41. $this->setOption('default', $value);
  42. return $this;
  43. }
  44. /**
  45. * Returns the default value for the widget.
  46. *
  47. * @return string The default value
  48. */
  49. public function getDefault()
  50. {
  51. return $this->getOption('default');
  52. }
  53. /**
  54. * Sets the label for the widget.
  55. *
  56. * @param string $value The label
  57. *
  58. * @return sfWidget The current widget instance
  59. */
  60. public function setLabel($value)
  61. {
  62. $this->setOption('label', $value);
  63. return $this;
  64. }
  65. /**
  66. * Returns the label for the widget.
  67. *
  68. * @return string The label
  69. */
  70. public function getLabel()
  71. {
  72. return $this->getOption('label');
  73. }
  74. /**
  75. * Sets the format for HTML id attributes.
  76. *
  77. * @param string $format The format string (must contain a %s for the id placeholder)
  78. *
  79. * @return sfWidget The current widget instance
  80. */
  81. public function setIdFormat($format)
  82. {
  83. $this->setOption('id_format', $format);
  84. return $this;
  85. }
  86. /**
  87. * Gets the HTML format string for id attributes.
  88. *
  89. * @return string The format string
  90. */
  91. public function getIdFormat()
  92. {
  93. return $this->getOption('id_format');
  94. }
  95. /**
  96. * Returns true if the widget is hidden.
  97. *
  98. * @return Boolean true if the widget is hidden, false otherwise
  99. */
  100. public function isHidden()
  101. {
  102. return $this->getOption('is_hidden');
  103. }
  104. /**
  105. * Sets the hidden flag for the widget.
  106. *
  107. * @param bool $boolean true if the widget must be hidden, false otherwise
  108. *
  109. * @return sfWidget The current widget instance
  110. */
  111. public function setHidden($boolean)
  112. {
  113. $this->setOption('is_hidden', (boolean) $boolean);
  114. return $this;
  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 $tag The tag name
  132. * @param array $attributes 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 method 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. * This method also changes all invalid characters to an underscore (_):
  190. *
  191. * Ids must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits
  192. * ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
  193. *
  194. * @param string $name The field name
  195. * @param string $value The field value
  196. *
  197. * @return string The field id or null.
  198. */
  199. public function generateId($name, $value = null)
  200. {
  201. if (false === $this->getOption('id_format'))
  202. {
  203. return null;
  204. }
  205. // check to see if we have an array variable for a field name
  206. if (strstr($name, '['))
  207. {
  208. $name = str_replace(array('[]', '][', '[', ']'), array((null !== $value ? '_'.$value : ''), '_', '_', ''), $name);
  209. }
  210. if (false !== strpos($this->getOption('id_format'), '%s'))
  211. {
  212. $name = sprintf($this->getOption('id_format'), $name);
  213. }
  214. // remove illegal characters
  215. $name = preg_replace(array('/^[^A-Za-z]+/', '/[^A-Za-z0-9\:_\.\-]/'), array('', '_'), $name);
  216. return $name;
  217. }
  218. /**
  219. * Generates a two chars range
  220. *
  221. * @param int $start
  222. * @param int $stop
  223. * @return array
  224. */
  225. static protected function generateTwoCharsRange($start, $stop)
  226. {
  227. $results = array();
  228. for ($i = $start; $i <= $stop; $i++)
  229. {
  230. $results[$i] = sprintf('%02d', $i);
  231. }
  232. return $results;
  233. }
  234. /**
  235. * Sets the parent widget schema.
  236. *
  237. * @param sfWidgetFormSchema|null $widgetSchema
  238. *
  239. * @return sfWidgetForm The current widget instance
  240. */
  241. public function setParent(sfWidgetFormSchema $widgetSchema = null)
  242. {
  243. $this->parent = $widgetSchema;
  244. return $this;
  245. }
  246. /**
  247. * Returns the parent widget schema.
  248. *
  249. * If no schema has been set with setWidgetSchema(), NULL is returned.
  250. *
  251. * @return sfWidgetFormSchema|null
  252. */
  253. public function getParent()
  254. {
  255. return $this->parent;
  256. }
  257. /**
  258. * Translates the given text.
  259. *
  260. * @param string $text The text with optional placeholders
  261. * @param array $parameters The values to replace the placeholders
  262. *
  263. * @return string The translated text
  264. *
  265. * @see sfWidgetFormSchemaFormatter::translate()
  266. */
  267. protected function translate($text, array $parameters = array())
  268. {
  269. if (null === $this->parent)
  270. {
  271. return $text;
  272. }
  273. else
  274. {
  275. return $this->parent->getFormFormatter()->translate($text, $parameters);
  276. }
  277. }
  278. /**
  279. * Translates all values of the given array.
  280. *
  281. * @param array $texts The texts with optional placeholders
  282. * @param array $parameters The values to replace the placeholders
  283. *
  284. * @return array The translated texts
  285. *
  286. * @see sfWidgetFormSchemaFormatter::translate()
  287. */
  288. protected function translateAll(array $texts, array $parameters = array())
  289. {
  290. if (null === $this->parent)
  291. {
  292. return $texts;
  293. }
  294. else
  295. {
  296. $result = array();
  297. foreach ($texts as $key => $text)
  298. {
  299. $result[$key] = $this->parent->getFormFormatter()->translate($text, $parameters);
  300. }
  301. return $result;
  302. }
  303. }
  304. }

Debug toolbar