SelectAjax.php 4.3 KB

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