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: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287:
<?php
namespace MvcCore\Ext\Forms\Fields;
class File
extends \MvcCore\Ext\Forms\Field
implements \MvcCore\Ext\Forms\Fields\IVisibleField,
\MvcCore\Ext\Forms\Fields\ILabel,
\MvcCore\Ext\Forms\Fields\IMultiple,
\MvcCore\Ext\Forms\Fields\IFile,
\MvcCore\Ext\Forms\Fields\IAlwaysValidate {
use \MvcCore\Ext\Forms\Field\Props\VisibleField;
use \MvcCore\Ext\Forms\Field\Props\Label;
use \MvcCore\Ext\Forms\Field\Props\Multiple;
use \MvcCore\Ext\Forms\Field\Props\Files;
use \MvcCore\Ext\Forms\Field\Props\Wrapper;
const VERSION = '5.0.0';
const ALLOWED_FILE_NAME_CHARS_DEFAULT = '-a-zA-Z0-9@,._ ()+={}[]\'';
const CONFIG_ERR_NO_ACCEPT_PROPERTY = 100;
const CONFIG_ERR_WRONG_FORM_ENCTYPE = 101;
const CONFIG_ERR_UPLOADS_NOT_ALOWED = 102;
const CONFIG_ERR_MAX_UPLOAD_SIZE_LOWER = 103;
const CONFIG_ERR_MAX_POST_SIZE_LOWER = 104;
const CONFIG_ERR_MAX_FILES_COUNT_LOWER = 105;
const CONFIG_ERR_MISMATCH_MIN_MAX_COUNT = 106;
const CONFIG_ERR_MISMATCH_MIN_MAX_SIZE = 107;
protected static $configErrorMessages = [
self::CONFIG_ERR_NO_ACCEPT_PROPERTY => "No `accept` property defined.",
self::CONFIG_ERR_WRONG_FORM_ENCTYPE => "Form needs to define `enctype` attribute as `{0}`.",
self::CONFIG_ERR_UPLOADS_NOT_ALOWED => "System has not allowed file upload.",
self::CONFIG_ERR_MAX_UPLOAD_SIZE_LOWER => "System value for max. file upload size is lower than field configuration.",
self::CONFIG_ERR_MAX_POST_SIZE_LOWER => "System value for max. POST size is lower than field configuration.",
self::CONFIG_ERR_MAX_FILES_COUNT_LOWER => "System value for max. uploaded files count is lower than field configuration.",
self::CONFIG_ERR_MISMATCH_MIN_MAX_COUNT => "Mismatch in min. and max. uploaded files count in field configuration.",
self::CONFIG_ERR_MISMATCH_MIN_MAX_SIZE => "Mismatch in min. and max. uploaded files sizes in field configuration.",
];
protected $type = 'file';
protected $validators = ['Files'];
public function SetForm (\MvcCore\Ext\IForm $form) {
parent::SetForm($form);
$this->checkConfiguration();
return $this;
}
protected function checkConfiguration () {
if ($this->accept === NULL)
$this->throwConfigException(
static::CONFIG_ERR_NO_ACCEPT_PROPERTY
);
$multipartFormEnctype = \MvcCore\Ext\IForm::ENCTYPE_MULTIPART;
if ($this->form->GetEnctype() !== $multipartFormEnctype)
$this->throwConfigException(
str_replace(
'{0}', $multipartFormEnctype,
static::CONFIG_ERR_WRONG_FORM_ENCTYPE
)
);
$rawFileUploads = @ini_get("file_uploads");
if (
!$rawFileUploads ||
strtolower($rawFileUploads) == 'off'
) $this->throwConfigException(
static::CONFIG_ERR_UPLOADS_NOT_ALOWED
);
if ($this->maxSize !== NULL) {
$maxIniFileSize = $this->form->GetPhpIniSizeLimit(
"upload_max_filesize"
);
if (
$maxIniFileSize !== NULL &&
$this->maxSize > $maxIniFileSize
)
$this->throwConfigException(
static::CONFIG_ERR_MAX_UPLOAD_SIZE_LOWER
);
$maxIniPostSize = $this->form->GetPhpIniSizeLimit(
"post_max_size"
);
if (
$maxIniPostSize !== NULL &&
$this->maxSize > $maxIniPostSize
)
$this->throwConfigException(
static::CONFIG_ERR_MAX_POST_SIZE_LOWER
);
}
if ($this->multiple) {
$maxFiles = $this->form->GetPhpIniSizeLimit(
"max_file_uploads"
);
if (
$maxFiles !== NULL && (
$maxFiles < 2 || (
$this->maxCount !== NULL &&
$this->maxCount > $maxFiles
)
)
) $this->throwConfigException(
static::CONFIG_ERR_MAX_FILES_COUNT_LOWER
);
}
if (
$this->minCount !== NULL &&
$this->maxCount &&
$this->minCount > $this->maxCount
) $this->throwConfigException(
static::CONFIG_ERR_MISMATCH_MIN_MAX_COUNT
);
if (
$this->minSize !== NULL &&
$this->maxSize &&
$this->minSize > $this->maxSize
) $this->throwConfigException(
static::CONFIG_ERR_MISMATCH_MIN_MAX_SIZE
);
}
public function & GetValidatorData ($fieldPropsDefaultValidValues = []) {
$result = [
'multiple' => $this->multiple,
'accept' => $this->accept,
'allowedFileNameChars' => static::ALLOWED_FILE_NAME_CHARS_DEFAULT,
'minCount' => $this->minCount,
'maxCount' => $this->maxCount,
'minSize' => $this->minSize,
'maxSize' => $this->maxSize,
];
return $result;
}
public function PreDispatch () {
parent::PreDispatch();
$this->preDispatchTabIndex();
$this->checkConfiguration();
}
public function RenderControl () {
if ($this->minCount !== NULL)
$this->SetControlAttr('data-min-count', $this->minCount);
if ($this->maxCount !== NULL)
$this->SetControlAttr('data-max-count', $this->maxCount);
if ($this->minSize !== NULL)
$this->SetControlAttr('data-min-size', $this->minSize);
if ($this->maxSize !== NULL)
$this->SetControlAttr('data-max-size', $this->maxSize);
$attrsStr = $this->renderControlAttrsWithFieldVars([
'accept',
'capture',
]);
$attrsStrSep = strlen($attrsStr) > 0 ? ' ' : '';
if ($this->multiple) {
$attrsStr .= $attrsStrSep . 'multiple="multiple"';
$attrsStrSep = ' ';
}
if (!$this->form->GetFormTagRenderingStatus()) {
$attrsStr .= $attrsStrSep . 'form="' . $this->form->GetId() . '"';
}
$formViewClass = $this->form->GetViewClass();
$templates = static::$templates;
$result = $formViewClass::Format($templates->control, [
'id' => $this->id,
'name' => $this->name . ($this->multiple ? '[]' : ''),
'type' => $this->type,
'value' => '',
'attrs' => strlen($attrsStr) > 0 ? ' ' . $attrsStr : '',
]);
return $this->renderControlWrapper($result);
}
protected function throwConfigException ($errorNumber, $errorMsgArgs = []) {
$errorMessage = static::$configErrorMessages[$errorNumber];
$this->throwNewInvalidArgumentException(
$errorMessage
);
}
}