| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form\View\Conversational; |
| 4 |
|
| 5 |
use BitCode\BitForm\Admin\Form\Helpers; |
| 6 |
|
| 7 |
class ConversationalHelpers |
| 8 |
{ |
| 9 |
private $_formId; |
| 10 |
private $_conversationalSettings; |
| 11 |
private $_stepListObject; |
| 12 |
|
| 13 |
public function __construct($formId, $conversationalSettings) |
| 14 |
{ |
| 15 |
$this->_formId = $formId; |
| 16 |
$this->_conversationalSettings = $conversationalSettings; |
| 17 |
$this->_stepListObject = $this->_conversationalSettings->stepListObject; |
| 18 |
} |
| 19 |
|
| 20 |
public function getWelcomePageView() |
| 21 |
{ |
| 22 |
$welcomePageSettings = $this->getMergedStepSettings('welcomePage'); |
| 23 |
$welcomeTitle = ''; |
| 24 |
$welcomePageMarkup = ''; |
| 25 |
$buttonMarkup = ''; |
| 26 |
|
| 27 |
if (!$welcomePageSettings->enable) { |
| 28 |
return ''; |
| 29 |
} |
| 30 |
if (!empty($welcomePageSettings->title)) { |
| 31 |
$welcomeTitle = <<<WELCOMETITLE |
| 32 |
<div class="bc{$this->_formId}-welcome-title"> |
| 33 |
<h2>{$welcomePageSettings->title}</h2> |
| 34 |
</div> |
| 35 |
WELCOMETITLE; |
| 36 |
} |
| 37 |
|
| 38 |
$startBtnPreIcn = $this->conversationalSettingsIcon($welcomePageSettings, 'startBtnPreIcn', 'btn-pre-icon', 'Start Button Pre Icon'); |
| 39 |
$startBtnSufIcn = $this->conversationalSettingsIcon($welcomePageSettings, 'startBtnSufIcn', 'btn-suf-icon', 'Start Button Suf Icon'); |
| 40 |
|
| 41 |
$startBtnText = !empty($welcomePageSettings->btnTxt) ? $welcomePageSettings->btnTxt : 'Start'; |
| 42 |
|
| 43 |
$stepHints = !empty($welcomePageSettings->stepHints) ? $welcomePageSettings->stepHints : ''; |
| 44 |
|
| 45 |
$buttonMarkup = <<<BUTTONMARKUP |
| 46 |
<div class="bc{$this->_formId}-step-btn-wrpr"> |
| 47 |
<div class="bc{$this->_formId}-step-btn-inner-wrpr"> |
| 48 |
<div class="bc{$this->_formId}-step-btn-cntnt"> |
| 49 |
<button class="bc{$this->_formId}-start-btn bc{$this->_formId}-btn" type="button"> |
| 50 |
{$startBtnPreIcn} |
| 51 |
{$startBtnText} |
| 52 |
{$startBtnSufIcn} |
| 53 |
</button> |
| 54 |
<span class="bc{$this->_formId}-step-hints"> |
| 55 |
{$stepHints} |
| 56 |
</span> |
| 57 |
</div> |
| 58 |
</div> |
| 59 |
</div> |
| 60 |
BUTTONMARKUP; |
| 61 |
|
| 62 |
$welcomePageMarkup = <<<WELCOMEPAGECONTENT |
| 63 |
<div class="bc{$this->_formId}-welcome-content"> |
| 64 |
{$welcomeTitle} |
| 65 |
{$welcomePageSettings->content} |
| 66 |
{$buttonMarkup} |
| 67 |
</div> |
| 68 |
WELCOMEPAGECONTENT; |
| 69 |
$imageContent = $this->conversationalSettingsIcon($welcomePageSettings, 'layoutImage', 'step-img', 'Background Image'); |
| 70 |
$welcomeLayout = $welcomePageSettings->layout; |
| 71 |
$welcomeLayoutClsNames = "bc{$this->_formId}-welcome $welcomeLayout"; |
| 72 |
return $this->getStepLayout($this->_formId, $imageContent, $welcomePageMarkup, $welcomeLayout, $welcomeLayoutClsNames); |
| 73 |
} |
| 74 |
|
| 75 |
public function getNavigationView() |
| 76 |
{ |
| 77 |
$navigationSettings = $this->_conversationalSettings->navigationSettings; |
| 78 |
$formId = $this->_formId; |
| 79 |
$progressLabelMarkup = ''; |
| 80 |
$progressBarMarkup = ''; |
| 81 |
$progressMarkup = ''; |
| 82 |
$brandingMarkup = ''; |
| 83 |
$navBtnMarkup = ''; |
| 84 |
|
| 85 |
if ($navigationSettings->showProgressLabel) { |
| 86 |
$progressLabelMarkup = <<<PERCENTAGEMARKUP |
| 87 |
<div class="bc{$formId}-progress-lbl-wrpr"> |
| 88 |
<span class="bc{$formId}-progress-lbl"> |
| 89 |
{$navigationSettings->progressLabel} |
| 90 |
</span> |
| 91 |
</div> |
| 92 |
PERCENTAGEMARKUP; |
| 93 |
} |
| 94 |
|
| 95 |
if ($navigationSettings->showProgressBar) { |
| 96 |
$progressBarMarkup = <<<PROGRESSBARMARKUP |
| 97 |
<div class="bc{$formId}-progress-bar-wrpr"> |
| 98 |
<div class="bc{$formId}-progress-bar"> |
| 99 |
<div class="bc{$formId}-progress-fill" style="width: 40%"></div> |
| 100 |
</div> |
| 101 |
</div> |
| 102 |
PROGRESSBARMARKUP; |
| 103 |
} |
| 104 |
|
| 105 |
if ($navigationSettings->showProgressBar || $navigationSettings->showProgressLabel) { |
| 106 |
$progressMarkup = <<<PROGRESSMARKUP |
| 107 |
<div class="bc{$formId}-progress-wrpr"> |
| 108 |
<div class="bc{$formId}-progress-cntnt" > |
| 109 |
{$progressLabelMarkup} |
| 110 |
{$progressBarMarkup} |
| 111 |
</div> |
| 112 |
</div> |
| 113 |
PROGRESSMARKUP; |
| 114 |
} |
| 115 |
|
| 116 |
if ($navigationSettings->showBranding) { |
| 117 |
$brandingMarkup = <<<BRANDINGMARKUP |
| 118 |
<div class="bc{$formId}-branding-wrpr"> |
| 119 |
<div class="bc{$formId}-branding-cntnt"> |
| 120 |
<div class="bc{$formId}-branding-lbl"> |
| 121 |
<span class="bc{$formId}-prowered-by-lbl">Powered by</span> |
| 122 |
<span class="bc{$formId}-bit-form-lbl">Bit Form</span> |
| 123 |
</div> |
| 124 |
</div> |
| 125 |
</div> |
| 126 |
BRANDINGMARKUP; |
| 127 |
} |
| 128 |
|
| 129 |
if ($navigationSettings->showNavigateBtn) { |
| 130 |
$navBtnMarkup = <<<NAVBTNMARKUP |
| 131 |
<div class="bc{$formId}-nav-btn-wrpr"> |
| 132 |
<div class="bc{$formId}-nav-btn-cntnt"> |
| 133 |
<button class="bc{$formId}-nav-btn bc{$formId}-nav-btn-up" type="button"> |
| 134 |
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg> |
| 135 |
</button> |
| 136 |
<button class="bc{$formId}-nav-btn bc{$formId}-nav-btn-down" type="button"> |
| 137 |
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg> |
| 138 |
</button> |
| 139 |
</div> |
| 140 |
</div> |
| 141 |
NAVBTNMARKUP; |
| 142 |
} |
| 143 |
$navBtnContainer = ''; |
| 144 |
if ($navigationSettings->showNavigateBtn || $navigationSettings->showBranding) { |
| 145 |
$navBtnContainer = <<<NAVBTNCNTNR |
| 146 |
<div class="bc{$formId}-nav-btn-container"> |
| 147 |
{$brandingMarkup} |
| 148 |
{$navBtnMarkup} |
| 149 |
</div> |
| 150 |
NAVBTNCNTNR; |
| 151 |
} |
| 152 |
|
| 153 |
if ($navigationSettings->show) { |
| 154 |
return <<<NAVVIEW |
| 155 |
<div class="bc{$formId}-nav-wrpr"> |
| 156 |
<div class="bc{$formId}-nav-wrpr-cntnt"> |
| 157 |
{$progressMarkup} |
| 158 |
{$navBtnContainer} |
| 159 |
</div> |
| 160 |
</div> |
| 161 |
NAVVIEW; |
| 162 |
} |
| 163 |
return ''; |
| 164 |
} |
| 165 |
|
| 166 |
public function getStepButtonsView($fieldData, $stepSettings) |
| 167 |
{ |
| 168 |
$formId = $this->_formId; |
| 169 |
$isFieldRequired = !empty($fieldData->valid->req) && $fieldData->valid->req; |
| 170 |
$btnTxt = $isFieldRequired ? $stepSettings->nextBtnTxt : $stepSettings->btnTxt; |
| 171 |
$btnPreIcn = $this->conversationalSettingsIcon($stepSettings, 'btnPreIcn', 'btn-pre-icon', 'Button Leading Icon'); |
| 172 |
$btnSufIcn = $this->conversationalSettingsIcon($stepSettings, 'btnSufIcn', 'btn-suf-icon', 'Button Trailing Icon'); |
| 173 |
$hiddenClass = $isFieldRequired ? 'bc-grid-hide' : ''; |
| 174 |
$stepHints = !empty($stepSettings->stepHints) ? $stepSettings->stepHints : ''; |
| 175 |
|
| 176 |
$btnWrapperMarkup = <<<BTNWRAPPERMARKUP |
| 177 |
<div class="{$hiddenClass} bc{$formId}-step-btn-wrpr"> |
| 178 |
<div class="bc{$formId}-step-btn-inner-wrpr"> |
| 179 |
<div class="bc{$formId}-step-btn-cntnt"> |
| 180 |
<button class="bc{$formId}-btn bc{$formId}-btn-ok" type="button"> |
| 181 |
{$btnPreIcn} |
| 182 |
{$btnTxt} |
| 183 |
{$btnSufIcn} |
| 184 |
</button> |
| 185 |
<span class="bc{$formId}-step-hints"> |
| 186 |
{$stepHints} |
| 187 |
</span> |
| 188 |
</div> |
| 189 |
</div> |
| 190 |
</div> |
| 191 |
BTNWRAPPERMARKUP; |
| 192 |
|
| 193 |
return $btnWrapperMarkup; |
| 194 |
} |
| 195 |
|
| 196 |
public static function getStepLayout($formID, $imageContent, $fieldContent, $layoutName, $layoutClassNames = '') |
| 197 |
{ |
| 198 |
$imageContentWrapper = <<<IMAGECONTENTWRAPPER |
| 199 |
<div class="bc{$formID}-step-img-cntnr"> |
| 200 |
<picture class="bc{$formID}-step-img-wrpr"> |
| 201 |
{$imageContent} |
| 202 |
</picture> |
| 203 |
</div> |
| 204 |
IMAGECONTENTWRAPPER; |
| 205 |
$imageContentWrapper = 'normal-layout' === $layoutName ? '' : $imageContentWrapper; |
| 206 |
|
| 207 |
return <<<STEPLAYOUT |
| 208 |
<div class="bc{$formID}-step-wrapper bc{$formID}-step bc-step-deactive {$layoutClassNames}"> |
| 209 |
<div class="bc{$formID}-step-content"> |
| 210 |
{$imageContentWrapper} |
| 211 |
<div class="bc{$formID}-step-fld-wrpr"> |
| 212 |
{$fieldContent} |
| 213 |
</div> |
| 214 |
</div> |
| 215 |
</div> |
| 216 |
STEPLAYOUT; |
| 217 |
} |
| 218 |
|
| 219 |
public function conversationalSettingsIcon($data, $icnPropName, $element, $alt = 'Image') |
| 220 |
{ |
| 221 |
if (Helpers::property_exists_nested($data, $icnPropName, '', 1)) { |
| 222 |
$url = esc_url($data->$icnPropName); |
| 223 |
return <<<ICON |
| 224 |
<img |
| 225 |
class="bc{$this->_formId}-{$element}" |
| 226 |
src="{$url}" |
| 227 |
alt={$alt} |
| 228 |
/> |
| 229 |
ICON; |
| 230 |
} |
| 231 |
return ''; |
| 232 |
} |
| 233 |
|
| 234 |
public function filterConversationalAllowedFields($layout, $fields) |
| 235 |
{ |
| 236 |
$ignoreFields = $this::getConversationalIgnoredFields(); |
| 237 |
$newLayouts = (object) ['lg' => []]; |
| 238 |
foreach ($layout->lg as $row) { |
| 239 |
if (!in_array($fields->{$row->i}->typ, $ignoreFields)) { |
| 240 |
$newLayouts->lg[] = $row; |
| 241 |
} |
| 242 |
} |
| 243 |
return $newLayouts; |
| 244 |
} |
| 245 |
|
| 246 |
public static function getConversationalIgnoredFields() |
| 247 |
{ |
| 248 |
return [ |
| 249 |
'button', |
| 250 |
'divider', |
| 251 |
'title', |
| 252 |
'html', |
| 253 |
'recaptcha', |
| 254 |
'turnstile', |
| 255 |
'image', |
| 256 |
'stripe', |
| 257 |
'paypal', |
| 258 |
'razorpay', |
| 259 |
]; |
| 260 |
} |
| 261 |
|
| 262 |
public function getMergedStepSettings($stepPropertyName) |
| 263 |
{ |
| 264 |
$allStepsSettings = $this->_stepListObject->allSteps; |
| 265 |
$stepSettings = !empty($this->_stepListObject->{$stepPropertyName}) ? $this->_stepListObject->{$stepPropertyName} : (object) []; |
| 266 |
$mergedSettings = (object) array_merge((array) $allStepsSettings, (array) $stepSettings); |
| 267 |
|
| 268 |
return $mergedSettings; |
| 269 |
} |
| 270 |
} |
| 271 |
|