<?php
namespace App\Controller;
use App\Annotation\CmsAdminDash;
use App\Entity\Page;
use App\Entity\Templates;
use App\Form\TemplateType;
use App\Service\ServiceController;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
/**
* Developer controller.
*
* @Security("is_granted('ROLE_DEVELOPER')")
*/
#[Route(path: '/takeflight')]
class DeveloperController extends AbstractController
{
public function __construct(private readonly ServiceController $serviceController, private readonly RouterInterface $router) {}
//
// DEVELOPER ONLY ACTIONS
//
/**
* This is a simple menu placeholder function.
*
* @CmsAdminDash("Developer", role="ROLE_DEVELOPER", active=true, routeName="control_developer_index", icon="fa fa-code", dontLink=true, menuPosition=9999999)
*/
#[Route(path: '/', name: 'control_developer_index', methods: ['GET'])]
public function developerIndex()
{
if ($this->getUser()) {
return $this->redirectToRoute('control_dash');
}
return new Response();
}
/**
* Lists all Pages with Components with assigned attributes.
*
* @CmsAdminDash("Component Info",
* role="ROLE_DEVELOPER",
* active=true,
* routeName="control_developer_components_info",
* icon="glyphicon glyphicon-list",
* parentRouteName="control_developer_index"
* )
*/
#[Route(path: '/component-info', name: 'control_developer_components_info', methods: ['GET'])]
public function developerComponentInfo(EntityManagerInterface $em): Response
{
$data = [];
$componentData = [];
$cmsComponentArray = $this->serviceController->fetchCmsComponents();
foreach ($cmsComponentArray as $cmsComponent) {
$componentData[$cmsComponent['route']] = $cmsComponent;
}
$pages = $em->getRepository(Page::class)->findBy(['deleted' => false]);
// echo "<pre>".print_r($cmsComponentArray, true)."</pre>";
$assigned = [];
foreach ($pages as $page) {
$pageComps = $page->getComponents();
if ((is_countable($pageComps) ? count($pageComps) : 0) > 0) {
// echo "<pre>".print_r($pageComps, true)."</pre>";
$assignComps = [];
foreach ($pageComps as $pageComp) {
if ((!empty($pageComp['route'])) || ('' != $pageComp['route'])) {
if (!in_array($pageComp['route'], $assigned)) {
$assigned[] = $pageComp['route'];
}
$action = $this->routeToControllerName($pageComp['route']);
$controllerFunction = explode('::', (string) $action['_controller']);
$assignComps[] = ['position' => $pageComp['position'], 'component' => $componentData[$pageComp['route']], 'action' => end($controllerFunction)];
}
}
if (count($assignComps) > 0) {
$data[] = ['page' => $page, 'components' => $assignComps];
}
}
}
// echo "<pre>".print_r($cmsComponentArray, true)."</pre>";
// \Doctrine\Common\Util\Debug::dump($data);
$cmsComponentArrayExtra = [];
foreach ($cmsComponentArray as $cmsComponent) {
$controllerFunction = '';
if ((!empty($cmsComponent['route'])) || ('' != $cmsComponent['route'])) {
$action = $this->routeToControllerName($cmsComponent['route']);
$actionArray = explode('::', (string) $action['_controller']);
$controllerFunction = end($actionArray);
// echo "<pre>".print_r($action, true)."</pre>";
}
$inUse = in_array($cmsComponent['route'], $assigned);
if ('segment' != $cmsComponent['componentType']) {
$cmsComponentArrayExtra[] = [
'name' => $cmsComponent['name'],
'route' => $cmsComponent['route'],
'componentType' => $cmsComponent['componentType'],
'bundle' => $cmsComponent['bundle'],
'action' => $controllerFunction,
'inUse' => $inUse,
];
}
}
return $this->render('Developer/component-info.html.twig', [
'pagedata' => $data,
'cmsComponentArrayExtra' => $cmsComponentArrayExtra,
]);
}
/**
* Lists all templates for pages.
*
* @CmsAdminDash("Templates",
* role="ROLE_DEVELOPER",
* active=true,
* routeName="control_developer_templates_index",
* icon="glyphicon glyphicon-list",
* parentRouteName="control_developer_index"
* )
*/
#[Route(path: '/templates', name: 'control_developer_templates_index', methods: ['GET'])]
public function developerTemplatesIndex(EntityManagerInterface $em): Response
{
$templates = $em->getRepository(Templates::class)->findBy(['deleted' => false]);
return $this->render('Developer/templates-index.html.twig', [
'templates' => $templates,
]);
}
/**
* Creates a new Template entity.
*
* @return RedirectResponse|Response
*/
#[Route(path: '/templates/new', name: 'control_developer_templates_new', methods: ['GET', 'POST'])]
public function newTemplate(Request $request, EntityManagerInterface $em)
{
$template = new Templates();
$bundles = $this->serviceController->fetchCmsBundles();
$form = $this->createForm(TemplateType::class, $template, [
'bundles' => $bundles, ]);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em->persist($template);
$em->flush();
$this->addFlash('success', 'Success - template created');
return $this->redirectToRoute('control_developer_templates_index');
}
$this->addFlash('error', 'Error - news not saved');
}
return $this->render('Developer/templates-modify.html.twig', [
'template' => $template,
'form' => $form->createView(),
]);
}
/**
* Edit Template entity.
*
* @return RedirectResponse|Response
*/
#[Route(path: '/templates/{id}/edit', name: 'control_developer_templates_edit', methods: ['GET', 'POST'])]
public function editTemplate(Request $request, EntityManagerInterface $em, Templates $template)
{
$bundles = $this->serviceController->fetchCmsBundles();
$form = $this->createForm(TemplateType::class, $template, [
'bundles' => $bundles, ]);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em->persist($template);
$em->flush();
$this->addFlash('success', 'Success - template updated');
return $this->redirectToRoute('control_developer_templates_index');
}
$this->addFlash('error', 'Error - news not saved');
}
return $this->render('Developer/templates-modify.html.twig', [
'template' => $template,
'form' => $form->createView(),
]);
}
// Helper function
private function routeToControllerName($routename)
{
$routes = $this->router->getRouteCollection();
return $routes->get($routename)->getDefaults();
}
}