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:
<?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 rendering logic and methods.
* @property \MvcCore\Ext\Forms\View $view View instance.
*/
trait Rendering {
/**
* Render whole `<form>` with all content into HTML string to display it.
* - If form is not initialized, there is automatically
* called `$form->Init();` method.
* - If form is not pre-dispatched for rendering, there is
* automatically called `$form->PreDispatch();` method.
* - Create new form view instance and set up the view with local
* context variables.
* - Render form naturally or by custom template.
* - Clean session errors, because errors should be rendered
* only once, only when it's used and it's now - in this rendering process.
* @return string
*/
public function Render ($controllerDashedName = NULL, $actionDashedName = NULL) {
/** @var $this \MvcCore\Ext\Form */
$this->PreDispatch();
if ($this->viewScript) {
$result = $this->view->RenderTemplate();
} else {
$result = $this->view->RenderNaturally();
}
$this->cleanSessionErrorsAfterRender();
return $result;
}
/**
* Render form inner content, all field controls, content inside `<form>` tag,
* without form errors. Go through all `$form->fields` and call `$field->Render();`
* on every field instance and put field render result into an empty `<div>`
* element. Render each field in full possible way - naturally by label
* configuration with possible errors configured beside or with custom field template.
* @return string
*/
public function RenderContent () {
/** @var $this \MvcCore\Ext\Form */
return $this->view->RenderContent();
}
/**
* Render form errors to display them inside `<form>` element.
* If form is configured to render all errors together at form beginning,
* this function completes all form errors into `div.errors` with `div.error` elements
* inside containing each single errors message.
* @return string
*/
public function RenderErrors () {
/** @var $this \MvcCore\Ext\Form */
return $this->view->RenderErrors();
}
/**
* Render form begin - opening `<form>` tag and automatically
* prepared hidden input with CSRF (Cross Site Request Forgery) tokens.
* @return string
*/
public function RenderBegin () {
/** @var $this \MvcCore\Ext\Form */
return $this->view->RenderBegin();
}
/**
* Render form end - closing `</form>` tag and supporting javascript and css files
* only if there is necessary to add any supporting javascript or css files by
* form configuration and if form is not using external JS/CSS renderer(s).
* @return string
*/
public function RenderEnd () {
/** @var $this \MvcCore\Ext\Form */
$this->PreDispatch();
$this->SetFormTagRenderingStatus(FALSE);
$result = '</form>'
. $this->RenderSupportingJs()
. $this->RenderSupportingCss();
$this->cleanSessionErrorsAfterRender();
return $result;
}
/**
* Render all supporting CSS files directly
* as `<style>` tag content inside HTML template
* placed directly after `</form>` end tag or
* render all supporting CSS files by configured external
* CSS files renderer to add only links to HTML response `<head>`
* section, linked to external CSS source files.
* @return string
*/
public function RenderSupportingCss () {
/** @var $this \MvcCore\Ext\Form */
if (!$this->cssSupportFiles) return '';
$cssFiles = $this->completeSupportingFilesToRender(FALSE);
if (!$cssFiles) return '';
$cssFilesContent = '';
$useExternalRenderer = is_callable($this->cssSupportFilesRenderer);
foreach ($cssFiles as $cssFile) {
$this->renderSupportingFile(
$cssFilesContent, $cssFile,
$useExternalRenderer, $this->cssSupportFilesRenderer
);
}
if ($useExternalRenderer) return '';
return '<style type="text/css">'.$cssFilesContent.'</style>';
}
/**
* Render all supporting JS files directly
* as `<script>` tag content inside HTML template
* placed directly after `</form>` end tag or
* render all supporting JS files by configured external
* JS files renderer to add only links to HTML response `<head>`
* section, linked to external JS source files.
* Anyway there is always created at least one `<script>` tag
* placed directly after `</form>` end tag with supporting javascripts
* initializations - `new MvcCoreForm(/*javascript*\/);` - by rendered form fields
* options, names, counts, values etc...
* @return string
*/
public function RenderSupportingJs () {
/** @var $this \MvcCore\Ext\Form */
if (!$this->jsSupportFiles) return '';
$jsFiles = $this->completeSupportingFilesToRender(TRUE);
if (!$jsFiles) return '';
$jsFilesContent = '';
$fieldsConstructors = [];
$useExternalRenderer = is_callable($this->jsSupportFilesRenderer);
if (!isset(self::$allJsSupportFiles[static::$jsBaseSupportFile])) {
$jsBaseSupportFile = $this->absolutizeSupportingFilePath(static::$jsBaseSupportFile, 'js');
self::$allJsSupportFiles[static::$jsBaseSupportFile] = TRUE;
$this->renderSupportingFile(
$jsFilesContent, $jsBaseSupportFile,
$useExternalRenderer, $this->jsSupportFilesRenderer
);
}
foreach ($jsFiles as $jsFile)
$this->renderSupportingFile(
$jsFilesContent, $jsFile,
$useExternalRenderer, $this->jsSupportFilesRenderer
);
foreach ($this->jsSupportFiles as $jsSupportFile) {
list(, $jsFullClassName, $constructParams) = $jsSupportFile;
$constructParamsEncoded = json_encode($constructParams);
// remove beginning char and ending char from javascript array: `[`, `]`
$constructParamsEncoded = mb_substr(
$constructParamsEncoded, 1, mb_strlen($constructParamsEncoded) - 2
);
$fieldsConstructors[] = 'new ' . $jsFullClassName . '(' . $constructParamsEncoded . ')';
}
$result = $jsFilesContent . 'new MvcCoreForm('
. 'document.getElementById(\'' . $this->id . '\'),'
. '[' . implode(',', $fieldsConstructors) . ']'
. ')';
$viewDocType = \MvcCore\View::GetDoctype();
if (
$this->response->IsXmlOutput() ||
strpos($viewDocType, \MvcCore\View::DOCTYPE_XHTML) !== FALSE ||
strpos($viewDocType, \MvcCore\View::DOCTYPE_XML) !== FALSE
) $result = '/*<![CDATA[*/' . $result . '/*]]>*/';
return '<script type="text/javascript">' . $result . '</script>';
}
/**
* Call this function after form has been rendered
* to clear session errors, because there is not necessary
* to have there those errors anymore, because will be
* displayed in rendered form.
* @return \MvcCore\Ext\Form
*/
protected function cleanSessionErrorsAfterRender () {
/** @var $this \MvcCore\Ext\Form */
$this->errors = [];
$session = & $this->getSession();
$session->errors = [];
return $this;
}
}