<?php
namespace Boab\CmsBundle\Manager;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\AbstractType;
use Boab\CmsBundle\Util\UtilCommon;
use Symfony\Component\HttpFoundation\Request;
use Boab\CmsBundle\Entity\PageInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Boab\CmsBundle\Form\Type\UserEntityType;
use Boab\CmsBundle\Form\Type\CategoryEntityType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Validator\Constraints as Constraints;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Boab\CmsBundle\Repository\ContentRepositoryInterface;
use Boab\CmsBundle\Entity\ContentInterface;
use Boab\CmsBundle\Entity\Term;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Validator\Constraints\NotBlank;
abstract class AbstractContentType extends AbstractType implements TypeManagerInterface
{
protected $repository;
protected $configs;
protected $type;
use UtilCommon;
use ContentTypeConfigTrait;
public function setRepository(ContentRepositoryInterface $repository)
{
$this->repository = $repository;
}
public function setConfig(array $configs)
{
$this->configs = $configs;
}
public function setType(string $type)
{
$this->type = $type;
}
public function getType()
{
return $this->type;
}
public function getEntityClass()
{
return get_class($this->getEntity());
}
public function getCollection(Request $request, $page)
{
return $this->repository->findContentsByType($this->getEntityClass(), $page);
}
public function getContent(Request $request): ?ContentInterface
{
return $this->repository->findContentBySlug($request->get('slug'));
}
public function getContentById($contentId): ?ContentInterface
{
return $this->repository->findContentById($contentId);
}
public function getTypeId()
{
return $this->type;
}
public function supportsType($type)
{
return $this->supports($type);
}
public function supports(string $type)
{
return $type == $this->getType();
}
public function buildForm(FormBuilderInterface $builder, array $options=[])
{
$builder
->add('title', TextType::class,[
'required'=>true,
'attr' => [
'class' => 'form-control'
],
'constraints' => [
new Constraints\NotBlank()
],
])
->add('summary', TextareaType::class, [
'required'=>true,
'attr' => [
'class' => 'form-control'
],
'constraints' => [
new Constraints\NotBlank()
],
])
->add('body',TextareaType::class,[
'required'=>false,
'attr' => [
'class' => 'form-control tinymce'
],
'constraints' => [
new Constraints\NotBlank()
],
])
->add('metaKeywords', TextType::class, [
'attr'=>[
'class'=>'form-control'
],
'required'=>false,
])
->add('metaDescription', TextType::class, [
'attr'=>[
'class'=>'form-control'
],
'required'=>false,
])
->add('status', ChoiceType::class, [
'choices' => ['Published' => 'published', 'Draft' => 'draft'],
'empty_data' => false,
'required'=>false,
'attr'=>[
'class'=>'input-select'
],
'placeholder' => 'Choose an option',
'constraints' => [
new Constraints\NotBlank()
],
])
->add('datePublished', DateTimeType::class, [
'widget' => 'single_text',
'html5'=> false,
'required'=>false,
'attr'=>[
'class'=>'form-control datetime'
],
'empty_data' =>''
])
->add("publishOnSocialMedia", CheckboxType::class, [
'required' => false,
//'data' => true,
])
->add("isFeatured", CheckboxType::class, [
'label'=>'Set as feature content',
'required' => false,
//'data' => true,
])
->add('position', TextType::class, [
'label' => 'Position',
'attr'=>[
'class'=>'form-control'
],
'required'=>false,
])
->add("isHome", CheckboxType::class, [
'label'=>'Set as home page',
'required' => false
])
->add("user", UserEntityType::class,[
])
->add('uploadedThumbnail', FileType::class, [
'attr'=>[
'class'=>'browse-hide page-thumbnail-browse'
],
'data_class'=>null,
'required'=>false,
]);
$builder->add('cancel', ButtonType::class, [
'attr' => ['class' => 'btn btn-danger isThemeBtn'],
]);
$builder->add('submit', SubmitType::class, [
'attr' => [
'class' => 'btn btn-success isThemeBtn',
'id'=>'submit'
],
]);
if(!$this->getEntity() instanceof PageInterface && isset($this->configs['taxonomy'])){
$builder->add('term', CategoryEntityType::class, [
'vocabulary'=>$this->configs['taxonomy']['vocab_id'],
'class' => Term::class,
]);
$builder->get('term')->addEventListener(FormEvents::SUBMIT, function(FormEvent $event){
$content = $event->getData();
$form = $event->getForm();
if(!$content){
//$form->addError(new FormError('You must choose a category'));
}
});
}
$builder->addEventListener(FormEvents::SUBMIT, function(FormEvent $event){
$form = $event->getForm();
$content = $event->getData();
$datePublished = $form->get('datePublished');
if(!$datePublished->getData() && $form->get('status')->getData() == ContentInterface::STATUS_PUBLISHED){
$datePublished->addError(new FormError('Date must be set if status is published'));
}
if($content->getId()){
$content->setUpdatedAt(new \DateTimeImmutable('now'));
}
$content->setSlug($this->slugify($content->getTitle()));
$event->setData($content);
});
}
public function buildSearchForm(FormBuilderInterface $builder, array $options=[])
{
$builder->add( 'query', TextType::class, [
'label'=>false,
'constraints' => [
new NotBlank(),
],
'attr'=>[
'class'=>'form-control py-2 border-right-0 border',
'id'=> 'search-query',
'placeholder' => 'Search Courses...'
]
])
->add ( 'type', HiddenType::class, [
'constraints' => [
new NotBlank(),
],
'data'=> $options['type'],
]);
}
}