diff options
| author | Moritz Strohm <strohm@data-quest.de> | 2022-12-16 13:44:16 +0000 |
|---|---|---|
| committer | David Siegfried <david.siegfried@uni-vechta.de> | 2022-12-16 13:44:16 +0000 |
| commit | d6ec870033743fcc07f8b6c71cb32450cc75abb6 (patch) | |
| tree | 0e900032e4e555f7dbc2770737b9eeed0cdf747d /lib/classes/forms/Form.php | |
| parent | 20ebf68a5481623b362374ba1c0cd7025f2f6dfe (diff) | |
StEP 1596, closes #1596
Closes #1596
Merge request studip/studip!1038
Diffstat (limited to 'lib/classes/forms/Form.php')
| -rw-r--r-- | lib/classes/forms/Form.php | 69 |
1 files changed, 62 insertions, 7 deletions
diff --git a/lib/classes/forms/Form.php b/lib/classes/forms/Form.php index b588ddb..ba0258d 100644 --- a/lib/classes/forms/Form.php +++ b/lib/classes/forms/Form.php @@ -6,7 +6,7 @@ class Form extends Part { //models: - protected $afterStore = []; + protected $store_callbacks = []; //internals protected $inputs = []; @@ -14,7 +14,12 @@ class Form extends Part //appearance in html-form protected $url = null; + protected $save_button_text = ''; + protected $save_button_name = ''; + protected $autoStore = false; + protected $success_message = ''; + protected $collapsable = false; //to identify a form element @@ -57,6 +62,8 @@ class Form extends Part final public function __construct(...$parts) { parent::__construct(...$parts); + //Set a default for the success message: + $this->success_message = _('Daten wurden gespeichert.'); } /** @@ -160,6 +167,48 @@ class Form extends Part return $this->url; } + /** + * Sets the text for the "save" button in the form. + * + * @param string $text The text for the button to save the form. + * @return $this + */ + public function setSaveButtonText(string $text): Form + { + $this->save_button_text = $text; + return $this; + } + + /** + * @return string The text for the "save" button in the form. + */ + public function getSaveButtonText() : string + { + return $this->save_button_text ?: _('Speichern'); + } + + public function setSaveButtonName(string $name): Form + { + $this->save_button_name = $name; + return $this; + } + + public function getSaveButtonName() : string + { + return $this->save_button_name ?: $this->getSaveButtonText(); + } + + public function setSuccessMessage(string $success_message): Form + { + $this->success_message = $success_message; + return $this; + } + + public function getSuccessMessage() : string + { + return $this->success_message; + } + public function setCollapsable($collapsing = true) { $this->collapsable = $collapsing; @@ -182,7 +231,9 @@ class Form extends Part $this->autoStore = true; if (\Request::isPost() && \Request::isAjax() && !\Request::isDialog()) { $this->store(); - \PageLayout::postSuccess(_('Daten wurden gespeichert.')); + if ($this->success_message) { + \PageLayout::postSuccess($this->success_message); + } page_close(); die(); } @@ -200,9 +251,9 @@ class Form extends Part * @param callable $c * @return Form $this */ - public function addAfterStoreCallback(Callable $c) + public function addStoreCallback(Callable $c): Form { - $this->afterStore[] = $c; + $this->store_callbacks[] = $c; return $this; } @@ -240,11 +291,15 @@ class Form extends Part $stored = 0; //store by each input + $all_values = []; foreach ($this->getAllInputs() as $input) { $value = $this->getStorableValueFromRequest($input); if ($value !== null) { $callback = $this->getStoringCallback($input); - $stored += $callback($value, $input); + if (is_callable($callback)) { + $stored += $callback($value, $input); + } + $all_values[$input->getName()] = $value; } } @@ -255,9 +310,9 @@ class Form extends Part } } - foreach ($this->afterStore as $callback) { + foreach ($this->store_callbacks as $callback) { if (is_callable($callback)) { - $stored += call_user_func($callback, $this); + $stored += call_user_func($callback, $this, $all_values); } else { //throw warning if callback is not available: if ($callback === null) { |
