Completed
Push — master ( d3d201...be7133 )
by Laurent
22:22
created

ArticleController::sortAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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