123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- class sfWidgetFormChoice extends sfWidgetForm
- {
-
- protected function configure($options = array(), $attributes = array())
- {
- $this->addRequiredOption('choices');
- $this->addOption('multiple', false);
- $this->addOption('expanded', false);
- $this->addOption('renderer_class', false);
- $this->addOption('renderer_options', array());
- $this->addOption('renderer', false);
- }
-
- public function render($name, $value = null, $attributes = array(), $errors = array())
- {
- if ($this->getOption('multiple'))
- {
- $attributes['multiple'] = 'multiple';
- if ('[]' != substr($name, -2))
- {
- $name .= '[]';
- }
- }
- if (!$this->getOption('renderer') && !$this->getOption('renderer_class') && $this->getOption('expanded'))
- {
- unset($attributes['multiple']);
- }
- return $this->getRenderer()->render($name, $value, $attributes, $errors);
- }
-
- public function getStylesheets()
- {
- return $this->getRenderer()->getStylesheets();
- }
-
- public function getJavaScripts()
- {
- return $this->getRenderer()->getJavaScripts();
- }
- public function getChoices()
- {
- $choices = $this->getOption('choices');
- if ($choices instanceof sfCallable)
- {
- $choices = $choices->call();
- }
- return $choices;
- }
- public function getRenderer()
- {
- if ($this->getOption('renderer'))
- {
- return $this->getOption('renderer');
- }
- if (!$class = $this->getOption('renderer_class'))
- {
- $type = !$this->getOption('expanded') ? '' : ($this->getOption('multiple') ? 'checkbox' : 'radio');
- $class = sprintf('sfWidgetFormSelect%s', ucfirst($type));
- }
- return new $class(array_merge(array('choices' => new sfCallable(array($this, 'getChoices'))), $this->options['renderer_options']), $this->getAttributes());
- }
- 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));
- }
- }
- }
- }
|