Form.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits;
  3. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Buttons\ButtonSubmitForm;
  4. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\FormConstants;
  5. use \ModulesGarden\Servers\ProxmoxCloudVps\Core\ServiceLocator;
  6. /**
  7. * Form Elements related functions
  8. *
  9. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  10. */
  11. trait Form
  12. {
  13. /**
  14. * @var ButtonSubmitForm
  15. */
  16. protected $submit = null;
  17. protected $formType = FormConstants::UPDATE;
  18. protected $allowedActions = null;
  19. protected $confirmMessage = null;
  20. protected $translateConfirmMessage = true;
  21. protected $lang = null;
  22. protected $localLangReplacements = [];
  23. protected $defaultActions = [
  24. FormConstants::CREATE,
  25. FormConstants::READ,
  26. FormConstants::UPDATE,
  27. FormConstants::DELETE,
  28. FormConstants::REORDER,
  29. ];
  30. public function disableTranslateConfirmMessage()
  31. {
  32. $this->translateConfirmMessage = false;
  33. return $this;
  34. }
  35. public function isTranslateConfirmMessage()
  36. {
  37. return $this->translateConfirmMessage;
  38. }
  39. public function getAllowedActions()
  40. {
  41. if ($this->allowedActions === null)
  42. {
  43. $this->allowedActions = $this->getDefaultActions();
  44. }
  45. return $this->allowedActions;
  46. }
  47. public function addDefaultActions($defaultAction)
  48. {
  49. if (!in_array($defaultAction, $this->defaultActions, true))
  50. {
  51. $this->defaultActions[] = $defaultAction;
  52. }
  53. return $this;
  54. }
  55. public function removeDefaultAction($defaultAction)
  56. {
  57. if ($key = array_search($defaultAction, $this->defaultActions, true))
  58. {
  59. unset($this->defaultActions[$key]);
  60. }
  61. return $this;
  62. }
  63. protected function getDefaultActions()
  64. {
  65. return $this->defaultActions;
  66. }
  67. public function setAllowedActions(array $allowed)
  68. {
  69. $default = $this->getDefaultActions();
  70. $filtered = array_map(
  71. function(&$action) use ($default)
  72. {
  73. if (!in_array($action, $default))
  74. {
  75. unset($action);
  76. }
  77. }
  78. , $allowed);
  79. if (count($filtered) > 0)
  80. {
  81. $this->allowedActions = $filtered;
  82. }
  83. return $this;
  84. }
  85. public function setFormType($type)
  86. {
  87. $default = $this->getAllowedActions();
  88. if (in_array($type, $default))
  89. {
  90. $this->formType = $type;
  91. }
  92. return $this;
  93. }
  94. public function setSubmit($button)
  95. {
  96. $this->submit = $button;
  97. return $this;
  98. }
  99. public function getSubmitHtml()
  100. {
  101. return ($this->submit === null) ? '' : $this->submit->getHtml();
  102. }
  103. public function getFormType()
  104. {
  105. return $this->formType;
  106. }
  107. public function setConfirmMessage($message, $replacementParams = [])
  108. {
  109. if (is_string($message))
  110. {
  111. $this->confirmMessage = $message;
  112. }
  113. $this->addLocalLangReplacements($replacementParams);
  114. return $this;
  115. }
  116. public function getConfirmMessage()
  117. {
  118. return $this->confirmMessage;
  119. }
  120. protected function loadLang()
  121. {
  122. if ($this->lang === null)
  123. {
  124. $this->lang = ServiceLocator::call('lang');
  125. }
  126. return $this;
  127. }
  128. protected function addLangReplacements()
  129. {
  130. $this->loadLang();
  131. foreach ($this->localLangReplacements as $key => $value)
  132. {
  133. if ($value === null)
  134. {
  135. $tmpVal = $this->getFieldValueByName($key);
  136. $value = $tmpVal === null ? '' : $tmpVal;
  137. }
  138. $this->lang->addReplacementConstant($key, $value);
  139. }
  140. return $this;
  141. }
  142. protected function getFieldValueByName($fieldName = null)
  143. {
  144. foreach ($this->fields as $field)
  145. {
  146. if ($field->getName() === $fieldName)
  147. {
  148. return $field->getValue();
  149. }
  150. }
  151. return null;
  152. }
  153. protected function addLocalLangReplacements($replacementParams = [])
  154. {
  155. foreach ($replacementParams as $key => $value)
  156. {
  157. $this->localLangReplacements[$key] = $value ?: null;
  158. }
  159. return $this;
  160. }
  161. }