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:
<?php
namespace MvcCore\Ext\Form\Core;
use \MvcCore\Ext\Form;
class View extends \MvcCore\View
{
public $Form = null;
public $View = null;
public function __construct (\MvcCore\Ext\Form & $form) {
$ctrl = & $form->Controller;
if (class_exists('\MvcCore\Controller') && $ctrl instanceof \MvcCore\Controller) {
parent::__construct($ctrl);
} else {
$this->Controller = & $ctrl;
}
$this->Form = & $form;
include_once('Helpers.php');
$this->View = Helpers::GetControllerView($ctrl);
}
public function __call ($method, $arguments) {
if (isset($this->Field) && method_exists($this->Field, $method)) {
return call_user_func_array(array($this->Field, $method), $arguments);
} else {
return parent::__call($method, $arguments);
}
}
public function RenderTemplate () {
return $this->Render($this->Form->TemplateTypePath, $this->Form->TemplatePath);
}
public function RenderNaturally () {
return $this->RenderBegin() . $this->RenderErrors() . $this->RenderContent() . $this->RenderEnd();
}
public function RenderBegin () {
$result = "<form";
$attrs = array();
$form = & $this->Form;
$formProperties = array('Id', 'Action', 'Method', 'Enctype');
foreach ($formProperties as $property) {
if ($form->$property) $attrs[strtolower($property)] = $form->$property;
}
if ($form->CssClass) $attrs['class'] = $form->CssClass;
foreach ($form->Attributes as $key => $value) {
if (!in_array($key, $formProperties)) $attrs[$key] = $value;
}
$attrsStr = self::RenderAttrs($attrs);
if ($attrsStr) $result .= ' ' . $attrsStr;
$result .= '>';
$result .= $this->RenderCsrf();
return $result;
}
public function RenderCsrf () {
list ($name, $value) = $this->Form->SetUpCsrf();
return '<input type="hidden" name="'.$name.'" value="'.$value.'" />';
}
public function GetCsrf () {
return $this->Form->GetCsrf();
}
public function RenderErrors () {
$result = "";
include_once('Configuration.php');
if ($this->Form->Errors && $this->Form->ErrorsRenderMode == Configuration::ERROR_RENDER_MODE_ALL_TOGETHER) {
$result .= '<div class="errors">';
foreach ($this->Form->Errors as & $errorMessageAndFieldName) {
$errorMessage = $errorMessageAndFieldName[0];
$fieldName = isset($errorMessageAndFieldName[1]) ? $errorMessageAndFieldName[1] : '' ;
$result .= '<div class="error ' . $fieldName . '">'.$errorMessage.'</div>';
}
$result .= '</div>';
}
return $result;
}
public function RenderContent () {
$result = "";
$fieldRendered = "";
foreach ($this->Form->Fields as & $field) {
$fieldRendered = $field->Render();
include_once(__DIR__ . '/../Hidden.php');
if (!($field instanceof Form\Hidden)) {
$fieldRendered = "<div>".$fieldRendered."</div>";
}
$result .= $fieldRendered;
}
return $result;
}
public function RenderEnd () {
$result = "</form>";
if ($this->Js) $result .= $this->Form->RenderJs();
if ($this->Css) $result .= $this->Form->RenderCss();
return $result;
}
public static function Format ($str = '', array $args = array()) {
foreach ($args as $key => $value) {
$str = str_replace('{'.$key.'}', (string)$value, $str);
}
return $str;
}
public static function RenderAttrs (array $atrributes = array()) {
$result = array();
foreach ($atrributes as $attrName => $attrValue) {
$result[] = $attrName.'="'.$attrValue.'"';
}
return implode(' ', $result);
}
}