/lib/vendor/symfony-1.4.14/lib/widget/sfWidgetFormSelectCheckbox.class.php

https://github.com/yuya-takeyama/symfony-hackathon-20110924 · PHP · 129 lines · 69 code · 14 blank · 46 comment · 9 complexity · 1010bdffae5dbc7c869b745cea037124 MD5 · raw file

  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 30762 2010-08-25 12:33:33Z fabien $
  16. */
  17. class sfWidgetFormSelectCheckbox extends sfWidgetFormChoiceBase
  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 sfWidgetFormChoiceBase
  36. */
  37. protected function configure($options = array(), $attributes = array())
  38. {
  39. parent::configure($options, $attributes);
  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. * Renders the widget.
  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 ('[]' != substr($name, -2))
  61. {
  62. $name .= '[]';
  63. }
  64. if (null === $value)
  65. {
  66. $value = array();
  67. }
  68. $choices = $this->getChoices();
  69. // with groups?
  70. if (count($choices) && is_array(current($choices)))
  71. {
  72. $parts = array();
  73. foreach ($choices as $key => $option)
  74. {
  75. $parts[] = strtr($this->getOption('template'), array('%group%' => $key, '%options%' => $this->formatChoices($name, $value, $option, $attributes)));
  76. }
  77. return implode("\n", $parts);
  78. }
  79. else
  80. {
  81. return $this->formatChoices($name, $value, $choices, $attributes);
  82. }
  83. }
  84. protected function formatChoices($name, $value, $choices, $attributes)
  85. {
  86. $inputs = array();
  87. foreach ($choices as $key => $option)
  88. {
  89. $baseAttributes = array(
  90. 'name' => $name,
  91. 'type' => 'checkbox',
  92. 'value' => self::escapeOnce($key),
  93. 'id' => $id = $this->generateId($name, self::escapeOnce($key)),
  94. );
  95. if ((is_array($value) && in_array(strval($key), $value)) || strval($key) == strval($value))
  96. {
  97. $baseAttributes['checked'] = 'checked';
  98. }
  99. $inputs[$id] = array(
  100. 'input' => $this->renderTag('input', array_merge($baseAttributes, $attributes)),
  101. 'label' => $this->renderContentTag('label', self::escapeOnce($option), array('for' => $id)),
  102. );
  103. }
  104. return call_user_func($this->getOption('formatter'), $this, $inputs);
  105. }
  106. public function formatter($widget, $inputs)
  107. {
  108. $rows = array();
  109. foreach ($inputs as $input)
  110. {
  111. $rows[] = $this->renderContentTag('li', $input['input'].$this->getOption('label_separator').$input['label']);
  112. }
  113. return !$rows ? '' : $this->renderContentTag('ul', implode($this->getOption('separator'), $rows), array('class' => $this->getOption('class')));
  114. }
  115. }