Passed
Push — master ( 123b22...6ed3d5 )
by Jan
09:22 queued 11s
created

PartListsController::showManufacturer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 13
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
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
9
 * by 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 <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\Controller;
44
45
use App\DataTables\PartsDataTable;
46
use App\Entity\Parts\Category;
47
use App\Entity\Parts\Footprint;
48
use App\Entity\Parts\Manufacturer;
49
use App\Entity\Parts\Storelocation;
50
use App\Entity\Parts\Supplier;
51
use App\Services\Parts\PartsTableActionHandler;
52
use Doctrine\ORM\EntityManagerInterface;
53
use Omines\DataTablesBundle\DataTableFactory;
54
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
55
use Symfony\Component\HttpFoundation\JsonResponse;
56
use Symfony\Component\HttpFoundation\Request;
57
use Symfony\Component\HttpFoundation\Response;
58
use Symfony\Component\Routing\Annotation\Route;
59
60
class PartListsController extends AbstractController
61
{
62
    private $entityManager;
63
64
    public function __construct(EntityManagerInterface $entityManager)
65
    {
66
        $this->entityManager = $entityManager;
67
    }
68
69
    /**
70
     * @Route("/table/action", name="table_action", methods={"POST"})
71
     */
72
    public function tableAction(Request $request, PartsTableActionHandler $actionHandler): Response
73
    {
74
        $redirect = $request->request->get('redirect_back');
75
        $ids = $request->request->get('ids');
76
        $action = $request->request->get('action');
77
        $target = $request->request->get('target');
78
79
        if (!$this->isCsrfTokenValid('table_action', $request->request->get('_token'))) {
80
            $this->addFlash('error', 'csfr_invalid');
81
            return $this->redirect($redirect);
82
        }
83
84
        if ($action === null || $ids === null) {
85
            $this->addFlash('error', 'part.table.actions.no_params_given');
86
        } else {
87
            $parts = $actionHandler->idStringToArray($ids);
88
            $actionHandler->handleAction($action, $parts, $target ? (int) $target : null);
89
90
            //Save changes
91
            $this->entityManager->flush();
92
93
            $this->addFlash('success', 'part.table.actions.success');
94
        }
95
96
97
        return $this->redirect($redirect);
98
    }
99
100
    /**
101
     * @Route("/category/{id}/parts", name="part_list_category")
102
     *
103
     * @return JsonResponse|Response
104
     */
105
    public function showCategory(Category $category, Request $request, DataTableFactory $dataTable)
106
    {
107
        $table = $dataTable->createFromType(PartsDataTable::class, ['category' => $category])
108
            ->handleRequest($request);
109
110
        if ($table->isCallback()) {
111
            return $table->getResponse();
112
        }
113
114
        return $this->render('Parts/lists/category_list.html.twig', [
115
            'datatable' => $table,
116
            'entity' => $category,
117
            'repo' => $this->entityManager->getRepository(Category::class),
118
        ]);
119
    }
120
121
    /**
122
     * @Route("/footprint/{id}/parts", name="part_list_footprint")
123
     *
124
     * @return JsonResponse|Response
125
     */
126
    public function showFootprint(Footprint $footprint, Request $request, DataTableFactory $dataTable)
127
    {
128
        $table = $dataTable->createFromType(PartsDataTable::class, ['footprint' => $footprint])
129
            ->handleRequest($request);
130
131
        if ($table->isCallback()) {
132
            return $table->getResponse();
133
        }
134
135
        return $this->render('Parts/lists/footprint_list.html.twig', [
136
            'datatable' => $table,
137
            'entity' => $footprint,
138
            'repo' => $this->entityManager->getRepository(Footprint::class),
139
        ]);
140
    }
141
142
    /**
143
     * @Route("/manufacturer/{id}/parts", name="part_list_manufacturer")
144
     *
145
     * @return JsonResponse|Response
146
     */
147
    public function showManufacturer(Manufacturer $manufacturer, Request $request, DataTableFactory $dataTable)
148
    {
149
        $table = $dataTable->createFromType(PartsDataTable::class, ['manufacturer' => $manufacturer])
150
            ->handleRequest($request);
151
152
        if ($table->isCallback()) {
153
            return $table->getResponse();
154
        }
155
156
        return $this->render('Parts/lists/manufacturer_list.html.twig', [
157
            'datatable' => $table,
158
            'entity' => $manufacturer,
159
            'repo' => $this->entityManager->getRepository(Manufacturer::class),
160
        ]);
161
    }
162
163
    /**
164
     * @Route("/store_location/{id}/parts", name="part_list_store_location")
165
     *
166
     * @return JsonResponse|Response
167
     */
168
    public function showStorelocation(Storelocation $storelocation, Request $request, DataTableFactory $dataTable)
169
    {
170
        $table = $dataTable->createFromType(PartsDataTable::class, ['storelocation' => $storelocation])
171
            ->handleRequest($request);
172
173
        if ($table->isCallback()) {
174
            return $table->getResponse();
175
        }
176
177
        return $this->render('Parts/lists/store_location_list.html.twig', [
178
            'datatable' => $table,
179
            'entity' => $storelocation,
180
            'repo' => $this->entityManager->getRepository(Storelocation::class),
181
        ]);
182
    }
183
184
    /**
185
     * @Route("/supplier/{id}/parts", name="part_list_supplier")
186
     *
187
     * @return JsonResponse|Response
188
     */
189
    public function showSupplier(Supplier $supplier, Request $request, DataTableFactory $dataTable)
190
    {
191
        $table = $dataTable->createFromType(PartsDataTable::class, ['supplier' => $supplier])
192
            ->handleRequest($request);
193
194
        if ($table->isCallback()) {
195
            return $table->getResponse();
196
        }
197
198
        return $this->render('Parts/lists/supplier_list.html.twig', [
199
            'datatable' => $table,
200
            'entity' => $supplier,
201
            'repo' => $this->entityManager->getRepository(Supplier::class),
202
        ]);
203
    }
204
205
    /**
206
     * @Route("/parts/by_tag/{tag}", name="part_list_tags")
207
     *
208
     * @return JsonResponse|Response
209
     */
210
    public function showTag(string $tag, Request $request, DataTableFactory $dataTable)
211
    {
212
        $table = $dataTable->createFromType(PartsDataTable::class, ['tag' => $tag])
213
            ->handleRequest($request);
214
215
        if ($table->isCallback()) {
216
            return $table->getResponse();
217
        }
218
219
        return $this->render('Parts/lists/tags_list.html.twig', [
220
            'tag' => $tag,
221
            'datatable' => $table,
222
        ]);
223
    }
224
225
    /**
226
     * @Route("/parts/search", name="parts_search")
227
     *
228
     * @return JsonResponse|Response
229
     */
230
    public function showSearch(Request $request, DataTableFactory $dataTable)
231
    {
232
        $search = $request->query->get('keyword', '');
233
        $search_options = [
234
            'name' => $request->query->getBoolean('name'),
235
            'description' => $request->query->getBoolean('description'),
236
            'comment' => $request->query->getBoolean('comment'),
237
            'category' => $request->query->getBoolean('category'),
238
            'store_location' => $request->query->getBoolean('storelocation'),
239
            'supplier' => $request->query->getBoolean('supplier'),
240
            'ordernr' => $request->query->getBoolean('ordernr'),
241
            'manufacturer' => $request->query->getBoolean('manufacturer'),
242
            'footprint' => $request->query->getBoolean('footprint'),
243
            'tags' => $request->query->getBoolean('tags'),
244
            'regex' => $request->query->getBoolean('regex'),
245
        ];
246
247
        $table = $dataTable->createFromType(PartsDataTable::class, [
248
            'search' => $search, 'search_options' => $search_options,
249
        ])
250
            ->handleRequest($request);
251
252
        if ($table->isCallback()) {
253
            return $table->getResponse();
254
        }
255
256
        return $this->render('Parts/lists/search_list.html.twig', [
257
            'datatable' => $table,
258
            'keyword' => $search,
259
        ]);
260
    }
261
262
    /**
263
     * @Route("/parts", name="parts_show_all")
264
     *
265
     * @return JsonResponse|Response
266
     */
267
    public function showAll(Request $request, DataTableFactory $dataTable)
268
    {
269
        $table = $dataTable->createFromType(PartsDataTable::class)
270
            ->handleRequest($request);
271
272
        if ($table->isCallback()) {
273
            return $table->getResponse();
274
        }
275
276
        return $this->render('Parts/lists/all_list.html.twig', ['datatable' => $table]);
277
    }
278
}
279