--- /dev/null
+<?php
+
+namespace Cube\Forms;
+
+class Base
+{
+ private $fields = [];
+
+ // Field types
+ const BINARY = 'binary';
+ const CHECKBOX = 'checkbox';
+ const DATE = 'date';
+ const RADIO = 'radio';
+ const TEXT = 'text';
+ const TEXTAREA = 'textarea';
+
+
+ /**
+ * Output field HTML
+ * @param $name
+ * @param array $settings
+ * @return mixed
+ */
+ public function field($name, $settings = []) {
+
+ $default_settings = [
+ 'hide_labels' => false,
+ 'placeholder' => true,
+ ];
+
+ $settings = array_merge($default_settings, $settings);
+
+ $field = $this->getField($name);
+ if (!$field) return false;
+
+ $res = '<div class="form-field-title">'. $field['title'] .'</div>';
+
+ if (method_exists($this, $field['type'])) {
+ $res .= '<div class="form-field-'. $field['type'] .'">';
+ $res .= $this->{$field['type']}($name, $settings);
+ $res .= '</div>';
+ } else {
+ $res .= 'Unrecognised field: '. $field['type'];
+ }
+
+ return '<div class="form-field">'. $res .'</div>';
+ }
+
+ /**
+ * Generate a radio or checkbox input
+ * @param $name
+ * @param $settings
+ * @param string $type
+ * @return string
+ */
+ public function radio_or_checkbox($name, $settings, $type = 'radio') {
+
+ // Options can be overridden
+ $options = isset($settings['options']) ? $settings['options'] : $this->fields[$name]['options'];
+ $input_name = ($type == 'checkbox') ? $name.'[]' : $name;
+
+ $res = '';
+
+ foreach ($options as $option) {
+ $res .= '<label title="'. $option .'">';
+ $res .= '<input type="'. $type .'" name="'. $input_name .'" value="'. $option .'">';
+
+ if (!$settings['hide_labels']) {
+ $res .= '<span class="form-label">'. $option .'</span>';
+ }
+
+ $res .= '</label>';
+ }
+ return $res;
+
+ }
+
+
+ /**
+ * Radio input fields
+ * @param $name
+ * @param $settings
+ * @return string
+ */
+ public function radio($name, $settings) {
+ return $this->radio_or_checkbox($name, $settings, 'radio');
+ }
+
+ /**
+ * Checkbox input fields
+ * @param $name
+ * @param $settings
+ * @return string
+ */
+ public function checkbox($name, $settings) {
+ return $this->radio_or_checkbox($name, $settings, 'checkbox');
+ }
+
+ /**
+ * Date picker field
+ * @param $name
+ * @param $settings
+ * @return string
+ */
+ public function date($name, $settings) {
+ return '<input type="date" placeholder="'. __('JJ/MM/AAAA') .'" name="'. $name .'">';
+ }
+
+ /**
+ * Binary yes/no field
+ * @param $name
+ * @param $settings
+ * @return string
+ */
+ public function binary($name, $settings) {
+
+ $settings['options'] = [
+ __('Oui', 'ccv'),
+ __('Non', 'ccv'),
+ ];
+
+ return $this->radio_or_checkbox($name, $settings, 'radio');
+ }
+
+
+ /**
+ * Set multiple fields at once
+ * @param array $fields
+ */
+ public function setFields($fields) {
+ $this->fields = $fields;
+ }
+
+ /**
+ * Get all fields
+ * @return array
+ */
+ public function getFields() {
+ return $this->fields;
+ }
+
+ /**
+ * Get the field names
+ * @return array
+ */
+ public function getFieldKeys() {
+ return array_keys($this->fields);
+ }
+
+ /**
+ * Add a single field
+ * @param $name
+ * @param $title
+ * @param $type
+ * @param array $options
+ */
+ public function addField($name, $title, $type, $options = []) {
+ $this->fields[$name]['title'] = $title;
+ $this->fields[$name]['type'] = $type;
+
+ if (!empty($options)) {
+ $this->setFieldOptions($name, $options);
+ }
+ }
+
+ /**
+ * Check if field exists
+ * @param $name
+ * @return bool
+ */
+ public function exists($name) {
+ return isset($this->fields[$name]);
+ }
+
+ /**
+ * Get a single field by name
+ * @param $name
+ * @return mixed|null
+ */
+ public function getField($name) {
+ return $this->fields[$name] ?? null;
+ }
+
+ /**
+ * Set the options for a field
+ * @param string $name
+ * @param array $options
+ */
+ public function setFieldOptions($name, $options) {
+ if ($this->exists($name)) {
+ $this->fields[$name]['options'] = $options;
+ }
+ }
+
+ /**
+ * Helper function to print template code for all fields
+ * @return string
+ */
+ public function generateCode() {
+ $res = "<pre>AVAILABLE FIELDS:\n\n";
+ foreach ($this->getFieldKeys() as $key) {
+ $res .= "{!! \$form->field('$key') !!}\n";
+ }
+ $res .= '</pre>';
+ return $res;
+ }
+
+}
--- /dev/null
+<?php
+
+namespace Cube\Forms;
+
+
+class Consultation extends Base
+{
+ function __construct() {
+
+ $this->addField('main-problem', __('Problème principal', 'ccv'), self::RADIO,
+ [
+ __('Cervicales', 'ccv'),
+ __('Lombaires', 'ccv'),
+ __('Scoliose', 'ccv'),
+ ]
+ );
+
+ $this->addField('date-first-symptoms', __('Date des premiers symptômes :', 'ccv'), self::DATE);
+
+ $this->addField('date-pain-since', __('Douleurs permanentes depuis (le cas échéant) :', 'ccv'), self::DATE);
+
+ $this->addField('pain-arms-legs', __('Avez-vous des douleurs dans les bras ou les jambes (sciatiques, cruralgies, névralgies) ?', 'ccv'), self::BINARY);
+ $this->addField('pain-arms-legs-detail', __('Si oui, cochez les membres concernés :', 'ccv'), self::CHECKBOX,
+ [
+ __('Haut du bras gauche', 'ccv'),
+ __('Avant-bras gauche', 'ccv'),
+ __('Haut de la jambe gauche', 'ccv'),
+ __('Bas de la jambe gauche', 'ccv'),
+ ]
+ );
+
+ }
+}
<h2>{{ __('1. Vos symptômes') }}</h2>
<ul>
- <li>
- {{ __('Problème principal', 'ccv') }}
-
- <div class="flex justify-between items-center">
- <label>
- <input type="radio" name="problem-principal" value="{{ __('Cervicales', 'ccv') }}">
- <span class="ml-2">{{ __('Cervicales') }}</span>
- </label>
- <label>
- <input type="radio" name="problem-principal" value="{{ __('Lombaires', 'ccv') }}">
- <span class="ml-2">{{ __('Lombaires', 'ccv') }}</span>
- </label>
- <label>
- <input type="radio" name="problem-principal" value="{{ __('Scoliose', 'ccv') }}">
- <span class="ml-2">{{ __('Scoliose', 'ccv') }}</span>
- </label>
- </div>
- </li>
+ <li>{!! $form->field('main-problem') !!}</li>
+ <li>{!! $form->field('date-first-symptoms') !!}</li>
+ <li>{!! $form->field('date-pain-since') !!}</li>
+ <li>{!! $form->field('pain-arms-legs') !!}</li>
+ <li>{!! $form->field('pain-arms-legs-detail', ['hide_labels' => true]) !!}</li>
</ul>
</div>