Completed
Push — master ( 4b984d...a3b5f9 )
by Laurent
03:33
created

SupplierController::showAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 14
rs 9.4285
cc 1
eloc 8
nc 1
nop 1
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 View Code Duplication
    public function indexAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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('delete.reassign_wrong', array(), 'gs_suppliers');
193
            $this->addFlash('danger', $message);
194
            return $this->redirectToRoute('articles_reassign', array('slug' => $supplier->getSlug()));
195
        }
196
        
197
        $form = $this->createDeleteForm($supplier->getId(), 'suppliers_delete');
198
        if ($form->handleRequest($request)->isValid()) {
199
            $em = $this->getDoctrine()->getManager();
200
            $supplier->setActive(false);
201
            $em->persist($supplier);
202
            $em->flush();
203
        }
204
205
        return $this->redirectToRoute('suppliers');
206
    }
207
}
208