| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form\View; |
| 4 |
|
| 5 |
use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 6 |
|
| 7 |
class FormViewHelper |
| 8 |
{ |
| 9 |
private $_formContents; |
| 10 |
private $_formInfo; |
| 11 |
private $_layout; |
| 12 |
private $_form; |
| 13 |
private $_fieldHelpers; |
| 14 |
|
| 15 |
public function __construct(FrontendFormManager $form, $form_contents) |
| 16 |
{ |
| 17 |
$this->_form = $form; |
| 18 |
$this->_formContents = $form_contents; |
| 19 |
$this->_formInfo = isset($form_contents->formInfo) ? $form_contents->formInfo : ''; |
| 20 |
$this->_layout = isset($form_contents->layout) ? $form_contents->layout : ''; |
| 21 |
} |
| 22 |
|
| 23 |
private function getFormInfoProperty($propertyName) |
| 24 |
{ |
| 25 |
return isset($this->_formInfo->{$propertyName}) ? $this->_formInfo->{$propertyName} : null; |
| 26 |
} |
| 27 |
|
| 28 |
private function getFormId() |
| 29 |
{ |
| 30 |
return $this->_form->getFormID(); |
| 31 |
} |
| 32 |
|
| 33 |
public function filterHtml($str) |
| 34 |
{ |
| 35 |
return wp_kses_post(str_replace('$_bf_$', '\\', $str)); |
| 36 |
} |
| 37 |
|
| 38 |
private function getIconMarkup($source, $classElement) |
| 39 |
{ |
| 40 |
if (empty($source)) { |
| 41 |
return ''; |
| 42 |
} |
| 43 |
return <<<ICON |
| 44 |
<img |
| 45 |
class="{$this->_form->getAtomicCls($classElement)}" |
| 46 |
src="{$source}" |
| 47 |
alt="" |
| 48 |
/> |
| 49 |
ICON; |
| 50 |
} |
| 51 |
|
| 52 |
private function getHeaderLabelMarkup($lbl, $preIcn, $sufIcn, $uniqClass) |
| 53 |
{ |
| 54 |
$formID = $this->getFormId(); |
| 55 |
return <<<HEADERLABEL |
| 56 |
<span class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-{$uniqClass}")}'> |
| 57 |
{$this->getIconMarkup($preIcn, "_frm-b{$formID}-stp-{$uniqClass}-pre-i")} |
| 58 |
{$this->filterHtml($lbl)} |
| 59 |
{$this->getIconMarkup($sufIcn, "_frm-b{$formID}-stp-{$uniqClass}-suf-i")} |
| 60 |
</span> |
| 61 |
HEADERLABEL; |
| 62 |
} |
| 63 |
|
| 64 |
private function getStepButton($btnSettings) |
| 65 |
{ |
| 66 |
$key = $btnSettings->key; |
| 67 |
$txt = $btnSettings->txt; |
| 68 |
$btnTyp = $btnSettings->typ; |
| 69 |
$formID = $this->getFormId(); |
| 70 |
$preIcn = isset($btnSettings->preIcn) ? $btnSettings->preIcn : null; |
| 71 |
$sufIcn = isset($btnSettings->sufIcn) ? $btnSettings->sufIcn : null; |
| 72 |
$preIcnMrkp = $this->getIconMarkup($preIcn, "_frm-b{$formID}-{$key}-pre-i"); |
| 73 |
$sufIcnMrkp = $this->getIconMarkup($sufIcn, "_frm-b{$formID}-{$key}-suf-i"); |
| 74 |
$btnSpinner = '<span class="bf-spinner d-none"></span>'; |
| 75 |
return <<<BUTTON |
| 76 |
<button |
| 77 |
class="{$this->_form->getAtomicCls("_frm-b{$formID}-{$key}")} {$key}" |
| 78 |
type="{$btnTyp}" |
| 79 |
> |
| 80 |
{$preIcnMrkp} |
| 81 |
{$this->filterHtml($txt)} |
| 82 |
{$sufIcnMrkp} |
| 83 |
{$btnSpinner} |
| 84 |
</button> |
| 85 |
BUTTON; |
| 86 |
} |
| 87 |
|
| 88 |
public function getStepHeaderHtml() |
| 89 |
{ |
| 90 |
$multiStepSettings = $this->getFormInfoProperty('multiStepSettings'); |
| 91 |
|
| 92 |
if (isset($multiStepSettings->showStepHeader) && !$multiStepSettings->showStepHeader) { |
| 93 |
return ''; |
| 94 |
} |
| 95 |
|
| 96 |
$formID = $this->_form->getFormID(); |
| 97 |
$layout = $this->_layout; |
| 98 |
$stepHeaderHtml = <<<HEADERHTML |
| 99 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-cntnr")}'> |
| 100 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-wrpr")}'> |
| 101 |
HEADERHTML; |
| 102 |
|
| 103 |
$showHeadarIcon = isset($multiStepSettings->headerIcon->show) ? $multiStepSettings->headerIcon->show : false; |
| 104 |
$showLbl = isset($multiStepSettings->showLbl) ? $multiStepSettings->showLbl : false; |
| 105 |
$showSubtitle = isset($multiStepSettings->showSubtitle) ? $multiStepSettings->showSubtitle : false; |
| 106 |
|
| 107 |
$iconType = isset($multiStepSettings->headerIcon->iconType) ? $multiStepSettings->headerIcon->iconType : 'number'; |
| 108 |
|
| 109 |
$stepValidation = isset($multiStepSettings->validateOnStepChange) ? $multiStepSettings->validateOnStepChange : false; |
| 110 |
foreach ($layout as $key => $lay) { |
| 111 |
$step = $key + 1; |
| 112 |
$settings = isset($lay->settings) ? $lay->settings : null; |
| 113 |
|
| 114 |
if (empty($settings)) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
$iconWrapper = ''; |
| 118 |
$stepIcon = ''; |
| 119 |
|
| 120 |
if ('icon' === $iconType) { |
| 121 |
$stepIcon = "<img src='{$settings->icon}' class={$this->_form->getAtomicCls("_frm-b{$formID}-stp-icn")} alt='Step Icon' />"; |
| 122 |
} else { |
| 123 |
$stepIcon = "<span class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-num")}'>{$step}</span>"; |
| 124 |
} |
| 125 |
if ($showHeadarIcon) { |
| 126 |
$iconWrapper = <<<ICONWRPR |
| 127 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-icn-wrp")}'> |
| 128 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-icn-cntn")}'> |
| 129 |
{$stepIcon} |
| 130 |
</div> |
| 131 |
</div> |
| 132 |
ICONWRPR; |
| 133 |
} |
| 134 |
$showStepLbl = $showLbl && isset($settings->showLbl) ? $settings->showLbl : false; |
| 135 |
$showStepSubtitle = $showSubtitle && isset($settings->showSubtitle) ? $settings->showSubtitle : false; |
| 136 |
$lblPreIcn = isset($settings->lblPreIcn) ? $settings->lblPreIcn : null; |
| 137 |
$lblSufIcn = isset($settings->lblSufIcn) ? $settings->lblSufIcn : null; |
| 138 |
$subTlePreIcn = isset($settings->subTlePreIcn) ? $settings->subTlePreIcn : null; |
| 139 |
$subTleSufIcn = isset($settings->subTleSufIcn) ? $settings->subTleSufIcn : null; |
| 140 |
$stepLabelMarkup = $showStepLbl ? $this->getHeaderLabelMarkup($settings->lbl, $lblPreIcn, $lblSufIcn, 'lbl') : ''; |
| 141 |
$stepSubtitleMarkup = $showStepSubtitle ? $this->getHeaderLabelMarkup($settings->subtitle, $subTlePreIcn, $subTleSufIcn, 'sub-titl') : ''; |
| 142 |
|
| 143 |
$activeClass = 0 === $key ? 'active' : ''; |
| 144 |
$disableClass = (0 !== $key && $stepValidation) ? 'disabled' : ''; |
| 145 |
$stepHeaderHtml .= <<<HEADERHTML |
| 146 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr")} {$activeClass} {$disableClass}' data-step='{$step}'> |
| 147 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-cntnt")}'> |
| 148 |
{$iconWrapper} |
| 149 |
<span class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-titl-wrpr")}'> |
| 150 |
{$stepLabelMarkup} |
| 151 |
{$stepSubtitleMarkup} |
| 152 |
</span> |
| 153 |
</div> |
| 154 |
</div> |
| 155 |
HEADERHTML; |
| 156 |
} |
| 157 |
$stepHeaderHtml .= <<<HEADERHTML |
| 158 |
</div> |
| 159 |
</div> |
| 160 |
HEADERHTML; |
| 161 |
return $stepHeaderHtml; |
| 162 |
} |
| 163 |
|
| 164 |
public function getProgressBarMarkup() |
| 165 |
{ |
| 166 |
$multiStepSettings = $this->getFormInfoProperty('multiStepSettings'); |
| 167 |
$progressBarSettings = isset($multiStepSettings->progressSettings) ? $multiStepSettings->progressSettings : null; |
| 168 |
if (isset($progressBarSettings->show) && !$progressBarSettings->show) { |
| 169 |
return ''; |
| 170 |
} |
| 171 |
$precentage = (isset($progressBarSettings->showPercentage) && $progressBarSettings->showPercentage) ? '0%' : ''; |
| 172 |
$formID = $this->getFormId(); |
| 173 |
return <<<PROGRESSHTML |
| 174 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-progress-wrpr")}'> |
| 175 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-progress")}'> |
| 176 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-progress-bar")}'> |
| 177 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-progress-fill")}' style="width: 0%;"> |
| 178 |
{$precentage} |
| 179 |
</div> |
| 180 |
</div> |
| 181 |
</div> |
| 182 |
</div> |
| 183 |
PROGRESSHTML; |
| 184 |
} |
| 185 |
|
| 186 |
public function getStepButtonMarkup() |
| 187 |
{ |
| 188 |
$multiStepSettings = $this->getFormInfoProperty('multiStepSettings'); |
| 189 |
$btnSettings = isset($multiStepSettings->btnSettings) ? $multiStepSettings->btnSettings : null; |
| 190 |
if (is_null($btnSettings) || (empty($btnSettings->show))) { |
| 191 |
return ''; |
| 192 |
} |
| 193 |
$formID = $this->getFormId(); |
| 194 |
return <<<BUTTONWRPR |
| 195 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-btn-wrpr")}'> |
| 196 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-btn-cntnt")}'> |
| 197 |
{$this->getStepButton($btnSettings->prevBtn)} |
| 198 |
{$this->getStepButton($btnSettings->nextBtn)} |
| 199 |
</div> |
| 200 |
</div> |
| 201 |
BUTTONWRPR; |
| 202 |
} |
| 203 |
} |
| 204 |
|