SelectAjax.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. *
  18. * @return string
  19. */
  20. public function toHtml()
  21. {
  22. $iso = api_get_language_isocode();
  23. $formatResult = $this->getAttribute('formatResult');
  24. $formatCondition = '';
  25. if (!empty($formatResult)) {
  26. $formatCondition = ',
  27. templateResult : '.$formatResult.',
  28. templateSelection : '.$formatResult;
  29. }
  30. $width = 'element';
  31. $givenWidth = '100%';
  32. if (!empty($givenWidth)) {
  33. $width = $givenWidth;
  34. }
  35. //Get the minimumInputLength for select2
  36. $minimumInputLength = $this->getAttribute('minimumInputLength') > 3 ?
  37. $this->getAttribute('minimumInputLength') : 3
  38. ;
  39. $plHolder = $this->getAttribute('placeholder');
  40. if (empty($plHolder)) {
  41. $plHolder = get_lang('Please select an option');
  42. }
  43. $id = $this->getAttribute('id');
  44. if (empty($id)) {
  45. $id = $this->getAttribute('name');
  46. $this->setAttribute('id', $id);
  47. }
  48. // URL must return ajax json_encode arrady [items => [['id'=>1, 'text'='content']]
  49. $url = $this->getAttribute('url');
  50. if (!$url) {
  51. $url = $this->getAttribute('url_function');
  52. } else {
  53. $url = "'$url'";
  54. }
  55. $tagsAttr = $this->getAttribute('tags');
  56. $multipleAttr = $this->getAttribute('multiple');
  57. $tags = !empty($tagsAttr) ? (bool) $tagsAttr : false;
  58. $tags = $tags ? 'true' : 'false';
  59. $multiple = !empty($multipleAttr) ? (bool) $multipleAttr : false;
  60. $multiple = $multiple ? 'true' : 'false';
  61. $max = $this->getAttribute('maximumSelectionLength');
  62. $max = !empty($max) ? "maximumSelectionLength: $max, " : '';
  63. $html = <<<JS
  64. <script>
  65. $(function(){
  66. $('#{$this->getAttribute('id')}').select2({
  67. language: '$iso',
  68. placeholder: '$plHolder',
  69. allowClear: true,
  70. width: '$width',
  71. minimumInputLength: '$minimumInputLength',
  72. tags: $tags,
  73. ajax: {
  74. url: $url,
  75. dataType: 'json',
  76. data: function(params) {
  77. return {
  78. q: params.term, // search term
  79. page_limit: 10,
  80. };
  81. },
  82. processResults: function (data, page) {
  83. // Parse the results into the format expected by Select2
  84. if (data.items) {
  85. return {
  86. results: data.items
  87. };
  88. }
  89. return {
  90. results: ''
  91. };
  92. }
  93. $formatCondition
  94. }
  95. });
  96. });
  97. </script>
  98. JS;
  99. $this->removeAttribute('formatResult');
  100. $this->removeAttribute('minimumInputLength');
  101. $this->removeAttribute('maximumSelectionLength');
  102. $this->removeAttribute('tags');
  103. $this->removeAttribute('placeholder');
  104. $this->removeAttribute('class');
  105. $this->removeAttribute('url');
  106. $this->removeAttribute('url_function');
  107. $this->setAttribute('style', 'width: 100%;');
  108. return parent::toHtml().$html;
  109. }
  110. /**
  111. * We check the options and return only the values that _could_ have been
  112. * selected. We also return a scalar value if select is not "multiple".
  113. */
  114. public function exportValue(&$submitValues, $assoc = false)
  115. {
  116. $value = $this->_findValue($submitValues);
  117. if (!$value) {
  118. $value = '';
  119. }
  120. return $this->_prepareValue($value, $assoc);
  121. }
  122. }