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 ( 23896c...2d33dc )
by Luis Ramón
02:42
created

AdminController::genericIndexAction()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 17
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
    /**
62
     * @Route("/", name="admin_menu", methods={"GET"})
63
     * @Security("is_granted('ROLE_ADMIN')")
64
     */
65
    public function indexAction()
66
    {
67
        $menuItem = $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_menu');
68
        
69
        return $this->render('admin/menu.html.twig',
70
            [
71
                'menu_item' => $menuItem
72
            ]);
73
    }
74
75
    public function genericIndexAction($entityData, Request $request)
76
    {
77
        /** @var EntityManager $em */
78
        $em = $this->getDoctrine()->getManager();
79
80
        $query = $em->createQuery($entityData['query']);
81
82
        $paginator  = $this->get('knp_paginator');
83
        $pagination = $paginator->paginate(
84
            $query,
85
            $request->query->getInt('page', 1),
86
            $this->getParameter('page.size'),
87
            [
88
                'defaultSortFieldName' => $entityData['defaultSortFieldName'],
89
                'defaultSortDirection' => 'asc'
90
            ]
91
        );
92
93
        return $this->render('admin/manage_generic.html.twig',
94
            [
95
                'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName($request->get('_route')),
96
                'title' => null,
97
                'pagination' => $pagination,
98
                'entity' => $entityData['entity'],
99
                'columns' => $entityData['columns'],
100
                'data_columns' => $entityData['data_columns']
101
            ]);
102
    }
103
104
    public function genericFormAction($entityData, $element, Request $request)
105
    {
106
        $em = $this->getDoctrine()->getManager();
107
108
        $new = (null === $element);
109
        if ($new) {
110
            $element = new $entityData['entityClassName'];
111
            $em->persist($element);
112
        }
113
114
        $form = $this->createForm($entityData['entityFormType'], $element);
115
116
        $form->handleRequest($request);
117
118
        $menuItem = $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_' . $entityData['entity']);
119
120
        if ($form->isSubmitted() && $form->isValid()) {
121
122
            // Probar a guardar los cambios
123
            try {
124
                $em->flush();
125
                $this->addFlash('success', $this->get('translator')->trans('alert.saved', [], 'user'));
126
                return $this->redirectToRoute($menuItem->getRouteName(), $menuItem->getRouteParams());
127
            } catch (\Exception $e) {
128
                $this->addFlash('error', $this->get('translator')->trans('alert.not_saved', [], 'user'));
129
            }
130
        }
131
132
        $title = ((string) $element) ?: $this->get('translator')->trans('form.new', [], $entityData['entity']);
133
134
        return $this->render('admin/form_generic.html.twig', [
135
            'form' => $form->createView(),
136
            'new' => $new,
137
            'menu_item' => $menuItem,
138
            'breadcrumb' => [
139
                ['fixed' => $title]
140
            ],
141
            'element' => $element,
142
            'title' => $title,
143
            'entity' => $entityData['entity']
144
        ]);
145
    }
146
147
    public function genericDeleteAction($entityData, $element, Request $request)
148
    {
149
        if ('POST' === $request->getMethod() && $request->request->has('delete')) {
150
151
            // Eliminar el departamento de la base de datos
152
            $this->getDoctrine()->getManager()->remove($element);
153
            try {
154
                $this->getDoctrine()->getManager()->flush();
155
                $this->addFlash('success', $this->get('translator')->trans('alert.deleted', [], $entityData['entity']));
156
            }
157
            catch(\Exception $e) {
158
                $this->addFlash('error', $this->get('translator')->trans('alert.not_deleted', [], $entityData['entity']));
159
            }
160
            return $this->redirectToRoute('admin_' . $entityData['entity']);
161
        }
162
163
        $title = (string) $element;
164
165
        $breadcrumb = [
166
            ['fixed' => $title, 'path' => 'admin_' . $entityData['entity'] . '_form', 'options' => ['id' => $element->getId()]],
167
            ['caption' => 'menu.delete']
168
        ];
169
170
        return $this->render(':admin:delete_generic.html.twig', [
171
            'menu_item' => $this->get('app.menu_builders_chain')->getMenuItemByRouteName('admin_departments'),
172
            'breadcrumb' => $breadcrumb,
173
            'title' => $title,
174
            'element' => $element,
175
            'entity' => $entityData['entity']
176
        ]);
177
    }
178
179
    /**
180
     * @Route("/departamentos", name="admin_department", methods={"GET"})
181
     */
182
    public function departmentsIndexAction(Request $request)
183
    {
184
        return $this->genericIndexAction(self::$DEPARTMENT_ENTITY_DATA, $request);
185
    }
186
187
    /**
188
     * @Route("/departamentos/nuevo", name="admin_department_new", methods={"GET", "POST"})
189
     * @Route("/departamentos/{id}", name="admin_department_form", methods={"GET", "POST"}, requirements={"id": "\d+"})
190
     */
191
    public function departmentsFormAction(Department $element = null, Request $request)
192
    {
193
        return $this->genericFormAction(self::$DEPARTMENT_ENTITY_DATA, $element, $request);
194
    }
195
196
    /**
197
     * @Route("/departamentos/eliminar/{id}", name="admin_department_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} )
198
     */
199
    public function deleteElementAction(Department $element, Request $request)
200
    {
201
        return $this->genericDeleteAction(self::$DEPARTMENT_ENTITY_DATA, $element, $request);
202
    }
203
204
    /**
205
     * @Route("/empresas", name="admin_company", methods={"GET"})
206
     */
207
    public function companiesIndexAction(Request $request)
208
    {
209
        return $this->genericIndexAction(self::$COMPANY_ENTITY_DATA, $request);
210
    }
211
212
    /**
213
     * @Route("/empresas/nueva", name="admin_company_new", methods={"GET", "POST"})
214
     * @Route("/empresas/{id}", name="admin_company_form", methods={"GET", "POST"}, requirements={"id": "\d+"})
215
     */
216
    public function companyFormAction(Department $element = null, Request $request)
217
    {
218
        return $this->genericFormAction(self::$COMPANY_ENTITY_DATA, $element, $request);
219
    }
220
221
    /**
222
     * @Route("/empresas/eliminar/{id}", name="admin_company_delete", methods={"GET", "POST"}, requirements={"id": "\d+"} )
223
     */
224
    public function companyDeleteAction(Department $element, Request $request)
225
    {
226
        return $this->genericDeleteAction(self::$COMPANY_ENTITY_DATA, $element, $request);
227
    }
228
}
229