Completed
Push — master ( a907e7...1839ce )
by Laurent
03:34
created

SupplierController::deleteAction()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 4
Metric Value
c 6
b 1
f 4
dl 0
loc 26
rs 8.8571
cc 3
eloc 18
nc 3
nop 2
1
<?php
2
/**
3
 * DefaultController controller des fournisseurs.
4
 *
5
 * PHP Version 5
6
 *
7
 * @author    Quétier Laurent <[email protected]>
8
 * @copyright 2014 Dev-Int GLSR
9
 * @license   http://opensource.org/licenses/gpl-license.php GNU Public License
10
 *
11
 * @version   since 1.0.0
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace AppBundle\Controller;
16
17
use Symfony\Component\HttpFoundation\Request;
18
use AppBundle\Controller\AbstractController;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use AppBundle\Entity\Supplier;
23
use AppBundle\Form\Type\SupplierType;
24
25
/**
26
 * Supplier controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/suppliers")
31
 */
32
class SupplierController extends AbstractController
33
{
34
    /**
35
     * Lists all Supplier entities.
36
     *
37
     * @Route("/", name="suppliers")
38
     * @Method("GET")
39
     * @Template()
40
     */
41
    public function indexAction(Request $request)
42
    {
43
        $em = $this->getDoctrine()->getManager();
44
        $qb = $em->getRepository('AppBundle:Supplier')->getSuppliers();
45
        $this->addQueryBuilderSort($qb, 'supplier');
46
        $paginator = $this->get('knp_paginator')->paginate($qb, $request->query->get('page', 1), 20);
47
        
48
        return array(
49
            'paginator' => $paginator,
50
        );
51
    }
52
53
    /**
54
     * Finds and displays a Supplier entity.
55
     *
56
     * @Route("/{slug}/show", name="suppliers_show")
57
     * @Method("GET")
58
     * @Template()
59
     */
60
    public function showAction(Supplier $supplier)
61
    {
62
        $em = $this->getDoctrine()->getManager();
63
        
64
        $deleteForm = $this->createDeleteForm($supplier->getId(), 'suppliers_delete');
65
66
        // Récupérer les articles du fournisseur.
67
        $articles = $em->getRepository('AppBundle:Article')->getArticleFromSupplier($supplier);
68
        return array(
69
            'supplier' => $supplier,
70
            'articles' => $articles,
71
            'delete_form' => $deleteForm->createView(),
72
        );
73
    }
74
75
    /**
76
     * Displays a form to create a new Supplier entity.
77
     *
78
     * @Route("/admin/new", name="suppliers_new")
79
     * @Method("GET")
80
     * @Template()
81
     */
82
    public function newAction()
83
    {
84
        $supplier = new Supplier();
85
        $form = $this->createForm(new SupplierType(), $supplier);
86
87
        return array(
88
            'supplier' => $supplier,
89
            'form'   => $form->createView(),
90
        );
91
    }
92
93
    /**
94
     * Creates a new Supplier entity.
95
     *
96
     * @Route("/create", name="suppliers_create")
97
     * @Method("POST")
98
     * @Template("AppBundle:Supplier:new.html.twig")
99
     */
100
    public function createAction(Request $request)
101
    {
102
        $supplier = new Supplier();
103
        $form = $this->createForm(new SupplierType(), $supplier);
104
        if ($form->handleRequest($request)->isValid()) {
105
            $em = $this->getDoctrine()->getManager();
106
            $em->persist($supplier);
107
            $em->flush();
108
109
            return $this->redirectToRoute('suppliers_show', array('slug' => $supplier->getSlug()));
110
        }
111
112
        return array(
113
            'supplier' => $supplier,
114
            'form'   => $form->createView(),
115
        );
116
    }
117
118
    /**
119
     * Displays a form to edit an existing Supplier entity.
120
     *
121
     * @Route("/admin/{slug}/edit", name="suppliers_edit")
122
     * @Method("GET")
123
     * @Template()
124
     */
125
    public function editAction(Supplier $supplier)
126
    {
127
        $editForm = $this->createForm(new SupplierType(), $supplier, array(
128
            'action' => $this->generateUrl('suppliers_update', array('slug' => $supplier->getSlug())),
129
            'method' => 'PUT',
130
        ));
131
        $deleteForm = $this->createDeleteForm($supplier->getId(), 'suppliers_delete');
132
133
        return array(
134
            'supplier' => $supplier,
135
            'edit_form'   => $editForm->createView(),
136
            'delete_form' => $deleteForm->createView(),
137
        );
138
    }
139
140
    /**
141
     * Edits an existing Supplier entity.
142
     *
143
     * @Route("/{slug}/update", name="suppliers_update")
144
     * @Method("PUT")
145
     * @Template("AppBundle:Supplier:edit.html.twig")
146
     */
147
    public function updateAction(Supplier $supplier, Request $request)
148
    {
149
        $editForm = $this->createForm(new SupplierType(), $supplier, array(
150
            'action' => $this->generateUrl('suppliers_update', array('slug' => $supplier->getSlug())),
151
            'method' => 'PUT',
152
        ));
153
        if ($editForm->handleRequest($request)->isValid()) {
154
            $this->getDoctrine()->getManager()->flush();
155
            $this->addFlash('info', 'gestock.edit.ok');
156
        }
157
        $deleteForm = $this->createDeleteForm($supplier->getId(), 'suppliers_delete');
158
159
        return array(
160
            'supplier' => $supplier,
161
            'edit_form'   => $editForm->createView(),
162
            'delete_form' => $deleteForm->createView(),
163
        );
164
    }
165
166
167
    /**
168
     * Save order.
169
     *
170
     * @Route("/order/{field}/{type}", name="suppliers_sort")
171
     */
172
    public function sortAction($field, $type)
173
    {
174
        $this->setOrder('supplier', $field, $type);
175
176
        return $this->redirectToRoute('suppliers');
177
    }
178
179
    /**
180
     * Deletes a Supplier entity.
181
     *
182
     * @Route("/admin/{id}/delete", name="suppliers_delete", requirements={"id"="\d+"})
183
     * @Method("DELETE")
184
     */
185
    public function deleteAction(Supplier $supplier, Request $request)
186
    {
187
        $em = $this->getDoctrine()->getManager();
188
        // Test if there is no articles with this supplier.
189
        $articles = $em->getRepository('AppBundle:Article')->getArticleFromSupplier($supplier);
190
        if (!empty($articles)) {
191
            $message = $this->get('translator')
192
                ->trans(
193
                    'delete.reassign_wrong',
194
                    array(),
195
                    'gs_suppliers'
196
                );
197
            $this->addFlash('danger', $message);
198
            return $this->redirectToRoute('articles_reassign', array('slug' => $supplier->getSlug()));
199
        }
200
        
201
        $form = $this->createDeleteForm($supplier->getId(), 'suppliers_delete');
202
        if ($form->handleRequest($request)->isValid()) {
203
            $em = $this->getDoctrine()->getManager();
204
            $supplier->setActive(false);
205
            $em->persist($supplier);
206
            $em->flush();
207
        }
208
209
        return $this->redirectToRoute('suppliers');
210
    }
211
}
212