src/Controller/PrimaryController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\SitemapService;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. /**
  13.  * Class PrimaryController
  14.  * @package App\Controller
  15.  *
  16.  */
  17. class PrimaryController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/", name="app_main")
  21.      * @Route("/{url}", name="app_pages", requirements={"url" = "^(?!api|action|manual).*\/$"})
  22.      */
  23.     public function admin(): Response
  24.     {
  25.         return $this->render('index.html.twig', [
  26.             'application_name' => $_ENV['APP_NAME'],
  27.         ]);
  28.     }
  29.     /**
  30.      * Get documentation for authorized user only
  31.      * @Route("/manual", name="manual")
  32.      * 
  33.      * @return RedirectResponse 
  34.      */
  35.     public function manual(Request $request): RedirectResponse
  36.     {
  37.         $user $this->getUser();
  38.         $download filter_var($request->query->get('download'), FILTER_VALIDATE_BOOLEAN);
  39.         $subject 'Руководство использования.pdf';
  40.         $root $request->server->get('DOCUMENT_ROOT');
  41.         if ($user) {
  42.             $filename "manual.pdf";
  43.             $directory dirname($root).'/assets/doc';
  44.             if ($download) {
  45.                 $response = new Response();
  46.                 $response->headers->set('Content-type''application/octet-stream');
  47.                 $response->headers->set('Content-Disposition'sprintf('attachment; filename="%s"'$subject));
  48.                 $response->setContent(file_get_contents($directory.'/'.$filename));
  49.                 $response->setStatusCode(200);
  50.                 $response->headers->set('Content-Transfer-Encoding''binary');
  51.                 $response->headers->set('Pragma''no-cache');
  52.                 $response->headers->set('Expires''0');
  53.                 return $response;
  54.             }
  55.             header('Content-type: application/pdf');
  56.             header(sprintf('Content-Disposition: inline; filename="%s"'$subject));
  57.             header('Content-Length: ' filesize($directory.'/'.$filename));
  58.             @readfile($directory.'/'.$filename);
  59.             exit;
  60.         }
  61.         return $this->redirectToRoute('app_admin');
  62.     }
  63.     /**
  64.      * SEARCH ACTION: SET 
  65.      * @Route("/action/search",  methods={"get"})
  66.      * 
  67.      * @return RedirectResponse
  68.      */
  69.     public function search(Request $request): RedirectResponse
  70.     {
  71.         $session = new Session();
  72.         //$session->start();
  73.         
  74.         $query $request->query->get('query');
  75.         $route $request->query->get('route');
  76.         $page strtok($route':');
  77.         if ($query) {
  78.             $session->set('search', [
  79.                 'query' => $query,
  80.                 'page' =>  $page 
  81.             ]);
  82.         }
  83.         return $this->redirect('/admin/'.$page);
  84.     }
  85.     /**
  86.      * SEARCH ACTION: GET
  87.      * @Route("/action/search/get", methods={"get"})
  88.      * 
  89.      * @return JsonResponse 
  90.      */
  91.     public function getSearch(Session $session): JsonResponse {
  92.         return new JsonResponse(
  93.             $session->get('search') ?? array(), 
  94.             Response::HTTP_OK
  95.         );
  96.     }
  97.     /**
  98.      * SEARCH/SORT ACTION: DELETE
  99.      * @Route("/action/{prop<search|sorted>}/del", methods={"get"})
  100.      * 
  101.      * @return RedirectResponse
  102.      */
  103.     public function removeSessionByProp(Request $requestSession $sessionString $prop): RedirectResponse {
  104.         $session->remove($prop);
  105.         return $this->redirect($request->headers->get('referer'));
  106.     }
  107.     /**
  108.      * SORT ACTION: SET
  109.      * @Route("/action/sorted",  methods={"get"})
  110.      * 
  111.      * @return RedirectResponse
  112.      */
  113.     public function sorted(Request $requestSession $session): Response
  114.     {
  115.         $session->start();
  116.         $field $request->query->get('field');
  117.         $page $request->query->get('page');
  118.         $sort 'ASC';
  119.         if ($field) { 
  120.             $sorted $session->get('sorted');
  121.             $count $sorted['count'] ?? 1;
  122.             if ($sorted && $sorted['field'] === $field) {
  123.                 $sort $sorted['sort'] !== 'ASC' 'ASC' 'DESC';
  124.                 $count++;
  125.             }
  126.             $params = ['count''sort''field''page'];
  127.             foreach($params as $param) {
  128.                 $sorted[$param] = $$param;
  129.             }
  130.             $session->set('sorted'$sorted);
  131.         }
  132.         if ($sorted['count'] > 2) {
  133.             $session->remove('sorted');
  134.         }
  135.         $referer $request->headers->get('referer');
  136.         return $this->redirect($referer);
  137.     }
  138.     /**
  139.      * @Route("/action/reindexing-sitemap",  methods={"get"})
  140.      * 
  141.      * @return RedirectResponse
  142.      */
  143.     public function reindexingSitemap(Request $requestSitemapService $sitemapServiceManagerRegistry $registry) {
  144.         $pages $request->query->get('page') ?? array(); 
  145.         $pages is_string($pages) ? [$pages] : $pages;
  146.         $result = [
  147.             'success' => false
  148.             'message' => 'The value of some query parameters is undefined'
  149.             'errors' => []
  150.         ];
  151.       
  152.    
  153.         foreach ($pages as $page) {
  154.             $repository null;
  155.             $entityClassName 'App\Entity\\' ucfirst($page);
  156.             
  157.             if (class_exists($entityClassName)) {
  158.                 $repository $registry->getRepository($entityClassName);
  159.             }
  160.     
  161.             if ($repository != null) {
  162.                 $sitemapService->clearSitemap($page);
  163.                 $items $repository->findAll();
  164.         
  165.                 foreach($items as $item) {
  166.                     $link method_exists($item'getLink') ? $item->getLink() : $item->getId();
  167.                     $sitemapService->updateSitemap($page$linknull);
  168.                 }
  169.                 $result = [
  170.                     'success' => true
  171.                     'message' => 'Links in the sitemap were successfully indexed',
  172.                     'errors' => []
  173.                 ];
  174.             }
  175.         }
  176.         $this->addFlash('notice'$result);
  177.         $referer $request->headers->get('referer') ?? '/admin';
  178.         return $this->redirect($referer);
  179.     }
  180.     /**
  181.      * @Route("/action/{sel<modification|productOem>}/clear", methods={"post"})
  182.      */
  183.     public function clear(Request $requestManagerRegistry $registry): RedirectResponse 
  184.     {
  185.         $req $request->request->all();
  186.         $conn $registry->getManager()->getConnection();
  187.         $query '';
  188.   
  189.         try {
  190.             foreach($req as $key => $item) {
  191.                 if (filter_var($itemFILTER_VALIDATE_BOOLEAN)) {
  192.                     $query .= 'TRUNCATE TABLE '.strtolower(preg_replace('/(?<!^)[A-Z]/''_$0'$key)).'; ';
  193.                 }
  194.             }
  195.             if (!!$query) {
  196.                 $conn->executeStatement('SET FOREIGN_KEY_CHECKS=0');
  197.                 $conn->executeStatement($query);
  198.                 $conn->executeStatement('SET FOREIGN_KEY_CHECKS=1');
  199.             }
  200.         } catch(\Throwable $th) {
  201.             $errors $th->getMessage();
  202.             throw new \Exception($errors1);
  203.         }
  204.         $referer $request->headers->get('referer');
  205.         return $this->redirect($referer);
  206.    }
  207. }