Form.php 4.4 KB

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