Checkbox.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ 2Juc/7HKeZ9SHZoOqukALl4ZcB8gPPgu9JgWXn9brTg=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2017 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.10 [rev.1.10.02]
  11. */
  12. /**
  13. * creates a checkbox element
  14. */
  15. namespace Cube\Form\Element;
  16. use Cube\Form\Element;
  17. class Checkbox extends Element
  18. {
  19. /**
  20. *
  21. * type of element - override the variable from the parent class
  22. *
  23. * @var string
  24. */
  25. protected $_element = 'checkbox';
  26. /**
  27. *
  28. * class constructor
  29. *
  30. * @param string $name
  31. */
  32. public function __construct($name)
  33. {
  34. parent::__construct($this->_element, $name);
  35. }
  36. /**
  37. *
  38. * render the form element
  39. *
  40. * @return string
  41. */
  42. public function render()
  43. {
  44. $output = null;
  45. $value = $this->getValue();
  46. $translate = $this->getTranslate();
  47. $multiple = '';
  48. if (count((array)$this->_multiOptions) > 1 || $this->getMultiple() === true) {
  49. $multiple = $this->_brackets;
  50. }
  51. $output .= '<input type="hidden" name="' . $this->_name . $multiple . '" value=""'
  52. . $this->_endTag;
  53. foreach ((array)$this->_multiOptions as $key => $option) {
  54. $checked = (in_array($key, (array)$value)) ? 'checked="checked"' : '';
  55. if (is_array($option)) {
  56. $title = isset($option[0]) ? $option[0] : null;
  57. $description = isset($option[1]) ? $option[1] : null;
  58. if (isset($option[2])) {
  59. $this->addMultiOptionAttributes($key, $option[2]);
  60. }
  61. }
  62. else {
  63. $title = $option;
  64. $description = null;
  65. }
  66. $attributes = array(
  67. 'type="' . $this->_element . '"',
  68. 'name="' . $this->_name . $multiple . '"',
  69. 'value="' . $key . '"',
  70. $this->renderAttributes(),
  71. $this->renderOptionAttributes($key),
  72. $checked,
  73. );
  74. $output .= '<label class="checkbox">'
  75. . '<input ' . implode(' ', array_filter($attributes)) . '>'
  76. . ' ' . $translate->_($title)
  77. . ((!empty($description)) ? '<span class="help-block">' . $translate->_($description) . '</span>' : '')
  78. . '</label>'
  79. . "\n";
  80. }
  81. return $output;
  82. }
  83. }