| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form\View; |
| 4 |
|
| 5 |
use BitCode\BitForm\Admin\Form\Helpers; |
| 6 |
use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 7 |
|
| 8 |
class FormViewer |
| 9 |
{ |
| 10 |
private $_theme = 'Default'; |
| 11 |
private $_themeDetails; |
| 12 |
public $_fields; |
| 13 |
private $_layout; |
| 14 |
public $_nestedLayout; |
| 15 |
private $_buttons; |
| 16 |
public $_form; |
| 17 |
public $_error; |
| 18 |
private $_previousValue; |
| 19 |
public $_formAtomicClsMap; |
| 20 |
public $_tokens; |
| 21 |
public $_formContents; |
| 22 |
|
| 23 |
public function __construct(FrontendFormManager $formManager, $form_contents, $formAtomicClsMap, $errorMessages = null, $previousValue = null) |
| 24 |
{ |
| 25 |
$this->_tokens = Helpers::csrfEecrypted(); |
| 26 |
$this->_fields = $form_contents->fields; |
| 27 |
$this->_layout = $form_contents->layout; |
| 28 |
$this->_nestedLayout = isset($form_contents->nestedLayout) ? $form_contents->nestedLayout : ''; |
| 29 |
$this->_buttons = isset($form_contents->buttons) ? $form_contents->buttons : ''; |
| 30 |
$this->_form = $formManager; |
| 31 |
$this->_error = $errorMessages; |
| 32 |
$this->_formContents = $form_contents; |
| 33 |
$this->_previousValue = $previousValue; |
| 34 |
$this->_formAtomicClsMap = $formAtomicClsMap; |
| 35 |
} |
| 36 |
|
| 37 |
public function getView($hasFile, $msg = null) |
| 38 |
{ |
| 39 |
$name = str_replace(' ', '', $this->_theme); |
| 40 |
$file = false !== strpos($name, 'Theme') ? $name . '.php' : $name . 'Theme.php'; |
| 41 |
if (file_exists(__DIR__ . '/' . $file)) { |
| 42 |
$className = __NAMESPACE__ . '\\Theme\\' . $name . 'Theme'; |
| 43 |
$this->_themeDetails = new $className(); |
| 44 |
} else { |
| 45 |
$className = __NAMESPACE__ . '\\Theme\\' . 'DefaultTheme'; |
| 46 |
$this->_themeDetails = new $className(); |
| 47 |
} |
| 48 |
return $this->setView($hasFile, $msg); |
| 49 |
} |
| 50 |
|
| 51 |
public function honeypotField() |
| 52 |
{ |
| 53 |
$time = time(); |
| 54 |
$honeypodFldName = Helpers::honeypotEncryptedToken("_bitforms_{$this->getFormID()}_{$time}_"); |
| 55 |
$honeypotInput = ''; |
| 56 |
if (Helpers::property_exists_nested($this->_formContents, 'additional->enabled->honeypot', true)) { |
| 57 |
$honeypotInput = |
| 58 |
<<<HTMLa |
| 59 |
<input type="text" class="d-none" name="b_h_t" value="{$honeypodFldName}"> |
| 60 |
<input type="text" class="d-none" name="{$honeypodFldName}" required> |
| 61 |
HTMLa; |
| 62 |
return $honeypotInput; |
| 63 |
} |
| 64 |
return $honeypotInput; |
| 65 |
} |
| 66 |
|
| 67 |
public function setTheme($theme = 'Default') |
| 68 |
{ |
| 69 |
$this->_theme = $theme; |
| 70 |
} |
| 71 |
|
| 72 |
public function getError($field_name) |
| 73 |
{ |
| 74 |
return isset($this->_error[$field_name]) ? $this->_error[$field_name] : null; |
| 75 |
} |
| 76 |
|
| 77 |
public function getFieldName($rowId) |
| 78 |
{ |
| 79 |
$formIdentifier = $this->_form->getFormIdentifier(); |
| 80 |
$field_name = $rowId; |
| 81 |
if ('button' === $this->_fields->{$rowId}->typ && 'submit' === $this->_fields->{$rowId}->btnTyp) { |
| 82 |
$field_name = $formIdentifier; |
| 83 |
} |
| 84 |
$field_name .= isset($this->_fields->{$rowId}->lbl) ? preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\\!]/', '_', $this->_fields->{$rowId}->lbl) : ''; |
| 85 |
return $field_name; |
| 86 |
} |
| 87 |
|
| 88 |
public function getAtomicClaMap() |
| 89 |
{ |
| 90 |
return $this->_formAtomicClsMap; |
| 91 |
} |
| 92 |
|
| 93 |
public function getValue($field_name, $rowId) |
| 94 |
{ |
| 95 |
$valueToEsc = isset($this->_previousValue[$field_name]) ? |
| 96 |
$this->_previousValue[$field_name] : (isset($this->_fields->{$rowId}->val) ? $this->_fields->{$rowId}->val : null); |
| 97 |
if (empty($valueToEsc)) { |
| 98 |
$value = null; |
| 99 |
} else { |
| 100 |
if ((isset($this->_fields->{$rowId}->mul) || 'check' === $this->_fields->{$rowId}->typ || 'select' === $this->_fields->{$rowId}->typ)) { |
| 101 |
if ('select' === $this->_fields->{$rowId}->typ && is_string($valueToEsc)) { |
| 102 |
$valueToEsc = explode(',', $valueToEsc); |
| 103 |
} |
| 104 |
if (is_array($valueToEsc)) { |
| 105 |
$value = array_map('esc_attr', $valueToEsc); |
| 106 |
} else { |
| 107 |
$value = esc_attr($valueToEsc); |
| 108 |
} |
| 109 |
} else { |
| 110 |
$value = !is_array($valueToEsc) ? esc_attr($valueToEsc) : |
| 111 |
esc_attr($valueToEsc[count($valueToEsc) - 1]); |
| 112 |
} |
| 113 |
} |
| 114 |
return $value; |
| 115 |
} |
| 116 |
|
| 117 |
public function getFormID() |
| 118 |
{ |
| 119 |
return $this->_form->getFormID(); |
| 120 |
} |
| 121 |
|
| 122 |
public function fields($rowID) |
| 123 |
{ |
| 124 |
return $this->_fields->{$rowID}; |
| 125 |
} |
| 126 |
|
| 127 |
public function getNestedLayout($rowID) |
| 128 |
{ |
| 129 |
// return null; |
| 130 |
return isset($this->_nestedLayout->{$rowID}) ? $this->_nestedLayout->{$rowID} : null; |
| 131 |
} |
| 132 |
|
| 133 |
private function getLayoutHtml($lay) |
| 134 |
{ |
| 135 |
$html = ''; |
| 136 |
foreach ($lay->lg as $row) { |
| 137 |
$html .= $this->_themeDetails->inputWrapper($this, $row->i); |
| 138 |
} |
| 139 |
return $html; |
| 140 |
} |
| 141 |
|
| 142 |
private function setView($hasFile, $msg) |
| 143 |
{ |
| 144 |
$file_upload_tag = null; |
| 145 |
$restrictionMsg = ''; |
| 146 |
$formID = $this->_form->getFormID(); |
| 147 |
if ($hasFile) { |
| 148 |
$file_upload_tag = 'enctype="multipart/form-data"'; |
| 149 |
} |
| 150 |
$formIdentifier = $this->_form->getFormIdentifier(); |
| 151 |
$fieldHtml = ''; |
| 152 |
$layouts = $this->_layout; |
| 153 |
$isMultiStep = is_array($layouts) && count($layouts) > 1; |
| 154 |
$layoutIsArray = !$isMultiStep && is_array($layouts); |
| 155 |
if ($layoutIsArray) { |
| 156 |
$layout = $layouts[0]; |
| 157 |
$layouts = isset($layout->layout) ? $layout->layout : $layout; |
| 158 |
$this->_layout = $layouts; |
| 159 |
} |
| 160 |
if (!$isMultiStep) { |
| 161 |
$fieldHtml .= $this->getLayoutHtml($layouts); |
| 162 |
} else { |
| 163 |
$formViewHelper = new FormViewHelper($this->_form, $this->_formContents); |
| 164 |
|
| 165 |
$fieldHtml .= <<<STEPWRPR |
| 166 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-cntnr")}'> |
| 167 |
{$formViewHelper->getStepHeaderHtml()} |
| 168 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-wrpr")}'> |
| 169 |
{$formViewHelper->getProgressBarMarkup()} |
| 170 |
<div class='{$this->_form->getAtomicCls("_frm-b{$formID}-stp-cntnt-wrpr")}'> |
| 171 |
STEPWRPR; |
| 172 |
foreach ($layouts as $key => $lay) { |
| 173 |
$hideOtherSteps = $key > 0 ? 'deactive' : ''; |
| 174 |
$step = $key + 1; |
| 175 |
$fieldHtml .= <<<HTMLa |
| 176 |
<div class="{$this->_form->getAtomicCls("_frm-b{$formID}-stp-cntnt")} $hideOtherSteps" data-step="{$step}"> |
| 177 |
<div class="_frm-b{$formID}"> |
| 178 |
{$this->getLayoutHtml($lay->layout)} |
| 179 |
</div> |
| 180 |
{$formViewHelper->getStepButtonMarkup()} |
| 181 |
</div> |
| 182 |
HTMLa; |
| 183 |
} |
| 184 |
$fieldHtml .= <<<CLOSINGTAG |
| 185 |
</div> |
| 186 |
</div> |
| 187 |
</div> |
| 188 |
CLOSINGTAG; |
| 189 |
} |
| 190 |
|
| 191 |
$confMsg = $this->_form->getSuccessMessageMarkups(); |
| 192 |
$abandonmentMsg = $this->_form->getFormAbandonmentMessage(); |
| 193 |
|
| 194 |
if (!empty($this->_buttons)) { |
| 195 |
$this->_buttons->name = $formIdentifier; |
| 196 |
$buttonClass = "button-{$formIdentifier}"; |
| 197 |
$subBtn = $this->_themeDetails->getField($this->_buttons, $buttonClass, ''); |
| 198 |
} |
| 199 |
|
| 200 |
if (!empty($msg)) { |
| 201 |
$restrictionMsg = <<<MSG |
| 202 |
<div class="{$this->_form->getAtomicCls("_frm-ovrly-b{$formID}")}"> |
| 203 |
<p class="{$this->_form->getAtomicCls("_frm-ovrly-msg-b{$formID}")}"> |
| 204 |
$msg |
| 205 |
</p> |
| 206 |
</div> |
| 207 |
MSG; |
| 208 |
} |
| 209 |
$formHTML = |
| 210 |
<<<HTMLa |
| 211 |
<div id="{$formIdentifier}" class="{$this->_form->getAtomicCls("_frm-bg-b{$formID}")}"> |
| 212 |
$restrictionMsg |
| 213 |
<form novalidate id="form-{$formIdentifier}" class="{$this->_form->getAtomicCls("_frm-b{$formID}")}" $file_upload_tag method='post'> |
| 214 |
<input type="text" class="d-none" name="csrf" value="{$this->_tokens['csrf']}"> |
| 215 |
<input type="text" class="d-none" name="t_identity" value="{$this->_tokens['t_identity']}"> |
| 216 |
{$this->honeypotField()} |
| 217 |
<input type="text" class="d-none" name="bitforms_id" value="bitforms_{$this->_form->getFormID()}"> |
| 218 |
$fieldHtml |
| 219 |
</form> |
| 220 |
$abandonmentMsg |
| 221 |
<div id='bf-form-msg-wrp-{$formIdentifier}'></div> |
| 222 |
$confMsg |
| 223 |
</div> |
| 224 |
HTMLa; |
| 225 |
return $formHTML; |
| 226 |
} |
| 227 |
} |
| 228 |
|