Radio.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ CukK5Jfbrfv+Ux6Er/1HkfFj6DvYlJ23gmD8eg9Dtzw=
  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.01]
  11. */
  12. /**
  13. * radio buttons form element generator class
  14. */
  15. namespace Cube\Form\Element;
  16. use Cube\Form\Element;
  17. class Radio extends Element
  18. {
  19. /**
  20. *
  21. * type of element - override the variable from the parent class
  22. *
  23. * @var string
  24. */
  25. protected $_element = 'radio';
  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. foreach ((array)$this->_multiOptions as $key => $option) {
  48. $checked = ($value == $key) ? 'checked="checked" ' : '';
  49. if (is_array($option)) {
  50. $title = isset($option[0]) ? $option[0] : null;
  51. $description = isset($option[1]) ? $option[1] : null;
  52. if (isset($option[2])) {
  53. $this->addMultiOptionAttributes($key, $option[2]);
  54. }
  55. }
  56. else {
  57. $title = $option;
  58. $description = null;
  59. }
  60. $attributes = array(
  61. 'type="' . $this->_element . '"',
  62. 'name="' . $this->_name . '"',
  63. 'value="' . $key . '"',
  64. $this->renderAttributes(),
  65. $this->renderOptionAttributes($key),
  66. $checked
  67. );
  68. $output .= '<label class="radio">'
  69. . '<input ' . implode(' ', array_filter($attributes)) . '>'
  70. . ' ' . $translate->_($title)
  71. . (($description !== null) ? '<span class="help-block">' . $translate->_($description) . '</span>' : '')
  72. . '</label>'
  73. . "\n";
  74. }
  75. return $output;
  76. }
  77. }