Deprecated: Symfony\Component\Translation\t(): Implicitly marking parameter $domain as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/translation/Resources/functions.php on line 18

Deprecated: Symfony\Component\Dotenv\Dotenv::loadEnv(): Implicitly marking parameter $envKey as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/dotenv/Dotenv.php on line 110

Deprecated: Symfony\Component\Runtime\GenericRuntime::getResolver(): Implicitly marking parameter $reflector as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/runtime/GenericRuntime.php on line 89

Deprecated: Symfony\Component\Runtime\RuntimeInterface::getResolver(): Implicitly marking parameter $reflector as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/runtime/RuntimeInterface.php on line 26

Deprecated: Symfony\Component\Console\Input\ArgvInput::__construct(): Implicitly marking parameter $argv as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/console/Input/ArgvInput.php on line 46

Deprecated: Symfony\Component\Console\Input\ArgvInput::__construct(): Implicitly marking parameter $definition as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/console/Input/ArgvInput.php on line 46

Deprecated: Symfony\Component\Console\Input\Input::__construct(): Implicitly marking parameter $definition as nullable is deprecated, the explicit nullable type must be used instead in /var/www/html/t/taurushr/vendor/symfony/console/Input/Input.php on line 36

Deprecated: Constant E_STRICT is deprecated in /var/www/html/t/taurushr/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /var/www/html/t/taurushr/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

