| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace ThurData\Servers\KerioEmail\Core\UI\Traits;
- use \ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\FormConstants;
- use \ThurData\Servers\KerioEmail\Core\ServiceLocator;
- /**
- * Form Elements related functions
- *
- * @autor ThurData <info@thrudata.ch>
- */
- trait Form
- {
- protected $submit = null;
- protected $formType = FormConstants::UPDATE;
- protected $allowedActions = null;
- protected $confirmMessage = null;
- protected $translateConfirmMessage = true;
- protected $lang = null;
- protected $localLangReplacements = [];
- protected $defaultActions = [
- FormConstants::CREATE,
- FormConstants::READ,
- FormConstants::UPDATE,
- FormConstants::DELETE,
- FormConstants::REORDER,
- ];
- public function disableTranslateConfirmMessage()
- {
- $this->translateConfirmMessage = false;
-
- return $this;
- }
-
- public function isTranslateConfirmMessage()
- {
- return $this->translateConfirmMessage;
- }
- public function getAllowedActions()
- {
- if ($this->allowedActions === null)
- {
- $this->allowedActions = $this->getDefaultActions();
- }
- return $this->allowedActions;
- }
-
- public function addDefaultActions($defaultAction)
- {
- if (!in_array($defaultAction, $this->defaultActions, true))
- {
- $this->defaultActions[] = $defaultAction;
- }
-
- return $this;
- }
-
- public function removeDefaultAction($defaultAction)
- {
- if ($key = array_search($defaultAction, $this->defaultActions, true))
- {
- unset($this->defaultActions[$key]);
- }
-
- return $this;
- }
- protected function getDefaultActions()
- {
- return $this->defaultActions;
- }
- public function setAllowedActions(array $allowed)
- {
- $default = $this->getDefaultActions();
- $filtered = array_map(
- function(&$action) use ($default)
- {
- if (!in_array($action, $default))
- {
- unset($action);
- }
- }
- , $allowed);
- if (count($filtered) > 0)
- {
- $this->allowedActions = $filtered;
- }
-
- return $this;
- }
- public function setFormType($type)
- {
- $default = $this->getAllowedActions();
- if (in_array($type, $default))
- {
- $this->formType = $type;
- }
-
- return $this;
- }
- public function setSubmit($button)
- {
- $this->submit = $button;
-
- return $this;
- }
- public function getSubmitHtml()
- {
- return ($this->submit === null) ? '' : $this->submit->getHtml();
- }
- public function getFormType()
- {
- return $this->formType;
- }
- public function setConfirmMessage($message, $replacementParams = [])
- {
- if (is_string($message))
- {
- $this->confirmMessage = $message;
- }
- $this->addLocalLangReplacements($replacementParams);
- return $this;
- }
- public function getConfirmMessage()
- {
- return $this->confirmMessage;
- }
- protected function loadLang()
- {
- if ($this->lang === null)
- {
- $this->lang = ServiceLocator::call('lang');
- }
-
- return $this;
- }
- protected function addLangReplacements()
- {
- $this->loadLang();
- foreach ($this->localLangReplacements as $key => $value)
- {
- if ($value === null)
- {
- $tmpVal = $this->getFieldValueByName($key);
- $value = $tmpVal === null ? '' : $tmpVal;
- }
- $this->lang->addReplacementConstant($key, $value);
- }
-
- return $this;
- }
- protected function getFieldValueByName($fieldName = null)
- {
- foreach ($this->fields as $field)
- {
- if ($field->getName() === $fieldName)
- {
- return $field->getValue();
- }
- }
- return null;
- }
- protected function addLocalLangReplacements($replacementParams = [])
- {
- foreach ($replacementParams as $key => $value)
- {
- $this->localLangReplacements[$key] = $value ?: null;
- }
-
- return $this;
- }
- }
|