ButtonAjaxCustomAction.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Buttons;
  3. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Interfaces\AjaxElementInterface;
  4. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\ResponseTemplates;
  5. class ButtonAjaxCustomAction extends ButtonCustomAction implements AjaxElementInterface
  6. {
  7. protected $id = 'ButtonAjaxCustomAction';
  8. protected $class = ['lu-btn lu-btn-circle lu-btn-outline lu-btn-inverse lu-btn-default lu-btn-icon-only'];
  9. protected $icon = 'lu-zmdi lu-zmdi-plus';
  10. protected $title = 'ButtonAjaxCustomAction';
  11. protected $htmlAttributes = [
  12. 'href' => 'javascript:;',
  13. 'data-toggle' => 'lu-tooltip',
  14. ];
  15. protected $customActionName = null;
  16. protected $customActionParams = [];
  17. public function initContent()
  18. {
  19. $this->htmlAttributes['@click'] = 'makeCustomAction(' . $this->customActionName . ', ' . $this->parseCustomParams() . ', $event, \'' . $this->getNamespace() . '\', \'' . $this->getIndex() . '\')';
  20. }
  21. public function returnAjaxData()
  22. {
  23. return (new ResponseTemplates\RawDataJsonResponse(['exampleData' => 'example']))->setCallBackFunction($this->callBackFunction)->setRefreshTargetIds($this->refreshActionIds);
  24. }
  25. public function setCustomActionName($name)
  26. {
  27. $this->customActionName = $name;
  28. return $this;
  29. }
  30. public function setCustomActionParams(array $params)
  31. {
  32. $this->customActionParams = $params;
  33. return $this;
  34. }
  35. public function addCustomActionParam($key, $value)
  36. {
  37. $this->customActionParams[$key] = $value;
  38. return $this;
  39. }
  40. public function parseCustomParams()
  41. {
  42. if (count($this->customActionParams) === 0)
  43. {
  44. return '{}';
  45. }
  46. return $this->parseListTOJsString($this->customActionParams);
  47. }
  48. protected function parseListTOJsString($params)
  49. {
  50. $jsString = '{';
  51. foreach ($params as $key => $value)
  52. {
  53. $jsString .= ' ' . $key . ': ' . (is_array($value) ? ($this->parseListTOJsString($value) . ',') : ("'" . (string) $value) . "',");
  54. }
  55. $jsString = trim($jsString, ',') . ' } ';
  56. return $jsString;
  57. }
  58. }