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

namespace PotageBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
Mat's avatar
Mat committed
7
8
9
use PotageBundle\Entity\Legume;
use PotageBundle\Form\Legume\LegumeType;

Mat's avatar
Mat committed
10
11
12
13
14
15
16
17
18
19
20

class LegumeController extends Controller
{

    /**
    *   @param Request
    *   @return \Symfony\Component\HttpFoundation\Response
    *
    */
    public function createAction(Request $request)
    {
Mat's avatar
Mat committed
21
22
23
24
25
26
27
28
29
30
31
        $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();

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

        return $this->render('@Potage/Legume/create.html.twig', array(
            'formLegume' => $form->createView()
        ));
Mat's avatar
Mat committed
38
39
40
41
42
43
44
    }

    /**
    *
    *   @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
Mat committed
50
        $nbPerPage = 15;
Mat's avatar
Mat committed
51
52
53
54
55
56
57
58

        $em = $this->getDoctrine()->getManager();
        $legumes = $em->getRepository('PotageBundle:Legume')->findAllByRead($page, $nbPerPage);

        $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
73
    }

    /**
    *   @param Request
    *   @param $id
    *   @return \Symfony\Component\HttpFoundation\Response
    */
    public function updateAction(Request $request, $id)
    {
Mat's avatar
Mat committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        $em = $this->getDoctrine()->getManager();
        $legume = $em->getRepository('PotageBundle:Legume')->findOneByUpdate($id);

        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();

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

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

    /**
    *   @param $id
    *   @return \Symfony\Component\HttpFoundation\Response
    *
    */
    public function deleteAction($id)
    {
Mat's avatar
Mat committed
105
106
107
        $em = $this->getDoctrine()->getManager();
        $legume = $em->getRepository('PotageBundle:Legume')->findOneByDelete($id);

Mat's avatar
Mat committed
108
109
        if ($legume !== null)
        {
Mat's avatar
Mat committed
110
111
112
            $em->remove($legume);
            $em->flush();

Mat's avatar
Mat committed
113
            return $this->redirectToRoute('potage_legume_read', array('page'=> 1));
Mat's avatar
Mat committed
114
115
        }

Mat's avatar
Mat committed
116
117
    }

Mat's avatar
Mat committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    /**
    *   @param $id
    *   @return \Symfony\Component\HttpFoundation\Response
    *
    */
    public function duplicateAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $legume = $em->getRepository('PotageBundle:Legume')->findOneByDuplicate($id);

        if ($legume !== null)
        {
            $newRow = clone $legume;
            $em->detach($newRow);
            $em->persist($newRow);
            $em->flush();

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