From: Stephen Cameron Date: Mon, 4 May 2020 20:11:06 +0000 (+0200) Subject: Form builder refactoring + bug fixes. WIP #3383 @12 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=3c35f97bf1852ebfebfac1bda3fc9770aa24c8b4;p=ccv-wordpress.git Form builder refactoring + bug fixes. WIP #3383 @12 --- diff --git a/wp-content/mu-plugins/cube/src/Elementor/Widgets/Form.php b/wp-content/mu-plugins/cube/src/Elementor/Widgets/Form.php index ab75ec1..48e5d35 100644 --- a/wp-content/mu-plugins/cube/src/Elementor/Widgets/Form.php +++ b/wp-content/mu-plugins/cube/src/Elementor/Widgets/Form.php @@ -119,7 +119,14 @@ class Form extends _Base { /* @var $form BaseForm */ $form = new $forms[$form_name]; $form->init(); - echo view("forms.common.wrapper", compact('form', 'form_name', 'config')); + + $view_data = [ + 'form' => $form->builder, + 'form_name' => $form_name, + 'config' => $config, + ]; + + echo view("forms.common.wrapper", $view_data); } echo ''; diff --git a/wp-content/mu-plugins/cube/src/Forms/Base.php b/wp-content/mu-plugins/cube/src/Forms/Base.php index 694fbd7..c8671e4 100644 --- a/wp-content/mu-plugins/cube/src/Forms/Base.php +++ b/wp-content/mu-plugins/cube/src/Forms/Base.php @@ -2,35 +2,28 @@ namespace Cube\Forms; +use Cube\Forms\Builder\Form as FormBuilder; use function Roots\asset; use function Roots\view; class Base { - private $action_name = 'cube_forms_process'; // Name of the AJAX action - private $destination = 'ccv@ccv-montpellier.fr'; // Default, can be overridden + protected $action_name = 'cube_forms_process'; // Name of the AJAX action + protected $destination = 'ccv@ccv-montpellier.fr'; // Default, can be overridden - private $forms = [ // All available forms + protected $forms = [ // All available forms 'consultation' => Consultation::class, 'training' => Training::class, 'contact' => Contact::class, ]; - private $fields = []; - private $config; - private $data; + protected $fields = []; + protected $config; + protected $data; - public $form_title = ''; + protected $form_title; - // Field types - const BINARY = 'binary'; - const CHECKBOX = 'checkbox'; - const DATE = 'date'; - const EMAIL = 'email'; - const RADIO = 'radio'; - const SELECT = 'select'; - const TEXT = 'text'; - const TEXTAREA = 'textarea'; + public $builder; // Early setup called by Init.php in mu-plugin to register // This is separated from init() so we can set up hooks for every request @@ -46,8 +39,15 @@ class Base } public function init() { - $this->register_scripts(); + $this->register_fields(); + + // Only register the scripts and instantiate HTML builder if this is a non-AJAX request + if (!wp_doing_ajax()) { + $this->register_scripts(); + + $this->builder = new FormBuilder($this->get_fields()); + } } public function register_scripts() { @@ -169,7 +169,7 @@ class Base // Load the form and process it... /* @var $form Base */ $form = new $this->forms[$form_name]; - $form->register_fields(); + $form->init(); $form->data = $this->get_form_data(); $form->config = $config; $form->set_destination($config['destination']); @@ -192,13 +192,15 @@ class Base $content_type = 'text/html'; $charset = get_bloginfo('charset'); $headers[] = sprintf('Content-Type: %s; charset=%s', $content_type, $charset); - $headers[] = sprintf('From: %s', $from); + + //TODO: Set up SMTP delivery via CCV's server if we want to change the from address. Attempting to set this results in messages never arriving (starting 30/04/20) + //$headers[] = sprintf('From: %s', $from); // Gather filled fields into label/value pairs foreach ($this->fields as $field_name => $field) { // TODO: handle file upload fields - should it be removed from HTML and just attached? Or include the filename reference? - if ($value = $this->data($field_name)) { - $data[$field['title']] = $value; + if ($value = $this->get_data($field_name)) { + $data[$field->get_title()] = $value; } } @@ -228,7 +230,7 @@ class Base public function post_process() {} - public function data($name, $default = null) { + public function get_data($name, $default = null) { if (isset($this->data[$name])) { return $this->data[$name]; } @@ -236,263 +238,6 @@ class Base return $default; } - - /** - * Output field HTML - * @param $name - * @param array $settings - * @return mixed - */ - public function field($name, $settings = []) { - - $field = $this->get_field($name); - if (!$field) return false; - - $default_settings = [ - 'class' => '', - 'title_class' => '', - 'input_class' => '', - 'show_title' => true, - 'show_labels' => true, - 'show_icon' => true, - 'placeholder' => $field['title'], - 'field_before' => '', - 'field_after' => '', - 'validation' => '', - ]; - $settings = array_merge($default_settings, $settings); - - // Set up default validation that can be overridden by setting validation field - if ($field['required'] && empty($settings['validation'])) { - $settings['validation'] = 'required'; // Default but could also be set with other ParsleyJS attributes - } - - $res = ''; - - if ($settings['show_title']) { - $res .= $this->title($name, $settings); - } - - // Special input styling for checkbox / radio fields - if (in_array($field['type'], [self::BINARY, self::CHECKBOX, self::RADIO])) { - $settings['input_class'] .= ' custom-checkbox'; - } - - if (method_exists($this, $field['type'])) { - $res .= '
'; - $res .= $settings['field_before']; - $res .= $this->{$field['type']}($name, $settings); - $res .= $settings['field_after']; - $res .= '
'; - } else { - $res .= 'Unrecognised field: '. $field['type']; - } - - return '
'. $res .'
'; - } - - /** - * Generate field title - * @param $name - * @param $settings - * @return bool|string - */ - public function title($name, $settings = []) { - - $title_class = $settings['title_class'] ?? ''; - - $field = $this->get_field($name); - if (!$field) return false; - - return '
'. $field['title'] .'
'; - } - - /** - * 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 = []) { - wp_enqueue_style('cube-flatpickr-css'); - wp_enqueue_script('flatpickr'); - - $flatpickr_defaults = [ - 'wrap' => true, - 'altInput' => true, - 'altFormat' => 'd/m/Y', // Format shown to user (https://flatpickr.js.org/formatting/) - 'dateFormat' => 'Y-m-d', // Format returned to the input field - ]; - - if (!isset($settings['flatpickr'])) $settings['flatpickr'] = []; - $flatpickr = array_merge($flatpickr_defaults, $settings['flatpickr']); - - // Load localisation if available - if (function_exists('pll_current_language')) { - $locale = pll_current_language(); - $locale_handle = "flatpickr-locale-$locale"; - - if (wp_script_is($locale_handle, 'registered')) { - wp_enqueue_script($locale_handle); - $flatpickr['locale'] = $locale; - } - } - - // Finally, enqueue the trigger - wp_enqueue_script('cube-flatpickr'); - - $res = '
'; - $res .= ''; - - if ($settings['show_icon']) { - $res .= ''; - } - - $res .= '
'; - - return $res; - } - - /** - * 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'); - } - - /** - * Textarea field - * @param $name - * @param $settings - * @return string - */ - public function textarea($name, $settings = []) { - return ''; - } - - /** - * Text input field with placeholder - * @param $name - * @param $settings - * @return string - */ - public function text($name, $settings = []) { - return ''; - } - - /** - * E-mail input field with placeholder - * @param $name - * @param $settings - * @return string - */ - public function email($name, $settings = []) { - return ''; - } - - /** - * Select box - * @param $name - * @param $settings - * @return string - */ - public function select($name, $settings = []) { - - $options = isset($settings['options']) ? $settings['options'] : $this->fields[$name]['options']; - - // Placeholder can be used to set first element if it is passed as an array - if (is_array($settings['placeholder'])) { - $options = $settings['placeholder'] + $options; // Prepend to options - } - - $res = ''; - - return $res; - } - - - /** - * HTML (submit) button - * @param string $text Button label - * @param array $settings - * @return string - */ - public function button($text, $settings = []) { - $default_settings = [ - 'type' => 'submit', - 'class' => 'btn', - 'loading_text' => '', - ]; - - $settings = array_merge($default_settings, $settings); - - return ''; - } - - public function get_destination() { return $this->destination; } @@ -503,45 +248,14 @@ class Base } } - /** - * Set multiple fields at once - * @param array $fields - */ - public function set_fields($fields) { - $this->fields = $fields; - } - - /** - * Get all fields - * @return array - */ - public function get_fields() { - return $this->fields; - } - - /** - * Get the field names - * @return array - */ - public function get_field_keys() { - return array_keys($this->fields); - } + public function add_fields($fields) { - /** - * Add a single field - * @param $name - * @param $title - * @param $type - * @param array $options - * @param bool $required - */ - public function add_field($name, $title, $type, $options = [], $required = true) { - $this->fields[$name]['title'] = $title; - $this->fields[$name]['type'] = $type; - $this->fields[$name]['required'] = $required; + if (!is_array($fields)) { + $fields[] = $fields; + } - if (!empty($options)) { - $this->set_field_options($name, $options); + foreach ($fields as $field) { + $this->fields[$field->get_name()] = $field; } } @@ -564,40 +278,11 @@ class Base } /** - * Set the options for a field - * @param string $name - * @param array $options - */ - public function set_field_options($name, $options) { - if ($this->exists($name)) { - $this->fields[$name]['options'] = $options; - } - } - - /** - * Get the options for a field - * @param string $name + * Get all fields * @return array */ - public function get_field_options($name) { - if ($this->exists($name) && isset($this->fields[$name]['options'])) { - return $this->fields[$name]['options']; - } - - return []; - } - - /** - * Helper function to print template code for all fields - * @return string - */ - public function generate_code() { - $res = "
AVAILABLE FIELDS:\n\n";
-        foreach ($this->get_field_keys() as $key) {
-            $res .= "{!! \$form->field('$key') !!}\n";
-        }
-        $res .= '
'; - return $res; + public function get_fields() { + return $this->fields; } } diff --git a/wp-content/mu-plugins/cube/src/Forms/Builder/Field.php b/wp-content/mu-plugins/cube/src/Forms/Builder/Field.php new file mode 100644 index 0000000..d3d8e28 --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Forms/Builder/Field.php @@ -0,0 +1,113 @@ +type = strtolower($type); + $this->name($name); + $this->title($title); + } + + // Allow dynamic chaining with sub-classes - eg. Text::field('test')->title('My title') + public static function field($name, $title = '') { + // The class name is the type + $type = explode('\\', static::class); + $type = array_pop($type); + return new static($type, $name, $title); + } + + public function render($settings) { + return ''; // Sub-classes are responsible for output + } + + //=================== + + public function get_type() { + return $this->type; + } + + /** + * @return string + */ + public function get_name(): string { + return $this->name; + } + + /** + * Set field name + * @param string $name + * @return Field + */ + public function name($name): self { + $this->name = $name; + return $this; + } + + /** + * @return string + */ + public function get_title() { + return $this->title; + } + + /** + * Set field title + * @param string $title + * @return Field + */ + public function title($title): self { + $this->title = $title; + return $this; + } + + + + /** + * @return array + */ + public function get_options(): array { + return $this->options; + } + + /** + * Set field options + * @param array $options + * @return Field + */ + public function options(array $options): self { + $this->options = $options; + return $this; + } + + /** + * @return bool + */ + public function is_required(): bool { + return $this->required; + } + + /** + * Set required flag + * @param bool $required + * @return Field + */ + public function required(bool $required = true): self { + $this->required = $required; + return $this; + } +} diff --git a/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Binary.php b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Binary.php new file mode 100644 index 0000000..3cfc603 --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Binary.php @@ -0,0 +1,19 @@ + true, + 'altInput' => true, + 'altFormat' => 'd/m/Y', // Format shown to user (https://flatpickr.js.org/formatting/) + 'dateFormat' => 'Y-m-d', // Format returned to the input field + ]; + + if (!isset($settings['flatpickr'])) $settings['flatpickr'] = []; + $flatpickr = array_merge($flatpickr_defaults, $settings['flatpickr']); + + // Load localisation if available + if (function_exists('pll_current_language')) { + $locale = pll_current_language(); + $locale_handle = "flatpickr-locale-$locale"; + + if (wp_script_is($locale_handle, 'registered')) { + wp_enqueue_script($locale_handle); + $flatpickr['locale'] = $locale; + } + } + + // Finally, enqueue the trigger + wp_enqueue_script('cube-flatpickr'); + + $res = '
'; + $res .= ''; + + if ($settings['show_icon']) { + $res .= ''; + } + + $res .= '
'; + + return $res; + } +} diff --git a/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Email.php b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Email.php new file mode 100644 index 0000000..203238e --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Email.php @@ -0,0 +1,12 @@ +get_name() .'" placeholder="'. $settings['placeholder'] .'" '. $settings['validation'] .'>'; + } +} diff --git a/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Multi.php b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Multi.php new file mode 100644 index 0000000..9ad4169 --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Multi.php @@ -0,0 +1,35 @@ +get_options(); + $input_name = ($this->input_type == 'checkbox') ? $this->get_name().'[]' : $this->get_name(); + + $res = ''; + + foreach ($options as $option) { + $res .= ''; + } + return $res; + + + + } +} diff --git a/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Radio.php b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Radio.php new file mode 100644 index 0000000..6ef6c7b --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Radio.php @@ -0,0 +1,8 @@ +get_options(); + + // Placeholder can be used to set first element if it is passed as an array + if (is_array($settings['placeholder'])) { + $options = $settings['placeholder'] + $options; // Prepend to options + } + + $res = ''; + + return $res; + } +} diff --git a/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Text.php b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Text.php new file mode 100644 index 0000000..be9a979 --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Text.php @@ -0,0 +1,12 @@ +get_name() .'" placeholder="'. $settings['placeholder'] .'" '. $settings['validation'] .'>'; + } +} diff --git a/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Textarea.php b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Textarea.php new file mode 100644 index 0000000..b540e19 --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Forms/Builder/Fields/Textarea.php @@ -0,0 +1,12 @@ +get_name() .'" '. $settings['validation'] .'>'; + } +} diff --git a/wp-content/mu-plugins/cube/src/Forms/Builder/Form.php b/wp-content/mu-plugins/cube/src/Forms/Builder/Form.php new file mode 100644 index 0000000..d26a4c4 --- /dev/null +++ b/wp-content/mu-plugins/cube/src/Forms/Builder/Form.php @@ -0,0 +1,129 @@ +fields = $fields; + } + + public function process_settings($field, $settings = []) { + + $default_settings = [ + 'class' => '', + 'title_class' => '', + 'input_class' => '', + 'show_title' => true, + 'show_labels' => true, + 'show_icon' => true, + 'placeholder' => $field->get_title(), + 'field_before' => '', + 'field_after' => '', + 'validation' => '', + ]; + + return array_merge($default_settings, $settings); + } + + /** + * Output field HTML + * @param $name + * @param array $settings + * @return mixed + */ + public function field($name, $settings = []) { + + /* @var $field Field */ + $field = $this->get_field($name); + if (!$field) return false; + + $settings = $this->process_settings($field, $settings); + + // Set up default validation that can be overridden by setting validation field + if ($field->is_required() && empty($settings['validation'])) { + $settings['validation'] = 'required'; // Default but could also be set with other ParsleyJS attributes + } + + // Special styling class for checkbox / radio fields + if (in_array($field->get_type(), ['binary', 'checkbox', 'radio'])) { + $settings['input_class'] .= ' custom-checkbox'; + } + + $res = ''; + + if ($settings['show_title']) { + $res .= $this->title($name, $settings); + } + + $res .= '
'; + $res .= $settings['field_before']; + $res .= $field->render($settings); + $res .= $settings['field_after']; + $res .= '
'; + + return '
'. $res .'
'; + } + + /** + * Generate field title + * @param $name + * @param $settings + * @return bool|string + */ + public function title($name, $settings = []) { + + $title_class = $settings['title_class'] ?? ''; + + $field = $this->get_field($name); + if (!$field) return false; + + return '
'. $field->get_title() .'
'; + } + + + /** + * HTML (submit) button + * @param string $text Button label + * @param array $settings + * @return string + */ + public function button($text, $settings = []) { + $default_settings = [ + 'type' => 'submit', + 'class' => 'btn', + 'loading_text' => '', + ]; + + $settings = array_merge($default_settings, $settings); + + return ''; + } + + + /** + * Get the field names + * @return array + */ + public function get_field_keys() { + return array_keys($this->fields); + } + + /** + * Helper function to print template code for all fields + * @return string + */ + public function generate_code() { + $res = "
AVAILABLE FIELDS:\n\n";
+        foreach ($this->get_field_keys() 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 index bbff867..d30008f 100644 --- a/wp-content/mu-plugins/cube/src/Forms/Consultation.php +++ b/wp-content/mu-plugins/cube/src/Forms/Consultation.php @@ -2,6 +2,15 @@ namespace Cube\Forms; +use Cube\Forms\Builder\Fields\Binary; +use Cube\Forms\Builder\Fields\Checkbox; +use Cube\Forms\Builder\Fields\Date; +use Cube\Forms\Builder\Fields\Email; +use Cube\Forms\Builder\Fields\Radio; +use Cube\Forms\Builder\Fields\Select; +use Cube\Forms\Builder\Fields\Text; +use Cube\Forms\Builder\Fields\Textarea; + use function Roots\asset; class Consultation extends Base @@ -22,101 +31,96 @@ class Consultation extends Base function register_fields() { - parent::register_fields(); - - //== SYMPTOMS - $this->add_field('main-problem', __('Problème principal', 'ccv'), self::RADIO, - [ - __('Cervicales', 'ccv'), - __('Lombaires', 'ccv'), - __('Scoliose', 'ccv'), - ] - ); - - $this->add_field('date-first-symptoms', __('Date des premiers symptômes :', 'ccv'), self::DATE); - - $this->add_field('date-pain-since', __('Douleurs permanentes depuis (le cas échéant) :', 'ccv'), self::DATE, [], false); - - $this->add_field('pain-arms-legs', __('Avez-vous des douleurs dans les bras ou les jambes (sciatiques, cruralgies, névralgies) ?', 'ccv'), self::BINARY); - - $this->add_field('pain-arms-legs-detail', __('Si oui, cochez les membres concernés :', 'ccv'), self::CHECKBOX, - [ - __('Haut du bras gauche', 'ccv'), - __('Haut du bras droit', 'ccv'), - __('Avant-bras gauche', 'ccv'), - __('Avant-bras droit', 'ccv'), - __('Haut de la jambe gauche', 'ccv'), - __('Haut de la jambe droite', 'ccv'), - __('Bas de la jambe gauche', 'ccv'), - __('Bas de la jambe droite', 'ccv'), - ], - false // Set not required - ); - - $this->add_field('main-pain', __('La douleur principale est-elle ?', 'ccv'), self::RADIO, - [ - __('Dans la colonne', 'ccv'), - __('Dans les membres', 'ccv'), - __('Les deux', 'ccv'), - ] - ); - - $this->add_field('tingling-numbness', __('Avez-vous des fourmillements ou une sensation d’engourdissement dans un des membres ? ', 'ccv'), self::BINARY); - - $this->add_field('tingling-numbness-date', __('Si oui depuis quand ?', 'ccv'), self::DATE, [], false); - - $this->add_field('strength-loss', __('Avez-vous une perte de force importante dans un des membres ?', 'ccv'), self::BINARY); - - $this->add_field('strength-loss-date', __('Si oui depuis quand ?', 'ccv'), self::DATE, [], false); - - //=== TREATMENTS - $this->add_field('medication', __('Indiquez ici les médicaments que vous avez pris pour vos douleurs (le cas échéant)', 'ccv'), self::TEXTAREA, [], false); - - $this->add_field('kine-osteo', __('Kinésithérapie ou ostéopathie', 'ccv'), self::BINARY); - $this->add_field('corset', __('Corset ou ceinture lombaire', 'ccv'), self::BINARY); - $this->add_field('hospitalisation', __('Séjour en hospitalisation', 'ccv'), self::BINARY); - $this->add_field('infiltration', __('Infiltration ou thermocoagulation', 'ccv'), self::BINARY); - - $this->add_field('surgeries', __('Indiquez ici vos précédentes chirurgies de la colonne et leurs dates (le cas échéant)', 'ccv'), self::TEXTAREA, [], false); - - //=== IMAGERY - $this->add_field('imagery-type', __('Imagerie', 'ccv'), null, [], false); // This is a special case and will be output manually so only using this for the e-mail label - $this->add_field('imagery-online', __('Images en ligne', 'ccv'), self::TEXTAREA, [], false); // Again, a manually handled field - $this->add_field('imagery-posted', __('Images envoyé par courrier', 'ccv'), self::CHECKBOX, [], false); - - //=== PERSONAL INFORMATION - $this->add_field('last-name', _x('Nom', 'Nom de famille', 'ccv'), self::TEXT); - $this->add_field('first-name', __('Prénom', 'ccv'), self::TEXT); - $this->add_field('profession', __('Profession', 'ccv'), self::TEXT); - $this->add_field('city', __('Ville', 'ccv'), self::TEXT); - $this->add_field('country', __('Pays', 'ccv'), self::TEXT); - $this->add_field('phone', __('Tel', 'ccv'), self::TEXT); - $this->add_field('email', __('Email', 'ccv'), self::EMAIL); - $this->add_field('sex', __('Sexe', 'ccv'), self::RADIO, [_x('M', 'Sexe (M)', 'ccv'), _x('F', 'Sexe (F)', 'ccv')]); - $this->add_field('age', __('Âge :', 'ccv'), self::TEXT); - $this->add_field('message', __('Avez vous un message (ou une demande) spécifique à nous formuler ?', 'ccv'), self::TEXTAREA, [], false); - - // Special field: if a surgeon is selected, their e-mail address will be override the default delivery address - $this->add_field('surgeon', __('Chirurgien spécifique'), self::SELECT, - [ - // TODO: Add proper e-mail addresses here once we have them - 'Dr Guilhaume GENESTE' => 'xxxxx@ccv-montpellier.fr', - 'Dr Grégory EDGARD-ROSA' => 'xxxxx@ccv-montpellier.fr', - 'Dr Martin GRAU ORTIZ' => 'xxxxx@ccv-montpellier.fr', - 'Dr Caroline HIRSH' => 'xxxxx@ccv-montpellier.fr', - ], - false // Set not required - ); + $this->add_fields([ + + //== SYMPTOMS + Radio::field('main-problem', __('Problème principal', 'ccv')) + ->options([ + __('Cervicales', 'ccv'), + __('Lombaires', 'ccv'), + __('Scoliose', 'ccv'), + ]), + + Date::field('date-first-symptoms', __('Date des premiers symptômes :', 'ccv')), + + Date::field('date-pain-since', __('Douleurs permanentes depuis (le cas échéant) :', 'ccv'))->required(false), + + Binary::field('pain-arms-legs', __('Avez-vous des douleurs dans les bras ou les jambes (sciatiques, cruralgies, névralgies) ?', 'ccv')), + + Checkbox::field('pain-arms-legs-detail', __('Si oui, cochez les membres concernés :', 'ccv')) + ->required(false) + ->options([ + __('Haut du bras gauche', 'ccv'), + __('Haut du bras droit', 'ccv'), + __('Avant-bras gauche', 'ccv'), + __('Avant-bras droit', 'ccv'), + __('Haut de la jambe gauche', 'ccv'), + __('Haut de la jambe droite', 'ccv'), + __('Bas de la jambe gauche', 'ccv'), + __('Bas de la jambe droite', 'ccv'), + ]), + + Radio::field('main-pain', __('La douleur principale est-elle ?', 'ccv')) + ->options([ + __('Dans la colonne', 'ccv'), + __('Dans les membres', 'ccv'), + __('Les deux', 'ccv'), + ]), + + Binary::field('tingling-numbness', __('Avez-vous des fourmillements ou une sensation d’engourdissement dans un des membres ? ', 'ccv')), + Date::field('tingling-numbness-date', __('Si oui depuis quand ?', 'ccv'))->required(false), + + Binary::field('strength-loss', __('Avez-vous une perte de force importante dans un des membres ?', 'ccv')), + Date::field('strength-loss-date', __('Si oui depuis quand ?', 'ccv'))->required(false), + + //=== TREATMENTS + Textarea::field('medication', __('Indiquez ici les médicaments que vous avez pris pour vos douleurs (le cas échéant)', 'ccv'))->required(false), + Binary::field('kine-osteo', __('Kinésithérapie ou ostéopathie', 'ccv')), + Binary::field('corset', __('Corset ou ceinture lombaire', 'ccv')), + Binary::field('hospitalisation', __('Séjour en hospitalisation', 'ccv')), + Binary::field('infiltration', __('Infiltration ou thermocoagulation', 'ccv')), + Textarea::field('surgeries', __('Indiquez ici vos précédentes chirurgies de la colonne et leurs dates (le cas échéant)', 'ccv'))->required(false), + + //=== IMAGERY + // This is a special case and will be output manually so only using this for the e-mail label + Radio::field('imagery-type', __('Imagerie', 'ccv'))->required(false), + Textarea::field('imagery-online', __('Images en ligne', 'ccv'))->required(false), // Again, a manually handled field + Checkbox::field('imagery-posted', __('Images envoyé par courrier', 'ccv'))->required(false), + + //=== PERSONAL INFORMATION + Text::field('last-name', _x('Nom', 'Nom de famille', 'ccv')), + Text::field('first-name', __('Prénom', 'ccv')), + Text::field('profession', __('Profession', 'ccv')), + Text::field('city', __('Ville', 'ccv')), + Text::field('country', __('Pays', 'ccv')), + Text::field('phone', __('Tel', 'ccv')), + Email::field('email', __('Email', 'ccv')), + Radio::field('sex', __('Sexe', 'ccv')) + ->options([ + _x('M', 'Sexe (M)', 'ccv'), + _x('F', 'Sexe (F)', 'ccv'), + ]), + Text::field('age', __('Âge :', 'ccv')), + Textarea::field('message', __('Avez vous un message (ou une demande) spécifique à nous formuler ?', 'ccv'))->required(false), + + // Special field: if a surgeon is selected, their e-mail address will be override the default delivery address + Select::field('surgeon', __('Chirurgien spécifique')) + ->required(false) + ->options([ + // TODO: Add proper e-mail addresses here once we have them + 'Dr Guilhaume GENESTE' => 'xxxxx@ccv-montpellier.fr', + 'Dr Grégory EDGARD-ROSA' => 'xxxxx@ccv-montpellier.fr', + 'Dr Martin GRAU ORTIZ' => 'xxxxx@ccv-montpellier.fr', + 'Dr Caroline HIRSH' => 'xxxxx@ccv-montpellier.fr', + ]), + ]); } public function pre_process() { - // If a surgeon is selected, send the form directly to them - $surgeons = $this->get_field_options('surgeon'); - if (!empty($this->data('surgeon')) && array_key_exists($this->data('surgeon'), $surgeons)) { - $this->set_destination($surgeons[$this->data('surgeon')]); + $surgeons = $this->get_field('surgeon')->get_options(); + if (!empty($this->get_data('surgeon')) && array_key_exists($this->get_data('surgeon'), $surgeons)) { + $this->set_destination($surgeons[$this->get_data('surgeon')]); } } - - } diff --git a/wp-content/mu-plugins/cube/src/Forms/Contact.php b/wp-content/mu-plugins/cube/src/Forms/Contact.php index 9ba13ac..89c5212 100644 --- a/wp-content/mu-plugins/cube/src/Forms/Contact.php +++ b/wp-content/mu-plugins/cube/src/Forms/Contact.php @@ -2,6 +2,8 @@ namespace Cube\Forms; +use Cube\Forms\Builder\Fields\Text; + class Contact extends Base { public function __construct() { @@ -10,10 +12,11 @@ class Contact extends Base function register_fields() { - parent::register_fields(); + $this->add_fields([ + Text::field('last-name', _x('Nom', 'Nom de famille', 'ccv')), + Text::field('first-name', __('Prénom', 'ccv')), + Text::field('phone', __('Téléphone', 'ccv')) + ]); - $this->add_field('last-name', _x('Nom', 'Nom de famille', 'ccv'), self::TEXT); - $this->add_field('first-name', __('Prénom', 'ccv'), self::TEXT); - $this->add_field('phone', __('Téléphone', 'ccv'), self::TEXT); } } diff --git a/wp-content/mu-plugins/cube/src/Forms/Training.php b/wp-content/mu-plugins/cube/src/Forms/Training.php index 49ae615..167f9af 100644 --- a/wp-content/mu-plugins/cube/src/Forms/Training.php +++ b/wp-content/mu-plugins/cube/src/Forms/Training.php @@ -2,66 +2,63 @@ namespace Cube\Forms; +use Cube\Forms\Builder\Fields\Date; +use Cube\Forms\Builder\Fields\Email; +use Cube\Forms\Builder\Fields\Radio; +use Cube\Forms\Builder\Fields\Text; + class Training extends Base { public function __construct() { $this->form_title = __('Demande de formation', 'ccv'); } - public function register_scripts() { - parent::register_scripts(); - } - - function register_fields() { - parent::register_fields(); + $this->add_fields([ - //== IDENTITY - $this->add_field('last-name', _x('Nom', 'Nom de famille', 'ccv'), self::TEXT); - $this->add_field('first-name', __('Prénom', 'ccv'), self::TEXT); - $this->add_field('birth-date', __('Date de naissance', 'ccv'), self::DATE); - $this->add_field('phone', __('Tel', 'ccv'), self::TEXT); - $this->add_field('email', __('Mail', 'ccv'), self::EMAIL); - $this->add_field('country-residence', __('Pays de résidence', 'ccv'), self::TEXT); - $this->add_field('country-training', __('Pays de formation chirurgicale', 'ccv'), self::TEXT); + //== IDENTITY + Text::field('last-name', _x('Nom', 'Nom de famille', 'ccv')), + Text::field('first-name', __('Prénom', 'ccv')), + Date::field('birth-date', __('Date de naissance', 'ccv')), + Text::field('phone', __('Tel', 'ccv')), + Email::field('email', __('Mail', 'ccv')), + Text::field('country-residence', __('Pays de résidence', 'ccv')), + Text::field('country-training', __('Pays de formation chirurgicale', 'ccv')), + Radio::field('speciality', __('Spécialité chirurgicale', 'ccv')) + ->options([ + __('Neurochirurgien', 'ccv'), + __('Chirurgien orthopédiste avec chirurgie du rachis exclusif', 'ccv'), + __('Chirurgien orthopédiste généraliste', 'ccv'), + ]), + Radio::field('status', __('Statut', 'ccv')) + ->options([ + __('Etudiant (interne, fellow)', 'ccv'), + __('Chirurgien en activité', 'ccv'), + ]), - $this->add_field('speciality', __('Spécialité chirurgicale', 'ccv'), self::RADIO, - [ - __('Neurochirurgien', 'ccv'), - __('Chirurgien orthopédiste avec chirurgie du rachis exclusif', 'ccv'), - __('Chirurgien orthopédiste généraliste', 'ccv'), - ] - ); - $this->add_field('status', __('Statut', 'ccv'), self::RADIO, - [ - __('Etudiant (interne, fellow)', 'ccv'), - __('Chirurgien en activité', 'ccv'), - ] - ); + //== AREA OF INTERVENTION + Radio::field('experience-lumbar-anterior', __("Quel niveau d'expertise avez-vous dans ce domaine ?", 'ccv')) + ->options([ + __("Pas d'expérience", 'ccv'), + __('Moins de 20 chirurgies', 'ccv'), + __('Entre 20 et 100', 'ccv'), + __('Plus de 100 chirurgies', 'ccv'), + ]), + Radio::field('experience-scoliosis', __("Quel niveau d’expertise avez-vous dans la chirurgie de la scoliose ?", 'ccv')) + ->options([ + __("Pas d'expérience", 'ccv'), + __('Moins de 20 chirurgies', 'ccv'), + __('Entre 20 et 100', 'ccv'), + __('Plus de 100 chirurgies', 'ccv'), + ]), - //== AREA OF INTERVENTION - $this->add_field('experience-lumbar-anterior', __("Quel niveau d'expertise avez-vous dans ce domaine ?", 'ccv'), self::RADIO, - [ - __("Pas d'expérience", 'ccv'), - __('Moins de 20 chirurgies', 'ccv'), - __('Entre 20 et 100', 'ccv'), - __('Plus de 100 chirurgies', 'ccv'), - ] - ); - $this->add_field('experience-scoliosis', __("Quel niveau d’expertise avez-vous dans la chirurgie de la scoliose ?", 'ccv'), self::RADIO, - [ - __("Pas d'expérience", 'ccv'), - __('Moins de 20 chirurgies', 'ccv'), - __('Entre 20 et 100', 'ccv'), - __('Plus de 100 chirurgies', 'ccv'), - ] - ); + //== TYPE OF TRAINING + // Since this question has a complex layout, the options are set in the template (formation.blade.php) + Radio::field('training-type', __('Par quel type de formation au CCV Montpellier êtes-vous intéressé ?', 'ccv')) - //== TYPE OF TRAINING - // Since this question has a complex layout, the options are set in the template (formation.blade.php) - $this->add_field('training-type', __('Par quel type de formation au CCV Montpellier êtes-vous intéressé ?', 'ccv'), self::RADIO, null); + ]); } } diff --git a/wp-content/themes/CCV/resources/assets/styles/components/forms.styl b/wp-content/themes/CCV/resources/assets/styles/components/forms.styl index f87cdc5..2a5693a 100644 --- a/wp-content/themes/CCV/resources/assets/styles/components/forms.styl +++ b/wp-content/themes/CCV/resources/assets/styles/components/forms.styl @@ -75,15 +75,15 @@ input[type="submit"] .form-label:before, .form-label-reversed:after - content: '' - display: inline-block - vertical-align: middle - width: 24px - height: @width - border: 1px solid #000 - background-color: #fff - box-shadow: inset 0 0 0 4px #fff // Inner border for checkbox - cursor: pointer + content: '' + display: inline-block + vertical-align: middle + width: 24px + height: @width + border: 1px solid #000 + background-color: #fff + box-shadow: inset 0 0 0 4px #fff // Inner border for checkbox + cursor: pointer .form-label:before margin-right: 0.5em @@ -92,7 +92,7 @@ input[type="submit"] input:checked ~ .form-label:before, input:checked ~ .form-label-reversed:after - @apply bg-pink + @apply bg-pink input position: absolute @@ -108,8 +108,8 @@ input[type="submit"] margin-bottom: 0.75em .form-field-input - > *:not(:last-child) - margin-right: 1.5em + > *:not(:first-child) + margin-left: 1.5em label white-space: nowrap // Ensure text stays beside input control @@ -158,6 +158,7 @@ input[type="submit"] left: 0 bottom: -2em padding: 0 + margin-left: 0 !important font-size: 0.7em white-space: nowrap 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 300b0c5..a81b6e2 100644 --- a/wp-content/themes/CCV/resources/views/forms/consultation.blade.php +++ b/wp-content/themes/CCV/resources/views/forms/consultation.blade.php @@ -1,5 +1,5 @@ {{-- CONSULTATION FORM --}} -@php /* @var $form \Cube\Forms\Consultation */ @endphp +@php /* @var $form \Cube\Forms\Builder\Form */ @endphp {{-- SYMPTOMS --}}
@@ -46,7 +46,7 @@
- @foreach ($form->get_field_options('pain-arms-legs-detail') as $option) + @foreach ($form->get_field('pain-arms-legs-detail')->get_options() as $option)
{{ __("ou je souhaite l'envoyer à un chirurgien spécifique :", 'ccv') }} - {!! $form->field('surgeon', [ - 'class' => 'mt-4', - 'show_title' => false, + + @php // The options for this field contains the e-mail address as the value but we don't want to expose that here // so we override options with a new array made up of just their names as both the key and value. // Once the form is processed, this will be used to look up the e-mail address. - 'options' => array_combine(array_keys($form->get_field_options('surgeon')), array_keys($form->get_field_options('surgeon'))), + $surgeons = array_keys($form->get_field('surgeon')->get_options()); + $surgeons_options = array_combine($surgeons, $surgeons); + @endphp + {!! $form->field('surgeon', [ + 'class' => 'mt-4', + 'show_title' => false, + 'options' => $surgeons_options, 'placeholder' => [ '' => __('Sélectionner', 'ccv') // First select option ] diff --git a/wp-content/themes/CCV/resources/views/forms/contact.blade.php b/wp-content/themes/CCV/resources/views/forms/contact.blade.php index 4b94f05..c33236d 100644 --- a/wp-content/themes/CCV/resources/views/forms/contact.blade.php +++ b/wp-content/themes/CCV/resources/views/forms/contact.blade.php @@ -1,5 +1,5 @@ {{-- CONTACT FORM --}} -@php /* @var $form \Cube\Forms\Contact */ @endphp +@php /* @var $form \Cube\Forms\Builder\Form */ @endphp
{!! $form->field('last-name', ['show_title' => false]) !!} diff --git a/wp-content/themes/CCV/resources/views/forms/training.blade.php b/wp-content/themes/CCV/resources/views/forms/training.blade.php index 170bdea..eb0ee71 100644 --- a/wp-content/themes/CCV/resources/views/forms/training.blade.php +++ b/wp-content/themes/CCV/resources/views/forms/training.blade.php @@ -1,5 +1,5 @@ {{-- TRAINING FORM --}} -@php /* @var $form \Cube\Forms\Training */ @endphp +@php /* @var $form \Cube\Forms\Builder\Form */ @endphp {{-- IDENTITY --}}