select_ajax.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'HTML/QuickForm/select.php';
  4. require_once 'HTML/QuickForm/html.php';
  5. /**
  6. * A drop down list with all languages to use with QuickForm
  7. */
  8. class HTML_QuickForm_Select_Ajax extends HTML_QuickForm_select
  9. {
  10. /**
  11. * Class constructor
  12. */
  13. function HTML_QuickForm_Select_Ajax($elementName = null, $elementLabel = null, $options = null, $attributes = null)
  14. {
  15. parent::HTML_QuickForm_Select($elementName, $elementLabel, $options, $attributes);
  16. }
  17. /**
  18. * The ajax call must contain an array of id and text
  19. * @return string
  20. */
  21. function toHtml()
  22. {
  23. $html = api_get_js('select2/select2.js');
  24. $iso = api_get_language_isocode(api_get_interface_language());
  25. $localeFile = 'select2_locale_'.$iso.'.js';
  26. if (file_exists(api_get_path(LIBRARY_PATH).'javascript/select2/'.$localeFile)) {
  27. $html .= api_get_js('select2/'.$localeFile);
  28. }
  29. $html .= api_get_css(api_get_path(WEB_LIBRARY_PATH).'javascript/select2/select2.css');
  30. $formatResult = $this->getAttribute('formatResult');
  31. $formatCondition = null;
  32. if (!empty($formatResult)) {
  33. $formatCondition = ',
  34. formatResult : '.$formatResult.',
  35. formatSelection : '.$formatResult.',';
  36. }
  37. $defaultValues = $this->getAttribute('defaults');
  38. $dataCondition = null;
  39. $tags = null;
  40. if (!empty($defaultValues)) {
  41. $result = json_encode($defaultValues);
  42. $result = str_replace('"id"', 'id', $result);
  43. $result = str_replace('"text"', 'text', $result);
  44. $dataCondition = '$("#'.$this->getAttribute('name').'").select2("data", '.$result.')';
  45. $tags = ', tags : function() { return '.$result.'} ';
  46. }
  47. $width = 'element';
  48. $givenWidth = $this->getAttribute('width');
  49. if (!empty($givenWidth)) {
  50. $width = $givenWidth;
  51. }
  52. //Get the minimumInputLength for select2
  53. $minimumInputLength = $this->getAttribute('minimumInputLength') > 3 ?
  54. $this->getAttribute('minimumInputLength') :
  55. 3
  56. ;
  57. $plHolder = $this->getAttribute('placeholder');
  58. if (empty($plHolder)) {
  59. $plHolder = get_lang('SelectAnOption');
  60. }
  61. $html .= '<script>
  62. $(function() {
  63. $("#'.$this->getAttribute('name').'").select2({
  64. placeholder: "' . $plHolder . '",
  65. allowClear: true,
  66. width: "'.$width.'",
  67. minimumInputLength: ' . $minimumInputLength . ',
  68. // instead of writing the function to execute the request we use Select2s convenient helper
  69. ajax: {
  70. url: "'.$this->getAttribute('url').'",
  71. dataType: "json",
  72. data: function (term, page) {
  73. return {
  74. q: term, // search term
  75. page_limit: 10,
  76. };
  77. },
  78. results: function (data, page) { // parse the results into the format expected by Select2.
  79. // since we are using custom formatting functions we do not need to alter remote JSON data
  80. return {
  81. results: data
  82. };
  83. }
  84. }
  85. '.$tags.'
  86. '.$formatCondition.'
  87. });
  88. '.$dataCondition.'
  89. });
  90. </script>';
  91. $html .= '<input id="'.$this->getAttribute('name').'" name="'.$this->getAttribute('name').'" />';
  92. return $html;
  93. }
  94. }