GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 2d33dc...ef1041 )
by Luis Ramón
02:50
created

AdminController::trainingFormAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/*
3
  ÁTICA - Aplicación web para la gestión documental de centros educativos
4
5
  Copyright (C) 2015-2016: Luis Ramón López López
6
7
  This program is free software: you can redistribute it and/or modify
8
  it under the terms of the GNU Affero General Public License as published by
9
  the Free Software Foundation, either version 3 of the License, or
10
  (at your option) any later version.
11
12
  This program is distributed in the hope that it will be useful,
13
  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
  GNU Affero General Public License for more details.
16
17
  You should have received a copy of the GNU Affero General Public License
18
  along with this program.  If not, see [http://www.gnu.org/licenses/].
19
*/
20
21
namespace AppBundle\Controller;
22
23
use AppBundle\Entity\Department;
24
use Doctrine\ORM\EntityManager;
25
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
27
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
28
use Symfony\Component\HttpFoundation\Request;
29
30
/**
31
 * @Route("/admin")
32
 */
33
class AdminController extends Controller
34
{
35
    public static $DEPARTMENT_ENTITY_DATA = [
36
            'entity' => 'department',
37
            'entityClassName' => 'AppBundle\Entity\Department',
38
            'entityFormType' => 'AppBundle\Form\Type\DepartmentType',
39
            'query' => 'SELECT d FROM AppBundle:Department d JOIN d.head h JOIN h.person p',
40
            'defaultSortFieldName' => 'd.name',
41
            'columns' => [
42
                ['size' => '5', 'sort_field' => 'd.name', 'name' => 'department.name'],
43
                ['size' => '4', 'sort_field' => 'p.displayName', 'name' => 'department.head'],
44
            ],
45
            'data_columns' => ['name', 'head']
46
        ];
47
48
    public static $COMPANY_ENTITY_DATA = [
49
        'entity' => 'company',
50
        'entityClassName' => 'AppBundle\Entity\Company',
51
        'entityFormType' => 'AppBundle\Form\Type\CompanyType',
52
        'query' => 'SELECT c FROM AppBundle:Company c',
53
        'defaultSortFieldName' => 'c.name',
54
        'columns' => [
55
            ['size' => '2', 'sort_field' => 'c.code', 'name' => 'company.code'],
56
            ['size' => '7', 'sort_field' => 'c.name', 'name' => 'company.name'],
57
        ],
58
        'data_columns' => ['code', 'name']
59
    ];
60
61
    public static $GROUP_ENTITY_DATA = [
62
        'entity' => 'group',
63
        'entityClassName' => 'AppBundle\Entity\Group',
64
        'entityFormType' => 'AppBundle\Form\Type\GroupType',
65
        'query' => 'SELECT g FROM AppBundle:Group g JOIN g.training t',
66
        'defaultSortFieldName' => 'g.name',
67
        'columns' => [
68
            ['size' => '4', 'sort_field' => 'g.name', 'name' => 'form.name'],
69
            ['size' => '5', 'sort_field' => 'g.training', 'name' => 'form.training']
70
        ],
71
        'data_columns' => ['name', 'training']
72
    ];
73
74
    public static $TRAINING_ENTITY_DATA = [
75
        'entity' => 'training',
76
        'entityClassName' => 'AppBundle\Entity\Training',
77
        'entityFormType' => 'AppBundle\Form\Type\TrainingType',
78
        'query' => 'SELECT t FROM AppBundle:Training t JOIN t.department d',
79
        'defaultSortFieldName' => 't.name',
80
        'columns' => [
81
            ['size' => '5', 'sort_field' => 't.name', 'name' => 'form.name'],
82
            ['size' => '4', 'sort_field' => 't.department', 'name' => 'form.department']
83
        ],
84
        'data_columns' => ['name', 'department']
85
    ];
86
87
    /**
88
     * @Route("/", name="admin_menu", methods={"GET"})
89
     * @Security("is_granted('ROLE_ADMIN')")
90
     */
91
    public function indexAction()
92
    {
93
        $menuItem = $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_menu');
94
        
95
        return $this->render('admin/menu.html.twig',
96
            [
97
                'menu_item' => $menuItem
98
            ]);
99
    }
100
101
    public function genericIndexAction($entityData, Request $request)
102
    {
103
        /** @var EntityManager $em */
104
        $em = $this->getDoctrine()->getManager();
105
106
        $query = $em->createQuery($entityData['query']);
107
108
        $paginator  = $this->get('knp_paginator');
109
        $pagination = $paginator->paginate(
110
            $query,
111
            $request->query->getInt('page', 1),
112
            $this->getParameter('page.size'),
113
            [
114
                'defaultSortFieldName' => $entityData['defaultSortFieldName'],
115
                'defaultSortDirection' => 'asc'
116
            ]
117
        );
118
119
        return $this->render('admin/manage_generic.html.twig',
120
            [
121
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName($request->get('_route')),
122
                'title' => null,
123
                'pagination' => $pagination,
124
                'entity' => $entityData['entity'],
125
                'columns' => $entityData['columns'],
126
                'data_columns' => $entityData['data_columns']
127
            ]);
128
    }
129
130
    public function genericFormAction($entityData, $element, Request $request)
131
    {
132
        $em = $this->getDoctrine()->getManager();
133
134
        $new = (null === $element);
135
        if ($new) {
136
            $element = new $entityData['entityClassName'];
137
            $em->persist($element);
138
        }
139
140
        $form = $this->createForm($entityData['entityFormType'], $element);
141
142
        $form->handleRequest($request);
143
144
        $menuItem = $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_' . $entityData['entity']);
145
146
        if ($form->isSubmitted() && $form->isValid()) {
147
148
            // Probar a guardar los cambios
149
            try {
150
                $em->flush();
151
                $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'user'));
152
                return $this->redirectToRoute($menuItem->getRouteName(), $menuItem->getRouteParams());
153
            } catch (\Exception $e) {
154
                $this->addFlash('error', $this->get('translator')->trans('alert.not_saved', [], 'user'));
155
            }
156
        }
