sfWidgetFormChoice.class.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. * sfWidgetFormChoice represents a choice widget.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetFormChoice.class.php 12409 2008-10-29 14:06:02Z fabien $
  16. */
  17. class sfWidgetFormChoice extends sfWidgetForm
  18. {
  19. /**
  20. * Constructor.
  21. *
  22. * Available options:
  23. *
  24. * * choices: An array of possible choices (required)
  25. * * multiple: true if the select tag must allow multiple selections
  26. * * expanded: true to display an expanded widget
  27. * if expanded is false, then the widget will be a select
  28. * if expanded is true and multiple is false, then the widget will be a list of radio
  29. * if expanded is true and multiple is true, then the widget will be a list of checkbox
  30. * * renderer_class: The class to use instead of the default ones
  31. * * renderer_options: The options to pass to the renderer constructor
  32. * * renderer: A renderer widget (overrides the expanded and renderer_options options)
  33. * The choices option must be: new sfCallable($thisWidgetInstance, 'getChoices')
  34. * @param array $options An array of options
  35. * @param array $attributes An array of default HTML attributes
  36. *
  37. * @see sfWidgetForm
  38. */
  39. protected function configure($options = array(), $attributes = array())
  40. {
  41. $this->addRequiredOption('choices');
  42. $this->addOption('multiple', false);
  43. $this->addOption('expanded', false);
  44. $this->addOption('renderer_class', false);
  45. $this->addOption('renderer_options', array());
  46. $this->addOption('renderer', false);
  47. }
  48. /**
  49. * @param string $name The element name
  50. * @param string $value The value selected in this widget
  51. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  52. * @param array $errors An array of errors for the field
  53. *
  54. * @return string An HTML tag string
  55. *
  56. * @see sfWidgetForm
  57. */
  58. public function render($name, $value = null, $attributes = array(), $errors = array())
  59. {
  60. if ($this->getOption('multiple'))
  61. {
  62. $attributes['multiple'] = 'multiple';
  63. if ('[]' != substr($name, -2))
  64. {
  65. $name .= '[]';
  66. }
  67. }
  68. if (!$this->getOption('renderer') && !$this->getOption('renderer_class') && $this->getOption('expanded'))
  69. {
  70. unset($attributes['multiple']);
  71. }
  72. return $this->getRenderer()->render($name, $value, $attributes, $errors);
  73. }
  74. /**
  75. * Gets the stylesheet paths associated with the widget.
  76. *
  77. * @return array An array of stylesheet paths
  78. */
  79. public function getStylesheets()
  80. {
  81. return $this->getRenderer()->getStylesheets();
  82. }
  83. /**
  84. * Gets the JavaScript paths associated with the widget.
  85. *
  86. * @return array An array of JavaScript paths
  87. */
  88. public function getJavaScripts()
  89. {
  90. return $this->getRenderer()->getJavaScripts();
  91. }
  92. public function getChoices()
  93. {
  94. $choices = $this->getOption('choices');
  95. if ($choices instanceof sfCallable)
  96. {
  97. $choices = $choices->call();
  98. }
  99. return $choices;
  100. }
  101. public function getRenderer()
  102. {
  103. if ($this->getOption('renderer'))
  104. {
  105. return $this->getOption('renderer');
  106. }
  107. if (!$class = $this->getOption('renderer_class'))
  108. {
  109. $type = !$this->getOption('expanded') ? '' : ($this->getOption('multiple') ? 'checkbox' : 'radio');
  110. $class = sprintf('sfWidgetFormSelect%s', ucfirst($type));
  111. }
  112. return new $class(array_merge(array('choices' => new sfCallable(array($this, 'getChoices'))), $this->options['renderer_options']), $this->getAttributes());
  113. }
  114. public function __clone()
  115. {
  116. if ($this->getOption('choices') instanceof sfCallable)
  117. {
  118. $callable = $this->getOption('choices')->getCallable();
  119. if (is_array($callable))
  120. {
  121. $callable[0] = $this;
  122. $this->setOption('choices', new sfCallable($callable));
  123. }
  124. }
  125. }
  126. }