Select.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ GMkcZk/MWdcfItv/ExaALlToUS8v9wndJJ+qidS5/U0=
  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. * select form element generator class
  14. * apply translator to select options
  15. */
  16. namespace Cube\Form\Element;
  17. use Cube\Form\Element;
  18. class Select extends Element
  19. {
  20. /**
  21. *
  22. * type of element - override the variable from the parent class
  23. *
  24. * @var string
  25. */
  26. protected $_element = 'select';
  27. /**
  28. *
  29. * hide default value
  30. *
  31. * @var bool
  32. */
  33. protected $_hideDefault = false;
  34. /**
  35. *
  36. * class constructor
  37. *
  38. * @param string $name
  39. */
  40. public function __construct($name)
  41. {
  42. parent::__construct($this->_element, $name);
  43. }
  44. /**
  45. *
  46. * get hide default option
  47. *
  48. * @return string
  49. */
  50. public function isHideDefault()
  51. {
  52. return $this->_hideDefault;
  53. }
  54. /**
  55. *
  56. * set hide default option
  57. *
  58. * @param boolean $default
  59. *
  60. * @return $this
  61. */
  62. public function setHideDefault($default = true)
  63. {
  64. $this->_hideDefault = (bool)$default;
  65. return $this;
  66. }
  67. /**
  68. *
  69. * render the form element
  70. *
  71. * @return string
  72. */
  73. public function render()
  74. {
  75. $output = null;
  76. $brackets = null;
  77. $value = $this->getValue();
  78. $translate = $this->getTranslate();
  79. $multipleAttribute = $this->getAttribute('multiple');
  80. if ($this->getMultiple() === true || !empty($multipleAttribute)) {
  81. $brackets = $this->getBrackets();
  82. if (!empty($multipleAttribute)) {
  83. $output .= '<input type="hidden" name="' . $this->_name . '" value=""'
  84. . $this->_endTag;
  85. }
  86. }
  87. $output .=
  88. $this->getPrefix()
  89. . ' <select name="' . $this->_name . $brackets . '" '
  90. . $this->renderAttributes() . '>';
  91. $required = $this->getRequired();
  92. $hideDefault = $this->isHideDefault();
  93. if ($required && !$hideDefault) {
  94. $output .= '<option value="" selected>' . $translate->_('-- select --') . '</option>';
  95. }
  96. $output .= '<optgroup disabled hidden></optgroup>';
  97. foreach ((array)$this->_multiOptions as $key => $option) {
  98. $selected = (in_array($key, (array)$value)) ? 'selected' : '';
  99. if (is_array($option)) {
  100. $title = isset($option[0]) ? $option[0] : null;
  101. if (isset($option[1])) {
  102. $this->addMultiOptionAttributes($key, $option[1]);
  103. }
  104. }
  105. else {
  106. $title = $option;
  107. }
  108. $attributes = array(
  109. 'value="' . $key . '"',
  110. $this->renderOptionAttributes($key),
  111. $selected
  112. );
  113. $output .= '<option ' . implode(' ', array_filter($attributes)) . '>'
  114. . $translate->_($title) . '</option>';
  115. }
  116. $output .= '</select> '
  117. . $this->getSuffix();
  118. return $output;
  119. }
  120. }