Create a new form from template

[MAILPOET-2987]
This commit is contained in:
Pavel Dohnal
2020-06-22 15:25:33 +02:00
committed by Veljko V
parent a2ff7a34db
commit 07637c69cf
2 changed files with 141 additions and 127 deletions

View File

@ -20,6 +20,127 @@ use MailPoet\Settings\Pages;
use MailPoet\WP\Functions as WPFunctions; use MailPoet\WP\Functions as WPFunctions;
class FormEditor { 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 */ /** @var PageRenderer */
private $pageRenderer; private $pageRenderer;
@ -70,12 +191,12 @@ class FormEditor {
} }
public function render() { public function render() {
if (!isset($_GET['id']) && !isset($_GET['action'])) { if (!isset($_GET['id']) && !isset($_GET['action'])) {
$this->renderTemplateSelection(); $this->renderTemplateSelection();
return; return;
} }
if ($_GET['action'] === 'create') { if (isset($_GET['action']) && $_GET['action'] === 'create') {
$this->createEmptyForm(); $this->createForm($_GET['template-id']);
} }
$form = Form::findOne((int)$_GET['id']); $form = Form::findOne((int)$_GET['id']);
if ($form instanceof Form) { if ($form instanceof Form) {
@ -113,128 +234,9 @@ class FormEditor {
} }
private function renderTemplateSelection() { private function renderTemplateSelection() {
$templates = [ $templates = array_values(self::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;}
',
],
];
if (empty($templates) || !$this->flagsController->isSupported(FeaturesController::TEMPLATES_SELECTION)) { if (empty($templates) || !$this->flagsController->isSupported(FeaturesController::TEMPLATES_SELECTION)) {
$this->createEmptyForm(); $this->createForm();
} }
$data = [ $data = [
'templates' => $templates, 'templates' => $templates,
@ -242,8 +244,13 @@ class FormEditor {
$this->pageRenderer->displayPage('form/template_selection.html', $data); $this->pageRenderer->displayPage('form/template_selection.html', $data);
} }
private function createEmptyForm() { private function createForm($templateId = null) {
$form = $this->formsFactory->createEmptyForm(); 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->wpSafeRedirect(
$this->wp->getSiteUrl(null, $this->wp->getSiteUrl(null,

View File

@ -5,6 +5,13 @@ namespace MailPoet\Form;
use MailPoet\Models\Form; use MailPoet\Models\Form;
class FormFactory { class FormFactory {
public function createFormFromTemplate(array $template) {
if (isset($template['id'])) {
unset($template['id']);
}
return Form::createOrUpdate($template);
}
/** @return Form */ /** @return Form */
public function createEmptyForm() { public function createEmptyForm() {
$data = [ $data = [
@ -42,6 +49,6 @@ class FormFactory {
'segments_selected_by' => 'admin', 'segments_selected_by' => 'admin',
], ],
]; ];
return Form::createOrUpdate($data); return $this->createFormFromTemplate($data);
} }
} }