BaseForm.php 5.5 KB

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