ModalActionButtons.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\UI\Traits;
  3. use \ThurData\Servers\KerioEmail\Core\DependencyInjection;
  4. use \ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ModalActionButtons\BaseAcceptButton;
  5. use \ThurData\Servers\KerioEmail\Core\UI\Widget\Buttons\ModalActionButtons\BaseCancelButton;
  6. /**
  7. * Modal Action Buttons related functions
  8. *
  9. * @autor ThurData <info@thrudata.ch>
  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 \ThurData\Servers\KerioEmail\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. /**
  92. * Remove action button using action button object
  93. * @param $button
  94. * @return bool
  95. */
  96. public function removeActionButton($button)
  97. {
  98. foreach($this->actionButtons as $key => $obj)
  99. {
  100. if($obj === $button)
  101. {
  102. unset($this->actionButtons[$key]);
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. /**
  109. * Remove action button using index
  110. * @param $index
  111. * @return bool
  112. */
  113. public function removeActionButtonByIndex($index)
  114. {
  115. if(array_key_exists($index, $this->actionButtons))
  116. {
  117. unset($this->actionButtons[$index]);
  118. return true;
  119. }
  120. return false;
  121. }
  122. }