<?php
namespace Cms\ContainerBundle\Form\Type\Container;
use Cms\ContainerBundle\Entity\Container;
use Cms\ContainerBundle\Form\Type\ContainerModuleFlagsType;
use Cms\CoreBundle\Form\Type\ColorsType;
use Cms\CoreBundle\Form\Type\HtmlOverridesType;
use Cms\CoreBundle\Form\Type\LocaleSettingsType;
use Cms\CoreBundle\Form\Type\SlugType;
use Cms\CoreBundle\Form\Type\SwitchType;
use Cms\CoreBundle\Util\Doctrine\EntityManager;
use Cms\ModuleBundle\Validator\Constraints\ModuleReservedSlugs;
use Cms\ModuleBundle\Validator\Constraints\ModuleSlug;
use Cms\ThemeBundle\Entity\InnerLayout;
use Cms\ThemeBundle\Entity\OuterLayout;
use Cms\ThemeBundle\Service\ThemeManager;
use Doctrine\Common\Util\ClassUtils;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Class AbstractContainerType
* @package Cms\ContainerBundle\Form\Type\Container
*/
abstract class AbstractContainerType extends AbstractType
{
/**
* @var EntityManager
*/
protected $em;
/**
* @var ThemeManager
*/
protected $themeManager;
/**
* @param EntityManager $em
* @param ThemeManager $themeManager
*/
public function __construct(EntityManager $em, ThemeManager $themeManager)
{
$this->em = $em;
$this->themeManager = $themeManager;
}
/**
* @param Container $container
* @return Container[]
*/
protected function getContainerChain(Container $container)
{
$containers = $this->em->getRepository(Container::class)->findAncestors($container);
array_push($containers, $container);
$containers = array_reverse($containers);
return $containers;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
// obtain the container, should be the data for the form
$container = $builder->getData();
if ( ! $container instanceof Container) {
throw new \Exception();
}
// get the default theme for the container
$theme = $container->getDefaultTheme();
if (empty($theme)) {
$containers = $this->getContainerChain($container);
$theme = $this->themeManager->effectiveTheme($containers);
}
// get all of the layouts for the existing default theme
$innerLayouts = $this->em->getRepository(InnerLayout::class)->findByTheme($theme);
$outerLayouts = $this->em->getRepository(OuterLayout::class)->findByTheme($theme);
// create form fields
$builder
->add('name', TextType::class, array(
'categoryName' => 'General',
))
->add('slug', SlugType::class, array(
'dependency' => array('name'),
'categoryName' => 'General',
'helpText' => 'Attention - changing the wording on Slug field can cause broken links across your site. Proceed with caution.',
'constraints' => array(
new ModuleSlug(),
new ModuleReservedSlugs(),
),
))
->add('hidden', SwitchType::class, array(
'categoryName' => 'General',
'required' => false,
))
->add('color', ColorsType::class, array(
'categoryName' => 'General',
'required' => false,
'colors' => call_user_func(
function () use ($container)
{
$colors = array_fill(1, 24, []);
/** @var array|Container[] $depts */
$depts = $this->em->getRepository(ClassUtils::getClass($container))
->createQueryBuilder('departments')
->andWhere('departments.rt = :rt')
->setParameter('rt', $container->getRoot())
->andWhere('departments.color IS NOT NULL AND departments.color > 0')
->andWhere('departments.id <> :id')
->setParameter('id', $container->getId())
->addOrderBy('departments.lft', 'ASC')
->getQuery()
->getResult();
foreach ($depts as $dept) {
if ( ! empty($dept->getColor())) {
$colors[$dept->getColor()][] = $dept;
}
}
return $colors;
}
),
))
->add('defaultOuterLayout', EntityType::class, array(
'categoryName' => 'Theme',
'class' => OuterLayout::class,
'required' => false,
'empty_data' => null,
'placeholder' => '...',
'choice_label' =>
function (OuterLayout $choice) {
return $choice->getName();
},
'choices' => $outerLayouts,
))
->add('defaultInnerLayout', EntityType::class, array(
'categoryName' => 'Theme',
'class' => InnerLayout::class,
'required' => false,
'empty_data' => null,
'placeholder' => '...',
'choice_label' =>
function (InnerLayout $choice) {
return $choice->getName();
},
'choices' => $innerLayouts,
))
->add('moduleFlags', ContainerModuleFlagsType::class, array(
'categoryName' => 'Active Modules',
'embedded' => true,
'required' => false,
))
->add('htmlOverrides', HtmlOverridesType::class, [])
->add('schoolNowMigrated', SwitchType::class, array(
'categoryName' => 'General',
'label' => 'SchoolNow Migrated',
'required' => false,
))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
array(
'isRoot' => false
)
);
}
}