src/Cms/ContainerBundle/Form/Type/Container/AbstractContainerType.php line 150

Open in your IDE?
  1. <?php
  2. namespace Cms\ContainerBundle\Form\Type\Container;
  3. use Cms\ContainerBundle\Entity\Container;
  4. use Cms\ContainerBundle\Form\Type\ContainerModuleFlagsType;
  5. use Cms\CoreBundle\Form\Type\ColorsType;
  6. use Cms\CoreBundle\Form\Type\HtmlOverridesType;
  7. use Cms\CoreBundle\Form\Type\LocaleSettingsType;
  8. use Cms\CoreBundle\Form\Type\SlugType;
  9. use Cms\CoreBundle\Form\Type\SwitchType;
  10. use Cms\CoreBundle\Util\Doctrine\EntityManager;
  11. use Cms\ModuleBundle\Validator\Constraints\ModuleReservedSlugs;
  12. use Cms\ModuleBundle\Validator\Constraints\ModuleSlug;
  13. use Cms\ThemeBundle\Entity\InnerLayout;
  14. use Cms\ThemeBundle\Entity\OuterLayout;
  15. use Cms\ThemeBundle\Service\ThemeManager;
  16. use Doctrine\Common\Util\ClassUtils;
  17. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  18. use Symfony\Component\Form\AbstractType;
  19. use Symfony\Component\Form\Extension\Core\Type\TextType;
  20. use Symfony\Component\Form\FormBuilderInterface;
  21. use Symfony\Component\OptionsResolver\OptionsResolver;
  22. /**
  23.  * Class AbstractContainerType
  24.  * @package Cms\ContainerBundle\Form\Type\Container
  25.  */
  26. abstract class AbstractContainerType extends AbstractType
  27. {
  28.     /**
  29.      * @var EntityManager
  30.      */
  31.     protected $em;
  32.     /**
  33.      * @var ThemeManager
  34.      */
  35.     protected $themeManager;
  36.     /**
  37.      * @param EntityManager $em
  38.      * @param ThemeManager $themeManager
  39.      */
  40.     public function __construct(EntityManager $emThemeManager $themeManager)
  41.     {
  42.         $this->em $em;
  43.         $this->themeManager $themeManager;
  44.     }
  45.     /**
  46.      * @param Container $container
  47.      * @return Container[]
  48.      */
  49.     protected function getContainerChain(Container $container)
  50.     {
  51.         $containers $this->em->getRepository(Container::class)->findAncestors($container);
  52.         array_push($containers$container);
  53.         $containers array_reverse($containers);
  54.         return $containers;
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function buildForm(FormBuilderInterface $builder, array $options)
  60.     {
  61.         // obtain the container, should be the data for the form
  62.         $container $builder->getData();
  63.         if ( ! $container instanceof Container) {
  64.             throw new \Exception();
  65.         }
  66.         // get the default theme for the container
  67.         $theme $container->getDefaultTheme();
  68.         if (empty($theme)) {
  69.             $containers $this->getContainerChain($container);
  70.             $theme $this->themeManager->effectiveTheme($containers);
  71.         }
  72.         // get all of the layouts for the existing default theme
  73.         $innerLayouts $this->em->getRepository(InnerLayout::class)->findByTheme($theme);
  74.         $outerLayouts $this->em->getRepository(OuterLayout::class)->findByTheme($theme);
  75.         // create form fields
  76.         $builder
  77.             ->add('name'TextType::class, array(
  78.                 'categoryName' => 'General',
  79.             ))
  80.             ->add('slug'SlugType::class, array(
  81.                 'dependency' => array('name'),
  82.                 'categoryName' => 'General',
  83.                 'helpText' => 'Attention - changing the wording on Slug field can cause broken links across your site. Proceed with caution.',
  84.                 'constraints' => array(
  85.                     new ModuleSlug(),
  86.                     new ModuleReservedSlugs(),
  87.                 ),
  88.             ))
  89.             ->add('hidden'SwitchType::class, array(
  90.                 'categoryName' => 'General',
  91.                 'required' => false,
  92.             ))
  93.             ->add('color'ColorsType::class, array(
  94.                 'categoryName' => 'General',
  95.                 'required' => false,
  96.                 'colors' => call_user_func(
  97.                     function () use ($container)
  98.                     {
  99.                         $colors array_fill(124, []);
  100.                         /** @var array|Container[] $depts */
  101.                         $depts $this->em->getRepository(ClassUtils::getClass($container))
  102.                             ->createQueryBuilder('departments')
  103.                             ->andWhere('departments.rt = :rt')
  104.                             ->setParameter('rt'$container->getRoot())
  105.                             ->andWhere('departments.color IS NOT NULL AND departments.color > 0')
  106.                             ->andWhere('departments.id <> :id')
  107.                             ->setParameter('id'$container->getId())
  108.                             ->addOrderBy('departments.lft''ASC')
  109.                             ->getQuery()
  110.                             ->getResult();
  111.                         foreach ($depts as $dept) {
  112.                             if ( ! empty($dept->getColor())) {
  113.                                 $colors[$dept->getColor()][] = $dept;
  114.                             }
  115.                         }
  116.                         return $colors;
  117.                     }
  118.                 ),
  119.             ))
  120.             ->add('defaultOuterLayout'EntityType::class, array(
  121.                 'categoryName' => 'Theme',
  122.                 'class' => OuterLayout::class,
  123.                 'required' => false,
  124.                 'empty_data' => null,
  125.                 'placeholder' => '...',
  126.                 'choice_label' =>
  127.                     function (OuterLayout $choice) {
  128.                         return $choice->getName();
  129.                     },
  130.                 'choices' => $outerLayouts,
  131.             ))
  132.             ->add('defaultInnerLayout'EntityType::class, array(
  133.                 'categoryName' => 'Theme',
  134.                 'class' => InnerLayout::class,
  135.                 'required' => false,
  136.                 'empty_data' => null,
  137.                 'placeholder' => '...',
  138.                 'choice_label' =>
  139.                     function (InnerLayout $choice) {
  140.                         return $choice->getName();
  141.                     },
  142.                 'choices' => $innerLayouts,
  143.             ))
  144.             ->add('moduleFlags'ContainerModuleFlagsType::class, array(
  145.                 'categoryName' => 'Active Modules',
  146.                 'embedded' => true,
  147.                 'required' => false,
  148.             ))
  149.             ->add('htmlOverrides'HtmlOverridesType::class, [])
  150.             ->add('schoolNowMigrated'SwitchType::class, array(
  151.                 'categoryName' => 'General',
  152.                 'label' => 'SchoolNow Migrated',
  153.                 'required' => false,
  154.             ))
  155.         ;
  156.     }
  157.     /**
  158.      * @param OptionsResolver $resolver
  159.      */
  160.     public function configureOptions(OptionsResolver $resolver)
  161.     {
  162.         $resolver->setDefaults(
  163.             array(
  164.                 'isRoot' => false
  165.             )
  166.         );
  167.     }
  168. }