sfWidgetFormSelectRadio.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 11591 2008-09-16 05:02:41Z 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. * * class: The class to use for the main <ul> tag
  28. * * formatter: A callable to call to format the radio choices
  29. * The formatter callable receives the widget and the array of inputs as arguments
  30. * * template: The template to use when grouping option in groups (%group% %options%)
  31. *
  32. * @param array $options An array of options
  33. * @param array $attributes An array of default HTML attributes
  34. *
  35. * @see sfWidgetForm
  36. */
  37. protected function configure($options = array(), $attributes = array())
  38. {
  39. $this->addRequiredOption('choices');
  40. $this->addOption('class', 'radio_list');
  41. $this->addOption('label_separator', '&nbsp;');
  42. $this->addOption('separator', "\n");
  43. $this->addOption('formatter', array($this, 'formatter'));
  44. $this->addOption('template', '%group% %options%');
  45. }
  46. /**
  47. * @param string $name The element name
  48. * @param string $value The value selected in this widget
  49. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  50. * @param array $errors An array of errors for the field
  51. *
  52. * @return string An HTML tag string
  53. *
  54. * @see sfWidgetForm
  55. */
  56. public function render($name, $value = null, $attributes = array(), $errors = array())
  57. {
  58. if ('[]' != substr($name, -2))
  59. {
  60. $name .= '[]';
  61. }
  62. $choices = $this->getOption('choices');
  63. if ($choices instanceof sfCallable)
  64. {
  65. $choices = $choices->call();
  66. }
  67. // with groups?
  68. if (count($choices) && is_array(next($choices)))
  69. {
  70. $parts = array();
  71. foreach ($choices as $key => $option)
  72. {
  73. $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
  74. }
  75. return implode("\n", $parts);
  76. }
  77. else
  78. {
  79. return $this->formatChoices($name, $value, $choices, $attributes);;
  80. }
  81. }
  82. protected function formatChoices($name, $value, $choices, $attributes)
  83. {
  84. $inputs = array();
  85. foreach ($choices as $key => $option)
  86. {
  87. $baseAttributes = array(
  88. 'name' => substr($name, 0, -2),
  89. 'type' => 'radio',
  90. 'value' => self::escapeOnce($key),
  91. 'id' => $id = $this->generateId($name, self::escapeOnce($key)),
  92. );
  93. if (strval($key) == strval($value === false ? 0 : $value))
  94. {
  95. $baseAttributes['checked'] = 'checked';
  96. }
  97. $inputs[] = array(
  98. 'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
  99. 'label' => $this->renderContentTag('label', $option, array('for' => $id)),
  100. );
  101. }
  102. return call_user_func($this->getOption('formatter'), $this, $inputs);
  103. }
  104. public function formatter($widget, $inputs)
  105. {
  106. $rows = array();
  107. foreach ($inputs as $input)
  108. {
  109. $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
  110. }
  111. return $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
  112. }
  113. public function __clone()
  114. {
  115. if ($this->getOption('choices') instanceof sfCallable)
  116. {
  117. $callable = $this->getOption('choices')->getCallable();
  118. if (is_array($callable))
  119. {
  120. $callable[0] = $this;
  121. $this->setOption('choices', new sfCallable($callable));
  122. }
  123. }
  124. }
  125. }