<?php
namespace App\Controller;
use App\Entity\Authors;
use App\Form\AuthorsType;
use App\Service\EditionFunction;
use App\Service\MetaTags\MetaTags;
use App\Service\Path\Classes\PathEntity;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class AuthorsController extends AbstractController
{
private $pathEntity;
private $metaTags;
private $editionFunction;
private $em;
public function __construct(PathEntity $pathEntity, MetaTags $metaTags, EditionFunction $editionFunction, EntityManagerInterface $em)
{
$this->pathEntity = $pathEntity;
$this->metaTags = $metaTags;
$this->editionFunction = $editionFunction;
$this->em = $em;
}
/**
* @Route("/admin/ajax/authors/get/{name}", name="admin_ajax_get_authors", methods={"GET"})
*/
public function ajax_get_authors($name)
{
$authors = [];
$result = $this->em->getRepository(Authors::class)->createQueryBuilder('a')
->select('a.id, a.name')
->andWhere('a.name LIKE :name')
->setParameter('name', '%'.$name.'%')
->orderBy('a.name', 'ASC')
->getQuery()
->getArrayResult();
if($result){
foreach ($result as $item){
$authors[] = ['name' => $item['name'].' ['.$item['id'].']'];
}
}
return $this->json($authors?$authors:FALSE);
}
/**
* @Route("/authors", name="authors_index", methods={"GET"})
*/
public function index(Request $request, PaginatorInterface $paginator)
{
$Authors = $this->em->getRepository(Authors::class)->createQueryBuilder('a')->andWhere('a.status = 1')->orderBy('a.name', 'ASC');
$defaultData = array('name' => '');
$form = $this->createFormBuilder($defaultData, ['csrf_protection' => false])
->add('name', TextType::class, [
'required' => false,
])
->setMethod('GET')
->getForm();
if ($request->getMethod() == 'GET') {
$form->handleRequest($request);
$data = $form->getData();
}
if(!empty($data['name'])){
$Authors->andWhere('a.name LIKE :name')
->setParameter('name', '%'.$data['name'].'%');
}
$pagination = $paginator->paginate(
$Authors->getQuery(), $request->query->getInt('page', 1), 25);
return $this->render('authors/index.html.twig', [
'pagination' => $pagination,
'form' => $form->createView(),
'metaTags' => $this->metaTags->getAuthors(),
]);
}
/**
* @Route("/admin/authors", name="admin_authors_index", methods={"GET"})
*/
public function admin_index(Request $request, PaginatorInterface $paginator): Response
{
$Authors = $this->em->getRepository(Authors::class)->createQueryBuilder('a')->orderBy('a.id', 'DESC');
$defaultData = array('name' => '');
$form = $this->createFormBuilder($defaultData, ['csrf_protection' => false])
->add('name', TextType::class, [
'required' => false,
'label' => 'ФИО',
])
->setMethod('GET')
->getForm();
if ($request->getMethod() == 'GET') {
$form->handleRequest($request);
$data = $form->getData();
}
if(!empty($data['name'])){
$Authors->andWhere('a.name LIKE :name')
->setParameter('name', '%'.$data['name'].'%');
}
$pagination = $paginator->paginate(
$Authors->getQuery(), $request->query->getInt('page', 1), 25);
$pagination->setTemplate('@KnpPaginator/Pagination/twitter_bootstrap_v4_pagination.html.twig');
return $this->render('admin/authors/index.html.twig', [
'pagination' => $pagination,
'form' => $form->createView(),
]);
}
/**
* @Route("/admin/authors/new", name="authors_new", methods={"GET","POST"})
*/
public function new(Request $request): Response
{
$author = new Authors();
$form = $this->createForm(AuthorsType::class, $author);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$author->setCreated(time());
$author->setNid($this->em->getRepository(Authors::class)->GetNextNid());
$this->em->persist($author);
$this->em->flush();
$this->pathEntity->setPath($author);
return $this->redirectToRoute('admin_authors_index');
}
return $this->render('admin/authors/new.html.twig', [
'author' => $author,
'form' => $form->createView(),
]);
}
public function show(Authors $author): Response
{
if($author->getStatus() || $this->isGranted('ROLE_ADMIN')) {
$this->metaTags->setMetaTagsAuthors($author);
foreach ($author->getArticleAuthors() as $articleAuthors) {
$articleAuthors->getArticle()->setExitData($this->editionFunction->getExitDataArticles($articleAuthors->getArticle()));
}
foreach ($author->getArticleScidirectors() as $articleAuthors) {
$articleAuthors->getArticle()->setExitData($this->editionFunction->getExitDataArticles($articleAuthors->getArticle()));
}
return $this->render('authors/show.html.twig', [
'author' => $author,
]);
}
throw $this->createNotFoundException('404');
}
/**
* @Route("/admin/authors/{id}/edit", name="authors_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Authors $author): Response
{
$form = $this->createForm(AuthorsType::class, $author);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if(!$author->getNid()) $author->setNid($this->em->getRepository(Authors::class)->GetNextNid());
$this->em->flush();
$this->pathEntity->setPath($author);
return $this->redirectToRoute('authors_index');
}
return $this->render('admin/authors/edit.html.twig', [
'author' => $author,
'form' => $form->createView(),
]);
}
/**
* @Route("/admin/authors/{id}", priority=11, name="authors_delete", methods={"DELETE"})
*/
public function delete(Request $request, Authors $author): Response
{
if ($this->isCsrfTokenValid('delete'.$author->getId(), $request->request->get('_token'))) {
$this->em->remove($author);
$this->em->flush();
}
return $this->redirectToRoute('authors_index');
}
}