Use Doctrine in form editor
[MAILPOET-3644]
This commit is contained in:
@@ -10,6 +10,7 @@ use MailPoet\Entities\FormEntity;
|
||||
use MailPoet\Entities\SegmentEntity;
|
||||
use MailPoet\Form\Block;
|
||||
use MailPoet\Form\FormFactory;
|
||||
use MailPoet\Form\FormsRepository;
|
||||
use MailPoet\Form\Renderer as FormRenderer;
|
||||
use MailPoet\Form\Templates\TemplateRepository;
|
||||
use MailPoet\Form\Templates\Templates\Template10BelowPages;
|
||||
@@ -74,7 +75,7 @@ use MailPoet\Form\Templates\Templates\Template7SlideIn;
|
||||
use MailPoet\Form\Templates\Templates\Template7Widget;
|
||||
use MailPoet\Form\Util\CustomFonts;
|
||||
use MailPoet\Form\Util\Export;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\NotFoundException;
|
||||
use MailPoet\Router\Endpoints\FormPreview;
|
||||
use MailPoet\Router\Router;
|
||||
use MailPoet\Segments\SegmentsSimpleListRepository;
|
||||
@@ -120,6 +121,9 @@ class FormEditor {
|
||||
/** @var SegmentsSimpleListRepository */
|
||||
private $segmentsListRepository;
|
||||
|
||||
/** @var FormsRepository */
|
||||
private $formsRepository;
|
||||
|
||||
private $activeTemplates = [
|
||||
FormEntity::DISPLAY_TYPE_POPUP => [
|
||||
Template1Popup::ID,
|
||||
@@ -205,6 +209,7 @@ class FormEditor {
|
||||
UserFlagsController $userFlags,
|
||||
WPPostListLoader $wpPostListLoader,
|
||||
TemplateRepository $templateRepository,
|
||||
FormsRepository $formsRepository,
|
||||
SegmentsSimpleListRepository $segmentsListRepository
|
||||
) {
|
||||
$this->pageRenderer = $pageRenderer;
|
||||
@@ -219,6 +224,7 @@ class FormEditor {
|
||||
$this->userFlags = $userFlags;
|
||||
$this->wpPostListLoader = $wpPostListLoader;
|
||||
$this->segmentsListRepository = $segmentsListRepository;
|
||||
$this->formsRepository = $formsRepository;
|
||||
}
|
||||
|
||||
public function render() {
|
||||
@@ -231,9 +237,12 @@ class FormEditor {
|
||||
}
|
||||
$form = $this->getFormData((int)$_GET['id']);
|
||||
$customFields = $this->customFieldsRepository->findAll();
|
||||
if (!$form instanceof FormEntity) {
|
||||
throw new NotFoundException('Form does not exist');
|
||||
}
|
||||
$dateTypes = $this->dateBlock->getDateTypes();
|
||||
$data = [
|
||||
'form' => $form,
|
||||
'form' => $form->toArray(),
|
||||
'form_exports' => [
|
||||
'php' => Export::get('php', $form),
|
||||
'iframe' => Export::get('iframe', $form),
|
||||
@@ -352,17 +361,16 @@ class FormEditor {
|
||||
return $translations;
|
||||
}
|
||||
|
||||
private function getFormData(int $id) {
|
||||
$form = Form::findOne($id);
|
||||
if (!$form instanceof Form) {
|
||||
private function getFormData(int $id): ?FormEntity {
|
||||
$form = $this->formsRepository->findOneById($id);
|
||||
if (!$form instanceof FormEntity) {
|
||||
return null;
|
||||
}
|
||||
$form = $form->asArray();
|
||||
$form['styles'] = $this->formRenderer->getCustomStyles($form);
|
||||
$form->setStyles($this->formRenderer->getCustomStyles($form));
|
||||
// Use empty settings in case they are corrupted or missing
|
||||
if (!is_array($form['settings'])) {
|
||||
if (!is_array($form->getSettings())) {
|
||||
$initialFormTemplate = $this->templatesRepository->getFormTemplate(TemplateRepository::INITIAL_FORM_TEMPLATE);
|
||||
$form['settings'] = $initialFormTemplate->getSettings();
|
||||
$form->setSettings($initialFormTemplate->getSettings());
|
||||
}
|
||||
return $form;
|
||||
}
|
||||
|
@@ -3,11 +3,12 @@
|
||||
namespace MailPoet\Form\Util;
|
||||
|
||||
use MailPoet\Config\Env;
|
||||
use MailPoet\Entities\FormEntity;
|
||||
use MailPoet\Form\Widget;
|
||||
use MailPoet\WP\Functions as WPFunctions;
|
||||
|
||||
class Export {
|
||||
public static function getAll($form = null) {
|
||||
public static function getAll(FormEntity $form = null) {
|
||||
return [
|
||||
'html' => static::get('html', $form),
|
||||
'php' => static::get('php', $form),
|
||||
@@ -16,12 +17,13 @@ class Export {
|
||||
];
|
||||
}
|
||||
|
||||
public static function get($type = 'html', $form = null) {
|
||||
public static function get($type = 'html', FormEntity $form = null) {
|
||||
if (!$form instanceof FormEntity) return '';
|
||||
switch ($type) {
|
||||
case 'iframe':
|
||||
// generate url to load iframe's content
|
||||
$iframeUrl = WPFunctions::get()->addQueryArg([
|
||||
'mailpoet_form_iframe' => $form['id'],
|
||||
'mailpoet_form_iframe' => $form->getId(),
|
||||
], WPFunctions::get()->siteUrl());
|
||||
|
||||
// generate iframe
|
||||
@@ -47,7 +49,7 @@ class Export {
|
||||
$output = [
|
||||
'$form_widget = new \MailPoet\Form\Widget();',
|
||||
'echo $form_widget->widget(array(\'form\' => ' .
|
||||
(int)$form['id'] .
|
||||
(int)$form->getId() .
|
||||
', \'form_type\' => \'php\'));',
|
||||
];
|
||||
return join("\n", $output);
|
||||
@@ -93,13 +95,13 @@ class Export {
|
||||
|
||||
$formWidget = new Widget();
|
||||
$output[] = $formWidget->widget([
|
||||
'form' => (int)$form['id'],
|
||||
'form' => (int)$form->getId(),
|
||||
'form_type' => 'php',
|
||||
]);
|
||||
return join("\n", $output);
|
||||
|
||||
case 'shortcode':
|
||||
return '[mailpoet_form id="' . (int)$form['id'] . '"]';
|
||||
return '[mailpoet_form id="' . (int)$form->getId() . '"]';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user