AdvertController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\AdSearch;
6
use App\Entity\Advert;
7
use App\Entity\Category;
8
use App\Entity\Department;
9
use App\Form\AddAdvertType;
10
use App\Form\AdSearchType;
11
use App\Form\DepartmentType;
12
use App\Repository\AdvertRepository;
13
use Doctrine\Common\Persistence\ObjectManager;
14
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15
use Symfony\Component\HttpFoundation\File\Exception\FileException;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
use Symfony\Component\Routing\Annotation\Route;
19
use Knp\Component\Pager\PaginatorInterface;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
21
22
class AdvertController extends AbstractController
23
{
24
    /**
25
     * @var AdvertRepository
26
     */
27
    private $repository;
28
    /**
29
     * @var ObjectManager
30
     */
31
    private $em;
32
33
    public function __construct(AdvertRepository $repository, ObjectManager $em)
34
    {
35
36
        $this->repository = $repository;
37
        $this->em = $em;
38
    }
39
40
    /**
41
     * @Route("/adverts", name="adverts")
42
     */
43
    public function index(Request $request, PaginatorInterface $paginator)
44
    {
45
46
        $search = new AdSearch();
47
        $form = $this->createForm(AdSearchType::class, $search);
48
        $form->handleRequest($request);
49
50
        //dump($form->getData(), $search);
51
            $adverts = $paginator->paginate(
52
                $queryBuilder = $this->repository->findAll(),
53
                $request->query->getInt('page', 1)/*page number*/,
54
                10/*limit per page*/
55
            );
56
57
        if ($form->isSubmitted() && $form->isValid()) {
58
            if ($search->getDepartment()) {
59
                $department = $request->query->get('department');
60
                $adverts = $paginator->paginate(
61
                    $queryBuilder = $this->repository->findDepartment($department),
62
                    $request->query->getInt('page', 1)/*page number*/,
63
                    10/*limit per page*/
64
                );
65
            }
66
67
        }
68
69
70
        return $this->render('advert/index.html.twig', [
71
            'pagination' => $adverts,
72
            'formSearch' => $form->createView(),
73
        ]);
74
    }
75
76
    /**
77
     * @Route("/advert/add", name="add_advert")
78
     */
79
    public function add(Request $request, ObjectManager $manager)
80
    {
81
        $advert = new Advert();
82
        $form = $this->createForm(AddAdvertType::class, $advert);
83
        $form->handleRequest($request);
84
        if ($form->isSubmitted() && $form->isValid()) {
85
            $advert->setDate(new \DateTime());
86
            $advert->setAuthor($this->getUser());
87
88
            $manager->persist($advert);
89
            $manager->flush();
90
91
            $this->addFlash('success', 'Votre annonce a bien été ajoutée');
92
            return $this->redirectToRoute('advert', ['id' => $advert->getId()]);
93
        }
94
        return $this->render('advert/add.html.twig', [
95
            'formAddAdvert' => $form->createView(),
96
        ]);
97
    }
98
99
    /**
100
     * @Route("/advert/{id}", name="advert")
101
     */
102
    public function view($id)
103
    {
104
        $em = $this->getDoctrine()->getManager();
105
        $advert = $em->getRepository(Advert::class)->find($id);
106
        if (null == $advert) {
107
            throw new NotFoundHttpException("L'annonce d'id ".$id." n'existe pas");
108
        }
109
        return $this->render('advert/view.html.twig', [
110
            'advert' => $advert,
111
        ]);
112
    }
113
114
    /**
115
     * @Route("/advert/edit/{id}", name="edit_advert")
116
     *
117
     */
118
    public function edit(Advert $advert, Request $request, ObjectManager $manager)
119
    {
120
        //$advert = $this->getDoctrine()->getRepository(Advert::class)->find($id);
121
        if (null == $advert) {
122
            throw new NotFoundHttpException("Pas d\'annonce avec cet identifiant");
123
        }
124
        if ($advert->getAuthor() === $this->getUser()) {
125
            $form = $this->createForm(AddAdvertType::class, $advert);
126
            $form->handleRequest($request);
127
            if ($form->isSubmitted() && $form->isValid()) {
128
                $advert->setAuthor($this->getUser());
129
130
                $advert->setPublished(false);
131
132
                $categories = $advert->getCategories();
133
134
                // On boucle sur les catégories pour les lier à l'annonce
135
                foreach ($categories as $category) {
136
                    $advert->addCategory($category);
137
                }
138
139
                //$manager->persist($advert);
140
                $manager->flush();
141
                $this->addFlash(
142
                    'notice',
143
                    'L\'annonce a bien été modifiée! En attente de validation par l\'administrateur'
144
                );
145
                return $this->redirectToRoute('advert', ['id' => $advert->getId()]);
146
            }
147
            return $this->render('advert/edit.html.twig', [
148
                'advert' => $advert,
149
                'formEditAdvert' => $form->createView()
150
            ]);
151
        } else {
152
            $this->addFlash('warning', 'Vous devez être l\'auteur pour modifier cette annonce !');
153
            return $this->redirectToRoute('advert', ['id' => $advert->getId()]);
154
        }
155
    }
156
157
    /**
158
     * Supprimer une annonce
159
     *
160
     * @Route("/advert/delete/{id}", name="delete_advert")
161
     */
162
    public function deleteAction($id)
163
    {
164
        $em = $this->getDoctrine()->getManager();
165
        $advert = $em->getRepository(Advert::class)->find($id);
166
        if (null === $advert) {
167
            throw new NotFoundHttpException("L'annonce d'id ".$id." nexiste pas");
168
        }
169
        if ($advert->getAuthor() === $this->getUser()) {
170
            $em->remove($advert);
171
            $em->flush();
172
            $this->addFlash(
173
                'notice',
174
                'L\'annonce a bien été supprimée!'
175
            );
176
        } else {
177
            $this->addFlash('notice', 'Vous devez être l\'auteur pour supprimer cette annonce !');
178
            return $this->redirectToRoute('view', ['id' => $advert->getId()]);
179
        }
180
        return $this->redirectToRoute('home');
181
    }
182
}
183