sfWidgetFormSelectRadio.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. * sfWidgetFormSelectRadio represents radio HTML tags.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetFormSelectRadio.class.php 17068 2009-04-07 08:24:53Z fabien $
  16. */
  17. class sfWidgetFormSelectRadio extends sfWidgetForm
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * Available options:
  23. *
  24. * * choices: An array of possible choices (required)
  25. * * label_separator: The separator to use between the input radio and the label
  26. * * separator: The separator to use between each input radio
  27. * * formatter: A callable to call to format the radio choices
  28. * The formatter callable receives the widget and the array of inputs as arguments
  29. *
  30. * @param array $options An array of options
  31. * @param array $attributes An array of default HTML attributes
  32. *
  33. * @see sfWidgetForm
  34. */
  35. protected function configure($options = array(), $attributes = array())
  36. {
  37. $this->addRequiredOption('choices');
  38. $this->addOption('label_separator', '&nbsp;');
  39. $this->addOption('separator', "\n");
  40. $this->addOption('formatter', array($this, 'formatter'));
  41. }
  42. /**
  43. * @param string $name The element name
  44. * @param string $value The value selected in this widget
  45. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  46. * @param array $errors An array of errors for the field
  47. *
  48. * @return string An HTML tag string
  49. *
  50. * @see sfWidgetForm
  51. */
  52. public function render($name, $value = null, $attributes = array(), $errors = array())
  53. {
  54. if ('[]' != substr($name, -2))
  55. {
  56. $name .= '[]';
  57. }
  58. $choices = $this->getOption('choices');
  59. if ($choices instanceof sfCallable)
  60. {
  61. $choices = $choices->call();
  62. }
  63. $inputs = array();
  64. foreach ($choices as $key => $option)
  65. {
  66. $baseAttributes = array(
  67. 'name' => substr($name, 0, -2),
  68. 'type' => 'radio',
  69. 'value' => self::escapeOnce($key),
  70. 'id' => $id = $this->generateId($name, self::escapeOnce($key)),
  71. );
  72. if (strval($key) == strval($value === false ? 0 : $value))
  73. {
  74. $baseAttributes['checked'] = 'checked';
  75. }
  76. $inputs[] = array(
  77. 'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
  78. 'label' => $this->renderContentTag('label', $option, array('for' => $id)),
  79. );
  80. }
  81. return call_user_func($this->getOption('formatter'), $this, $inputs);
  82. }
  83. public function formatter($widget, $inputs)
  84. {
  85. $rows = array();
  86. foreach ($inputs as $input)
  87. {
  88. $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
  89. }
  90. return $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => 'radio_list'));
  91. }
  92. public function __clone()
  93. {
  94. if ($this->getOption('choices') instanceof sfCallable)
  95. {
  96. $callable = $this->getOption('choices')->getCallable();
  97. $class = __CLASS__;
  98. if (is_array($callable) && $callable[0] instanceof $class)
  99. {
  100. $callable[0] = $this;
  101. $this->setOption('choices', new sfCallable($callable));
  102. }
  103. }
  104. }
  105. }