157
158
        $title = ((string) $element) ?: $this->get('translator')->trans('form.new', [], $entityData['entity']);
159
160
        return $this->render('admin/form_generic.html.twig', [
161
            'form' => $form->createView(),
162
            'new' => $new,
163
            'menu_item' => $menuItem,
164
            'breadcrumb' => [
165
                ['fixed' => $title]
166
            ],
167
            'element' => $element,
168
            'title' => $title,
169
            'entity' => $entityData['entity']
170
        ]);
171
    }
172
173
    public function genericDeleteAction($entityData, $element, Request $request)
174
    {
175
        if ('POST' === $request->getMethod() && $request->request->has('delete')) {
176
177
            // Eliminar el departamento de la base de datos
178
            $this->getDoctrine()->getManager()->remove($element);
179
            try {
180
                $this->getDoctrine()->getManager()->flush();
181
                $this->addFlash('success', $this->get('translator')->trans('alert.deleted', [], $entityData['entity']));
182
            }
183
            catch(\Exception $e) {
184
                $this->addFlash('error', $this->get('translator')->trans('alert.not_deleted', [], $entityData['entity']));
185
            }
186
            return $this->redirectToRoute('admin_' . $entityData['entity']);
187
        }
188
189
        $title = (string) $element;
190
191
        $breadcrumb = [
192
            ['fixed' => $title, 'path' => 'admin_' . $entityData['entity'] . '_form', 'options' => ['id' => $element->getId()]],
193
            ['caption' => 'menu.delete']
194
        ];
195
196
        return $this->render(':admin:delete_generic.html.twig', [
197
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_departments'),
198
            'breadcrumb' => $breadcrumb,
199
            'title' => $title,
200
            'element' => $element,
201
            'entity' => $entityData['entity']
202
        ]);
203
    }
204
205
    /**
206
     * @Route("/departamentos", name="admin_department", methods={"GET"})
207
     */
208
    public function departmentsIndexAction(Request $request)
