Completed
Push — master ( 3f9c93...d3d201 )
by Laurent
04:34
created

ArticleController::reassignAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 2 Features 1
Metric Value
c 6
b 2
f 1
dl 0
loc 24
rs 8.9713
cc 1
eloc 16
nc 1
nop 1
1
<?php
2
/**
3
 * ArticleController controller des articles.
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\Article;
23
use AppBundle\Entity\Supplier;
24
use AppBundle\Form\Type\ArticleReassignType;
25
26
/**
27
 * Article controller.
28
 *
29
 * @category Controller
30
 *
31
 * @Route("/article")
32
 */
33
class ArticleController extends AbstractController
34
{
35
    /**
36
     * Lists all Article entities.
37
     *
38
     * @Route("/", name="article")
39
     * @Method("GET")
40
     * @Template()
41
     *
42
     * @param \Symfony\Component\HttpFoundation\Request $request Paginate request
43
     * @return array
44
     */
45
    public function indexAction(Request $request)
46
    {
47
        $etm = $this->getDoctrine()->getManager();
48
        $qbd = $etm->getRepository('AppBundle:Article')->getArticles();
49
        $this->addQueryBuilderSort($qbd, 'article');
50
        $paginator = $this->get('knp_paginator')->paginate($qbd, $request->query->get('page', 1), 5);
51
        
52
        return array(
53
            'paginator' => $paginator,
54
        );
55
    }
56
57
    /**
58
     * Finds and displays a Article entity.
59
     *
60
     * @Route("/{slug}/show", name="article_show")
61
     * @Method("GET")
62
     * @Template()
63
     *
64
     * @param \AppBundle\Entity\Article $article Article item to display
65
     * @return array
66
     */
67
    public function showAction(Article $article)
68
    {
69
        $return = $this->abstractShowAction($article, 'article');
70
71
        return $return;
72
    }
73
74
    /**
75
     * Displays a form to create a new Article entity.
76
     *
77
     * @Route("/admin/new", name="article_new")
78
     * @Method("GET")
79
     * @Template()
80
     *
81
     * @return array
82
     */
83
    public function newAction()
84
    {
85
        $return = $this->abstractNewAction(
86
            'Article',
87
            'AppBundle\Entity\Article',
88
            'AppBundle\Form\Type\ArticleType'
89
        );
90
91
        return $return;
92
    }
93
94
    /**
95
     * Creates a new Article entity.
96
     *
97
     * @Route("/create", name="article_create")
98
     * @Method("POST")
99
     * @Template("AppBundle:Article:new.html.twig")
100
     *
101
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
102
     * @return array
103
     */
104
    public function createAction(Request $request)
105
    {
106
        $return = $this->abstractCreateAction(
107
            $request,
108
            'Article',
109
            'AppBundle\Entity\Article',
110
            'AppBundle\Form\Type\ArticleType'
111
        );
112
113
        return $return;
114
    }
115
116
    /**
117
     * Displays a form to edit an existing Article entity.
118
     *
119
     * @Route("/admin/{slug}/edit", name="article_edit")
120
     * @Method("GET")
121
     * @Template()
122
     *
123
     * @param \AppBundle\Entity\Article $article Article item to edit
124
     * @return array
125
     */
126
    public function editAction(Article $article)
127
    {
128
        $return = $this->abstractEditAction($article, 'article', 'AppBundle\Form\Type\ArticleType');
129
130
        return $return;
131
    }
132
133
    /**
134
     * Edits an existing Article entity.
135
     *
136
     * @Route("/{slug}/update", name="article_update")
137
     * @Method("PUT")
138
     * @Template("AppBundle:Article:edit.html.twig")
139
     *
140
     * @param \AppBundle\Entity\Article                 $article Article item to update
141
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
142
     * @return array
143
     */
144
    public function updateAction(Article $article, Request $request)
145
    {
146
        $return = $this->abstractUpdateAction(
147
            $article,
148
            $request,
149
            'article',
150
            'AppBundle\Form\Type\ArticleType'
151
        );
152
153
        return $return;
154
    }
155
156
    /**
157
     * Réassigner les articles d'un fournisseur.
158
     *
159
     * @param Supplier $supplier Fournisseur à réassigner
160
     * @Route("/{slug}/reassign", name="article_reassign")
161
     * @Method("GET")
162
     * @Template()
163
     * 
164
     * @param \AppBundle\Entity\Supplier $supplier Supplier articles to reassign
165
     * @return array
166
     */
167
    public function reassignAction(Supplier $supplier)
168
    {
169
        $etm = $this->getDoctrine()->getManager();
170
        $suppliers = $etm->getRepository('AppBundle:Supplier')->getSupplierForReassign($supplier);
171
        $articles = $etm->getRepository('AppBundle:Article')
172
            ->getArticleFromSupplier($supplier->getId());
173
174
        $reassignForm = $this->createForm(
175
            new ArticleReassignType(),
176
            $articles,
177
            array(
178
                'action' => $this->generateUrl(
179
                    'article_change',
180
                    array('slug' => $supplier->getSlug())
181
                ),
182
            )
183
        );
184
185
        return array(
186
            'reassign_form' => $reassignForm->createView(),
187
            'suppliers' => $suppliers,
188
            'articles' => $articles
189
        );
190
    }
191
192
    /**
193
     * Creates a new Article entity.
194
     *
195
     * @Route("/{slug}/change", name="article_change")
196
     * @Method("POST")
197
     * @Template("AppBundle:Article:reassign.html.twig")
198
     *
199
     * @param \Symfony\Component\HttpFoundation\Request $request  Form request
200
     * @param \AppBundle\Entity\Supplier                $supplier Supplier to desactivate
201
     * @return array
202
     */
203
    public function changeAction(Request $request, Supplier $supplier)
204
    {
205
        $etm = $this->getDoctrine()->getManager();
206
        $articles = $etm->getRepository('AppBundle:Article')->getArticleFromSupplier($supplier->getId());
207
208
        $reassignForm = $this->createForm(new ArticleReassignType(), $articles, array(
209
            'action' => $this->generateUrl('article_change', array('slug' => $supplier->getSlug())),
210
        ));
211
        $datas = $reassignForm->handleRequest($request);
212
213
        foreach ($datas as $data) {
214
            $input = explode('-', $data->getName());
215
            list($inputName, $articleId) = $input;
216
            $inputData = $data->getViewData();
217
            if ($inputName === 'supplier') {
218
                $newArticles = $etm->getRepository('AppBundle:Article')->find($articleId);
219
                $newSupplier = $etm->getRepository('AppBundle:Supplier')->find($inputData);
220
                //On modifie le fournisseur de l'article
221
                $newArticles->setSupplier($newSupplier);
222
                // On enregistre l'objet $article dans la base de données
223
                $etm->persist($newArticles);
224
            }
225
        }
226
            $etm->flush();
227
            $message = $this->get('translator')
228
                ->trans('delete.reassign_ok', array('%supplier.name%' => $supplier->getName()), 'gs_suppliers');
229
            $this->addFlash('info', $message);
230
            return $this->redirectToRoute('supplier');
231
    }
232
233
    /**
234
     * Save order.
235
     *
236
     * @Route("/order/{entity}/{field}/{type}", name="article_sort")
237
     * 
238
     * @param string $entity Entity of the field to sort
239
     * @param string $field  Field to sort
240
     * @param string $type   type of sort
241
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
242
     */
243
    public function sortAction($entity, $field, $type)
244
    {
245
        $this->setOrder('article', $entity, $field, $type);
246
247
        return $this->redirect($this->generateUrl('article'));
248
    }
249
    /**
250
     * Deletes a Article entity.
251
     *
252
     * @Route("/admin/{id}/delete", name="article_delete", requirements={"id"="\d+"})
253
     * @Method("DELETE")
254
     *
255
     * @param \AppBundle\Entity\Article                 $article Article item to delete
256
     * @param \Symfony\Component\HttpFoundation\Request $request Form request
257
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
258
     */
259
    public function deleteAction(Article $article, Request $request)
260
    {
261
        $form = $this->createDeleteForm($article->getId(), 'article_delete');
262
        if ($form->handleRequest($request)->isValid()) {
263
            $etm = $this->getDoctrine()->getManager();
264
            $article->setActive(false);
265
            $etm->persist($article);
266
            $etm->flush();
267
        }
268
269
        return $this->redirectToRoute('article');
270
    }
271
}
272