# bit-form/2.0/includes/Frontend/Form/View/FormViewer.php

Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator &amp; Custom Form Builder, version 2.0. 150 lines.

- Page: https://pluginprobe.com/plugins/bit-form/2.0/code/includes/Frontend/Form/View/FormViewer.php
- Raw: https://pluginprobe.com/plugins/bit-form/2.0/raw/includes/Frontend/Form/View/FormViewer.php
- Modified: 2023-02-28T14:03:48+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/bit-form/2.0/code/includes/Frontend/Form/View/FormViewer.php#L10-L20`.

```php
<?php

namespace BitCode\BitForm\Frontend\Form\View;

use BitCode\BitForm\Admin\Form\Helpers;
use BitCode\BitForm\Frontend\Form\FrontendFormManager;

class FormViewer {
  private $_theme = 'Default';
  private $_themeDetails;
  public $_fields;
  private $_layout;
  private $_buttons;
  public $_form;
  public $_error;
  private $_previousValue;
  public $_formAtomicClsMap;
  public $_tokens;

  public function __construct(FrontendFormManager $formManager, $form_contents, $formAtomicClsMap, $errorMessages = null, $previousValue = null) {
    $this->_tokens = Helpers::csrfEecrypted();
    $this->_fields = $form_contents->fields;
    $this->_layout = $form_contents->layout;
    $this->_buttons = isset($form_contents->buttons) ? $form_contents->buttons : '';
    $this->_form = $formManager;
    $this->_error = $errorMessages;
    $this->form_contents = $form_contents;
    $this->_previousValue = $previousValue;
    $this->_formAtomicClsMap = $formAtomicClsMap;
  }

  public function getView($hasFile) {
    $name = str_replace(' ', '', $this->_theme);
    $file = false !== strpos($name, 'Theme') ? $name . '.php' : $name . 'Theme.php';
    if (file_exists(__DIR__ . '/' . $file)) {
      $className = __NAMESPACE__ . '\\Theme\\' . $name . 'Theme';
      $this->_themeDetails = new $className();
    } else {
      $className = __NAMESPACE__ . '\\Theme\\' . 'DefaultTheme';
      $this->_themeDetails = new $className();
    }
    return $this->setView($hasFile);
  }

  public function honeypotField() {
    $time = time();
    $honeypodFldName = Helpers::honeypotEncryptedToken("_bitforms_{$this->getFormID()}_{$time}_");
    $honeypotInput = '';
    if (Helpers::property_exists_nested($this->form_contents, 'additional->enabled->honeypot', true)) {
      $honeypotInput =
      <<<HTMLa
        <input type="text" class="d-none" name="b_h_t" value="{$honeypodFldName}">
        <input type="text" class="d-none" name="{$honeypodFldName}" required>
HTMLa;
      return $honeypotInput;
    }
    return $honeypotInput;
  }

  public function setTheme($theme = 'Default') {
    $this->_theme = $theme;
  }

  public function getError($field_name) {
    return isset($this->_error[$field_name]) ? $this->_error[$field_name] : null;
  }

  public function getFieldName($rowId) {
    $formIdentifier = $this->_form->getFormIdentifier();
    $field_name = $rowId;
    if ('button' === $this->_fields->{$rowId}->typ && 'submit' === $this->_fields->{$rowId}->btnTyp) {
      $field_name = $formIdentifier;
    }
    $field_name .= isset($this->_fields->{$rowId}->lbl) ? preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\\!]/', '_', $this->_fields->{$rowId}->lbl) : '';
    return $field_name;
  }

  public function getAtomicClaMap() {
    return $this->_formAtomicClsMap;
  }

  public function getValue($field_name, $rowId) {
    $valueToEsc = isset($this->_previousValue[$field_name]) ?
      $this->_previousValue[$field_name] : (isset($this->_fields->{$rowId}->val) ? $this->_fields->{$rowId}->val : null);
    if (empty($valueToEsc)) {
      $value = null;
    } else {
      if ((isset($this->_fields->{$rowId}->mul) || 'check' === $this->_fields->{$rowId}->typ || 'select' === $this->_fields->{$rowId}->typ)) {
        if ('select' === $this->_fields->{$rowId}->typ && is_string($valueToEsc)) {
          $valueToEsc = explode(',', $valueToEsc);
        }
        if (is_array($valueToEsc)) {
          $value = array_map('esc_attr', $valueToEsc);
        } else {
          $value = esc_attr($valueToEsc);
        }
      } else {
        $value = !is_array($valueToEsc) ? esc_attr($valueToEsc) :
          esc_attr($valueToEsc[count($valueToEsc) - 1]);
      }
    }
    return $value;
  }

  public function getFormID() {
    return $this->_form->getFormID();
  }

  public function fields($rowID) {
    return $this->_fields->{$rowID};
  }

  private function setView($hasFile) {
    $file_upload_tag = null;
    $formID = $this->_form->getFormID();
    if ($hasFile) {
      $file_upload_tag = 'enctype="multipart/form-data"';
      wp_enqueue_script('bitforms-frontend-file');
    }
    $formIdentifier = $this->_form->getFormIdentifier();
    $fieldHtml = '';
    foreach ($this->_layout->lg as $row) {
      $fieldHtml .= $this->_themeDetails->inputWrapper($this, $row->i);
    }

    $confMsg = $this->_form->getSuccessMessageMarkups();

    if (!empty($this->_buttons)) {
      $this->_buttons->name = $formIdentifier;
      $buttonClass = "button-{$formIdentifier}";
      $subBtn = $this->_themeDetails->getField($this->_buttons, $buttonClass, '');
    }
    $formHTML =
      <<<HTMLa
      <div id="{$formIdentifier}" class="{$this->_form->getAtomicCls("_frm-bg-b{$formID}")}">
          <form novalidate id="form-{$formIdentifier}" class="{$this->_form->getAtomicCls("_frm-b{$formID}")}" $file_upload_tag method='post'>
              <input type="text" class="d-none" name="csrf" value="{$this->_tokens['csrf']}">
              <input type="text" class="d-none" name="t_identy" value="{$this->_tokens['t_identy']}">
              {$this->honeypotField()}
              <input type="text" class="d-none" name="bitforms_id" value="bitforms_{$this->_form->getFormID()}">
                  $fieldHtml
          </form>
          <div id='bf-form-msg-wrp-{$formIdentifier}'></div>
          $confMsg
      </div>
HTMLa;
    return $formHTML;
  }
}

```
