Multiselect.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Form select element
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Data\Form\Element;
  12. use Magento\Framework\Escaper;
  13. class Multiselect extends AbstractElement
  14. {
  15. /**
  16. * @param Factory $factoryElement
  17. * @param CollectionFactory $factoryCollection
  18. * @param Escaper $escaper
  19. * @param array $data
  20. */
  21. public function __construct(
  22. Factory $factoryElement,
  23. CollectionFactory $factoryCollection,
  24. Escaper $escaper,
  25. $data = []
  26. ) {
  27. parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
  28. $this->setType('select');
  29. $this->setExtType('multiple');
  30. $this->setSize(10);
  31. }
  32. /**
  33. * Get the name
  34. *
  35. * @return string
  36. */
  37. public function getName()
  38. {
  39. $name = parent::getName();
  40. if (strpos($name, '[]') === false) {
  41. $name .= '[]';
  42. }
  43. return $name;
  44. }
  45. /**
  46. * Get the element as HTML
  47. *
  48. * @return string
  49. */
  50. public function getElementHtml()
  51. {
  52. $this->addClass('select multiselect admin__control-multiselect');
  53. $html = '';
  54. if ($this->getCanBeEmpty()) {
  55. $html .= '
  56. <input type="hidden" id="' . $this->getHtmlId() . '_hidden" name="' . parent::getName() . '" value="" />
  57. ';
  58. }
  59. if (!empty($this->_data['disabled'])) {
  60. $html .= '<input type="hidden" name="' . parent::getName() . '_disabled" value="" />';
  61. }
  62. $html .= '<select id="' . $this->getHtmlId() . '" name="' . $this->getName() . '" ' . $this->serialize(
  63. $this->getHtmlAttributes()
  64. ) . $this->_getUiId() . ' multiple="multiple">' . "\n";
  65. $value = $this->getValue();
  66. if (!is_array($value)) {
  67. $value = explode(',', $value);
  68. }
  69. $values = $this->getValues();
  70. if ($values) {
  71. foreach ($values as $option) {
  72. if (is_array($option['value'])) {
  73. $html .= '<optgroup label="' . $option['label'] . '">' . "\n";
  74. foreach ($option['value'] as $groupItem) {
  75. $html .= $this->_optionToHtml($groupItem, $value);
  76. }
  77. $html .= '</optgroup>' . "\n";
  78. } else {
  79. $html .= $this->_optionToHtml($option, $value);
  80. }
  81. }
  82. }
  83. $html .= '</select>' . "\n";
  84. $html .= $this->getAfterElementHtml();
  85. return $html;
  86. }
  87. /**
  88. * Get the HTML attributes
  89. *
  90. * @return string[]
  91. */
  92. public function getHtmlAttributes()
  93. {
  94. return [
  95. 'title',
  96. 'class',
  97. 'style',
  98. 'onclick',
  99. 'onchange',
  100. 'disabled',
  101. 'size',
  102. 'tabindex',
  103. 'data-form-part',
  104. 'data-role',
  105. 'data-action'
  106. ];
  107. }
  108. /**
  109. * Get the default HTML
  110. *
  111. * @return string
  112. */
  113. public function getDefaultHtml()
  114. {
  115. $result = $this->getNoSpan() === true ? '' : '<span class="field-row">' . "\n";
  116. $result .= $this->getLabelHtml();
  117. $result .= $this->getElementHtml();
  118. if ($this->getSelectAll() && $this->getDeselectAll()) {
  119. $result .= '<a href="#" onclick="return ' .
  120. $this->getJsObjectName() .
  121. '.selectAll()">' .
  122. $this->getSelectAll() .
  123. '</a> <span class="separator">&nbsp;|&nbsp;</span>';
  124. $result .= '<a href="#" onclick="return ' .
  125. $this->getJsObjectName() .
  126. '.deselectAll()">' .
  127. $this->getDeselectAll() .
  128. '</a>';
  129. }
  130. $result .= $this->getNoSpan() === true ? '' : '</span>' . "\n";
  131. $result .= '<script type="text/javascript">' . "\n";
  132. $result .= ' var ' . $this->getJsObjectName() . ' = {' . "\n";
  133. $result .= ' selectAll: function() { ' . "\n";
  134. $result .= ' var sel = $("' . $this->getHtmlId() . '");' . "\n";
  135. $result .= ' for(var i = 0; i < sel.options.length; i ++) { ' . "\n";
  136. $result .= ' sel.options[i].selected = true; ' . "\n";
  137. $result .= ' } ' . "\n";
  138. $result .= ' return false; ' . "\n";
  139. $result .= ' },' . "\n";
  140. $result .= ' deselectAll: function() {' . "\n";
  141. $result .= ' var sel = $("' . $this->getHtmlId() . '");' . "\n";
  142. $result .= ' for(var i = 0; i < sel.options.length; i ++) { ' . "\n";
  143. $result .= ' sel.options[i].selected = false; ' . "\n";
  144. $result .= ' } ' . "\n";
  145. $result .= ' return false; ' . "\n";
  146. $result .= ' }' . "\n";
  147. $result .= ' }' . "\n";
  148. $result .= "\n" . '</script>';
  149. return $result;
  150. }
  151. /**
  152. * Get the name of the JS object
  153. *
  154. * @return string
  155. */
  156. public function getJsObjectName()
  157. {
  158. return $this->getHtmlId() . 'ElementControl';
  159. }
  160. /**
  161. * @param array $option
  162. * @param array $selected
  163. * @return string
  164. */
  165. protected function _optionToHtml($option, $selected)
  166. {
  167. $html = '<option value="' . $this->_escape($option['value']) . '"';
  168. $html .= isset($option['title']) ? 'title="' . $this->_escape($option['title']) . '"' : '';
  169. $html .= isset($option['style']) ? 'style="' . $option['style'] . '"' : '';
  170. if (in_array((string)$option['value'], $selected)) {
  171. $html .= ' selected="selected"';
  172. }
  173. $html .= '>' . $this->_escape($option['label']) . '</option>' . "\n";
  174. return $html;
  175. }
  176. }