PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/widget/sfWidgetFormSelectRadio.class.php

https://github.com/coolpink/CpSymfony
PHP | 122 lines | 65 code | 13 blank | 44 comment | 6 complexity | 604f64ef6b2539ee728a27ab405b1200 MD5 | raw file
  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 27738 2010-02-08 15:07:33Z Kris.Wallsmith $
  16. */
  17. class sfWidgetFormSelectRadio extends sfWidgetFormChoiceBase
  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 sfWidgetFormChoiceBase
  36. */
  37. protected function configure($options = array(), $attributes = array())
  38. {
  39. parent::configure($options, $attributes);
  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->getChoices();
  63. // with groups?
  64. if (count($choices) && is_array(next($choices)))
  65. {
  66. $parts = array();
  67. foreach ($choices as $key => $option)
  68. {
  69. $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
  70. }
  71. return implode("\n", $parts);
  72. }
  73. else
  74. {
  75. return $this->formatChoices($name, $value, $choices, $attributes);
  76. }
  77. }
  78. protected function formatChoices($name, $value, $choices, $attributes)
  79. {
  80. $inputs = array();
  81. foreach ($choices as $key => $option)
  82. {
  83. $baseAttributes = array(
  84. 'name' => substr($name, 0, -2),
  85. 'type' => 'radio',
  86. 'value' => self::escapeOnce($key),
  87. 'id' => $id = $this->generateId($name, self::escapeOnce($key)),
  88. );
  89. if (strval($key) == strval($value === false ? 0 : $value))
  90. {
  91. $baseAttributes['checked'] = 'checked';
  92. }
  93. $inputs[$id] = array(
  94. 'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
  95. 'label' => $this->renderContentTag('label', self::escapeOnce($option), array('for' => $id)),
  96. );
  97. }
  98. return call_user_func($this->getOption('formatter'), $this, $inputs);
  99. }
  100. public function formatter($widget, $inputs)
  101. {
  102. $rows = array();
  103. foreach ($inputs as $input)
  104. {
  105. $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
  106. }
  107. return !$rows ? '' : $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
  108. }
  109. }