176 lines
5.5 KiB
PHP
176 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Entity\Blog as Entity;
|
|
use App\Form\BlogType as Form;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
class BlogController extends AbstractController
|
|
{
|
|
private $data = 'blog';
|
|
private $route = 'app_blog';
|
|
private $render = 'Blog/';
|
|
private $entity = "App\Entity\Blog";
|
|
|
|
public function list(ManagerRegistry $em)
|
|
{
|
|
// Récupérer les blogs
|
|
$datas = $em->getRepository($this->entity)->findAll();
|
|
|
|
return $this->render($this->render.'list.html.twig', [
|
|
$this->data.'s' => $datas,
|
|
'useheader' => true,
|
|
'usesidebar' => true,
|
|
]);
|
|
}
|
|
|
|
public function submit(Request $request, ManagerRegistry $em)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = new Entity();
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(Form::class, $data, ['mode' => 'submit']);
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur erreur
|
|
$this->getErrorForm(null, $form, $request, $data, 'submit');
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
|
|
$em->getManager()->persist($data);
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la liste
|
|
return $this->redirectToRoute('app_child', ['catparent' => 'blog', 'idparent' => $data->getId()]);
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->render.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usesidebar' => true,
|
|
$this->data => $data,
|
|
'mode' => 'submit',
|
|
'form' => $form->createView(),
|
|
'from' => 'blog',
|
|
]);
|
|
}
|
|
|
|
public function update($id, Request $request, ManagerRegistry $em)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
$from = $request->get('from');
|
|
|
|
// Création du formulaire
|
|
$form = $this->createForm(Form::class, $data, ['mode' => 'update']);
|
|
|
|
// Récupération des data du formulaire
|
|
$form->handleRequest($request);
|
|
|
|
// Sur erreur
|
|
$this->getErrorForm(null, $form, $request, $data, 'update');
|
|
|
|
// Sur validation
|
|
if ($form->get('submit')->isClicked() && $form->isValid()) {
|
|
$data = $form->getData();
|
|
$em->getManager()->flush();
|
|
|
|
// Retour à la liste
|
|
if ('child' == $from) {
|
|
return $this->redirectToRoute('app_child', ['catparent' => 'blog', 'idparent' => $data->getId()]);
|
|
} else {
|
|
return $this->redirectToRoute($this->route);
|
|
}
|
|
}
|
|
|
|
// Affichage du formulaire
|
|
return $this->render($this->render.'edit.html.twig', [
|
|
'useheader' => true,
|
|
'usesidebar' => true,
|
|
$this->data => $data,
|
|
'mode' => 'update',
|
|
'form' => $form->createView(),
|
|
'from' => $from,
|
|
]);
|
|
}
|
|
|
|
public function delete($id, Request $request, ManagerRegistry $em)
|
|
{
|
|
// Initialisation de l'enregistrement
|
|
$data = $em->getRepository($this->entity)->find($id);
|
|
|
|
try {
|
|
$em->getManager()->remove($data);
|
|
$em->getManager()->flush();
|
|
} catch (\Exception $e) {
|
|
$request->getSession()->getFlashBag()->add('error', $e->getMessage());
|
|
|
|
return $this->redirectToRoute($this->route.'_update', ['id' => $id]);
|
|
}
|
|
|
|
return $this->redirectToRoute($this->route);
|
|
}
|
|
|
|
public function home(ManagerRegistry $em)
|
|
{
|
|
$heros = $em->getRepository("App\Entity\Child")->findHeads('blog');
|
|
$blogs = $em->getRepository($this->entity)->findBy([], ['submitdate' => 'DESC']);
|
|
|
|
return $this->render($this->render.'home.html.twig', [
|
|
'useheader' => true,
|
|
'usesidebar' => false,
|
|
'heros' => $heros,
|
|
'blogs' => $blogs,
|
|
]);
|
|
}
|
|
|
|
public function select(Request $request, ManagerRegistry $em)
|
|
{
|
|
$output = [];
|
|
$page_limit = $request->query->get('page_limit');
|
|
$q = $request->query->get('q');
|
|
|
|
$qb = $em->getManager()->createQueryBuilder();
|
|
$qb->select('table')->from($this->entity, 'table')
|
|
->where('table.name LIKE :value')
|
|
->setParameter('value', '%'.$q.'%')
|
|
->orderBy('table.name');
|
|
|
|
$datas = $qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult();
|
|
foreach ($datas as $data) {
|
|
array_push($output, ['id' => $data->getId(), 'text' => $data->getName()]);
|
|
}
|
|
|
|
$ret_string['results'] = $output;
|
|
|
|
return new JsonResponse($ret_string);
|
|
}
|
|
|
|
protected function getErrorForm($id, $form, $request, $data, $mode)
|
|
{
|
|
if ($form->get('submit')->isClicked() && 'delete' == $mode) {
|
|
}
|
|
|
|
if ($form->get('submit')->isClicked() && ('submit' == $mode || 'update' == $mode)) {
|
|
}
|
|
|
|
if ($form->get('submit')->isClicked() && !$form->isValid()) {
|
|
$this->get('session')->getFlashBag()->clear();
|
|
|
|
$errors = $form->getErrors();
|
|
foreach ($errors as $error) {
|
|
$request->getSession()->getFlashBag()->add('error', $error->getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|