ModalActionButtons.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\UI\Traits;
  3. use \ModulesGarden\ProxmoxAddon\Core\DependencyInjection;
  4. use \ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ModalActionButtons\BaseAcceptButton;
  5. use \ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ModalActionButtons\BaseCancelButton;
  6. /**
  7. * Modal Action Buttons related functions
  8. *
  9. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  10. */
  11. trait ModalActionButtons
  12. {
  13. protected $actionButtons = [];
  14. public function addActionButton($button)
  15. {
  16. $this->addButtonToList($button);
  17. return $this;
  18. }
  19. protected function addButtonToList($button)
  20. {
  21. if (is_string($button))
  22. {
  23. $button = DependencyInjection::create($button);
  24. }
  25. $button->setMainContainer($this->mainContainer);
  26. $id = $button->getId();
  27. if (!isset($this->actionButtons[$id]))
  28. {
  29. $this->actionButtons[$id] = $button;
  30. if ($button instanceof \ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AjaxElementInterface)
  31. {
  32. $this->mainContainer->addAjaxElement($this->actionButtons[$id]);
  33. }
  34. }
  35. return $button;
  36. }
  37. public function insertActionButton($buttonId)
  38. {
  39. if (!$this->actionButtons[$buttonId])
  40. {
  41. //add exception
  42. }
  43. else
  44. {
  45. $button = $this->actionButtons[$buttonId];
  46. return $button->getHtml();
  47. }
  48. return '';
  49. }
  50. public function getActionButtons()
  51. {
  52. $this->initActionButtons();
  53. return $this->actionButtons;
  54. }
  55. protected function initActionButtons()
  56. {
  57. if (!empty($this->actionButtons))
  58. {
  59. return $this;
  60. }
  61. $this->addActionButton(new BaseAcceptButton);
  62. $this->addActionButton(new BaseCancelButton);
  63. return $this;
  64. }
  65. public function replaceSubmitButton($button)
  66. {
  67. $this->initActionButtons();
  68. $added = $this->addButtonToList($button);
  69. if (isset($this->actionButtons[$added->getId()]) &&
  70. isset($this->actionButtons['baseAcceptButton']))
  71. {
  72. $this->actionButtons['baseAcceptButton'] = $this->actionButtons[$added->getId()];
  73. unset($this->actionButtons[$added->getId()]);
  74. }
  75. return $this;
  76. }
  77. public function replaceSubmitButtonClasses($classes)
  78. {
  79. $this->initActionButtons();
  80. if (isset($this->actionButtons['baseAcceptButton']))
  81. {
  82. $this->actionButtons['baseAcceptButton']->replaceClasses($classes);
  83. }
  84. return $this;
  85. }
  86. public function setSubmitButtonClassesDanger()
  87. {
  88. $this->replaceSubmitButtonClasses(['lu-btn lu-btn--danger submitForm']);
  89. return $this;
  90. }
  91. }