SelectAjax.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. public function __construct($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. 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 = $this->getAttribute('url');
  56. if (!$url) {
  57. $url = $this->getAttribute('url_function');
  58. } else {
  59. $url = "'$url'";
  60. }
  61. $html .= <<<JS
  62. <script>
  63. $(function(){
  64. $('#{$this->getAttribute('id')}').select2({
  65. $languageCondition
  66. placeholder: '$plHolder',
  67. allowClear: true,
  68. width: '$width',
  69. minimumInputLength: '$minimumInputLength',
  70. ajax: {
  71. url: $url,
  72. dataType: 'json',
  73. data: function(params) {
  74. return {
  75. q: params.term, // search term
  76. page_limit: 10,
  77. };
  78. },
  79. processResults: function (data, page) {
  80. //parse the results into the format expected by Select2
  81. return {
  82. results: data.items
  83. };
  84. }
  85. $formatCondition
  86. }
  87. });
  88. });
  89. </script>
  90. JS;
  91. $this->removeAttribute('formatResult');
  92. $this->removeAttribute('minimumInputLength');
  93. $this->removeAttribute('placeholder');
  94. $this->removeAttribute('class');
  95. $this->removeAttribute('url');
  96. $this->removeAttribute('url_function');
  97. $this->setAttribute('style', 'width: 100%;');
  98. return parent::toHtml() . $html;
  99. }
  100. }