1. sfWidgetFormSelectCheckbox.class.php
  2. /** * sfWidgetFormSelectCheckbox represents an array of checkboxes. * * @package symfony * @subpackage widget * @author Fabien Potencier * @version SVN: $Id: sfWidgetFormSelectCheckbox.class.php 23994 2009-11-15 22:55:24Z bschussek $ */
  3. class sfWidgetFormSelectCheckbox extends sfWidgetFormChoiceBase
  4. {
  5. /**
  6. * Constructor.
  7. *
  8. * Available options:
  9. *
  10. * * choices: An array of possible choices (required)
  11. * * label_separator: The separator to use between the input checkbox and the label
  12. * * class: The class to use for the main <ul> tag
  13. * * separator: The separator to use between each input checkbox
  14. * * formatter: A callable to call to format the checkbox choices
  15. * The formatter callable receives the widget and the array of inputs as arguments
  16. * * template: The template to use when grouping option in groups (%group% %options%)
  17. *
  18. * @param array $options An array of options
  19. * @param array $attributes An array of default HTML attributes
  20. *
  21. * @see sfWidgetFormChoiceBase
  22. */
  23. protected function configure($options = array(), $attributes = array())
  24. {
  25. parent::configure($options, $attributes);
  26. $this->addOption('class', 'checkbox_list');
  27. $this->addOption('label_separator', '&nbsp;');
  28. $this->addOption('separator', "\n");
  29. $this->addOption('formatter', array($this, 'formatter'));
  30. $this->addOption('template', '%group% %options%');
  31. }
  32. /**
  33. * @param string $name The element name
  34. * @param string $value The value selected in this widget
  35. * @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
  36. * @param array $errors An array of errors for the field
  37. *
  38. * @return string An HTML tag string
  39. *
  40. * @see sfWidgetForm
  41. */
  42. public function render($name, $value = null, $attributes = array(), $errors = array())
  43. {
  44. if ('[]' != substr($name, -2))
  45. {
  46. $name .= '[]';
  47. }
  48. if (null === $value)
  49. {
  50. $value = array();
  51. }
  52. $choices = $this->getChoices();
  53. // with groups?
  54. if (count($choices) && is_array(current($choices)))
  55. {
  56. $parts = array();
  57. foreach ($choices as $key => $option)
  58. {
  59. $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
  60. }
  61. return implode("\n", $parts);
  62. }
  63. else
  64. {
  65. return $this->formatChoices($name, $value, $choices, $attributes);
  66. }
  67. }
  68. protected function formatChoices($name, $value, $choices, $attributes)
  69. {
  70. $inputs = array();
  71. foreach ($choices as $key => $option)
  72. {
  73. $baseAttributes = array(
  74. 'name' => $name,
  75. 'type' => 'checkbox',
  76. 'value' => self::escapeOnce($key),
  77. 'id' => $id = $this->generateId($name, self::escapeOnce($key)),
  78. );
  79. if ((is_array($value) && in_array(strval($key), $value)) || strval($key) == strval($value))
  80. {
  81. $baseAttributes['checked'] = 'checked';
  82. }
  83. $inputs[$id] = array(
  84. 'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
  85. 'label' => $this->renderContentTag('label', $option, array('for' => $id)),
  86. );
  87. }
  88. return call_user_func($this->getOption('formatter'), $this, $inputs);
  89. }
  90. public function formatter($widget, $inputs)
  91. {
  92. $rows = array();
  93. foreach ($inputs as $input)
  94. {
  95. $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
  96. }
  97. return !$rows ? '' : $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
  98. }
  99. }

Debug toolbar