From 07637c69cf22c6b9d8de2c2f092fd86b6b42b2fb Mon Sep 17 00:00:00 2001 From: Pavel Dohnal Date: Mon, 22 Jun 2020 15:25:33 +0200 Subject: [PATCH] Create a new form from template [MAILPOET-2987] --- lib/AdminPages/Pages/FormEditor.php | 259 ++++++++++++++-------------- lib/Form/FormFactory.php | 9 +- 2 files changed, 141 insertions(+), 127 deletions(-) diff --git a/lib/AdminPages/Pages/FormEditor.php b/lib/AdminPages/Pages/FormEditor.php index 882ae2de04..edcda255a2 100644 --- a/lib/AdminPages/Pages/FormEditor.php +++ b/lib/AdminPages/Pages/FormEditor.php @@ -20,6 +20,127 @@ use MailPoet\Settings\Pages; use MailPoet\WP\Functions as WPFunctions; class FormEditor { + const TEMPLATES = [ + 'my-fancy-template1' => [ + 'id' => 'my-fancy-template1', + 'name' => 'My Fancy Form', + 'body' => [ + [ + 'type' => 'columns', + 'body' => + [ + [ + 'type' => 'column', + 'params' => ['class_name' => '', 'vertical_alignment' => '', 'width' => '50'], + 'body' => [ + [ + 'type' => 'text', + 'params' => ['label' => 'Email', 'class_name' => '', 'required' => '1'], + 'id' => 'email', + 'name' => 'Email', + 'styles' => ['full_width' => '1'] + ], + [ + 'type' => 'text', + 'params' => ['label' => 'First name', 'class_name' => ''], + 'id' => 'first_name', + 'name' => 'First name', + 'styles' => ['full_width' => '1'], + ], + ] + ], + [ + 'type' => 'column', + 'params' => ['class_name' => '', 'vertical_alignment' => '', 'width' => '50'], + 'body' => [ + [ + 'type' => 'paragraph', + 'id' => 'paragraph', + 'params' => [ + 'content' => 'Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.', + 'drop_cap' => '0', + 'align' => 'left', + 'font_size' => '', + 'text_color' => '', + 'background_color' => '', + 'class_name' => '', + ], + ], + ], + ], + ], + 'params' => [ + 'vertical_alignment' => '', + 'class_name' => '', + 'text_color' => '', + 'background_color' => '', + ], + ], + [ + 'type' => 'submit', + 'params' => ['label' => 'Subscribe!', 'class_name' => ''], + 'id' => 'submit', + 'name' => 'Submit', + 'styles' => [ + 'full_width' => '0', + 'bold' => '0', + 'background_color' => '#ff6900', + 'font_size' => '36', + 'font_color' => '#313131', + 'border_size' => '1', + 'border_radius' => '8', + 'border_color' => '#f78da7', + 'padding' => '5', + ], + ], + ], + 'settings' => [ + 'segments' => [], + 'on_success' => 'message', + 'success_message' => 'Check your inbox or spam folder to confirm your subscription.', + 'success_page' => '5', + 'segments_selected_by' => 'admin', + 'alignment' => 'left', + 'place_form_bellow_all_pages' => '', + 'place_form_bellow_all_posts' => '', + 'place_popup_form_on_all_pages' => '1', + 'place_popup_form_on_all_posts' => '1', + 'popup_form_delay' => '15', + 'place_fixed_bar_form_on_all_pages' => '', + 'place_fixed_bar_form_on_all_posts' => '', + 'fixed_bar_form_delay' => '15', + 'fixed_bar_form_position' => 'top', + 'place_slide_in_form_on_all_pages' => '', + 'place_slide_in_form_on_all_posts' => '', + 'slide_in_form_delay' => '15', + 'slide_in_form_position' => 'right', + 'border_radius' => '0', + 'border_size' => '0', + 'form_padding' => '20', + 'input_padding' => '5', + 'below_post_styles' => ['width' => ['unit' => 'percent', 'value' => '100']], + 'slide_in_styles' => ['width' => ['unit' => 'pixel', 'value' => '560']], + 'fixed_bar_styles' => ['width' => ['unit' => 'percent', 'value' => '100']], + 'popup_styles' => ['width' => ['unit' => 'pixel', 'value' => '560']], + 'other_styles' => ['width' => ['unit' => 'percent', 'value' => '100']], + ], + 'styles' => ' + /* form */.mailpoet_form {} + /* columns */.mailpoet_column_with_background { padding: 10px;}/* space between columns */.mailpoet_form_column:not(:first-child) { margin-left: 20px;} + /* input wrapper (label + input) */.mailpoet_paragraph { line-height:20px; margin-bottom: 20px;} + /* labels */.mailpoet_segment_label,.mailpoet_text_label,.mailpoet_textarea_label,.mailpoet_select_label,.mailpoet_radio_label,.mailpoet_checkbox_label,.mailpoet_list_label,.mailpoet_date_label { display:block; font-weight: normal;} + /* inputs */.mailpoet_text,.mailpoet_textarea,.mailpoet_select,.mailpoet_date_month,.mailpoet_date_day,.mailpoet_date_year,.mailpoet_date { display:block;} + .mailpoet_text,.mailpoet_textarea { width: 200px;} + .mailpoet_checkbox {} + .mailpoet_submit {} + .mailpoet_divider {} + .mailpoet_message {} + .mailpoet_form_loading { width: 30px; text-align: center; line-height: normal;} + .mailpoet_form_loading > span { width: 5px; height: 5px; background-color: #5b5b5b;} + ', + ], + ]; + /** @var PageRenderer */ private $pageRenderer; @@ -70,12 +191,12 @@ class FormEditor { } public function render() { - if (!isset($_GET['id']) && !isset($_GET['action'])) { + if (!isset($_GET['id']) && !isset($_GET['action'])) { $this->renderTemplateSelection(); return; } - if ($_GET['action'] === 'create') { - $this->createEmptyForm(); + if (isset($_GET['action']) && $_GET['action'] === 'create') { + $this->createForm($_GET['template-id']); } $form = Form::findOne((int)$_GET['id']); if ($form instanceof Form) { @@ -113,128 +234,9 @@ class FormEditor { } private function renderTemplateSelection() { - $templates = [ - [ - 'id' => 'my-fancy-template1', - 'name' => 'My First Form', - 'body' => [ - [ - 'type' => 'columns', - 'body' => - [ - [ - 'type' => 'column', - 'params' => ['class_name' => '', 'vertical_alignment' => '', 'width' => '50'], - 'body' => [ - [ - 'type' => 'text', - 'params' => ['label' => 'Email', 'class_name' => '', 'required' => '1'], - 'id' => 'email', - 'name' => 'Email', - 'styles' => ['full_width' => '1'] - ], - [ - 'type' => 'text', - 'params' => ['label' => 'First name', 'class_name' => ''], - 'id' => 'first_name', - 'name' => 'First name', - 'styles' => ['full_width' => '1'], - ], - ] - ], - [ - 'type' => 'column', - 'params' => ['class_name' => '', 'vertical_alignment' => '', 'width' => '50'], - 'body' => [ - [ - 'type' => 'paragraph', - 'id' => 'paragraph', - 'params' => [ - 'content' => 'Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean.', - 'drop_cap' => '0', - 'align' => 'left', - 'font_size' => '', - 'text_color' => '', - 'background_color' => '', - 'class_name' => '', - ], - ], - ], - ], - ], - 'params' => [ - 'vertical_alignment' => '', - 'class_name' => '', - 'text_color' => '', - 'background_color' => '', - ], - ], - [ - 'type' => 'submit', - 'params' => ['label' => 'Subscribe!', 'class_name' => ''], - 'id' => 'submit', - 'name' => 'Submit', - 'styles' => [ - 'full_width' => '0', - 'bold' => '0', - 'background_color' => '#ff6900', - 'font_size' => '36', - 'font_color' => '#313131', - 'border_size' => '1', - 'border_radius' => '8', - 'border_color' => '#f78da7', - 'padding' => '5', - ], - ], - ], - 'settings' => [ - 'segments' => ['3'], - 'on_success' => 'message', - 'success_message' => 'Check your inbox or spam folder to confirm your subscription.', - 'success_page' => '5', - 'segments_selected_by' => 'admin', - 'alignment' => 'left', - 'place_form_bellow_all_pages' => '', - 'place_form_bellow_all_posts' => '', - 'place_popup_form_on_all_pages' => '1', - 'place_popup_form_on_all_posts' => '1', - 'popup_form_delay' => '15', - 'place_fixed_bar_form_on_all_pages' => '', - 'place_fixed_bar_form_on_all_posts' => '', - 'fixed_bar_form_delay' => '15', - 'fixed_bar_form_position' => 'top', - 'place_slide_in_form_on_all_pages' => '', - 'place_slide_in_form_on_all_posts' => '', - 'slide_in_form_delay' => '15', - 'slide_in_form_position' => 'right', - 'border_radius' => '0', - 'border_size' => '0', - 'form_padding' => '20', - 'input_padding' => '5', - 'below_post_styles' => ['width' => ['unit' => 'percent', 'value' => '100']], - 'slide_in_styles' => ['width' => ['unit' => 'pixel', 'value' => '560']], - 'fixed_bar_styles' => ['width' => ['unit' => 'percent', 'value' => '100']], - 'popup_styles' => ['width' => ['unit' => 'pixel', 'value' => '560']], - 'other_styles' => ['width' => ['unit' => 'percent', 'value' => '100']], - ], - 'styles' => ' - /* form */.mailpoet_form {} - /* columns */.mailpoet_column_with_background { padding: 10px;}/* space between columns */.mailpoet_form_column:not(:first-child) { margin-left: 20px;} - /* input wrapper (label + input) */.mailpoet_paragraph { line-height:20px; margin-bottom: 20px;} - /* labels */.mailpoet_segment_label,.mailpoet_text_label,.mailpoet_textarea_label,.mailpoet_select_label,.mailpoet_radio_label,.mailpoet_checkbox_label,.mailpoet_list_label,.mailpoet_date_label { display:block; font-weight: normal;} - /* inputs */.mailpoet_text,.mailpoet_textarea,.mailpoet_select,.mailpoet_date_month,.mailpoet_date_day,.mailpoet_date_year,.mailpoet_date { display:block;} - .mailpoet_text,.mailpoet_textarea { width: 200px;} - .mailpoet_checkbox {} - .mailpoet_submit {} - .mailpoet_divider {} - .mailpoet_message {} - .mailpoet_form_loading { width: 30px; text-align: center; line-height: normal;} - .mailpoet_form_loading > span { width: 5px; height: 5px; background-color: #5b5b5b;} - ', - ], - ]; + $templates = array_values(self::TEMPLATES); if (empty($templates) || !$this->flagsController->isSupported(FeaturesController::TEMPLATES_SELECTION)) { - $this->createEmptyForm(); + $this->createForm(); } $data = [ 'templates' => $templates, @@ -242,8 +244,13 @@ class FormEditor { $this->pageRenderer->displayPage('form/template_selection.html', $data); } - private function createEmptyForm() { - $form = $this->formsFactory->createEmptyForm(); + private function createForm($templateId = null) { + if (!is_null($templateId) && isset(self::TEMPLATES[$templateId])) { + $form = $this->formsFactory->createFormFromTemplate(self::TEMPLATES[$templateId]); + } + if (!isset($form)) { + $form = $this->formsFactory->createEmptyForm(); + } $this->wp->wpSafeRedirect( $this->wp->getSiteUrl(null, diff --git a/lib/Form/FormFactory.php b/lib/Form/FormFactory.php index bed3080b11..e6889e1465 100644 --- a/lib/Form/FormFactory.php +++ b/lib/Form/FormFactory.php @@ -5,6 +5,13 @@ namespace MailPoet\Form; use MailPoet\Models\Form; class FormFactory { + public function createFormFromTemplate(array $template) { + if (isset($template['id'])) { + unset($template['id']); + } + return Form::createOrUpdate($template); + } + /** @return Form */ public function createEmptyForm() { $data = [ @@ -42,6 +49,6 @@ class FormFactory { 'segments_selected_by' => 'admin', ], ]; - return Form::createOrUpdate($data); + return $this->createFormFromTemplate($data); } }