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: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229:
<?php
namespace MvcCore\Ext;
class Form
extends \MvcCore\Controller
implements \MvcCore\Ext\IForm {
const VERSION = '5.0.0';
use \MvcCore\Ext\Form\InternalProps;
use \MvcCore\Ext\Form\ConfigProps;
use \MvcCore\Ext\Form\GetMethods;
use \MvcCore\Ext\Form\SetMethods;
use \MvcCore\Ext\Form\AddMethods;
use \MvcCore\Ext\Form\FieldMethods;
use \MvcCore\Ext\Form\Session;
use \MvcCore\Ext\Form\Csrf;
use \MvcCore\Ext\Form\Rendering;
use \MvcCore\Ext\Form\Assets;
use \MvcCore\Ext\Form\Submitting;
public function __construct (\MvcCore\IController $controller = NULL) {
if ($controller === NULL) {
$controller = \MvcCore\Ext\Form::GetCallerControllerInstance();
if ($controller === NULL)
$controller = \MvcCore\Application::GetInstance()->GetController();
if ($controller === NULL) $this->throwNewInvalidArgumentException(
'There was not possible to determinate caller controller, '
.'where is form instance create. Provide `$controller` instance explicitly '
.'by first `\MvcCore\Ext\Form::__construct($controller);` argument.'
);
}
$controller->AddChildController($this, $this->id);
if (static::$jsSupportFilesRootDir === NULL || static::$cssSupportFilesRootDir === NULL) {
$baseAssetsPath = str_replace('\\', '/', __DIR__) . '/Forms/assets';
if (static::$jsSupportFilesRootDir === NULL)
static::$jsSupportFilesRootDir = $baseAssetsPath;
if (static::$cssSupportFilesRootDir === NULL)
static::$cssSupportFilesRootDir = $baseAssetsPath;
}
if (self::$sessionClass === NULL)
self::$sessionClass = $this->application->GetSessionClass();
if (self::$toolClass === NULL)
self::$toolClass = $this->application->GetToolClass();
}
protected function throwNewInvalidArgumentException ($errorMsg) {
$str = '['.get_class().'] ' . $errorMsg . ' ('
. 'form id: `'.$this->id . '`, '
. 'form type: `'.get_class($this->form).'`'
.')';
throw new \InvalidArgumentException($str);
}
public function Init ($submit = FALSE) {
if ($this->dispatchState > \MvcCore\IController::DISPATCH_STATE_CREATED)
return $this;
parent::Init();
$this->dispatchState = \MvcCore\IController::DISPATCH_STATE_INITIALIZED;
if (!$this->id)
throw new \RuntimeException("No form `id` property defined in `".get_class($this)."`.");
if (isset(self::$instances[$this->id])) {
$anotherForm = self::$instances[$this->id];
if ($anotherForm !== $this)
throw new \RuntimeException("Form id `".$this->id."` already defined.");
} else {
self::$instances[$this->id] = TRUE;
}
$this->translate = $this->translator !== NULL && is_callable($this->translator);
return $this;
}
public function PreDispatch ($submit = FALSE) {
if ($this->dispatchState > \MvcCore\IController::DISPATCH_STATE_INITIALIZED)
return $this;
$this->viewEnabled = !$submit;
parent::PreDispatch();
$session = & $this->getSession();
$this->preDispatchLoadErrors($session);
$this->preDispatchLoadValues($session);
if ($submit) {
$this->dispatchState = \MvcCore\IController::DISPATCH_STATE_PRE_DISPATCHED;
return $this;
}
foreach ($this->fields as $field)
$field->PreDispatch();
if ($this->translate && $this->translateTitle && $this->title !== NULL)
$this->title = $this->Translate($this->title);
$viewClass = $this->viewClass;
$this->view = $viewClass::CreateInstance()
->SetForm($this);
if ($this->viewScript)
$this->view
->SetController($this->parentController)
->SetView($this->parentController->GetView());
if ($this->csrfEnabled)
$this->SetUpCsrf();
$this->dispatchState = \MvcCore\IController::DISPATCH_STATE_PRE_DISPATCHED;
return $this;
}
protected function preDispatchLoadErrors (\MvcCore\ISession $session) {
if (!$session->errors) return;
foreach ($session->errors as $errorMsgAndFieldNames) {
list($errorMsg, $fieldNames) = array_merge([], $errorMsgAndFieldNames);
$this->AddError($errorMsg, $fieldNames);
}
}
protected function preDispatchLoadValues (\MvcCore\ISession $session) {
if (!$session->values) return;
foreach ($session->values as $fieldName => $fieldValue) {
if (!array_key_exists($fieldName, $this->fields)) continue;
$field = $this->fields[$fieldName];
$configuredFieldValue = $field->GetValue();
$multiple = FALSE;
if ($field instanceof \MvcCore\Ext\Forms\Fields\IMultiple)
$multiple = $field->GetMultiple() ?: FALSE;
if (
$configuredFieldValue === NULL ||
($multiple && is_array($configuredFieldValue) && count($configuredFieldValue) === 0)
) {
$this->values[$fieldName] = $fieldValue;
$field->SetValue($fieldValue);
}
}
}
public function Translate ($key, $replacements = []) {
return call_user_func_array($this->translator, func_get_args());
}
}