Send custom fields into template
[MAILPOET-2453]
This commit is contained in:
committed by
Rostislav Wolný
parent
809051a883
commit
d069b0dab4
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\API\JSON\ResponseBuilders;
|
||||
|
||||
use MailPoet\Entities\CustomFieldEntity;
|
||||
|
||||
class CustomFieldsResponseBuilder {
|
||||
|
||||
/**
|
||||
* @param CustomFieldEntity[] $custom_fields
|
||||
* @return array
|
||||
*/
|
||||
function buildBatch(array $custom_fields) {
|
||||
return array_map([$this, 'build'], $custom_fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param CustomFieldEntity $custom_field
|
||||
* @return array
|
||||
*/
|
||||
function build(CustomFieldEntity $custom_field) {
|
||||
return [
|
||||
'id' => $custom_field->getId(),
|
||||
'name' => $custom_field->getName(),
|
||||
'type' => $custom_field->getType(),
|
||||
'params' => $custom_field->getParams(),
|
||||
'created_at' => $custom_field->getCreatedAt()->format(DATE_ATOM),
|
||||
'updated_at' => $custom_field->getUpdatedAt()->format(DATE_ATOM),
|
||||
];
|
||||
}
|
||||
}
|
@ -3,10 +3,13 @@
|
||||
namespace MailPoet\AdminPages\Pages;
|
||||
|
||||
use MailPoet\AdminPages\PageRenderer;
|
||||
use MailPoet\API\JSON\ResponseBuilders\CustomFieldsResponseBuilder;
|
||||
use MailPoet\CustomFields\CustomFieldsRepository;
|
||||
use MailPoet\Features\FeaturesController;
|
||||
use MailPoet\Form\Block;
|
||||
use MailPoet\Form\Renderer as FormRenderer;
|
||||
use MailPoet\Form\Util\Export;
|
||||
use MailPoet\Models\CustomField;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Models\Segment;
|
||||
use MailPoet\Settings\Pages;
|
||||
@ -18,9 +21,22 @@ class FormEditor {
|
||||
/** @var FeaturesController */
|
||||
private $features_controller;
|
||||
|
||||
function __construct(PageRenderer $page_renderer, FeaturesController $features_controller) {
|
||||
/** @var CustomFieldsRepository */
|
||||
private $custom_fields_repository;
|
||||
|
||||
/** @var CustomFieldsResponseBuilder */
|
||||
private $custom_fields_response_builder;
|
||||
|
||||
function __construct(
|
||||
PageRenderer $page_renderer,
|
||||
FeaturesController $features_controller,
|
||||
CustomFieldsRepository $custom_fields_repository,
|
||||
CustomFieldsResponseBuilder $custom_fields_response_builder
|
||||
) {
|
||||
$this->page_renderer = $page_renderer;
|
||||
$this->features_controller = $features_controller;
|
||||
$this->custom_fields_repository = $custom_fields_repository;
|
||||
$this->custom_fields_response_builder = $custom_fields_response_builder;
|
||||
}
|
||||
|
||||
function render() {
|
||||
@ -48,6 +64,8 @@ class FormEditor {
|
||||
|
||||
if ($this->features_controller->isSupported(FeaturesController::NEW_FORM_EDITOR)) {
|
||||
$data['form']['styles'] = FormRenderer::getStyles($form);
|
||||
$custom_fields = $this->custom_fields_repository->findAll();
|
||||
$data['custom_fields'] = $this->custom_fields_response_builder->buildBatch($custom_fields);
|
||||
$this->page_renderer->displayPage('form/editor.html', $data);
|
||||
} else {
|
||||
$this->page_renderer->displayPage('form/editor_legacy.html', $data);
|
||||
|
20
lib/CustomFields/CustomFieldsRepository.php
Normal file
20
lib/CustomFields/CustomFieldsRepository.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\CustomFields;
|
||||
|
||||
use MailPoet\Doctrine\Repository;
|
||||
use MailPoet\Entities\CustomFieldEntity;
|
||||
|
||||
/**
|
||||
* @method CustomFieldEntity[] findBy(array $criteria, array $order_by = null, int $limit = null, int $offset = null)
|
||||
* @method CustomFieldEntity[] findAll()
|
||||
* @method CustomFieldEntity|null findOneBy(array $criteria, array $order_by = null)
|
||||
* @method CustomFieldEntity|null findOneById(mixed $id)
|
||||
* @method void persist(CustomFieldEntity $entity)
|
||||
* @method void remove(CustomFieldEntity $entity)
|
||||
*/
|
||||
class CustomFieldsRepository extends Repository {
|
||||
protected function getEntityClassName() {
|
||||
return CustomFieldEntity::class;
|
||||
}
|
||||
}
|
@ -87,6 +87,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\API\JSON\v1\WoocommerceSettings::class)->setPublic(true);
|
||||
// API response builders
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\NewslettersResponseBuilder::class);
|
||||
$container->autowire(\MailPoet\API\JSON\ResponseBuilders\CustomFieldsResponseBuilder::class);
|
||||
// Config
|
||||
$container->autowire(\MailPoet\Config\AccessControl::class)->setPublic(true);
|
||||
$container->autowire(\MailPoet\Config\Activator::class)->setPublic(true);
|
||||
@ -158,6 +159,7 @@ class ContainerConfigurator implements IContainerConfigurator {
|
||||
$container->autowire(\MailPoet\Cron\Workers\WooCommercePastOrders::class)->setPublic(true);
|
||||
// Custom field
|
||||
$container->autowire(\MailPoet\CustomFields\ApiDataSanitizer::class);
|
||||
$container->autowire(\MailPoet\CustomFields\CustomFieldsRepository::class);
|
||||
// Features
|
||||
$container->autowire(\MailPoet\Features\FeaturesController::class);
|
||||
$container->autowire(\MailPoet\Features\FeatureFlagsController::class)->setPublic(true);
|
||||
|
60
lib/Entities/CustomFieldEntity.php
Normal file
60
lib/Entities/CustomFieldEntity.php
Normal file
@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace MailPoet\Entities;
|
||||
|
||||
use DateTimeInterface;
|
||||
use MailPoet\Doctrine\EntityTraits\AutoincrementedIdTrait;
|
||||
use MailPoet\Doctrine\EntityTraits\CreatedAtTrait;
|
||||
use MailPoet\Doctrine\EntityTraits\UpdatedAtTrait;
|
||||
use MailPoetVendor\Doctrine\ORM\Mapping as ORM;
|
||||
use MailPoetVendor\Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
/**
|
||||
* @ORM\Entity()
|
||||
* @ORM\Table(name="custom_fields")
|
||||
*/
|
||||
class CustomFieldEntity {
|
||||
use AutoincrementedIdTrait;
|
||||
use CreatedAtTrait;
|
||||
use UpdatedAtTrait;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="string", nullable=false, unique=true)
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="string", nullable=false)
|
||||
* @var string
|
||||
*/
|
||||
private $type;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="json_or_serialized")
|
||||
* @Assert\NotBlank()
|
||||
* @var array
|
||||
*/
|
||||
private $params;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType() {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getParams() {
|
||||
return $this->params;
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
var mailpoet_form_exports = <%= json_encode(form_exports) %>;
|
||||
var mailpoet_form_segments = <%= json_encode(segments) %>;
|
||||
var mailpoet_form_pages = <%= json_encode(pages) %>;
|
||||
var mailpoet_custom_fields = <%= json_encode(custom_fields) %>;
|
||||
<% endautoescape %>
|
||||
</script>
|
||||
|
||||
|
Reference in New Issue
Block a user