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:
<?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\Forms\Fields;
/**
* Responsibility: init, pre-dispatch and render submit button
* based on `<input>` HTML element with type `image`.
*/
class Image
extends \MvcCore\Ext\Forms\Field
implements \MvcCore\Ext\Forms\Fields\IVisibleField,
\MvcCore\Ext\Forms\Fields\ISubmit {
use \MvcCore\Ext\Forms\Field\Props\VisibleField;
use \MvcCore\Ext\Forms\Field\Props\Submit;
use \MvcCore\Ext\Forms\Field\Props\FormAttrs;
use \MvcCore\Ext\Forms\Field\Props\WidthHeight;
/**
* Possible values: `image` (this type has for all browsers the same behaviour as type `submit`).
* @var string
*/
protected $type = 'image';
/**
* Displayed image as button background, relative or absolute path.
* @requires
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-src
* @var string|NULL
*/
protected $src = NULL;
/**
* Alternative button text in `alt` attribute, default value is: `Submit`.
* @var string
*/
protected $alt = 'Submit';
/**
* Standard field control natural template string.
* @var string
*/
protected static $templates = [
'control' => '<input type="image" id="{id}" name="{name}" src="{src}"{attrs} />',
];
/**
* Get displayed image as button background, relative or absolute path.
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-src
* @return string|NULL
*/
public function GetSrc () {
return $this->src;
}
/**
* Set displayed image as button background, relative or absolute path.
* @requires
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-src
* @param string $src
* @return \MvcCore\Ext\Forms\Field
*/
public function SetSrc ($src) {
/** @var $this \MvcCore\Ext\Forms\Field */
$this->src = $src;
return $this;
}
/**
* Get alternative button text for `alt` attribute.
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-alt
* @return string|NULL
*/
public function GetAlt () {
return $this->alt;
}
/**
* Set alternative button text for `alt` attribute.
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-alt
* @param string $alt
* @return \MvcCore\Ext\Forms\Field
*/
public function SetAlt ($alt) {
/** @var $this \MvcCore\Ext\Forms\Field */
$this->alt = $alt;
return $this;
}
/**
* Create new form `<input type="image" />` control instance.
* @param array $cfg Config array with public properties and it's
* values which you want to configure, presented
* in camel case properties names syntax.
* @throws \InvalidArgumentException
* @return \MvcCore\Ext\Forms\Fields\Image
*/
public function __construct (array $cfg = []) {
parent::__construct($cfg);
static::$templates = (object) self::$templates;
}
/**
* This INTERNAL method is called from `\MvcCore\Ext\Form` after field
* is added into form instance by `$form->AddField();` method. Do not
* use this method even if you don't develop any form field.
* - Check if field has any name, which is required.
* - Set up form and field id attribute by form id and field name.
* - Set up required.
* - Set up translate boolean property.
* - Check if there is defined any value for `src` attribute.
* @param \MvcCore\Ext\Form $form
* @throws \InvalidArgumentException
* @return \MvcCore\Ext\Forms\Fields\Image
*/
public function SetForm (\MvcCore\Ext\IForm $form) {
parent::SetForm($form);
if (!$this->src) $this->throwNewInvalidArgumentException(
'No input:image `src` attribute defined.'
);
return $this;
}
/**
* This INTERNAL method is called from `\MvcCore\Ext\Form` just before
* field is naturally rendered. It sets up field for rendering process.
* Do not use this method even if you don't develop any form field.
* - Set up field render mode if not defined.
* - Translate label text if necessary.
* - Set up tab-index if necessary.
* - Translate `alt` attribute text if necessary.
* @return void
*/
public function PreDispatch () {
parent::PreDispatch();
$this->preDispatchTabIndex();
if (!$this->translate) return;
$form = $this->form;
if ($this->alt !== NULL && $this->alt !== '')
$this->alt = $form->translate($this->alt);
}
/**
* This INTERNAL method is called from `\MvcCore\Ext\Forms\Field\Rendering`
* in rendering process. Do not use this method even if you don't develop any form field.
*
* Render control tag only without label or specific errors,
* including all select `<option>` tags or `<optgroup>` tags
* if there are options configured for.
* @return string
*/
public function RenderControl () {
if ($this->customResultState !== NULL)
$this->SetControlAttr('data-result', $this->customResultState);
$attrsStr = $this->renderControlAttrsWithFieldVars([
'formAction', 'formEnctype', 'formMethod', 'formNoValidate', 'formTarget',
'width', 'height',
]);
if (!$this->form->GetFormTagRenderingStatus())
$attrsStr .= (strlen($attrsStr) > 0 ? ' ' : '')
. 'form="' . $this->form->GetId() . '"';
$formViewClass = $this->form->GetViewClass();
/** @var $templates \stdClass */
$templates = static::$templates;
return $formViewClass::Format($templates->control, [
'id' => $this->id,
'name' => $this->name,
'src' => htmlspecialchars_decode(htmlspecialchars($this->src, ENT_QUOTES), ENT_QUOTES),
'attrs' => strlen($attrsStr) > 0 ? ' ' . $attrsStr : '',
]);
}
}