| 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 sprintf( |
| 44 |
'<img |
| 45 |
class="%1$s _frm-b-stp-icn" |
| 46 |
src="%2$s" |
| 47 |
alt="" |
| 48 |
/> |
| 49 |
', |
| 50 |
$this->_form->getAtomicCls($classElement), |
| 51 |
$source |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
private function getHeaderLabelMarkup($lbl, $preIcn, $sufIcn, $uniqClass) |
| 56 |
{ |
| 57 |
$formID = $this->getFormId(); |
| 58 |
return sprintf( |
| 59 |
'<span class="%1$s _frm-b-stp-hdr-%2$s"> |
| 60 |
%3$s |
| 61 |
%4$s |
| 62 |
%5$s |
| 63 |
</span>', |
| 64 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-{$uniqClass}"), |
| 65 |
$uniqClass, |
| 66 |
$this->getIconMarkup($preIcn, "_frm-b{$formID}-stp-{$uniqClass}-pre-i"), |
| 67 |
$this->filterHtml($lbl), |
| 68 |
$this->getIconMarkup($sufIcn, "_frm-b{$formID}-stp-{$uniqClass}-suf-i") |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
private function getStepButton($btnSettings) |
| 73 |
{ |
| 74 |
$key = $btnSettings->key; |
| 75 |
$txt = $btnSettings->txt; |
| 76 |
$btnTyp = $btnSettings->typ; |
| 77 |
$formID = $this->getFormId(); |
| 78 |
$preIcn = isset($btnSettings->preIcn) ? $btnSettings->preIcn : null; |
| 79 |
$sufIcn = isset($btnSettings->sufIcn) ? $btnSettings->sufIcn : null; |
| 80 |
$preIcnMrkp = $this->getIconMarkup($preIcn, "_frm-b{$formID}-{$key}-pre-i"); |
| 81 |
$sufIcnMrkp = $this->getIconMarkup($sufIcn, "_frm-b{$formID}-{$key}-suf-i"); |
| 82 |
$btnSpinner = '<span class="bf-spinner d-none"></span>'; |
| 83 |
return sprintf( |
| 84 |
'<button |
| 85 |
class="%1$s %2$s" |
| 86 |
name="%2$s" |
| 87 |
type="%3$s" |
| 88 |
> |
| 89 |
%4$s |
| 90 |
%5$s |
| 91 |
%6$s |
| 92 |
%7$s |
| 93 |
</button>', |
| 94 |
$this->_form->getAtomicCls("_frm-b{$formID}-{$key}"), |
| 95 |
$key, |
| 96 |
$btnTyp, |
| 97 |
$preIcnMrkp, |
| 98 |
$this->filterHtml($txt), |
| 99 |
$sufIcnMrkp, |
| 100 |
$btnSpinner |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
public function getStepHeaderHtml() |
| 105 |
{ |
| 106 |
$multiStepSettings = $this->getFormInfoProperty('multiStepSettings'); |
| 107 |
|
| 108 |
if (isset($multiStepSettings->showStepHeader) && !$multiStepSettings->showStepHeader) { |
| 109 |
return ''; |
| 110 |
} |
| 111 |
|
| 112 |
$formID = $this->_form->getFormID(); |
| 113 |
$layout = $this->_layout; |
| 114 |
$stepHeaderHtml = sprintf( |
| 115 |
'<div class="%1$s _frm-b-stp-hdr-cntnr"> |
| 116 |
<div class="%2$s _frm-b-stp-hdr-wrpr">', |
| 117 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-cntnr"), |
| 118 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-wrpr") |
| 119 |
); |
| 120 |
|
| 121 |
$showHeadarIcon = isset($multiStepSettings->headerIcon->show) ? $multiStepSettings->headerIcon->show : false; |
| 122 |
$showLbl = isset($multiStepSettings->showLbl) ? $multiStepSettings->showLbl : false; |
| 123 |
$showSubtitle = isset($multiStepSettings->showSubtitle) ? $multiStepSettings->showSubtitle : false; |
| 124 |
|
| 125 |
$iconType = isset($multiStepSettings->headerIcon->iconType) ? $multiStepSettings->headerIcon->iconType : 'number'; |
| 126 |
|
| 127 |
$stepValidation = isset($multiStepSettings->validateOnStepChange) ? $multiStepSettings->validateOnStepChange : false; |
| 128 |
foreach ($layout as $key => $lay) { |
| 129 |
$step = $key + 1; |
| 130 |
$settings = isset($lay->settings) ? $lay->settings : null; |
| 131 |
|
| 132 |
if (empty($settings)) { |
| 133 |
continue; |
| 134 |
} |
| 135 |
$iconWrapper = ''; |
| 136 |
$stepIcon = ''; |
| 137 |
|
| 138 |
if ('icon' === $iconType) { |
| 139 |
$stepIcon = "<img src='{$settings->icon}' class={$this->_form->getAtomicCls("_frm-b{$formID}-stp-icn")} alt='Step Icon' />"; |
| 140 |
} else { |
| 141 |
$stepIcon = "<span class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-num")}'>{$step}</span>"; |
| 142 |
} |
| 143 |
if ($showHeadarIcon) { |
| 144 |
$iconWrapper = sprintf( |
| 145 |
'<div class="%1$s _frm-b-stp-hdr-icn-wrp"> |
| 146 |
<div class="%2$s _frm-b-stp-icn-cntn"> |
| 147 |
%3$s |
| 148 |
</div> |
| 149 |
</div>', |
| 150 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-icn-wrp"), |
| 151 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-icn-cntn"), |
| 152 |
$stepIcon |
| 153 |
); |
| 154 |
} |
| 155 |
$showStepLbl = $showLbl && isset($settings->showLbl) ? $settings->showLbl : false; |
| 156 |
$showStepSubtitle = $showSubtitle && isset($settings->showSubtitle) ? $settings->showSubtitle : false; |
| 157 |
$lblPreIcn = isset($settings->lblPreIcn) ? $settings->lblPreIcn : null; |
| 158 |
$lblSufIcn = isset($settings->lblSufIcn) ? $settings->lblSufIcn : null; |
| 159 |
$subTlePreIcn = isset($settings->subTlePreIcn) ? $settings->subTlePreIcn : null; |
| 160 |
$subTleSufIcn = isset($settings->subTleSufIcn) ? $settings->subTleSufIcn : null; |
| 161 |
$stepLabelMarkup = $showStepLbl ? $this->getHeaderLabelMarkup($settings->lbl, $lblPreIcn, $lblSufIcn, 'lbl') : ''; |
| 162 |
$stepSubtitleMarkup = $showStepSubtitle ? $this->getHeaderLabelMarkup($settings->subtitle, $subTlePreIcn, $subTleSufIcn, 'sub-titl') : ''; |
| 163 |
|
| 164 |
$activeClass = 0 === $key ? 'active' : ''; |
| 165 |
$disableClass = (0 !== $key && $stepValidation) ? 'disabled' : ''; |
| 166 |
$stepHeaderHtml .= sprintf( |
| 167 |
' |
| 168 |
<div class="%1$s _frm-b-stp-hdr %2$s %3$s" data-step="%4$s"> |
| 169 |
<div class="%5$s _frm-b-stp-hdr-cntnt"> |
| 170 |
%6$s |
| 171 |
<span class="%7$s _frm-b-stp-hdr-titl-wrpr"> |
| 172 |
%8$s |
| 173 |
%9$s |
| 174 |
</span> |
| 175 |
</div> |
| 176 |
</div>', |
| 177 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr"), |
| 178 |
$activeClass, |
| 179 |
$disableClass, |
| 180 |
$step, |
| 181 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-cntnt"), |
| 182 |
$iconWrapper, |
| 183 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-hdr-titl-wrpr"), |
| 184 |
$stepLabelMarkup, |
| 185 |
$stepSubtitleMarkup |
| 186 |
); |
| 187 |
} |
| 188 |
$stepHeaderHtml .= ' |
| 189 |
</div> |
| 190 |
</div>'; |
| 191 |
return $stepHeaderHtml; |
| 192 |
} |
| 193 |
|
| 194 |
public function getProgressBarMarkup() |
| 195 |
{ |
| 196 |
$multiStepSettings = $this->getFormInfoProperty('multiStepSettings'); |
| 197 |
$progressBarSettings = isset($multiStepSettings->progressSettings) ? $multiStepSettings->progressSettings : null; |
| 198 |
if (isset($progressBarSettings->show) && !$progressBarSettings->show) { |
| 199 |
return ''; |
| 200 |
} |
| 201 |
$precentage = (isset($progressBarSettings->showPercentage) && $progressBarSettings->showPercentage) ? '0%' : ''; |
| 202 |
$formID = $this->getFormId(); |
| 203 |
return sprintf( |
| 204 |
'<div class="%1$s"> |
| 205 |
<div class="%2$s"> |
| 206 |
<div class="%3$s"> |
| 207 |
<div class="%4$s" style="width: 0%%;"> |
| 208 |
%5$s |
| 209 |
</div> |
| 210 |
</div> |
| 211 |
</div> |
| 212 |
</div>', |
| 213 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-progress-wrpr"), |
| 214 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-progress"), |
| 215 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-progress-bar"), |
| 216 |
$this->_form->getAtomicCls("_frm-b{$formID}-progress-fill"), |
| 217 |
$precentage |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
public function getStepButtonMarkup() |
| 222 |
{ |
| 223 |
$multiStepSettings = $this->getFormInfoProperty('multiStepSettings'); |
| 224 |
$btnSettings = isset($multiStepSettings->btnSettings) ? $multiStepSettings->btnSettings : null; |
| 225 |
if (is_null($btnSettings) || (empty($btnSettings->show))) { |
| 226 |
return ''; |
| 227 |
} |
| 228 |
$formID = $this->getFormId(); |
| 229 |
return sprintf( |
| 230 |
'<div class="%1$s"> |
| 231 |
<div class="%2$s"> |
| 232 |
%3$s |
| 233 |
%4$s |
| 234 |
</div> |
| 235 |
</div>', |
| 236 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-btn-wrpr"), |
| 237 |
$this->_form->getAtomicCls("_frm-b{$formID}-stp-btn-cntnt"), |
| 238 |
$this->getStepButton($btnSettings->prevBtn), |
| 239 |
$this->getStepButton($btnSettings->nextBtn) |
| 240 |
); |
| 241 |
} |
| 242 |
} |
| 243 |
|