From d9bb9fbeb79904faacf380d2b4f8a259307f8864 Mon Sep 17 00:00:00 2001 From: Stephen Cameron Date: Tue, 31 Mar 2020 21:42:14 +0200 Subject: [PATCH] WIP #3445 @8 --- .../src/Elementor/Widgets/HeaderSlideshow.php | 2 +- wp-content/mu-plugins/cube/src/Forms/Base.php | 208 ++++++++++++++++++ .../cube/src/Forms/Consultation.php | 33 +++ .../cube/src/Shortcodes/CCVForm.php | 8 +- .../views/forms/consultation.blade.php | 23 +- 5 files changed, 252 insertions(+), 22 deletions(-) create mode 100644 wp-content/mu-plugins/cube/src/Forms/Base.php create mode 100644 wp-content/mu-plugins/cube/src/Forms/Consultation.php diff --git a/wp-content/mu-plugins/cube/src/Elementor/Widgets/HeaderSlideshow.php b/wp-content/mu-plugins/cube/src/Elementor/Widgets/HeaderSlideshow.php index 57e86d7..3dc4283 100644 --- a/wp-content/mu-plugins/cube/src/Elementor/Widgets/HeaderSlideshow.php +++ b/wp-content/mu-plugins/cube/src/Elementor/Widgets/HeaderSlideshow.php @@ -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 index 0000000..803675e --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Forms/Base.php @@ -0,0 +1,208 @@ + false, + 'placeholder' => true, + ]; + + $settings = array_merge($default_settings, $settings); + + $field = $this->getField($name); + if (!$field) return false; + + $res = '
'. $field['title'] .'
'; + + if (method_exists($this, $field['type'])) { + $res .= '
'; + $res .= $this->{$field['type']}($name, $settings); + $res .= '
'; + } else { + $res .= 'Unrecognised field: '. $field['type']; + } + + return '
'. $res .'
'; + } + + /** + * 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 .= ''; + } + 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 ''; + } + + /** + * 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 = "
AVAILABLE FIELDS:\n\n";
+        foreach ($this->getFieldKeys() as $key) {
+            $res .= "{!! \$form->field('$key') !!}\n";
+        }
+        $res .= '
'; + 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 index 0000000..34fd972 --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Forms/Consultation.php @@ -0,0 +1,33 @@ +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'), + ] + ); + + } +} diff --git a/wp-content/mu-plugins/cube/src/Shortcodes/CCVForm.php b/wp-content/mu-plugins/cube/src/Shortcodes/CCVForm.php index a1d0857..9e3f8bf 100644 --- a/wp-content/mu-plugins/cube/src/Shortcodes/CCVForm.php +++ b/wp-content/mu-plugins/cube/src/Shortcodes/CCVForm.php @@ -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; diff --git a/wp-content/themes/CCV/resources/views/forms/consultation.blade.php b/wp-content/themes/CCV/resources/views/forms/consultation.blade.php index dc0fdbb..d090f88 100644 --- a/wp-content/themes/CCV/resources/views/forms/consultation.blade.php +++ b/wp-content/themes/CCV/resources/views/forms/consultation.blade.php @@ -5,24 +5,11 @@

{{ __('1. Vos symptômes') }}

-- 2.39.5