LegumeController.php 5.23 KB
Newer Older
Mat's avatar
Mat committed
1 2 3 4
<?php

namespace PotageBundle\Controller;

Mat's avatar
Mat committed
5
use APIBundle\Form\LegumeAPIType;
6
use APIBundle\Form\OffreLegumesInsertAPIType;
Mat's avatar
Mat committed
7
use Symfony\Component\HttpFoundation\Request;
Mat's avatar
Mat committed
8 9 10
use PotageBundle\Entity\Legume;
use PotageBundle\Form\Legume\LegumeType;

Mat's avatar
Mat committed
11

Mat's avatar
Mat committed
12
class LegumeController extends MasterController
Mat's avatar
Mat committed
13 14 15
{

    /**
16 17 18
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
Mat's avatar
Mat committed
19 20
    public function createAction(Request $request)
    {
Mat's avatar
Mat committed
21 22 23 24 25 26 27 28 29 30
        $legume = new Legume();

        $form = $this->createForm(LegumeType::class, $legume);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($legume);
            $em->flush();
31
            $this->addFlash('success', "Le nouveau légume a bien été ajouté");
Mat's avatar
Mat committed
32

Mat's avatar
Mat committed
33
            return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
Mat's avatar
Mat committed
34 35 36 37 38
        }

        return $this->render('@Potage/Legume/create.html.twig', array(
            'formLegume' => $form->createView()
        ));
Mat's avatar
Mat committed
39
    }
40
    
Mat's avatar
Mat committed
41
    /**
42 43 44
     * @param $page
     * @return \Symfony\Component\HttpFoundation\Response
     */
Mat's avatar
Mat committed
45
    public function readAction($page)
Mat's avatar
Mat committed
46
    {
Mat's avatar
Mat committed
47 48 49
        if ($page < 1) {
            throw $this->createNotFoundException("Page ".$page." inexistante");
        }
Mat's avatar
hop  
Mat committed
50
        $nbPerPage = 10;
Mat's avatar
Mat committed
51 52

        $em = $this->getDoctrine()->getManager();
53
        $legumes = $em->getRepository('PotageBundle:Legume')->findAllForRead($page, $nbPerPage);
Mat's avatar
Mat committed
54 55 56 57 58

        $nbPages = ceil(count($legumes)/$nbPerPage);
        if ($page > $nbPerPage) {
            throw $this->createNotFoundException("Page ".$page." inexistante");
        }
Mat's avatar
Mat committed
59

Mat's avatar
Mat committed
60
        return $this->render('@Potage/Legume/read.html.twig', array(
Mat's avatar
Mat committed
61 62 63
            'legumes' => $legumes,
            'nbPages' => $nbPages,
            'page' => $page
Mat's avatar
Mat committed
64
        ));
Mat's avatar
Mat committed
65 66 67
    }

    /**
68 69 70 71 72
     * @param Request $request
     * @param $id
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
Mat's avatar
Mat committed
73 74
    public function updateAction(Request $request, $id)
    {
Mat's avatar
Mat committed
75
        $em = $this->getDoctrine()->getManager();
76
        $legume = $em->getRepository('PotageBundle:Legume')->findOneForUpdate($id);
Mat's avatar
Mat committed
77 78 79 80 81 82 83 84 85 86 87 88

        if ($legume === null) {
            throw $this->createNotFoundException();
        }

        $form = $this->createForm(LegumeType::class, $legume);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid())
        {
            $em->persist($legume);
            $em->flush();
89
            $this->addFlash('success', "Le légume a bien été modifié");
Mat's avatar
Mat committed
90

Mat's avatar
Mat committed
91
            return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
Mat's avatar
Mat committed
92 93 94 95 96 97
        }

        return $this->render('@Potage/Legume/update.html.twig', array(
            'id' => $id,
            'formLegume' => $form->createView()
        ));
Mat's avatar
Mat committed
98 99 100
    }

    /**
101 102 103 104
     * @param $id
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
Mat's avatar
Mat committed
105 106
    public function deleteAction($id)
    {
Mat's avatar
Mat committed
107
        $em = $this->getDoctrine()->getManager();
108
        $legume = $em->getRepository('PotageBundle:Legume')->findOneForDelete($id);
109 110 111 112 113 114
    
        if ($legume === null)
        {
            throw $this->createNotFoundException('Non trouvé');
        
        } else
Mat's avatar
Mat committed
115
        {
Mat's avatar
Mat committed
116 117
            $em->remove($legume);
            $em->flush();
118
            $this->addFlash('success', "Le légume a bien été supprimé");
Mat's avatar
Mat committed
119

Mat's avatar
Mat committed
120
            return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
Mat's avatar
Mat committed
121 122
        }

Mat's avatar
Mat committed
123 124
    }

Mat's avatar
Mat committed
125
    /**
126 127 128 129
     * @param $id
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
Mat's avatar
Mat committed
130 131 132
    public function duplicateAction($id)
    {
        $em = $this->getDoctrine()->getManager();
133
        $legume = $em->getRepository('PotageBundle:Legume')->findOneForDuplicate($id);
Mat's avatar
Mat committed
134

135 136 137 138 139
        if ($legume === null)
        {
            throw $this->createNotFoundException('Non trouvé');
            
        } else
Mat's avatar
Mat committed
140 141 142 143 144
        {
            $newRow = clone $legume;
            $em->detach($newRow);
            $em->persist($newRow);
            $em->flush();
145
            $this->addFlash('success', "Le légume a bien été dupliqué");
Mat's avatar
Mat committed
146 147 148 149

            return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
        }
    }
150 151


Mat's avatar
Mat committed
152 153
    /**
     * @return \Symfony\Component\HttpFoundation\Response
154 155
     * @throws \Doctrine\ORM\NoResultException
     * @throws \Doctrine\ORM\NonUniqueResultException
Mat's avatar
Mat committed
156
     */
157
    public function ajaxDisplayAction()
Mat's avatar
Mat committed
158
    {
159
        $nbRows = $this->getDoctrine()->getManager()
160
            ->getRepository('PotageBundle:Legume')->countRowsForAjaxDisplay();
161
            // TODO trouver une meilleure façon d'insérer le nbre total de rangées
162

Mat's avatar
Mat committed
163
        $form = $this->createForm(LegumeAPIType::class);
164 165
        $formInsert = $this->createForm(OffreLegumesInsertAPIType::class);
        
166
        return $this->render('@Potage/Legume/ajaxDisplay.html.twig', array(
167
            'nbRows' => $nbRows,
168 169
            'formLegume' => $form->createView(),
            'formOffreLegumesInsert' => $formInsert->createView()
Mat's avatar
Mat committed
170 171
        ));
    }
Mat's avatar
Mat committed
172
}