|
- <?php
-
-
- namespace yii\bootstrap;
-
- use yii\helpers\ArrayHelper;
-
-
- class ActiveField extends \yii\widgets\ActiveField
- {
-
-
- public $inline = false;
-
-
- public $inputTemplate;
-
-
- public $wrapperOptions = [];
-
-
- public $horizontalCssClasses;
-
-
- public $checkboxTemplate = "<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";
-
-
- public $radioTemplate = "<div class=\"radio\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";
-
-
- public $horizontalCheckboxTemplate = "{beginWrapper}\n<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";
-
-
- public $horizontalRadioTemplate = "{beginWrapper}\n<div class=\"radio\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";
-
-
- public $inlineCheckboxListTemplate = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
-
-
- public $inlineRadioListTemplate = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
-
-
- public $enableError = true;
-
-
- public $enableLabel = true;
-
-
-
-
- public function __construct($config = [])
- {
- $layoutConfig = $this->createLayoutConfig($config);
- $config = ArrayHelper::merge($layoutConfig, $config);
- parent::__construct($config);
- }
-
-
-
- public function render($content = null)
- {
- if ($content === null) {
- if (!isset($this->parts['{beginWrapper}'])) {
- $options = $this->wrapperOptions;
- $tag = ArrayHelper::remove($options, 'tag', 'div');
- $this->parts['{beginWrapper}'] = Html::beginTag($tag, $options);
- $this->parts['{endWrapper}'] = Html::endTag($tag);
- }
- if ($this->enableLabel === false) {
- $this->parts['{label}'] = '';
- $this->parts['{beginLabel}'] = '';
- $this->parts['{labelTitle}'] = '';
- $this->parts['{endLabel}'] = '';
- } elseif (!isset($this->parts['{beginLabel}'])) {
- $this->renderLabelParts();
- }
- if ($this->enableError === false) {
- $this->parts['{error}'] = '';
- }
- if ($this->inputTemplate) {
- $input = isset($this->parts['{input}']) ?
- $this->parts['{input}'] : Html::activeTextInput($this->model, $this->attribute, $this->inputOptions);
- $this->parts['{input}'] = strtr($this->inputTemplate, ['{input}' => $input]);
- }
- }
- return parent::render($content);
- }
-
-
-
- public function checkbox($options = [], $enclosedByLabel = true)
- {
- if ($enclosedByLabel) {
- if (!isset($options['template'])) {
- $this->template = $this->form->layout === 'horizontal' ?
- $this->horizontalCheckboxTemplate : $this->checkboxTemplate;
- } else {
- $this->template = $options['template'];
- unset($options['template']);
- }
- if (isset($options['label'])) {
- $this->parts['{labelTitle}'] = $options['label'];
- }
- if ($this->form->layout === 'horizontal') {
- Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
- }
- $this->labelOptions['class'] = null;
- }
-
- return parent::checkbox($options, false);
- }
-
-
-
- public function radio($options = [], $enclosedByLabel = true)
- {
- if ($enclosedByLabel) {
- if (!isset($options['template'])) {
- $this->template = $this->form->layout === 'horizontal' ?
- $this->horizontalRadioTemplate : $this->radioTemplate;
- } else {
- $this->template = $options['template'];
- unset($options['template']);
- }
- if (isset($options['label'])) {
- $this->parts['{labelTitle}'] = $options['label'];
- }
- if ($this->form->layout === 'horizontal') {
- Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
- }
- $this->labelOptions['class'] = null;
- }
-
- return parent::radio($options, false);
- }
-
-
-
- public function checkboxList($items, $options = [])
- {
- if ($this->inline) {
- if (!isset($options['template'])) {
- $this->template = $this->inlineCheckboxListTemplate;
- } else {
- $this->template = $options['template'];
- unset($options['template']);
- }
- if (!isset($options['itemOptions'])) {
- $options['itemOptions'] = [
- 'labelOptions' => ['class' => 'checkbox-inline'],
- ];
- }
- } elseif (!isset($options['item'])) {
- $itemOptions = isset($options['itemOptions']) ? $options['itemOptions'] : [];
- $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions) {
- $options = array_merge(['label' => $label, 'value' => $value], $itemOptions);
- return '<div class="checkbox">' . Html::checkbox($name, $checked, $options) . '</div>';
- };
- }
- parent::checkboxList($items, $options);
- return $this;
- }
-
-
-
- public function radioList($items, $options = [])
- {
- if ($this->inline) {
- if (!isset($options['template'])) {
- $this->template = $this->inlineRadioListTemplate;
- } else {
- $this->template = $options['template'];
- unset($options['template']);
- }
- if (!isset($options['itemOptions'])) {
- $options['itemOptions'] = [
- 'labelOptions' => ['class' => 'radio-inline'],
- ];
- }
- } elseif (!isset($options['item'])) {
- $itemOptions = isset($options['itemOptions']) ? $options['itemOptions'] : [];
- $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions) {
- $options = array_merge(['label' => $label, 'value' => $value], $itemOptions);
- return '<div class="radio">' . Html::radio($name, $checked, $options) . '</div>';
- };
- }
- parent::radioList($items, $options);
- return $this;
- }
-
-
-
- public function staticControl($options = [])
- {
- $this->adjustLabelFor($options);
- $this->parts['{input}'] = Html::activeStaticControl($this->model, $this->attribute, $options);
- return $this;
- }
-
-
-
- public function label($label = null, $options = [])
- {
- if (is_bool($label)) {
- $this->enableLabel = $label;
- if ($label === false && $this->form->layout === 'horizontal') {
- Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
- }
- } else {
- $this->enableLabel = true;
- $this->renderLabelParts($label, $options);
- parent::label($label, $options);
- }
- return $this;
- }
-
-
-
- public function inline($value = true)
- {
- $this->inline = (bool) $value;
- return $this;
- }
-
-
-
- protected function createLayoutConfig($instanceConfig)
- {
- $config = [
- 'hintOptions' => [
- 'tag' => 'p',
- 'class' => 'help-block',
- ],
- 'errorOptions' => [
- 'tag' => 'p',
- 'class' => 'help-block help-block-error',
- ],
- 'inputOptions' => [
- 'class' => 'form-control',
- ],
- ];
-
- $layout = $instanceConfig['form']->layout;
-
- if ($layout === 'horizontal') {
- $config['template'] = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
- $cssClasses = [
- 'offset' => 'col-sm-offset-3',
- 'label' => 'col-sm-3',
- 'wrapper' => 'col-sm-6',
- 'error' => '',
- 'hint' => 'col-sm-3',
- ];
- if (isset($instanceConfig['horizontalCssClasses'])) {
- $cssClasses = ArrayHelper::merge($cssClasses, $instanceConfig['horizontalCssClasses']);
- }
- $config['horizontalCssClasses'] = $cssClasses;
- $config['wrapperOptions'] = ['class' => $cssClasses['wrapper']];
- $config['labelOptions'] = ['class' => 'control-label ' . $cssClasses['label']];
- $config['errorOptions'] = ['class' => 'help-block help-block-error ' . $cssClasses['error']];
- $config['hintOptions'] = ['class' => 'help-block ' . $cssClasses['hint']];
- } elseif ($layout === 'inline') {
- $config['labelOptions'] = ['class' => 'sr-only'];
- $config['enableError'] = false;
- }
-
- return $config;
- }
-
-
-
- protected function renderLabelParts($label = null, $options = [])
- {
- $options = array_merge($this->labelOptions, $options);
- if ($label === null) {
- if (isset($options['label'])) {
- $label = $options['label'];
- unset($options['label']);
- } else {
- $attribute = Html::getAttributeName($this->attribute);
- $label = Html::encode($this->model->getAttributeLabel($attribute));
- }
- }
- if (!isset($options['for'])) {
- $options['for'] = Html::getInputId($this->model, $this->attribute);
- }
- $this->parts['{beginLabel}'] = Html::beginTag('label', $options);
- $this->parts['{endLabel}'] = Html::endTag('label');
- if (!isset($this->parts['{labelTitle}'])) {
- $this->parts['{labelTitle}'] = $label;
- }
- }
- }
|