1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Ext\Form;
/**
* Trait for class `MvcCore\Ext\Form` containing logic and methods to work with
* values necessary store in session. It use configured core class `\MvcCore\Session`.
*/
trait Session {
/**
* Clear form values to empty array and clear form values in form session namespace,
* clear form errors to empty array and clear form errors in form session namespace and
* clear form CSRF tokens clear CRSF tokens in form session namespace.
* @return \MvcCore\Ext\Form
*/
public function ClearSession () {
/** @var $this \MvcCore\Ext\Form */
$this->values = [];
$this->errors = [];
$session = & $this->getSession();
$session->values = [];
$session->csrf = [];
$session->errors = [];
return $this;
}
/**
* Store form values, form errors and form CSRF tokens
* in it's own form session namespace.
* @return \MvcCore\Ext\Form
*/
public function SaveSession () {
/** @var $this \MvcCore\Ext\Form */
$session = & $this->getSession();
$session->errors = $this->errors;
$session->values = $this->values;
return $this;
}
/**
* Get session namespace reference with configured expiration
* and predefined fields `values`, `csrf` and `errors` as arrays.
* @return \MvcCore\Session
*/
protected function & getSession () {
if (isset(self::$allFormsSessions[$this->id])) {
$sessionNamespace = & self::$allFormsSessions[$this->id];
} else {
$sessionClass = self::$sessionClass;
$toolClass = self::$toolClass;
$formIdPc = $this->id;
if (strpos($formIdPc, '-') !== FALSE)
$formIdPc = $toolClass::GetPascalCaseFromDashed($formIdPc);
if (strpos($formIdPc, '_') !== FALSE)
$formIdPc = $toolClass::GetPascalCaseFromUnderscored($formIdPc);
$formIdPcUc = ucfirst($formIdPc);
$namespaceName = "\\MvcCore\\Ext\Form\\{$formIdPcUc}";
$sessionNamespace = $sessionClass::GetNamespace($namespaceName);
// Do not use hoops expiration, because there is better
// to set up any large value into session namespace
// or zero value to browser close and after rendered
// errors just clear the errors.
//$sessionNamespace->SetExpirationHoops(1);
$sessionNamespace->SetExpirationSeconds($this->GetSessionExpiration());
if (!isset($sessionNamespace->values)) $sessionNamespace->values = [];
if (!isset($sessionNamespace->csrf)) $sessionNamespace->csrf = [];
if (!isset($sessionNamespace->errors)) $sessionNamespace->errors = [];
self::$allFormsSessions[$this->id] = & $sessionNamespace;
}
return $sessionNamespace;
}
}