123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- class sfWidgetFormSelectRadio extends sfWidgetForm
- {
-
- protected function configure($options = array(), $attributes = array())
- {
- $this->addRequiredOption('choices');
- $this->addOption('class', 'radio_list');
- $this->addOption('label_separator', ' ');
- $this->addOption('separator', "\n");
- $this->addOption('formatter', array($this, 'formatter'));
- $this->addOption('template', '%group% %options%');
- }
-
- public function render($name, $value = null, $attributes = array(), $errors = array())
- {
- if ('[]' != substr($name, -2))
- {
- $name .= '[]';
- }
- $choices = $this->getOption('choices');
- if ($choices instanceof sfCallable)
- {
- $choices = $choices->call();
- }
-
- if (count($choices) && is_array(next($choices)))
- {
- $parts = array();
- foreach ($choices as $key => $option)
- {
- $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
- }
- return implode("\n", $parts);
- }
- else
- {
- return $this->formatChoices($name, $value, $choices, $attributes);;
- }
- }
- protected function formatChoices($name, $value, $choices, $attributes)
- {
- $inputs = array();
- foreach ($choices as $key => $option)
- {
- $baseAttributes = array(
- 'name' => substr($name, 0, -2),
- 'type' => 'radio',
- 'value' => self::escapeOnce($key),
- 'id' => $id = $this->generateId($name, self::escapeOnce($key)),
- );
- if (strval($key) == strval($value === false ? 0 : $value))
- {
- $baseAttributes['checked'] = 'checked';
- }
- $inputs[] = array(
- 'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
- 'label' => $this->renderContentTag('label', $option, array('for' => $id)),
- );
- }
- return call_user_func($this->getOption('formatter'), $this, $inputs);
- }
- public function formatter($widget, $inputs)
- {
- $rows = array();
- foreach ($inputs as $input)
- {
- $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
- }
- return $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
- }
- public function __clone()
- {
- if ($this->getOption('choices') instanceof sfCallable)
- {
- $callable = $this->getOption('choices')->getCallable();
- if (is_array($callable))
- {
- $callable[0] = $this;
- $this->setOption('choices', new sfCallable($callable));
- }
- }
- }
- }
|