]> _ Git - ccv-wordpress.git/commitdiff
WIP #3445 @8
authorStephen Cameron <stephen@cubedesigners.com>
Tue, 31 Mar 2020 19:42:14 +0000 (21:42 +0200)
committerStephen Cameron <stephen@cubedesigners.com>
Tue, 31 Mar 2020 19:42:14 +0000 (21:42 +0200)
wp-content/mu-plugins/cube/src/Elementor/Widgets/HeaderSlideshow.php
wp-content/mu-plugins/cube/src/Forms/Base.php [new file with mode: 0644]
wp-content/mu-plugins/cube/src/Forms/Consultation.php [new file with mode: 0644]
wp-content/mu-plugins/cube/src/Shortcodes/CCVForm.php
wp-content/themes/CCV/resources/views/forms/consultation.blade.php

index 57e86d72af4cf895c339c209c15266b3a7f7bcd5..3dc4283e364ad689387c7614b9996eb13336d981 100644 (file)
@@ -40,7 +40,7 @@ class HeaderSlideshow extends _Base {
         wp_register_script(
             'cube-header-slideshow',
             asset('scripts/header-slideshow.js'),
-            [], // Dependencies
+            ['jquery'], // Dependencies
             null, // Version
             true // In footer?
         );
diff --git a/wp-content/mu-plugins/cube/src/Forms/Base.php b/wp-content/mu-plugins/cube/src/Forms/Base.php
new file mode 100644 (file)
index 0000000..803675e
--- /dev/null
@@ -0,0 +1,208 @@
+<?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;
+    }
+
+}
diff --git a/wp-content/mu-plugins/cube/src/Forms/Consultation.php b/wp-content/mu-plugins/cube/src/Forms/Consultation.php
new file mode 100644 (file)
index 0000000..34fd972
--- /dev/null
@@ -0,0 +1,33 @@
+<?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'),
+            ]
+        );
+
+    }
+}
index a1d08578075d1e160e7bb89ad367e67c6386b8f9..9e3f8bf847af1f8dc5857d1b62f6a4829d7d2631 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Cube\Shortcodes;
 
+use Cube\Forms;
 use function Roots\view;
 
 class CCVForm {
@@ -30,11 +31,12 @@ class CCVForm {
 
         // List of all possible forms
         $templates = [
-            'consultation',
+            'consultation' => Forms\Consultation::class,
         ];
 
-        if (in_array($name, $templates)) {
-            return view("forms/$name");
+        if (array_key_exists($name, $templates)) {
+            $form = new $templates[$name];
+            return view("forms/$name", compact('form'));
         }
 
         return false;
index dc0fdbb68f333b2856686de33a89f8d853649257..d090f8841daa7fb020a5b4b156d10b8a664c9d00 100644 (file)
@@ -5,24 +5,11 @@
   <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>