SelectAjax.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * A drop down list with all languages to use with QuickForm
  5. */
  6. class SelectAjax extends HTML_QuickForm_select
  7. {
  8. /**
  9. * @inheritdoc
  10. */
  11. public function __construct($elementName, $elementLabel = '', $options = null, $attributes = null)
  12. {
  13. parent::__construct($elementName, $elementLabel, $options, $attributes);
  14. }
  15. /**
  16. * The ajax call must contain an array of id and text
  17. * @return string
  18. */
  19. public function toHtml()
  20. {
  21. $html = api_get_asset('select2/dist/js/select2.min.js');
  22. $iso = api_get_language_isocode(api_get_interface_language());
  23. $languageCondition = '';
  24. if (file_exists(api_get_path(SYS_PATH) . "web/assets/select2/dist/js/i18n/$iso.js")) {
  25. $html .= api_get_asset("select2/dist/js/i18n/$iso.js");
  26. $languageCondition = "language: '$iso',";
  27. }
  28. $html .= api_get_css(api_get_path(WEB_PATH).'web/assets/select2/dist/css/select2.min.css');
  29. $formatResult = $this->getAttribute('formatResult');
  30. $formatCondition = null;
  31. if (!empty($formatResult)) {
  32. $formatCondition = ',
  33. templateResult : '.$formatResult.',
  34. templateSelection : '.$formatResult;
  35. }
  36. $width = 'element';
  37. $givenWidth = '100%';
  38. if (!empty($givenWidth)) {
  39. $width = $givenWidth;
  40. }
  41. //Get the minimumInputLength for select2
  42. $minimumInputLength = $this->getAttribute('minimumInputLength') > 3 ?
  43. $this->getAttribute('minimumInputLength') :
  44. 3
  45. ;
  46. $plHolder = $this->getAttribute('placeholder');
  47. if (empty($plHolder)) {
  48. $plHolder = get_lang('SelectAnOption');
  49. }
  50. $id = $this->getAttribute('id');
  51. if (empty($id)) {
  52. $id = $this->getAttribute('name');
  53. $this->setAttribute('id', $id);
  54. }
  55. // URL must return ajax json_encode arrady [items => [['id'=>1, 'text'='content']]
  56. $url = $this->getAttribute('url');
  57. $iso = api_get_language_isocode();
  58. $languageCondition = "language: '$iso',";
  59. if (!$url) {
  60. $url = $this->getAttribute('url_function');
  61. } else {
  62. $url = "'$url'";
  63. }
  64. $tagsAttr = $this->getAttribute('tags');
  65. $multipleAttr = $this->getAttribute('multiple');
  66. $tags = !empty($tagsAttr) ? (bool) $tagsAttr : false;
  67. $tags = $tags ? 'true' : 'false';
  68. $multiple = !empty($multipleAttr) ? (bool) $multipleAttr : false;
  69. $multiple = $multiple ? 'true' : 'false';
  70. $max = $this->getAttribute('maximumSelectionLength');
  71. $max = !empty($max) ? "maximumSelectionLength: $max, ": '';
  72. $html .= <<<JS
  73. <script>
  74. $(function(){
  75. $('#{$this->getAttribute('id')}').select2({
  76. $languageCondition
  77. placeholder: '$plHolder',
  78. allowClear: true,
  79. width: '$width',
  80. minimumInputLength: '$minimumInputLength',
  81. tags: $tags,
  82. ajax: {
  83. url: $url,
  84. dataType: 'json',
  85. data: function(params) {
  86. return {
  87. q: params.term, // search term
  88. page_limit: 10,
  89. };
  90. },
  91. processResults: function (data, page) {
  92. //parse the results into the format expected by Select2
  93. return {
  94. results: data.items
  95. };
  96. }
  97. $formatCondition
  98. }
  99. });
  100. });
  101. </script>
  102. JS;
  103. $this->removeAttribute('formatResult');
  104. $this->removeAttribute('minimumInputLength');
  105. $this->removeAttribute('maximumSelectionLength');
  106. $this->removeAttribute('tags');
  107. $this->removeAttribute('placeholder');
  108. $this->removeAttribute('class');
  109. $this->removeAttribute('url');
  110. $this->removeAttribute('url_function');
  111. $this->setAttribute('style', 'width: 100%;');
  112. return parent::toHtml() . $html;
  113. }
  114. /**
  115. * We check the options and return only the values that _could_ have been
  116. * selected. We also return a scalar value if select is not "multiple"
  117. */
  118. public function exportValue(&$submitValues, $assoc = false)
  119. {
  120. $value = $this->_findValue($submitValues);
  121. if (!$value) {
  122. $value = '';
  123. }
  124. return $this->_prepareValue($value, $assoc);
  125. }
  126. }