src/Controller/DeveloperController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Annotation\CmsAdminDash;
  4. use App\Entity\Page;
  5. use App\Entity\Templates;
  6. use App\Form\TemplateType;
  7. use App\Service\ServiceController;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\Routing\RouterInterface;
  16. /**
  17.  * Developer controller.
  18.  *
  19.  * @Security("is_granted('ROLE_DEVELOPER')")
  20.  */
  21. #[Route(path'/takeflight')]
  22. class DeveloperController extends AbstractController
  23. {
  24.     public function __construct(private readonly ServiceController $serviceController, private readonly RouterInterface $router) {}
  25.     //
  26.     // DEVELOPER ONLY ACTIONS
  27.     //
  28.     /**
  29.      * This is a simple menu placeholder function.
  30.      *
  31.      * @CmsAdminDash("Developer", role="ROLE_DEVELOPER", active=true, routeName="control_developer_index", icon="fa fa-code", dontLink=true, menuPosition=9999999)
  32.      */
  33.     #[Route(path'/'name'control_developer_index'methods: ['GET'])]
  34.     public function developerIndex()
  35.     {
  36.         if ($this->getUser()) {
  37.             return $this->redirectToRoute('control_dash');
  38.         }
  39.         return new Response();
  40.     }
  41.     /**
  42.      * Lists all Pages with Components with assigned attributes.
  43.      *
  44.      * @CmsAdminDash("Component Info",
  45.      *     role="ROLE_DEVELOPER",
  46.      *     active=true,
  47.      *     routeName="control_developer_components_info",
  48.      *     icon="glyphicon glyphicon-list",
  49.      *     parentRouteName="control_developer_index"
  50.      * )
  51.      */
  52.     #[Route(path'/component-info'name'control_developer_components_info'methods: ['GET'])]
  53.     public function developerComponentInfo(EntityManagerInterface $em): Response
  54.     {
  55.         $data = [];
  56.         $componentData = [];
  57.         $cmsComponentArray $this->serviceController->fetchCmsComponents();
  58.         foreach ($cmsComponentArray as $cmsComponent) {
  59.             $componentData[$cmsComponent['route']] = $cmsComponent;
  60.         }
  61.         $pages $em->getRepository(Page::class)->findBy(['deleted' => false]);
  62.         // echo "<pre>".print_r($cmsComponentArray, true)."</pre>";
  63.         $assigned = [];
  64.         foreach ($pages as $page) {
  65.             $pageComps $page->getComponents();
  66.             if ((is_countable($pageComps) ? count($pageComps) : 0) > 0) {
  67.                 // echo "<pre>".print_r($pageComps, true)."</pre>";
  68.                 $assignComps = [];
  69.                 foreach ($pageComps as $pageComp) {
  70.                     if ((!empty($pageComp['route'])) || ('' != $pageComp['route'])) {
  71.                         if (!in_array($pageComp['route'], $assigned)) {
  72.                             $assigned[] = $pageComp['route'];
  73.                         }
  74.                         $action $this->routeToControllerName($pageComp['route']);
  75.                         $controllerFunction explode('::', (string) $action['_controller']);
  76.                         $assignComps[] = ['position' => $pageComp['position'], 'component' => $componentData[$pageComp['route']], 'action' => end($controllerFunction)];
  77.                     }
  78.                 }
  79.                 if (count($assignComps) > 0) {
  80.                     $data[] = ['page' => $page'components' => $assignComps];
  81.                 }
  82.             }
  83.         }
  84.         // echo "<pre>".print_r($cmsComponentArray, true)."</pre>";
  85.         // \Doctrine\Common\Util\Debug::dump($data);
  86.         $cmsComponentArrayExtra = [];
  87.         foreach ($cmsComponentArray as $cmsComponent) {
  88.             $controllerFunction '';
  89.             if ((!empty($cmsComponent['route'])) || ('' != $cmsComponent['route'])) {
  90.                 $action $this->routeToControllerName($cmsComponent['route']);
  91.                 $actionArray explode('::', (string) $action['_controller']);
  92.                 $controllerFunction end($actionArray);
  93.                 // echo "<pre>".print_r($action, true)."</pre>";
  94.             }
  95.             $inUse in_array($cmsComponent['route'], $assigned);
  96.             if ('segment' != $cmsComponent['componentType']) {
  97.                 $cmsComponentArrayExtra[] = [
  98.                     'name' => $cmsComponent['name'],
  99.                     'route' => $cmsComponent['route'],
  100.                     'componentType' => $cmsComponent['componentType'],
  101.                     'bundle' => $cmsComponent['bundle'],
  102.                     'action' => $controllerFunction,
  103.                     'inUse' => $inUse,
  104.                 ];
  105.             }
  106.         }
  107.         return $this->render('Developer/component-info.html.twig', [
  108.             'pagedata' => $data,
  109.             'cmsComponentArrayExtra' => $cmsComponentArrayExtra,
  110.         ]);
  111.     }
  112.     /**
  113.      * Lists all templates for pages.
  114.      *
  115.      * @CmsAdminDash("Templates",
  116.      *     role="ROLE_DEVELOPER",
  117.      *     active=true,
  118.      *     routeName="control_developer_templates_index",
  119.      *     icon="glyphicon glyphicon-list",
  120.      *     parentRouteName="control_developer_index"
  121.      * )
  122.      */
  123.     #[Route(path'/templates'name'control_developer_templates_index'methods: ['GET'])]
  124.     public function developerTemplatesIndex(EntityManagerInterface $em): Response
  125.     {
  126.         $templates $em->getRepository(Templates::class)->findBy(['deleted' => false]);
  127.         return $this->render('Developer/templates-index.html.twig', [
  128.             'templates' => $templates,
  129.         ]);
  130.     }
  131.     /**
  132.      * Creates a new Template entity.
  133.      *
  134.      * @return RedirectResponse|Response
  135.      */
  136.     #[Route(path'/templates/new'name'control_developer_templates_new'methods: ['GET''POST'])]
  137.     public function newTemplate(Request $requestEntityManagerInterface $em)
  138.     {
  139.         $template = new Templates();
  140.         $bundles $this->serviceController->fetchCmsBundles();
  141.         $form $this->createForm(TemplateType::class, $template, [
  142.             'bundles' => $bundles, ]);
  143.         $form->handleRequest($request);
  144.         if ($form->isSubmitted()) {
  145.             if ($form->isValid()) {
  146.                 $em->persist($template);
  147.                 $em->flush();
  148.                 $this->addFlash('success''Success - template created');
  149.                 return $this->redirectToRoute('control_developer_templates_index');
  150.             }
  151.             $this->addFlash('error''Error - news not saved');
  152.         }
  153.         return $this->render('Developer/templates-modify.html.twig', [
  154.             'template' => $template,
  155.             'form' => $form->createView(),
  156.         ]);
  157.     }
  158.     /**
  159.      * Edit Template entity.
  160.      *
  161.      * @return RedirectResponse|Response
  162.      */
  163.     #[Route(path'/templates/{id}/edit'name'control_developer_templates_edit'methods: ['GET''POST'])]
  164.     public function editTemplate(Request $requestEntityManagerInterface $emTemplates $template)
  165.     {
  166.         $bundles $this->serviceController->fetchCmsBundles();
  167.         $form $this->createForm(TemplateType::class, $template, [
  168.             'bundles' => $bundles, ]);
  169.         $form->handleRequest($request);
  170.         if ($form->isSubmitted()) {
  171.             if ($form->isValid()) {
  172.                 $em->persist($template);
  173.                 $em->flush();
  174.                 $this->addFlash('success''Success - template updated');
  175.                 return $this->redirectToRoute('control_developer_templates_index');
  176.             }
  177.             $this->addFlash('error''Error - news not saved');
  178.         }
  179.         return $this->render('Developer/templates-modify.html.twig', [
  180.             'template' => $template,
  181.             'form' => $form->createView(),
  182.         ]);
  183.     }
  184.     // Helper function
  185.     private function routeToControllerName($routename)
  186.     {
  187.         $routes $this->router->getRouteCollection();
  188.         return $routes->get($routename)->getDefaults();
  189.     }
  190. }