sfWidgetFormSelectCheckbox.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. * sfWidgetFormSelectCheckbox represents an array of checkboxes.
  11. *
  12. * @package symfony
  13. * @subpackage widget
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfWidgetFormSelectCheckbox.class.php 13324 2008-11-24 22:53:58Z FabianLange $
  16. */
  17. class sfWidgetFormSelectCheckbox 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 checkbox and the label
  26. * * class: The class to use for the main <ul> tag
  27. * * separator: The separator to use between each input checkbox
  28. * * formatter: A callable to call to format the checkbox 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', 'checkbox_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. if (is_null($value))
  63. {
  64. $value = array();
  65. }
  66. $choices = $this->getOption('choices');
  67. if ($choices instanceof sfCallable)
  68. {
  69. $choices = $choices->call();
  70. }
  71. // with groups?
  72. if (count($choices) && is_array(next($choices)))
  73. {
  74. $parts = array();
  75. foreach ($choices as $key => $option)
  76. {
  77. $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
  78. }
  79. return implode("\n", $parts);
  80. }
  81. else
  82. {
  83. return $this->formatChoices($name, $value, $choices, $attributes);;
  84. }
  85. }
  86. protected function formatChoices($name, $value, $choices, $attributes)
  87. {
  88. $inputs = array();
  89. foreach ($choices as $key => $option)
  90. {
  91. $baseAttributes = array(
  92. 'name' => $name,
  93. 'type' => 'checkbox',
  94. 'value' => self::escapeOnce($key),
  95. 'id' => $id = $this->generateId($name, self::escapeOnce($key)),
  96. );
  97. if ((is_array($value) && in_array(strval($key), $value)) || strval($key) == strval($value))
  98. {
  99. $baseAttributes['checked'] = 'checked';
  100. }
  101. $inputs[] = array(
  102. 'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
  103. 'label' => $this->renderContentTag('label', $option, array('for' => $id)),
  104. );
  105. }
  106. return call_user_func($this->getOption('formatter'), $this, $inputs);
  107. }
  108. public function formatter($widget, $inputs)
  109. {
  110. $rows = array();
  111. foreach ($inputs as $input)
  112. {
  113. $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
  114. }
  115. return $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
  116. }
  117. public function __clone()
  118. {
  119. if ($this->getOption('choices') instanceof sfCallable)
  120. {
  121. $callable = $this->getOption('choices')->getCallable();
  122. if (is_array($callable))
  123. {
  124. $callable[0] = $this;
  125. $this->setOption('choices', new sfCallable($callable));
  126. }
  127. }
  128. }
  129. }