Completed
Push — Recipes ( 630f49...c8afb0 )
by Laurent
12:15 queued 03:48
created

SupplierController::editAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * DefaultController Controller of suppliers.
4
 *
5
 * PHP Version 7
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 GIT: <git_id>
12
 *
13
 * @link      https://github.com/Dev-Int/glsr
14
 */
15
namespace App\Controller\Settings;
16
17
use App\Controller\AbstractAppController;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Routing\Annotation\Route;
20
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use App\Entity\Settings\Supplier;
23
use App\Form\Type\Settings\SupplierType;
24
25
/**
26
 * Supplier controller.
27
 *
28
 * @category Controller
29
 *
30
 * @Route("/supplier")
31
 */
32
class SupplierController extends AbstractAppController
33
{
34
    /**
35
     * Lists all Supplier entities.
36
     *
37
     * @Route("/", name="supplier")
38
     * @Method("GET")
39
     * @Template()
40
     *
41
     * @param \Symfony\Component\HttpFoundation\Request $request Paginate|Sort request
42
     * @return array
43
     */
44
    public function indexAction(Request $request)
45
    {
46
        $return = $this->abstractIndexAction('Settings\Supplier', 'supplier', $request);
47
        
48
        return $return;
49
    }
50
51
    /**
52
     * Finds and displays a Supplier entity.
53
     *
54
     * @Route("/{slug}/show", name="supplier_show")
55
     * @Method("GET")
56
     * @Template()
57
     *
58
     * @param \App\Entity\Settings\Supplier $supplier Supplier item to display
59
     * @return array
60
     */
61
    public function showAction(Supplier $supplier)
62
    {
63
        $etm = $this->getDoctrine()->getManager();
64
        
65
        $deleteForm = $this->createDeleteForm($supplier->getId(), 'supplier_delete');
66
67
        // Récupérer les articles du fournisseur.
68
        $articles = $etm->getRepository('App:Settings\Article')->getArticleFromSupplier($supplier);
69
        return ['supplier' => $supplier, 'articles' => $articles, 'delete_form' => $deleteForm->createView(),];
70
    }
71
72
    /**
73
     * Displays a form to create a new Supplier entity.
74
     *
75
     * @Route("/admin/new", name="supplier_new")
76
     * @Method("GET")
77
     * @Template()
78
     *
79
     * @return array
80
     */
81
    public function newAction()
82
    {
83
        $return = $this->abstractNewAction(
84
            'Settings\Supplier',
85
            'App\Entity\Settings\Supplier',
86
            SupplierType::class,
87
            'supplier'
88
        );
89
90
        return $return;
91
    }
92
93
    /**
94
     * Creates a new Supplier entity.
95
     *
96
     * @Route("/admin/create", name="supplier_create")
97
     * @Method("POST")
98
     * @Template("settings/supplier/new.html.twig")
99
     *
100
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
101
     * @return array
102
     */
103
    public function createAction(Request $request)
104
    {
105
        $return = $this->abstractCreateAction(
106
            $request,
107
            'Settings\Supplier',
108
            'App\Entity\Settings\Supplier',
109
            SupplierType::class,
110
            'supplier'
111
        );
112
113
        return $return;
114
    }
115
116
    /**
117
     * Displays a form to edit an existing Supplier entity.
118
     *
119
     * @Route("/admin/{slug}/edit", name="supplier_edit")
120
     * @Method("GET")
121
     * @Template()
122
     *
123
     * @param \App\Entity\Settings\Supplier $supplier Supplier item to edit
124
     * @return array
125
     */
126
    public function editAction(Supplier $supplier)
127
    {
128
        $return = $this->abstractEditAction(
129
            $supplier,
130
            'supplier',
131
            SupplierType::class
132
        );
133
134
        return $return;
135
    }
136
137
    /**
138
     * Edits an existing Supplier entity.
139
     *
140
     * @Route("/{slug}/update", name="supplier_update")
141
     * @Method("PUT")
142
     * @Template("settings/supplier/edit.html.twig")
143
     *
144
     * @param \App\Entity\Settings\Supplier                $supplier Supplier item to update
145
     * @param \Symfony\Component\HttpFoundation\Request $request  Form request
146
     * @return array
147
     */
148
    public function updateAction(Supplier $supplier, Request $request)
149
    {
150
        $return = $this->abstractUpdateAction(
151
            $supplier,
152
            $request,
153
            'supplier',
154
            SupplierType::class
155
        );
156
157
        return $return;
158
    }
159
160
161
    /**
162
     * Save order.
163
     *
164
     * @Route("/order/{entity}/{field}/{type}", name="supplier_sort")
165
     *
166
     * @param string $entity Entity of the field to sort
167
     * @param string $field  Field to sort
168
     * @param string $type   type of sort
169
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
170
     */
171
    public function sortAction($entity, $field, $type)
172
    {
173
        $this->get('app.helper.controller')->setOrder('article', $entity, $field, $type);
174
175
        return $this->redirectToRoute('supplier');
176
    }
177
178
    /**
179
     * Deletes a Supplier entity.
180
     *
181
     * @Route("/admin/{id}/delete", name="supplier_delete", requirements={"id"="\d+"})
182
     * @Method("DELETE")
183
     *
184
     * @param \App\Entity\Settings\Supplier             $supplier Supplier item to delete
185
     * @param \Symfony\Component\HttpFoundation\Request $request  Form request
186
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
187
     */
188
    public function deleteAction(Supplier $supplier, Request $request)
189
    {
190
        $etm = $this->getDoctrine()->getManager();
191
        // Test if there is no articles with this supplier.
192
        $articles = $etm->getRepository('App:Article')->getArticleFromSupplier($supplier);
193
        $this->abstractDeleteAction($supplier, $request, 'supplier');
194
195
        $return = $this->redirectToRoute('supplier');
196
197
        if (!empty($articles)) {
198
            $message = $this->get('translator')
199
                ->trans('delete.reassign_wrong', [], 'gs_suppliers');
200
            $this->addFlash('danger', $message);
201
            $return = $this->redirectToRoute('article_reassign', ['slug' => $supplier->getSlug()]);
202
        }
203
204
        return $return;
205
    }
206
}
207