SelectAjax.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * Class constructor
  10. */
  11. function SelectAjax($elementName = null, $elementLabel = null, $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. 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. $defaultValues = $this->getAttribute('defaults');
  37. $defaultValues = empty($defaultValues) ? [] : $defaultValues;
  38. $width = 'element';
  39. $givenWidth = '300';
  40. if (!empty($givenWidth)) {
  41. $width = $givenWidth;
  42. }
  43. //Get the minimumInputLength for select2
  44. $minimumInputLength = $this->getAttribute('minimumInputLength') > 3 ?
  45. $this->getAttribute('minimumInputLength') :
  46. 3
  47. ;
  48. $plHolder = $this->getAttribute('placeholder');
  49. if (empty($plHolder)) {
  50. $plHolder = get_lang('SelectAnOption');
  51. }
  52. $html .= <<<JS
  53. <script>
  54. $(function(){
  55. $('#{$this->getAttribute('name')}').select2({
  56. $languageCondition
  57. placeholder_: '$plHolder',
  58. allowClear: true,
  59. width: '$width',
  60. minimumInputLength: '$minimumInputLength',
  61. // instead of writing the function to execute the request we use Select2s convenient helper
  62. ajax: {
  63. url: '{$this->getAttribute('url')}',
  64. dataType: 'json',
  65. data: function(params) {
  66. return {
  67. q: params.term, // search term
  68. page_limit: 10,
  69. };
  70. },
  71. processResults: function (data, page) {
  72. //parse the results into the format expected by Select2
  73. return {
  74. results: data.items
  75. };
  76. }
  77. $formatCondition
  78. }
  79. });
  80. });
  81. </script>
  82. JS;
  83. $html .= Display::select(
  84. $this->getAttribute('name'),
  85. $defaultValues,
  86. array_keys($defaultValues),
  87. [
  88. 'id' => $this->getAttribute('name'),
  89. 'style' => 'width: 100%;'
  90. ],
  91. false
  92. );
  93. return $html;
  94. }
  95. }