From b4dc7782308ef922b824e15f1b5ef897b4d9890a Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Wed, 1 Jul 2020 13:44:05 +0200 Subject: [PATCH] Load form templates data for template selection page from repository [MAILPOET-2985] --- lib/AdminPages/Pages/FormEditor.php | 144 +++------------------- lib/Form/Templates/TemplateRepository.php | 14 ++- 2 files changed, 33 insertions(+), 125 deletions(-) diff --git a/lib/AdminPages/Pages/FormEditor.php b/lib/AdminPages/Pages/FormEditor.php index b0643abf3e..e71369e807 100644 --- a/lib/AdminPages/Pages/FormEditor.php +++ b/lib/AdminPages/Pages/FormEditor.php @@ -9,6 +9,7 @@ use MailPoet\CustomFields\CustomFieldsRepository; use MailPoet\Form\Block; use MailPoet\Form\FormFactory; use MailPoet\Form\Renderer as FormRenderer; +use MailPoet\Form\Templates\TemplateRepository; use MailPoet\Form\Util\CustomFonts; use MailPoet\Form\Util\Export; use MailPoet\Models\Form; @@ -19,127 +20,6 @@ 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; @@ -164,6 +44,13 @@ class FormEditor { /** @var Localizer */ private $localizer; + /** @var TemplateRepository */ + private $templatesRepository; + + private $activeTemplates = [ + 'demo_form', + ]; + public function __construct( PageRenderer $pageRenderer, CustomFieldsRepository $customFieldsRepository, @@ -172,7 +59,8 @@ class FormEditor { Block\Date $dateBlock, WPFunctions $wp, FormFactory $formsFactory, - Localizer $localizer + Localizer $localizer, + TemplateRepository $templateRepository ) { $this->pageRenderer = $pageRenderer; $this->customFieldsRepository = $customFieldsRepository; @@ -182,6 +70,7 @@ class FormEditor { $this->wp = $wp; $this->formsFactory = $formsFactory; $this->localizer = $localizer; + $this->templatesRepository = $templateRepository; } public function render() { @@ -228,9 +117,16 @@ class FormEditor { } public function renderTemplateSelection() { - $templates = array_values(self::TEMPLATES); + $templateForms = $this->templatesRepository->getFormsForTemplates($this->activeTemplates); + $templatesData = []; + foreach ($templateForms as $templateId => $form) { + $templatesData[] = [ + 'id' => $templateId, + 'name' => $form->getName(), + ]; + } $data = [ - 'templates' => $templates, + 'templates' => $templatesData, ]; $this->pageRenderer->displayPage('form/template_selection.html', $data); } diff --git a/lib/Form/Templates/TemplateRepository.php b/lib/Form/Templates/TemplateRepository.php index 2156dffbc6..3736fdf407 100644 --- a/lib/Form/Templates/TemplateRepository.php +++ b/lib/Form/Templates/TemplateRepository.php @@ -23,7 +23,7 @@ class TemplateRepository { $this->settings = $settings; } - public function getFormEntityForTemplate($templateId): FormEntity { + public function getFormEntityForTemplate(string $templateId): FormEntity { if (!isset($this->templates[$templateId])) { throw UnexpectedValueException::create() ->withErrors(["Template with id $templateId doesn't exist."]); @@ -39,6 +39,18 @@ class TemplateRepository { return $formEntity; } + /** + * @param string[] $templateIds + * @return FormEntity[] associative array with template ids as keys + */ + public function getFormsForTemplates(array $templateIds): array { + $result = []; + foreach ($templateIds as $templateId) { + $result[$templateId] = $this->getFormEntityForTemplate($templateId); + } + return $result; + } + private function getDefaultSuccessMessage() { if ($this->settings->get('signup_confirmation.enabled')) { return __('Check your inbox or spam folder to confirm your subscription.', 'mailpoet');