<?php
namespace Cms\Modules\AlertBundle\Form\Type\Alert;
use Cms\ContainerBundle\Entity\Container;
use Cms\ContainerBundle\Service\ContainerService;
use Cms\CoreBundle\Form\Type\DateTimeType;
use Cms\CoreBundle\Form\Type\HtmlType;
use Cms\CoreBundle\Form\Type\SlugType;
use Cms\CoreBundle\Util\DateTimeUtils;
use Cms\FileBundle\Form\Type\FileModalType;
use Cms\FileBundle\Form\Type\SpecialFileModalType;
use Cms\ModuleBundle\Form\StandardMetadataType;
use Cms\ModuleBundle\Form\Type\AbstractModuleDataType;
use Cms\ModuleBundle\Validator\Constraints\ModuleReservedSlugs;
use Cms\ModuleBundle\Validator\Constraints\ModuleSlug;
use Cms\Modules\AlertBundle\Form\Type\AlertBehaviorType;
use Cms\Modules\AlertBundle\Form\Type\AlertLevelType;
use Cms\Modules\AlertBundle\Model\Alert\AlertData;
use Cms\TagBundle\Form\Type\TaggableType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Constraints\NotBlank;
/**
* Class AlertDataType
*
* @package Cms\Modules\AlertBundle\Form\Type\Alert
*/
final class AlertDataType extends AbstractModuleDataType
{
/**
* @var ContainerService
*/
private ContainerService $containerService;
/**
* @param ContainerService $containerService
*/
public function __construct(
ContainerService $containerService
)
{
$this->containerService = $containerService;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
/** @var Container $container */
$container = $options['container'];
$builder
->add('title', TextType::class, array(
'constraints' => array(
new NotBlank(),
),
))
->add('slug', SlugType::class, array(
'dependency' => array('title'),
'helpText' => 'Attention! Changing the URL can cause broken links across your site. Proceed with caution.',
'constraints' => array(
new ModuleSlug(),
new ModuleReservedSlugs(),
),
'slugPrefix' => sprintf(
'%s/alert/{id}/',
rtrim($this->containerService->getFrontPath($container), '/')
),
))
->add('startDate', DateTimeType::class, array(
'constraints' => array(
new NotBlank(),
),
))
->add('expiryDate', DateTimeType::class, array(
'constraints' => array(
new NotBlank(),
),
))
->add('externalLink', SpecialFileModalType::class, array(
'required' => false,
'container' => $container->getId(),
))
->add('abstract', TextareaType::class, array(
'required' => false,
))
->add('content', HtmlType::class, array(
'baseHref' => $this->containerService->getFrontLink($container),
))
/*
->add('attachments', AttachableType::class, array(
'container' => $container->getId(),
))
*/
->add('standardMetadata', StandardMetadataType::class, [])
->add('socialImage', FileModalType::class, array(
'container' => $container->getId(),
))
->add('tags', TaggableType::class, [])
->add('level', AlertLevelType::class, [])
->add('behavior', AlertBehaviorType::class, [])
;
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
/** @var AlertData $data */
$data = $event->getData();
// Add interval only for new items
if ($options['create'] && ( ! $options['clone']) && $data->getExpiryDate() instanceof \DateTime) {
// +1 hour gap between start and end dates
$data->setExpiryDate(
$data->getExpiryDate()->add(DateTimeUtils::duration('PT1H'))
);
}
});
}
}