BaseForm.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\Forms;
  3. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Builder\BaseContainer;
  4. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\ResponseTemplates;
  5. use function \ModulesGarden\Servers\ZimbraEmail\Core\Helper\sl;
  6. /**
  7. * BaseForm controler
  8. *
  9. * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
  10. */
  11. class BaseForm extends BaseContainer implements \ModulesGarden\Servers\ZimbraEmail\Core\UI\Interfaces\AjaxElementInterface, \ModulesGarden\Servers\ZimbraEmail\Core\UI\Interfaces\FormInterface
  12. {
  13. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\Form;
  14. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\Fields;
  15. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\Sections;
  16. use \ModulesGarden\Servers\ZimbraEmail\Core\UI\Traits\FormDataProvider;
  17. protected $id = 'baseForm';
  18. protected $name = 'baseForm';
  19. protected $formAction = null;
  20. protected $requestObj = null;
  21. protected $htmlAttributes = [
  22. 'onsubmit' => 'return false;'
  23. ];
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. $formAction = $this->getRequestValue('mgformtype', false);
  28. if ($formAction === FormConstants::RELOAD)
  29. {
  30. $this->formAction = $formAction;
  31. }
  32. }
  33. public function returnAjaxData()
  34. {
  35. $this->loadProvider();
  36. $this->formAction = $this->getRequestValue('mgformtype', false);
  37. $resp = new ResponseTemplates\HtmlDataJsonResponse();
  38. $resp->setCallBackFunction($this->getCallBackFunction());
  39. $resp->setRefreshTargetIds($this->refreshActionIds);
  40. if (!$this->isFormActionValid())
  41. {
  42. return $resp->setMessageAndTranslate('undefinedAction')->setStatusError();
  43. }
  44. $this->reloadFormStructure();
  45. if (!$this->validateForm())
  46. {
  47. $resp = new ResponseTemplates\RawDataJsonResponse();
  48. $resp->setCallBackFunction($this->getCallBackFunction());
  49. return $resp->setMessageAndTranslate('formValidationError')->setStatusError()->setData(['FormValidationErrors' => $this->validationErrors]);
  50. }
  51. $response = $this->dataProvider->{$this->formAction}();
  52. if ($response instanceof \ModulesGarden\Servers\ZimbraEmail\Core\UI\Interfaces\ResponseInterface)
  53. {
  54. return $response;
  55. }
  56. return $resp->setMessageAndTranslate('changesHasBeenSaved');
  57. }
  58. protected function validateForm()
  59. {
  60. if (in_array($this->formAction, [FormConstants::READ, FormConstants::REORDER, FormConstants::RELOAD]))
  61. {
  62. return true;
  63. }
  64. $this->validateFields($this->request);
  65. $this->validateSections($this->request);
  66. if (count($this->validationErrors) > 0)
  67. {
  68. return false;
  69. }
  70. return true;
  71. }
  72. protected function isReadDatatoForm()
  73. {
  74. if (!$this->formAction)
  75. {
  76. $this->formAction = $this->getRequestValue('mgformtype', false);
  77. }
  78. return ($this->formAction && ($this->formAction === FormConstants::READ || $this->formAction === FormConstants::REORDER
  79. || $this->formAction === FormConstants::RELOAD));
  80. }
  81. protected function loadDataToForm()
  82. {
  83. if(!sl('request')->get('ajax') || !$this->isReadDatatoForm())
  84. {
  85. return;
  86. }
  87. $this->loadProvider();
  88. $this->dataProvider->initData();
  89. foreach ($this->fields as &$field)
  90. {
  91. $field->setValue($this->dataProvider->getValueById($field->getId()));
  92. $avValues = $this->dataProvider->getAvailableValuesById($field->getId());
  93. if ($avValues && method_exists($field, 'setAvailableValues'))
  94. {
  95. $field->setAvailableValues($avValues);
  96. }
  97. if ($this->dataProvider->isDisabledById($field->getId()))
  98. {
  99. $field->disableField();
  100. }
  101. }
  102. foreach ($this->sections as &$section)
  103. {
  104. $section->loadDataToForm($this->dataProvider);
  105. }
  106. $this->addLangReplacements();
  107. }
  108. protected function isFormActionValid()
  109. {
  110. if ($this->formAction === false || !in_array($this->formAction, $this->getAllowedActions())
  111. || !method_exists($this->dataProvider, $this->formAction))
  112. {
  113. return false;
  114. }
  115. return true;
  116. }
  117. protected function loadDataToFormByName()
  118. {
  119. $this->loadProvider();
  120. foreach ($this->fields as &$field)
  121. {
  122. $field->setValue($this->dataProvider->getValueByName($field->getName()));
  123. if ($this->dataProvider->isDisabledById($field->getId()))
  124. {
  125. $field->disableField();
  126. }
  127. }
  128. foreach ($this->sections as &$section)
  129. {
  130. $section->loadDataToFormByName($this->dataProvider);
  131. }
  132. $this->addLangReplacements();
  133. }
  134. protected function runFormAction()
  135. {
  136. $this->dataProvider->{$this->formAction}();
  137. }
  138. protected function reloadFormStructure()
  139. {
  140. //to be overwritten
  141. }
  142. protected function reloadForm()
  143. {
  144. if ($this->formAction === FormConstants::RELOAD)
  145. {
  146. $this->reloadFormStructure();
  147. $this->runFormAction();
  148. }
  149. }
  150. public function getHtml()
  151. {
  152. $this->reloadForm();
  153. if ($this->html === '')
  154. {
  155. $this->buildHtml();
  156. }
  157. return $this->html;
  158. }
  159. }