209
    {
210
        return $this->genericIndexAction(self::$DEPARTMENT_ENTITY_DATA, $request);
211
    }
212
213
    /**
214
     * @Route("/departamentos/nuevo", name="admin_department_new", methods={"GET", "POST"})
215
     * @Route("/departamentos/{id}", name="admin_department_form", methods={"GET", "POST"}, requirements={"id": "\d+"})
216
     */
217
    public function departmentsFormAction(Department $element = null, Request $request)
218
    {
219
        return $this->genericFormAction(self::$DEPARTMENT_ENTITY_DATA, $element, $request);
220
    }
221
222
    /**
223
     * @Route("/departamentos/eliminar/{id}", name="admin_department_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} )
224
     */
225
    public function deleteElementAction(Department $element, Request $request)
226
    {
227
        return $this->genericDeleteAction(self::$DEPARTMENT_ENTITY_DATA, $element, $request);
228
    }
229
230
    /**
231
     * @Route("/empresas", name="admin_company", methods={"GET"})
232
     */
233
    public function companiesIndexAction(Request $request)
234
    {
235
        return $this->genericIndexAction(self::$COMPANY_ENTITY_DATA, $request);
236
    }
237
238
    /**
239
     * @Route("/empresas/nueva", name="admin_company_new", methods={"GET", "POST"})
240
     * @Route("/empresas/{id}", name="admin_company_form", methods={"GET", "POST"}, requirements={"id": "\d+"})
241
     */
242
    public function companyFormAction(Department $element = null, Request $request)
243
    {
244
        return $this->genericFormAction(self::$COMPANY_ENTITY_DATA, $element, $request);
245
    }
246
247
    /**
248
     * @Route("/empresas/eliminar/{id}", name="admin_company_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} )
249
     */
250
    public function companyDeleteAction(Department $element, Request $request)
251
    {
252
        return $this->genericDeleteAction(self::$COMPANY_ENTITY_DATA, $element, $request);
253
    }
254
255
    /**
256
     * @Route("/grupos", name="admin_group", methods={"GET"})
257
     */
258
    public function groupIndexAction(Request $request)
259
    {
260
        return $this->genericIndexAction(self::$GROUP_ENTITY_DATA, $request);
261
    }
262
263
    /**
264
     * @Route("/grupos/nuevo", name="admin_group_new", methods={"GET", "POST"})
265
     * @Route("/grupos/{id}", name="admin_group_form", methods={"GET", "POST"}, requirements={"id": "\d+"})
266
     */
267
    public function groupFormAction(Department $element = null, Request $request)
268
    {
269
        return $this->genericFormAction(self::$GROUP_ENTITY_DATA, $element, $request);
270
    }
271
272
    /**
273
     * @Route("/grupos/eliminar/{id}", name="admin_group_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} )
274
     */
275
    public function groupDeleteAction(Department $element, Request $request)
276
    {
277
        return $this->genericDeleteAction(self::$GROUP_ENTITY_DATA, $element, $request);
278
    }
279
280
    /**
281
     * @Route("/ensenanzas", name="admin_training", methods={"GET"})
282
     */
283
    public function trainingIndexAction(Request $request)
284
    {
285
        return $this->genericIndexAction(self::$TRAINING_ENTITY_DATA, $request);
286
    }
287
288
    /**
289
     * @Route("/ensenanzas/nueva", name="admin_training_new", methods={"GET", "POST"})
290
     * @Route("/ensenanzas/{id}", name="admin_training_form", methods={"GET", "POST"}, requirements={"id": "\d+"})
291
     */
292
    public function trainingFormAction(Department $element = null, Request $request)
293
    {
294
        return $this->genericFormAction(self::$TRAINING_ENTITY_DATA, $element, $request);
295
    }
296
297
    /**
298
     * @Route("/ensenanzas/eliminar/{id}", name="admin_training_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} )
299
     */
300
    public function trainingDeleteAction(Department $element, Request $request)
301
    {
302
        return $this->genericDeleteAction(self::$TRAINING_ENTITY_DATA, $element, $request);
303
    }
304
}
305