Select.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\AjaxFields;
  3. use ThurData\Servers\KerioEmail\Core\UI\Interfaces\AjaxElementInterface;
  4. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates;
  5. /**
  6. * Select field controler
  7. *
  8. * @autor ThurData <info@thrudata.ch>
  9. */
  10. class Select extends \ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Fields\Select implements AjaxElementInterface
  11. {
  12. use \ThurData\Servers\KerioEmail\Core\UI\Traits\HideByDefaultIfNoData;
  13. protected $id = 'ajaxSelect';
  14. protected $name = 'ajaxSelect';
  15. protected $vueComponent = true;
  16. protected $defaultVueComponentName = 'mg-ajax-select';
  17. /**
  18. * a list of fields id's, fi those fields are changed the select will reload its content
  19. * @var type array
  20. */
  21. protected $reloadOnChangeFields = [];
  22. /**
  23. * do not overwrite this function
  24. * @return type \ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\RawDataJsonResponse
  25. */
  26. public function returnAjaxData()
  27. {
  28. $this->prepareAjaxData();
  29. $returnData = [
  30. 'options' => $this->getAvailableValues(),
  31. 'selected' => $this->getValue(),
  32. 'additionalData' => $this->data['additionalData']
  33. ];
  34. return (new ResponseTemplates\RawDataJsonResponse($returnData))->setCallBackFunction($this->callBackFunction);
  35. }
  36. /**
  37. * overwrite this function, use setSelectedValue && setAvailableValues functions
  38. */
  39. public function prepareAjaxData()
  40. {
  41. $this->setAvailableValues([
  42. ['key' => '1', 'value' => 'value1'],
  43. ['key' => '2', 'value' => 'value2']
  44. ]);
  45. // '2' for single, ['1', '2'] for multiple
  46. $this->setSelectedValue('2');
  47. }
  48. public function initContent()
  49. {
  50. }
  51. public function addReloadOnChangeField($fieldId = null)
  52. {
  53. if (is_string($fieldId) && $fieldId !== '')
  54. {
  55. $this->reloadOnChangeFields[] = $fieldId;
  56. }
  57. return $this;
  58. }
  59. public function getReloadOnChangeFields()
  60. {
  61. return $this->reloadOnChangeFields;
  62. }
  63. public function wrappReloadIdsToString()
  64. {
  65. $str = '';
  66. foreach ($this->reloadOnChangeFields as $key => $value)
  67. {
  68. $str .= (string)$key . " : '" . $value . "'" . ($key === end(array_keys($this->reloadOnChangeFields)) ? ' ' : ', ');
  69. }
  70. return $str;
  71. }
  72. }