| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form\View; |
| 4 |
|
| 5 |
use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 6 |
|
| 7 |
class FormViewer |
| 8 |
{ |
| 9 |
private $_theme = 'Default'; |
| 10 |
private $_themeDetails; |
| 11 |
private $_fields; |
| 12 |
private $_layout; |
| 13 |
private $_buttons; |
| 14 |
private $_form; |
| 15 |
private $_error; |
| 16 |
private $_previousValue; |
| 17 |
public function __construct(FrontendFormManager $formManager, $form_contents, $errorMessages = null, $previousValue = null) |
| 18 |
{ |
| 19 |
$this->_fields = $form_contents->fields; |
| 20 |
$this->_layout = $form_contents->layout; |
| 21 |
$this->_buttons = isset($form_contents->buttons) ? $form_contents->buttons : ''; |
| 22 |
$this->_form = $formManager; |
| 23 |
$this->_error = $errorMessages; |
| 24 |
$this->_previousValue = $previousValue; |
| 25 |
} |
| 26 |
public function getView($hasFile) |
| 27 |
{ |
| 28 |
$name = str_replace(' ', '', $this->_theme); |
| 29 |
$file = strpos($name, 'Theme') !== false ? $name . '.php' : $name . 'Theme.php'; |
| 30 |
if (file_exists(__DIR__ . '/' . $file)) { |
| 31 |
$className = __NAMESPACE__ . '\\Theme\\' . $name . 'Theme'; |
| 32 |
$this->_themeDetails = new $className; |
| 33 |
} else { |
| 34 |
$className = __NAMESPACE__ . '\\Theme\\' . 'DefaultTheme'; |
| 35 |
$this->_themeDetails = new $className; |
| 36 |
} |
| 37 |
return $this->setView($hasFile); |
| 38 |
} |
| 39 |
|
| 40 |
public function setTheme($theme = 'Default') |
| 41 |
{ |
| 42 |
$this->_theme = $theme; |
| 43 |
} |
| 44 |
|
| 45 |
private function setView($hasFile) |
| 46 |
{ |
| 47 |
$file_upload_tag = null; |
| 48 |
$formID = $this->_form->getFormID(); |
| 49 |
if ($hasFile) { |
| 50 |
$file_upload_tag = 'enctype="multipart/form-data"'; |
| 51 |
wp_enqueue_script('bitforms-frontend-file'); |
| 52 |
} |
| 53 |
$formIdentifier = $this->_form->getFormIdentifier(); |
| 54 |
$fieldHtml = ""; |
| 55 |
foreach ($this->_layout->lg as $key => $row) { |
| 56 |
$field_name = $row->i; |
| 57 |
if ($this->_fields->{$row->i}->typ === 'button' && $this->_fields->{$row->i}->btnTyp === 'submit') { |
| 58 |
$field_name = $formIdentifier; |
| 59 |
} |
| 60 |
// $field_name .= isset($this->_fields->{$row->i}->lbl) ? preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\\!]/', '_', $this->_fields->{$row->i}->lbl) : ""; |
| 61 |
$error = isset($this->_error[$field_name]) ? $this->_error[$field_name] : null; |
| 62 |
$valueToEsc = isset($this->_previousValue[$field_name]) ? |
| 63 |
$this->_previousValue[$field_name] : (isset($this->_fields->{$row->i}->val) ? $this->_fields->{$row->i}->val : null); |
| 64 |
if (empty($valueToEsc)) { |
| 65 |
$value = null; |
| 66 |
} else { |
| 67 |
if ((isset($this->_fields->{$row->i}->mul) || $this->_fields->{$row->i}->typ === 'check' || $this->_fields->{$row->i}->typ === 'select')) { |
| 68 |
if ($this->_fields->{$row->i}->typ === 'select' && is_string($valueToEsc)) { |
| 69 |
$valueToEsc = explode(',', $valueToEsc); |
| 70 |
} |
| 71 |
if (is_array($valueToEsc)) { |
| 72 |
$value = array_map('esc_attr', $valueToEsc); |
| 73 |
} else { |
| 74 |
$value = esc_attr($valueToEsc); |
| 75 |
} |
| 76 |
} else { |
| 77 |
$value = !is_array($valueToEsc) ? esc_attr($valueToEsc) : |
| 78 |
esc_attr($valueToEsc[count($valueToEsc) - 1]); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
$fieldHtml .= $this->_themeDetails->inputWrapper( |
| 83 |
$this->_fields->{$row->i}, |
| 84 |
$row->i, |
| 85 |
$field_name, |
| 86 |
$error, |
| 87 |
$value, |
| 88 |
$formID |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
if (!empty($this->_buttons)) { |
| 93 |
$this->_buttons->name = $formIdentifier; |
| 94 |
$buttonClass = "button-{$formIdentifier}"; |
| 95 |
$subBtn = $this->_themeDetails->getField($this->_buttons, $buttonClass, ''); |
| 96 |
} |
| 97 |
|
| 98 |
$formHTML = |
| 99 |
<<<FORM |
| 100 |
<div id="f-{$formID}"> |
| 101 |
<form novalidate id="form-{$formIdentifier}" class="_frm-bg-{$formID}" action="" $file_upload_tag method='post'> |
| 102 |
<input type="hidden" name="bitforms_token" value="{$this->_form->getFormToken()}"> |
| 103 |
<input type="hidden" name="bitforms_id" value="bitforms_{$this->_form->getFormID()}"> |
| 104 |
<div class="_frm-{$formID}"> |
| 105 |
<div class="_frm-g _frm-g-{$formID}"> |
| 106 |
$fieldHtml |
| 107 |
</div> |
| 108 |
</div> |
| 109 |
</form> |
| 110 |
</div> |
| 111 |
FORM; |
| 112 |
return $formHTML; |
| 113 |
} |
| 114 |
} |
| 115 |
|