| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- namespace ThurData\Servers\KerioEmail\Core\UI\Widget\Forms;
- use \ThurData\Servers\KerioEmail\Core\UI\Builder\BaseContainer;
- use \ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates;
- use function \ThurData\Servers\KerioEmail\Core\Helper\sl;
- /**
- * BaseForm controler
- *
- * @autor ThurData <info@thurdata.ch>
- */
- class BaseForm extends BaseContainer implements \ThurData\Servers\KerioEmail\Core\UI\Interfaces\AjaxElementInterface, \ThurData\Servers\KerioEmail\Core\UI\Interfaces\FormInterface
- {
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\Form;
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\Fields;
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\Sections;
- use \ThurData\Servers\KerioEmail\Core\UI\Traits\FormDataProvider;
-
- protected $id = 'baseForm';
- protected $name = 'baseForm';
- protected $formAction = null;
- protected $requestObj = null;
- protected $htmlAttributes = [
- 'onsubmit' => 'return false;'
- ];
- public function __construct()
- {
- parent::__construct();
- $formAction = $this->getRequestValue('mgformtype', false);
- if ($formAction === FormConstants::RELOAD)
- {
- $this->formAction = $formAction;
- }
- }
- public function returnAjaxData()
- {
- $this->loadProvider();
- $this->formAction = $this->getRequestValue('mgformtype', false);
-
- $resp = new ResponseTemplates\HtmlDataJsonResponse();
- $resp->setCallBackFunction($this->getCallBackFunction());
- $resp->setRefreshTargetIds($this->refreshActionIds);
- if (!$this->isFormActionValid())
- {
- return $resp->setMessageAndTranslate('undefinedAction')->setStatusError();
- }
- $this->reloadFormStructure();
- if (!$this->validateForm())
- {
- $resp = new ResponseTemplates\RawDataJsonResponse();
- $resp->setCallBackFunction($this->getCallBackFunction());
-
- return $resp->setMessageAndTranslate('formValidationError')->setStatusError()->setData(['FormValidationErrors' => $this->validationErrors]);
- }
- $response = $this->dataProvider->{$this->formAction}();
- if ($response instanceof \ThurData\Servers\KerioEmail\Core\UI\Interfaces\ResponseInterface)
- {
- return $response;
- }
- return $resp->setMessageAndTranslate('changesHasBeenSaved');
- }
- protected function validateForm()
- {
- if (in_array($this->formAction, [FormConstants::READ, FormConstants::REORDER, FormConstants::RELOAD]))
- {
- return true;
- }
- $this->validateFields($this->request);
- $this->validateSections($this->request);
- if (count($this->validationErrors) > 0)
- {
- return false;
- }
- return true;
- }
-
- protected function isReadDatatoForm()
- {
- if (!$this->formAction)
- {
- $this->formAction = $this->getRequestValue('mgformtype', false);
- }
-
- return ($this->formAction && ($this->formAction === FormConstants::READ || $this->formAction === FormConstants::REORDER
- || $this->formAction === FormConstants::RELOAD));
- }
- protected function loadDataToForm()
- {
- if(!sl('request')->get('ajax') || !$this->isReadDatatoForm())
- {
- return;
- }
- $this->loadProvider();
- $this->dataProvider->initData();
- foreach ($this->fields as &$field)
- {
- $field->setValue($this->dataProvider->getValueById($field->getId()));
- $avValues = $this->dataProvider->getAvailableValuesById($field->getId());
- if ($avValues && method_exists($field, 'setAvailableValues'))
- {
- $field->setAvailableValues($avValues);
- }
- if ($this->dataProvider->isDisabledById($field->getId()))
- {
- $field->disableField();
- }
- }
- foreach ($this->sections as &$section)
- {
- $section->loadDataToForm($this->dataProvider);
- }
- $this->addLangReplacements();
- }
- protected function isFormActionValid()
- {
- if ($this->formAction === false || !in_array($this->formAction, $this->getAllowedActions())
- || !method_exists($this->dataProvider, $this->formAction))
- {
- return false;
- }
- return true;
- }
-
- protected function loadDataToFormByName()
- {
- $this->loadProvider();
- foreach ($this->fields as &$field)
- {
- $field->setValue($this->dataProvider->getValueByName($field->getName()));
- if ($this->dataProvider->isDisabledById($field->getId()))
- {
- $field->disableField();
- }
- }
- foreach ($this->sections as &$section)
- {
- $section->loadDataToFormByName($this->dataProvider);
- }
-
- $this->addLangReplacements();
- }
- protected function runFormAction()
- {
- $this->dataProvider->{$this->formAction}();
- }
- protected function reloadFormStructure()
- {
- //to be overwritten
- }
- protected function reloadForm()
- {
- if ($this->formAction === FormConstants::RELOAD)
- {
- $this->reloadFormStructure();
- $this->runFormAction();
- }
- }
- public function getHtml()
- {
- $this->reloadForm();
- if ($this->html === '')
- {
- $this->buildHtml();
- }
- return $this->html;
- }
- }
|