| 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 |
private $_theme = 'Default'; |
| 10 |
private $_themeDetails; |
| 11 |
public $_fields; |
| 12 |
private $_layout; |
| 13 |
private $_buttons; |
| 14 |
public $_form; |
| 15 |
public $_error; |
| 16 |
private $_previousValue; |
| 17 |
public $_formAtomicClsMap; |
| 18 |
public $_tokens; |
| 19 |
|
| 20 |
public function __construct(FrontendFormManager $formManager, $form_contents, $formAtomicClsMap, $errorMessages = null, $previousValue = null) { |
| 21 |
$this->_tokens = Helpers::csrfEecrypted(); |
| 22 |
$this->_fields = $form_contents->fields; |
| 23 |
$this->_layout = $form_contents->layout; |
| 24 |
$this->_buttons = isset($form_contents->buttons) ? $form_contents->buttons : ''; |
| 25 |
$this->_form = $formManager; |
| 26 |
$this->_error = $errorMessages; |
| 27 |
$this->form_contents = $form_contents; |
| 28 |
$this->_previousValue = $previousValue; |
| 29 |
$this->_formAtomicClsMap = $formAtomicClsMap; |
| 30 |
} |
| 31 |
|
| 32 |
public function getView($hasFile) { |
| 33 |
$name = str_replace(' ', '', $this->_theme); |
| 34 |
$file = false !== strpos($name, 'Theme') ? $name . '.php' : $name . 'Theme.php'; |
| 35 |
if (file_exists(__DIR__ . '/' . $file)) { |
| 36 |
$className = __NAMESPACE__ . '\\Theme\\' . $name . 'Theme'; |
| 37 |
$this->_themeDetails = new $className(); |
| 38 |
} else { |
| 39 |
$className = __NAMESPACE__ . '\\Theme\\' . 'DefaultTheme'; |
| 40 |
$this->_themeDetails = new $className(); |
| 41 |
} |
| 42 |
return $this->setView($hasFile); |
| 43 |
} |
| 44 |
|
| 45 |
public function honeypotField() { |
| 46 |
$time = time(); |
| 47 |
$honeypodFldName = Helpers::honeypotEncryptedToken("_bitforms_{$this->getFormID()}_{$time}_"); |
| 48 |
$honeypotInput = ''; |
| 49 |
if (Helpers::property_exists_nested($this->form_contents, 'additional->enabled->honeypot', true)) { |
| 50 |
$honeypotInput = |
| 51 |
<<<HTMLa |
| 52 |
<input type="text" class="d-none" name="b_h_t" value="{$honeypodFldName}"> |
| 53 |
<input type="text" class="d-none" name="{$honeypodFldName}" required> |
| 54 |
HTMLa; |
| 55 |
return $honeypotInput; |
| 56 |
} |
| 57 |
return $honeypotInput; |
| 58 |
} |
| 59 |
|
| 60 |
public function setTheme($theme = 'Default') { |
| 61 |
$this->_theme = $theme; |
| 62 |
} |
| 63 |
|
| 64 |
public function getError($field_name) { |
| 65 |
return isset($this->_error[$field_name]) ? $this->_error[$field_name] : null; |
| 66 |
} |
| 67 |
|
| 68 |
public function getFieldName($rowId) { |
| 69 |
$formIdentifier = $this->_form->getFormIdentifier(); |
| 70 |
$field_name = $rowId; |
| 71 |
if ('button' === $this->_fields->{$rowId}->typ && 'submit' === $this->_fields->{$rowId}->btnTyp) { |
| 72 |
$field_name = $formIdentifier; |
| 73 |
} |
| 74 |
$field_name .= isset($this->_fields->{$rowId}->lbl) ? preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\\!]/', '_', $this->_fields->{$rowId}->lbl) : ''; |
| 75 |
return $field_name; |
| 76 |
} |
| 77 |
|
| 78 |
public function getAtomicClaMap() { |
| 79 |
return $this->_formAtomicClsMap; |
| 80 |
} |
| 81 |
|
| 82 |
public function getValue($field_name, $rowId) { |
| 83 |
$valueToEsc = isset($this->_previousValue[$field_name]) ? |
| 84 |
$this->_previousValue[$field_name] : (isset($this->_fields->{$rowId}->val) ? $this->_fields->{$rowId}->val : null); |
| 85 |
if (empty($valueToEsc)) { |
| 86 |
$value = null; |
| 87 |
} else { |
| 88 |
if ((isset($this->_fields->{$rowId}->mul) || 'check' === $this->_fields->{$rowId}->typ || 'select' === $this->_fields->{$rowId}->typ)) { |
| 89 |
if ('select' === $this->_fields->{$rowId}->typ && is_string($valueToEsc)) { |
| 90 |
$valueToEsc = explode(',', $valueToEsc); |
| 91 |
} |
| 92 |
if (is_array($valueToEsc)) { |
| 93 |
$value = array_map('esc_attr', $valueToEsc); |
| 94 |
} else { |
| 95 |
$value = esc_attr($valueToEsc); |
| 96 |
} |
| 97 |
} else { |
| 98 |
$value = !is_array($valueToEsc) ? esc_attr($valueToEsc) : |
| 99 |
esc_attr($valueToEsc[count($valueToEsc) - 1]); |
| 100 |
} |
| 101 |
} |
| 102 |
return $value; |
| 103 |
} |
| 104 |
|
| 105 |
public function getFormID() { |
| 106 |
return $this->_form->getFormID(); |
| 107 |
} |
| 108 |
|
| 109 |
public function fields($rowID) { |
| 110 |
return $this->_fields->{$rowID}; |
| 111 |
} |
| 112 |
|
| 113 |
private function setView($hasFile) { |
| 114 |
$file_upload_tag = null; |
| 115 |
$formID = $this->_form->getFormID(); |
| 116 |
if ($hasFile) { |
| 117 |
$file_upload_tag = 'enctype="multipart/form-data"'; |
| 118 |
wp_enqueue_script('bitforms-frontend-file'); |
| 119 |
} |
| 120 |
$formIdentifier = $this->_form->getFormIdentifier(); |
| 121 |
$fieldHtml = ''; |
| 122 |
foreach ($this->_layout->lg as $row) { |
| 123 |
$fieldHtml .= $this->_themeDetails->inputWrapper($this, $row->i); |
| 124 |
} |
| 125 |
|
| 126 |
$confMsg = $this->_form->getSuccessMessageMarkups(); |
| 127 |
|
| 128 |
if (!empty($this->_buttons)) { |
| 129 |
$this->_buttons->name = $formIdentifier; |
| 130 |
$buttonClass = "button-{$formIdentifier}"; |
| 131 |
$subBtn = $this->_themeDetails->getField($this->_buttons, $buttonClass, ''); |
| 132 |
} |
| 133 |
$formHTML = |
| 134 |
<<<HTMLa |
| 135 |
<div id="{$formIdentifier}" class="{$this->_form->getAtomicCls("_frm-bg-b{$formID}")}"> |
| 136 |
<form novalidate id="form-{$formIdentifier}" class="{$this->_form->getAtomicCls("_frm-b{$formID}")}" $file_upload_tag method='post'> |
| 137 |
<input type="text" class="d-none" name="csrf" value="{$this->_tokens['csrf']}"> |
| 138 |
<input type="text" class="d-none" name="t_identy" value="{$this->_tokens['t_identy']}"> |
| 139 |
{$this->honeypotField()} |
| 140 |
<input type="text" class="d-none" name="bitforms_id" value="bitforms_{$this->_form->getFormID()}"> |
| 141 |
$fieldHtml |
| 142 |
</form> |
| 143 |
<div id='bf-form-msg-wrp-{$formIdentifier}'></div> |
| 144 |
$confMsg |
| 145 |
</div> |
| 146 |
HTMLa; |
| 147 |
return $formHTML; |
| 148 |
} |
| 149 |
} |
| 150 |
|