src/Controller/ResourceController.php line 146

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Resource;
  4. use App\Form\ResourceType;
  5. use App\Annotation\CmsAdminDash;
  6. use App\Annotation\CmsComponent;
  7. use App\Entity\ResourceCategory;
  8. use App\Repository\ResourceRepository;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Knp\Component\Pager\PaginatorInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. #[Route('/takeflight/resource')]
  16. class ResourceController extends AbstractController
  17. {
  18.     /**
  19.      * @CmsAdminDash("Resources", active=true, routeName="app_resource_index",icon="fa fa-file-text-o", menuPosition=10, parentRouteName="control_resource_category_index")
  20.      */
  21.     #[Route('/'name'app_resource_index'methods: ['GET'])]
  22.     public function index(ResourceRepository $resourceRepository): Response
  23.     {
  24.         return $this->render('resource/index.html.twig', [
  25.             'resources' => $resourceRepository->findBy(['deleted' => false]),
  26.         ]);
  27.     }
  28.     #[Route('/new'name'app_resource_new'methods: ['GET''POST'])]
  29.     public function new(Request $request): Response
  30.     {
  31.         $resource = new Resource();
  32.         $form $this->createForm(ResourceType::class, $resource);
  33.         $form->handleRequest($request);
  34.         if ($form->isSubmitted() && $form->isValid()) {
  35.             // REQUIRED IF USING UPLOAD FILE TRAIT
  36.             $resource->uploadFile();
  37.             $entityManager $this->getDoctrine()->getManager();
  38.             $entityManager->persist($resource);
  39.             $entityManager->flush();
  40.             return $this->redirectToRoute('app_resource_index', [], Response::HTTP_SEE_OTHER);
  41.         }
  42.         return $this->render('resource/new.html.twig', [
  43.             'resource' => $resource,
  44.             'form' => $form->createView(),
  45.         ]);
  46.     }
  47.     #[Route('/{id}'name'app_resource_show'methods: ['GET'])]
  48.     public function show(Resource $resource): Response
  49.     {
  50.         return $this->render('resource/show.html.twig', [
  51.             'resource' => $resource,
  52.         ]);
  53.     }
  54.     #[Route('/{id}/edit'name'app_resource_edit'methods: ['GET''POST'])]
  55.     public function edit(Request $requestResource $resource): Response
  56.     {
  57.         $form $this->createForm(ResourceType::class, $resource);
  58.         $form->handleRequest($request);
  59.         if ($form->isSubmitted() && $form->isValid()) {
  60.             // REQUIRED IF USING UPLOAD FILE TRAIT
  61.             $resource->uploadFile();
  62.             $this->getDoctrine()->getManager()->flush();
  63.             return $this->redirectToRoute('app_resource_index', [], Response::HTTP_SEE_OTHER);
  64.         }
  65.         return $this->render('resource/edit.html.twig', [
  66.             'resource' => $resource,
  67.             'form' => $form->createView(),
  68.         ]);
  69.     }
  70.     #[Route('/{id}'name'app_resource_delete'methods: ['POST'])]
  71.     public function delete(Request $requestResource $resource): Response
  72.     {
  73.         if ($this->isCsrfTokenValid('delete'.$resource->getId(), $request->request->get('_token'))) {
  74.             $entityManager $this->getDoctrine()->getManager();
  75.             $resource->setActive(false);
  76.             $resource->setDeleted(true);
  77.             $entityManager->persist($resource);
  78.             $entityManager->flush();
  79.             $this->addFlash('success''Success - Resource deleted');
  80.         }
  81.         return $this->redirectToRoute('app_resource_index', [], Response::HTTP_SEE_OTHER);
  82.     }
  83.     /**
  84.      * @CmsComponent("Embed Resources", active=true, routeName="embed_resources")
  85.      */
  86.     #[Route(path'/pcgc-resources'name'embed_resources')]
  87.     public function embedResources(Request $requestEntityManagerInterface $emPaginatorInterface $paginator): Response
  88.     {
  89.         $category null;
  90.         $perpage 12;
  91.         $query $em->createQueryBuilder('e')
  92.             ->select('e')
  93.             ->from(Resource::class, 'e')
  94.             ->where('e.deleted = 0')
  95.             ->andWhere('e.active = 1')
  96.             ->orderBy('e.sortOrder''ASC');
  97.         $categoryIds $this->sanitiseCategoryIds($request);
  98.         if ($categoryIds) {
  99.             $query
  100.                 ->andWhere('e.category IN (:categories)')
  101.                 ->setParameter('categories'$categoryIds);
  102.         }
  103.         $searchParam $request->query->get('search'null);
  104.         if ($searchParam !== null) {
  105.             $query
  106.                 ->andWhere('e.title LIKE :search OR e.content LIKE :search OR e.file LIKE :search')
  107.                 ->setParameter('search''%'.$searchParam.'%')
  108.             ;
  109.         }
  110.         $paginatedResources $paginator->paginate($query$request->query->getInt('page'1), $perpage);
  111.         return $this->render('@theme/resource/index.html.twig', [
  112.             'resources' => $paginatedResources,
  113.             'category' => $category,
  114.             'categoryIds' => $categoryIds,
  115.             'searchParam' => $searchParam,
  116.         ]);
  117.     }
  118.     /**
  119.      * @CmsComponent("Embed Resources Filters", active=true, routeName="embed_resources_filters")
  120.      */
  121.     #[Route(path'/pcgc-resources-filters'name'embed_resources_filters')]
  122.     public function embedResourceFilters(Request $requestEntityManagerInterface $em): Response
  123.     {
  124.         $categoryIds $this->sanitiseCategoryIds($request);
  125.         $searchParam $request->query->get('search'null);
  126.         $categories $em->getRepository(ResourceCategory::class)->findBy(['deleted' => false'active' => true], ['sortOrder' => 'ASC']);
  127.         return $this->render('@theme/resource/filters.html.twig', [
  128.             'categories' => $categories,
  129.             'categoryIds' => $categoryIds,
  130.             'searchParam' => $searchParam,
  131.         ]);
  132.     }
  133.     private function sanitiseCategoryIds(Request $request): array
  134.     {
  135.         $categoryIds $request->query->get('category', []);
  136.         // check if categoryIds is an array and only contains numeric values
  137.         if (!is_array($categoryIds) || array_filter($categoryIds'is_numeric') !== $categoryIds) {
  138.             $categoryIds = [];
  139.         }
  140.         return $categoryIds;
  141.     }
  142. }