Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

Controller/PagePartAdminController.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\PagePartBundle\Controller;
4
5
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
6
use Kunstmaan\PagePartBundle\Helper\PagePartInterface;
7
use Kunstmaan\PagePartBundle\PagePartAdmin\PagePartAdmin;
8
use Symfony\Component\Routing\Annotation\Route;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\Form\Extension\Core\Type\FormType;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * Controller for the pagepart administration
16
 */
17
class PagePartAdminController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
18
{
19
    /**
20
     * @Route("/newPagePart", name="KunstmaanPagePartBundle_admin_newpagepart")
21
     * @Template("@KunstmaanPagePart/PagePartAdminTwigExtension/pagepart.html.twig")
22
     *
23
     * @param \Symfony\Component\HttpFoundation\Request $request
24
     *
25
     * @return array
26
     */
27
    public function newPagePartAction(Request $request)
28
    {
29
        $em = $this->get('doctrine.orm.entity_manager');
30
31
        $pageId = $request->get('pageid');
32
        $pageClassName = $request->get('pageclassname');
33
        $context = $request->get('context');
34
        $pagePartClass = $request->get('type');
35
36
        /** @var HasPagePartsInterface $page */
37
        $page = $em->getRepository($pageClassName)->find($pageId);
38
39
        if (false === $page instanceof HasPagePartsInterface) {
40
            throw new \RuntimeException(sprintf('Given page (%s:%d) has no pageparts', $pageClassName, $pageId));
41
        }
42
43
        $pagePartConfigurationReader = $this->container->get('kunstmaan_page_part.page_part_configuration_reader');
44
        $pagePartAdminConfigurators = $pagePartConfigurationReader->getPagePartAdminConfigurators($page);
45
46
        $pagePartAdminConfigurator = null;
47
        foreach ($pagePartAdminConfigurators as $ppac) {
48
            if ($context == $ppac->getContext()) {
49
                $pagePartAdminConfigurator = $ppac;
50
            }
51
        }
52
53
        if (\is_null($pagePartAdminConfigurator)) {
54
            throw new \RuntimeException(sprintf('No page part admin configurator found for context "%s".', $context));
55
        }
56
57
        $pagePartAdmin = new PagePartAdmin($pagePartAdminConfigurator, $em, $page, $context, $this->container);
58
        /** @var PagePartInterface $pagePart */
59
        $pagePart = new $pagePartClass();
60
61
        if (false === $pagePart instanceof PagePartInterface) {
62
            throw new \RuntimeException(sprintf('Given pagepart expected to implement PagePartInterface, %s given', $pagePartClass));
63
        }
64
65
        $formFactory = $this->container->get('form.factory');
66
        $formBuilder = $formFactory->createBuilder(FormType::class);
67
        $pagePartAdmin->adaptForm($formBuilder);
68
        $id = 'newpp_' . time();
69
70
        $data = $formBuilder->getData();
71
        $data['pagepartadmin_' . $id] = $pagePart;
72
73
        $formBuilder->add('pagepartadmin_' . $id, $pagePart->getDefaultAdminType());
74
        $formBuilder->setData($data);
75
        $form = $formBuilder->getForm();
76
        $formview = $form->createView();
77
        $extended = $this->getParameter('kunstmaan_page_part.extended');
78
79
        return [
80
            'id' => $id,
81
            'form' => $formview,
82
            'pagepart' => $pagePart,
83
            'pagepartadmin' => $pagePartAdmin,
84
            'page' => $pagePartAdmin->getPage(),
85
            'editmode' => true,
86
            'extended' => $extended,
87
        ];
88
    }
89
}
90