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