Passed
Pull Request — master (#78)
by
unknown
09:17 queued 04:28
created

PartListsController::showStorelocation()   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
82
            return $this->redirect($redirect);
83
        }
84
85
        if (null === $action || null === $ids) {
86
            $this->addFlash('error', 'part.table.actions.no_params_given');
87
        } else {
88
            $parts = $actionHandler->idStringToArray($ids);
89
            $actionHandler->handleAction($action, $parts, $target ? (int) $target : null);
90
91
            //Save changes
92
            $this->entityManager->flush();
93
94
            $this->addFlash('success', 'part.table.actions.success');
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,
249
            'search_options' => $search_options,
250
        ])
251
            ->handleRequest($request);
252
253
        if ($table->isCallback()) {
254
            return $table->getResponse();
255
        }
256
257
        return $this->render('Parts/lists/search_list.html.twig', [
258
            'datatable' => $table,
259
            'keyword' => $search,
260
        ]);
261
    }
262
263
    /**
264
     * @Route("/parts", name="parts_show_all")
265
     *
266
     * @return JsonResponse|Response
267
     */
268
    public function showAll(Request $request, DataTableFactory $dataTable)
269
    {
270
        $table = $dataTable->createFromType(PartsDataTable::class)
271
            ->handleRequest($request);
272
273
        if ($table->isCallback()) {
274
            return $table->getResponse();
275
        }
276
277
        return $this->render('Parts/lists/all_list.html.twig', ['datatable' => $table]);
278
    }
279
280
    /**
281
     * @Route("/lowstock", name="parts_show_low_stock")
282
     *
283
     * @return JsonResponse|Response
284
     */
285
    public function showLowStock(Request $request, DataTableFactory $dataTable)
286
    {
287
        $table = $dataTable->createFromType(PartsDataTable::class, ['lowstock'=>true])
288
            ->handleRequest($request);
289
290
        if ($table->isCallback()) {
291
            return $table->getResponse();
292
        }
293
294
        return $this->render('Parts/lists/lowstock_list.html.twig', ['datatable' => $table]);
295
    }
296
}
297