src/Controller/AuthorsController.php line 141

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Authors;
  4. use App\Form\AuthorsType;
  5. use App\Service\EditionFunction;
  6. use App\Service\MetaTags\MetaTags;
  7. use App\Service\Path\Classes\PathEntity;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Knp\Component\Pager\PaginatorInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class AuthorsController extends AbstractController
  16. {
  17.     private $pathEntity;
  18.     private $metaTags;
  19.     private $editionFunction;
  20.     private $em;
  21.     public function __construct(PathEntity $pathEntityMetaTags $metaTagsEditionFunction $editionFunctionEntityManagerInterface $em)
  22.     {
  23.         $this->pathEntity $pathEntity;
  24.         $this->metaTags $metaTags;
  25.         $this->editionFunction $editionFunction;
  26.         $this->em $em;
  27.     }
  28.     /**
  29.      * @Route("/admin/ajax/authors/get/{name}", name="admin_ajax_get_authors", methods={"GET"})
  30.      */
  31.     public function ajax_get_authors($name)
  32.     {
  33.         $authors = [];
  34.         $result $this->em->getRepository(Authors::class)->createQueryBuilder('a')
  35.             ->select('a.id, a.name')
  36.             ->andWhere('a.name LIKE :name')
  37.             ->setParameter('name''%'.$name.'%')
  38.             ->orderBy('a.name''ASC')
  39.             ->getQuery()
  40.             ->getArrayResult();
  41.         if($result){
  42.             foreach ($result as $item){
  43.                 $authors[] = ['name' => $item['name'].' ['.$item['id'].']'];
  44.             }
  45.         }
  46.         return $this->json($authors?$authors:FALSE);
  47.     }
  48.     /**
  49.      * @Route("/authors", name="authors_index", methods={"GET"})
  50.      */
  51.     public function index(Request $requestPaginatorInterface $paginator)
  52.     {
  53.         $Authors $this->em->getRepository(Authors::class)->createQueryBuilder('a')->andWhere('a.status = 1')->orderBy('a.name''ASC');
  54.         $defaultData = array('name' => '');
  55.         $form $this->createFormBuilder($defaultData, ['csrf_protection' => false])
  56.             ->add('name'TextType::class, [
  57.                 'required' => false,
  58.             ])
  59.             ->setMethod('GET')
  60.             ->getForm();
  61.         if ($request->getMethod() == 'GET') {
  62.             $form->handleRequest($request);
  63.             $data $form->getData();
  64.         }
  65.         if(!empty($data['name'])){
  66.             $Authors->andWhere('a.name LIKE :name')
  67.                 ->setParameter('name''%'.$data['name'].'%');
  68.         }
  69.         $pagination $paginator->paginate(
  70.             $Authors->getQuery(), $request->query->getInt('page'1), 25);
  71.         return $this->render('authors/index.html.twig', [
  72.             'pagination' => $pagination,
  73.             'form' => $form->createView(),
  74.             'metaTags' => $this->metaTags->getAuthors(),
  75.         ]);
  76.     }
  77.     /**
  78.      * @Route("/admin/authors", name="admin_authors_index", methods={"GET"})
  79.      */
  80.     public function admin_index(Request $requestPaginatorInterface $paginator): Response
  81.     {
  82.         $Authors $this->em->getRepository(Authors::class)->createQueryBuilder('a')->orderBy('a.id''DESC');
  83.         $defaultData = array('name' => '');
  84.         $form $this->createFormBuilder($defaultData, ['csrf_protection' => false])
  85.             ->add('name'TextType::class, [
  86.                 'required' => false,
  87.                 'label' => 'ФИО',
  88.             ])
  89.             ->setMethod('GET')
  90.             ->getForm();
  91.         if ($request->getMethod() == 'GET') {
  92.             $form->handleRequest($request);
  93.             $data $form->getData();
  94.         }
  95.         if(!empty($data['name'])){
  96.             $Authors->andWhere('a.name LIKE :name')
  97.                 ->setParameter('name''%'.$data['name'].'%');
  98.         }
  99.         $pagination $paginator->paginate(
  100.             $Authors->getQuery(), $request->query->getInt('page'1), 25);
  101.         $pagination->setTemplate('@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig');
  102.         return $this->render('admin/authors/index.html.twig', [
  103.             'pagination' => $pagination,
  104.             'form' => $form->createView(),
  105.         ]);
  106.     }
  107.     /**
  108.      * @Route("/admin/authors/new", name="authors_new", methods={"GET","POST"})
  109.      */
  110.     public function new(Request $request): Response
  111.     {
  112.         $author = new Authors();
  113.         $form $this->createForm(AuthorsType::class, $author);
  114.         $form->handleRequest($request);
  115.         if ($form->isSubmitted() && $form->isValid()) {
  116.             $author->setCreated(time());
  117.             $author->setNid($this->em->getRepository(Authors::class)->GetNextNid());
  118.             $this->em->persist($author);
  119.             $this->em->flush();
  120.             $this->pathEntity->setPath($author);
  121.             return $this->redirectToRoute('admin_authors_index');
  122.         }
  123.         return $this->render('admin/authors/new.html.twig', [
  124.             'author' => $author,
  125.             'form' => $form->createView(),
  126.         ]);
  127.     }
  128.     public function show(Authors $author): Response
  129.     {
  130.         if($author->getStatus() || $this->isGranted('ROLE_ADMIN')) {
  131.             $this->metaTags->setMetaTagsAuthors($author);
  132.             foreach ($author->getArticleAuthors() as $articleAuthors) {
  133.                 $articleAuthors->getArticle()->setExitData($this->editionFunction->getExitDataArticles($articleAuthors->getArticle()));
  134.             }
  135.             foreach ($author->getArticleScidirectors() as $articleAuthors) {
  136.                 $articleAuthors->getArticle()->setExitData($this->editionFunction->getExitDataArticles($articleAuthors->getArticle()));
  137.             }
  138.             return $this->render('authors/show.html.twig', [
  139.                 'author' => $author,
  140.             ]);
  141.         }
  142.         throw $this->createNotFoundException('404');
  143.     }
  144.     /**
  145.      * @Route("/admin/authors/{id}/edit", name="authors_edit", methods={"GET","POST"})
  146.      */
  147.     public function edit(Request $requestAuthors $author): Response
  148.     {
  149.         $form $this->createForm(AuthorsType::class, $author);
  150.         $form->handleRequest($request);
  151.         if ($form->isSubmitted() && $form->isValid()) {
  152.             if(!$author->getNid()) $author->setNid($this->em->getRepository(Authors::class)->GetNextNid());
  153.             $this->em->flush();
  154.             $this->pathEntity->setPath($author);
  155.             return $this->redirectToRoute('authors_index');
  156.         }
  157.         return $this->render('admin/authors/edit.html.twig', [
  158.             'author' => $author,
  159.             'form' => $form->createView(),
  160.         ]);
  161.     }
  162.     /**
  163.      * @Route("/admin/authors/{id}", priority=11, name="authors_delete", methods={"DELETE"})
  164.      */
  165.     public function delete(Request $requestAuthors $author): Response
  166.     {
  167.         if ($this->isCsrfTokenValid('delete'.$author->getId(), $request->request->get('_token'))) {
  168.             $this->em->remove($author);
  169.             $this->em->flush();
  170.         }
  171.         return $this->redirectToRoute('authors_index');
  172.     }
  173. }