/**
  * @Route("/newPagePart", name="KunstmaanPagePartBundle_admin_newpagepart")
  * @Template("KunstmaanPagePartBundle:PagePartAdminTwigExtension:pagepart.html.twig")
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *
  * @return array
  */
 public function newPagePartAction(Request $request)
 {
     $em = $this->getDoctrine()->getManager();
     $pageId = $request->get('pageid');
     $pageClassName = $request->get('pageclassname');
     $context = $request->get('context');
     $pagePartClass = $request->get('type');
     $page = $em->getRepository($pageClassName)->findOneById($pageId);
     $pagePartConfigurationReader = new PagePartConfigurationReader($this->container->get('kernel'));
     $pagePartAdminConfigurators = $pagePartConfigurationReader->getPagePartAdminConfigurators($page);
     $pagePartAdminConfigurator = null;
     foreach ($pagePartAdminConfigurators as $ppac) {
         if ($context == $ppac->getContext()) {
             $pagePartAdminConfigurator = $ppac;
         }
     }
     if (is_null($pagePartAdminConfigurator)) {
         throw new \RuntimeException(sprintf('No page part admin configurator found for context "%s".', $context));
     }
     $pagePartAdmin = new PagePartAdmin($pagePartAdminConfigurator, $em, $page, $context, $this->container);
     $pagePart = new $pagePartClass();
     $formFactory = $this->container->get('form.factory');
     $formBuilder = $formFactory->createBuilder(FormType::class);
     $pagePartAdmin->adaptForm($formBuilder);
     $id = 'newpp_' . time();
     $data = $formBuilder->getData();
     $data['pagepartadmin_' . $id] = $pagePart;
     $adminType = $pagePart->getDefaultAdminType();
     $adminTypeFqn = ClassUtils::getClass($adminType);
     $formBuilder->add('pagepartadmin_' . $id, $adminTypeFqn);
     $formBuilder->setData($data);
     $form = $formBuilder->getForm();
     $formview = $form->createView();
     return array('id' => $id, 'form' => $formview, 'pagepart' => $pagePart, 'pagepartadmin' => $pagePartAdmin, 'editmode' => true);
 }
 /**
  * @param AdaptFormEvent $event
  */
 public function adaptForm(AdaptFormEvent $event)
 {
     $page = $event->getPage();
     $tabPane = $event->getTabPane();
     if ($page instanceof HasPageTemplateInterface) {
         $pageTemplateWidget = new PageTemplateWidget($page, $event->getRequest(), $this->em, $this->kernel, $this->formFactory, $this->pagePartAdminFactory);
         /* @var Tab $propertiesTab */
         $propertiesTab = $tabPane->getTabByTitle('Properties');
         if (!is_null($propertiesTab)) {
             $propertiesWidget = $propertiesTab->getWidget();
             $tabPane->removeTab($propertiesTab);
             $tabPane->addTab(new Tab("Content", new ListWidget(array($propertiesWidget, $pageTemplateWidget))), 0);
         } else {
             $tabPane->addTab(new Tab("Content", $pageTemplateWidget), 0);
         }
     } else {
         if ($page instanceof HasPagePartsInterface) {
             /* @var HasPagePartsInterface $page */
             $pagePartConfigurationReader = new PagePartConfigurationReader($this->kernel);
             $pagePartAdminConfigurators = $pagePartConfigurationReader->getPagePartAdminConfigurators($page);
             foreach ($pagePartAdminConfigurators as $index => $pagePartAdminConfiguration) {
                 $pagePartWidget = new PagePartWidget($page, $event->getRequest(), $this->em, $pagePartAdminConfiguration, $this->formFactory, $this->pagePartAdminFactory);
                 if ($index == 0) {
                     /* @var Tab $propertiesTab */
                     $propertiesTab = $tabPane->getTabByTitle('Properties');
                     if (!is_null($propertiesTab)) {
                         $propertiesWidget = $propertiesTab->getWidget();
                         $tabPane->removeTab($propertiesTab);
                         $tabPane->addTab(new Tab($pagePartAdminConfiguration->getName(), new ListWidget(array($propertiesWidget, $pagePartWidget))), 0);
                         continue;
                     }
                 }
                 $tabPane->addTab(new Tab($pagePartAdminConfiguration->getName(), $pagePartWidget), sizeof($tabPane->getTabs()));
             }
         }
     }
 }
 /**
  * @param HasPageTemplateInterface $page                 The page
  * @param Request                  $request              The request
  * @param EntityManager            $em                   The entity manager
  * @param KernelInterface          $kernel               The kernel
  * @param FormFactoryInterface     $formFactory          The form factory
  * @param PagePartAdminFactory     $pagePartAdminFactory The page part admin factory
  */
 public function __construct(HasPageTemplateInterface $page, Request $request, EntityManager $em, KernelInterface $kernel, FormFactoryInterface $formFactory, PagePartAdminFactory $pagePartAdminFactory)
 {
     parent::__construct();
     $this->page = $page;
     $this->em = $em;
     $this->request = $request;
     $this->formFactory = $formFactory;
     $this->pagePartAdminFactory = $pagePartAdminFactory;
     $pageTemplateConfigurationReader = new PageTemplateConfigurationReader($kernel);
     $this->pageTemplates = $pageTemplateConfigurationReader->getPageTemplates($page);
     $pagePartConfigurationReader = new PagePartConfigurationReader($kernel);
     $this->pagePartAdminConfigurations = $pagePartConfigurationReader->getPagePartAdminConfigurators($this->page);
     $repo = $this->em->getRepository('KunstmaanPagePartBundle:PageTemplateConfiguration');
     $repo->setContainer($kernel->getContainer());
     $this->pageTemplateConfiguration = $repo->findOrCreateFor($page);
     foreach ($this->getPageTemplate()->getRows() as $row) {
         foreach ($row->getRegions() as $region) {
             $this->processRegion($region);
         }
     }
 }
 /**
  * @param HasPageTemplateInterface $page                 The page
  * @param Request                  $request              The request
  * @param EntityManager            $em                   The entity manager
  * @param KernelInterface          $kernel               The kernel
  * @param FormFactoryInterface     $formFactory          The form factory
  * @param PagePartAdminFactory     $pagePartAdminFactory The page part admin factory
  */
 public function __construct(HasPageTemplateInterface $page, Request $request, EntityManager $em, KernelInterface $kernel, FormFactoryInterface $formFactory, PagePartAdminFactory $pagePartAdminFactory)
 {
     parent::__construct();
     $this->page = $page;
     $this->em = $em;
     $this->request = $request;
     $pageTemplateConfigurationReader = new PageTemplateConfigurationReader($kernel);
     $this->pageTemplates = $pageTemplateConfigurationReader->getPageTemplates($page);
     $pagePartConfigurationReader = new PagePartConfigurationReader($kernel);
     $this->pagePartAdminConfigurations = $pagePartConfigurationReader->getPagePartAdminConfigurators($this->page);
     $repo = $this->em->getRepository('KunstmaanPagePartBundle:PageTemplateConfiguration');
     $repo->setContainer($kernel->getContainer());
     $this->pageTemplateConfiguration = $repo->findOrCreateFor($page);
     foreach ($this->getPageTemplate()->getRows() as $row) {
         foreach ($row->getRegions() as $region) {
             $pagePartAdminConfiguration = null;
             foreach ($this->pagePartAdminConfigurations as $ppac) {
                 if ($ppac->getContext() == $region->getName()) {
                     $pagePartAdminConfiguration = $ppac;
                 }
             }
             if ($pagePartAdminConfiguration !== null) {
                 $pagePartWidget = new PagePartWidget($page, $this->request, $this->em, $pagePartAdminConfiguration, $formFactory, $pagePartAdminFactory);
                 $this->widgets[$region->getName()] = $pagePartWidget;
             }
         }
     }
 }