|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nextras community extensions of Nette Framework |
| 5 | + * |
| 6 | + * @license MIT |
| 7 | + * @link https://github.com/nextras/forms-rendering |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Nextras\FormsRendering\Renderers; |
| 11 | + |
| 12 | +use Nette\Forms\Controls; |
| 13 | +use Nette\Forms\Form; |
| 14 | +use Nette\Forms\IControl; |
| 15 | +use Nette\Forms\Rendering\DefaultFormRenderer; |
| 16 | +use Nette\Utils\Html; |
| 17 | + |
| 18 | + |
| 19 | +/** |
| 20 | + * Form renderer for Bootstrap 3. |
| 21 | + */ |
| 22 | +class Bs3FormRenderer extends DefaultFormRenderer |
| 23 | +{ |
| 24 | + /** @var Controls\Button */ |
| 25 | + public $primaryButton = null; |
| 26 | + |
| 27 | + /** @var bool */ |
| 28 | + private $controlsInit = false; |
| 29 | + |
| 30 | + |
| 31 | + public function __construct() |
| 32 | + { |
| 33 | + $this->wrappers['controls']['container'] = null; |
| 34 | + $this->wrappers['pair']['container'] = 'div class=form-group'; |
| 35 | + $this->wrappers['pair']['.error'] = 'has-error'; |
| 36 | + $this->wrappers['control']['container'] = 'div class=col-sm-9'; |
| 37 | + $this->wrappers['label']['container'] = 'div class="col-sm-3 control-label"'; |
| 38 | + $this->wrappers['control']['description'] = 'span class=help-block'; |
| 39 | + $this->wrappers['control']['errorcontainer'] = 'span class=help-block'; |
| 40 | + $this->wrappers['error']['container'] = null; |
| 41 | + $this->wrappers['error']['item'] = 'div class="alert alert-danger"'; |
| 42 | + } |
| 43 | + |
| 44 | + |
| 45 | + public function renderBegin(): string |
| 46 | + { |
| 47 | + $this->controlsInit(); |
| 48 | + return parent::renderBegin(); |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public function renderEnd(): string |
| 53 | + { |
| 54 | + $this->controlsInit(); |
| 55 | + return parent::renderEnd(); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + public function renderBody(): string |
| 60 | + { |
| 61 | + $this->controlsInit(); |
| 62 | + return parent::renderBody(); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + public function renderControls($parent): string |
| 67 | + { |
| 68 | + $this->controlsInit(); |
| 69 | + return parent::renderControls($parent); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + public function renderPair(IControl $control): string |
| 74 | + { |
| 75 | + $this->controlsInit(); |
| 76 | + return parent::renderPair($control); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + public function renderPairMulti(array $controls): string |
| 81 | + { |
| 82 | + $this->controlsInit(); |
| 83 | + return parent::renderPairMulti($controls); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public function renderLabel(IControl $control): Html |
| 88 | + { |
| 89 | + $this->controlsInit(); |
| 90 | + return parent::renderLabel($control); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + public function renderControl(IControl $control): Html |
| 95 | + { |
| 96 | + $this->controlsInit(); |
| 97 | + return parent::renderControl($control); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + private function controlsInit() |
| 102 | + { |
| 103 | + if ($this->controlsInit) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + $this->controlsInit = true; |
| 108 | + $this->form->getElementPrototype()->addClass('form-horizontal'); |
| 109 | + foreach ($this->form->getControls() as $control) { |
| 110 | + if ($control instanceof Controls\Button) { |
| 111 | + $markAsPrimary = $control === $this->primaryButton || (!isset($this->primaryButton) && empty($usedPrimary) && $control->parent instanceof Form); |
| 112 | + if ($markAsPrimary) { |
| 113 | + $class = 'btn btn-primary'; |
| 114 | + $usedPrimary = true; |
| 115 | + } else { |
| 116 | + $class = 'btn btn-default'; |
| 117 | + } |
| 118 | + $control->getControlPrototype()->addClass($class); |
| 119 | + } elseif ($control instanceof Controls\TextBase || $control instanceof Controls\SelectBox || $control instanceof Controls\MultiSelectBox) { |
| 120 | + $control->getControlPrototype()->addClass('form-control'); |
| 121 | + } elseif ($control instanceof Controls\Checkbox || $control instanceof Controls\CheckboxList || $control instanceof Controls\RadioList) { |
| 122 | + if ($control instanceof Controls\Checkbox) { |
| 123 | + $control->getSeparatorPrototype()->setName('div')->appendAttribute('class', $control->getControlPrototype()->type); |
| 124 | + } else { |
| 125 | + $control->getItemLabelPrototype()->addClass($control->getControlPrototype()->type . '-inline'); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments