ButtonCustomAction.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Buttons;
  3. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Builder\BaseContainer;
  4. class ButtonCustomAction extends BaseContainer
  5. {
  6. protected $id = 'ButtonCustomAction';
  7. protected $class = ['lu-btn lu-btn--sm lu-btn--link lu-btn--icon lu-btn--plain lu-btn--default'];
  8. protected $icon = 'lu-zmdi lu-zmdi-plus';
  9. protected $title = 'ButtonCustomAction';
  10. protected $htmlAttributes = [
  11. 'href' => 'javascript:;',
  12. 'data-toggle' => 'lu-tooltip',
  13. ];
  14. protected $customActionName = null;
  15. protected $customActionParams = [];
  16. /**
  17. * Overwrite this function in order to set up:
  18. * $customActionName and $customActionParams values
  19. */
  20. public function initContent()
  21. {
  22. }
  23. /**
  24. * Do not use or override this method!
  25. */
  26. protected function afterInitContent()
  27. {
  28. $this->htmlAttributes['@click'] = 'makeCustomAction(\'' . $this->customActionName . '\', ' . $this->parseCustomParams() . ', $event, \'' . $this->getNamespace() . '\', \'' . $this->getIndex() . '\')';
  29. }
  30. public function setCustomActionName($name)
  31. {
  32. $this->customActionName = $name;
  33. return $this;
  34. }
  35. public function setCustomActionParams(array $params)
  36. {
  37. $this->customActionParams = $params;
  38. return $this;
  39. }
  40. public function addCustomActionParam($key, $value)
  41. {
  42. $this->customActionParams[$key] = $value;
  43. return $this;
  44. }
  45. protected function parseCustomParams()
  46. {
  47. if (count($this->customActionParams) === 0)
  48. {
  49. return '{}';
  50. }
  51. return $this->parseListTOJsString($this->customActionParams);
  52. }
  53. protected function parseListTOJsString($params)
  54. {
  55. $jsString = '{';
  56. foreach ($params as $key => $value)
  57. {
  58. $jsString .= ' ' . $key . ': ' . (is_array($value) ? ($this->parseListTOJsString($value) . ',') : ("'" . (string) $value) . "',");
  59. }
  60. $jsString = trim($jsString, ',') . ' } ';
  61. return $jsString;
  62. }
  63. }