Files
piratepoet/lib/Form/Templates/TemplateRepository.php
Rostislav Wolny a45a66b9d0 Move form templates data into subdirectory
[MAILPOET-2985]
2020-07-06 15:18:23 +02:00

64 lines
2.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace MailPoet\Form\Templates;
use MailPoet\Entities\FormEntity;
use MailPoet\Form\Templates\Templates\DefaultForm;
use MailPoet\Form\Templates\Templates\DemoForm;
use MailPoet\Form\Templates\Templates\InitialForm;
use MailPoet\Settings\SettingsController;
use MailPoet\UnexpectedValueException;
class TemplateRepository {
const INITIAL_FORM_TEMPLATE = 'initial_form';
const DEFAULT_FORM_TEMPLATE = 'default_form';
private $templates = [
'initial_form' => InitialForm::class,
'default_form' => DefaultForm::class,
'demo_form' => DemoForm::class,
];
/** @var SettingsController */
private $settings;
public function __construct(SettingsController $settings) {
$this->settings = $settings;
}
public function getFormEntityForTemplate(string $templateId): FormEntity {
if (!isset($this->templates[$templateId])) {
throw UnexpectedValueException::create()
->withErrors(["Template with id $templateId doesn't exist."]);
}
/** @var Template $template */
$template = new $this->templates[$templateId]();
$formEntity = new FormEntity($template->getName());
$formEntity->setBody($template->getBody());
$settings = $formEntity->getSettings();
$settings['success_message'] = $this->getDefaultSuccessMessage();
$formEntity->setSettings($settings);
$formEntity->setStyles($template->getStyles());
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');
}
return __('Youve been successfully subscribed to our newsletter!', 'mailpoet');
}
}