Passed
Push — master ( e276d1...cad953 )
by Jan
04:31
created

PartListsController::showCategory()   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 Doctrine\ORM\EntityManagerInterface;
52
use Omines\DataTablesBundle\DataTableFactory;
53
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
54
use Symfony\Component\HttpFoundation\JsonResponse;
55
use Symfony\Component\HttpFoundation\Request;
56
use Symfony\Component\HttpFoundation\Response;
57
use Symfony\Component\Routing\Annotation\Route;
58
59
class PartListsController extends AbstractController
60
{
61
    private $entityManager;
62
63
    public function __construct(EntityManagerInterface $entityManager)
64
    {
65
        $this->entityManager = $entityManager;
66
    }
67
68
    /**
69
     * @Route("/category/{id}/parts", name="part_list_category")
70
     *
71
     * @return JsonResponse|Response
72
     */
73
    public function showCategory(Category $category, Request $request, DataTableFactory $dataTable)
74
    {
75
        $table = $dataTable->createFromType(PartsDataTable::class, ['category' => $category])
76
            ->handleRequest($request);
77
78
        if ($table->isCallback()) {
79
            return $table->getResponse();
80
        }
81
82
        return $this->render('Parts/lists/category_list.html.twig', [
83
            'datatable' => $table,
84
            'entity' => $category,
85
            'repo' => $this->entityManager->getRepository(Category::class),
86
        ]);
87
    }
88
89
    /**
90
     * @Route("/footprint/{id}/parts", name="part_list_footprint")
91
     *
92
     * @return JsonResponse|Response
93
     */
94
    public function showFootprint(Footprint $footprint, Request $request, DataTableFactory $dataTable)
95
    {
96
        $table = $dataTable->createFromType(PartsDataTable::class, ['footprint' => $footprint])
97
            ->handleRequest($request);
98
99
        if ($table->isCallback()) {
100
            return $table->getResponse();
101
        }
102
103
        return $this->render('Parts/lists/footprint_list.html.twig', [
104
            'datatable' => $table,
105
            'entity' => $footprint,
106
            'repo' => $this->entityManager->getRepository(Footprint::class),
107
        ]);
108
    }
109
110
    /**
111
     * @Route("/manufacturer/{id}/parts", name="part_list_manufacturer")
112
     *
113
     * @return JsonResponse|Response
114
     */
115
    public function showManufacturer(Manufacturer $manufacturer, Request $request, DataTableFactory $dataTable)
116
    {
117
        $table = $dataTable->createFromType(PartsDataTable::class, ['manufacturer' => $manufacturer])
118
            ->handleRequest($request);
119
120
        if ($table->isCallback()) {
121
            return $table->getResponse();
122
        }
123
124
        return $this->render('Parts/lists/manufacturer_list.html.twig', [
125
            'datatable' => $table,
126
            'entity' => $manufacturer,
127
            'repo' => $this->entityManager->getRepository(Manufacturer::class),
128
        ]);
129
    }
130
131
    /**
132
     * @Route("/store_location/{id}/parts", name="part_list_store_location")
133
     *
134
     * @return JsonResponse|Response
135
     */
136
    public function showStorelocation(Storelocation $storelocation, Request $request, DataTableFactory $dataTable)
137
    {
138
        $table = $dataTable->createFromType(PartsDataTable::class, ['storelocation' => $storelocation])
139
            ->handleRequest($request);
140
141
        if ($table->isCallback()) {
142
            return $table->getResponse();
143
        }
144
145
        return $this->render('Parts/lists/store_location_list.html.twig', [
146
            'datatable' => $table,
147
            'entity' => $storelocation,
148
            'repo' => $this->entityManager->getRepository(Storelocation::class),
149
        ]);
150
    }
151
152
    /**
153
     * @Route("/supplier/{id}/parts", name="part_list_supplier")
154
     *
155
     * @return JsonResponse|Response
156
     */
157
    public function showSupplier(Supplier $supplier, Request $request, DataTableFactory $dataTable)
158
    {
159
        $table = $dataTable->createFromType(PartsDataTable::class, ['supplier' => $supplier])
160
            ->handleRequest($request);
161
162
        if ($table->isCallback()) {
163
            return $table->getResponse();
164
        }
165
166
        return $this->render('Parts/lists/supplier_list.html.twig', [
167
            'datatable' => $table,
168
            'entity' => $supplier,
169
            'repo' => $this->entityManager->getRepository(Supplier::class),
170
        ]);
171
    }
172
173
    /**
174
     * @Route("/parts/by_tag/{tag}", name="part_list_tags")
175
     *
176
     * @return JsonResponse|Response
177
     */
178
    public function showTag(string $tag, Request $request, DataTableFactory $dataTable)
179
    {
180
        $table = $dataTable->createFromType(PartsDataTable::class, ['tag' => $tag])
181
            ->handleRequest($request);
182
183
        if ($table->isCallback()) {
184
            return $table->getResponse();
185
        }
186
187
        return $this->render('Parts/lists/tags_list.html.twig', [
188
            'tag' => $tag,
189
            'datatable' => $table,
190
        ]);
191
    }
192
193
    /**
194
     * @Route("/parts/search", name="parts_search")
195
     *
196
     * @return JsonResponse|Response
197
     */
198
    public function showSearch(Request $request, DataTableFactory $dataTable)
199
    {
200
        $search = $request->query->get('keyword', '');
201
        $search_options = [
202
            'name' => $request->query->getBoolean('name'),
203
            'description' => $request->query->getBoolean('description'),
204
            'comment' => $request->query->getBoolean('comment'),
205
            'category' => $request->query->getBoolean('category'),
206
            'store_location' => $request->query->getBoolean('storelocation'),
207
            'supplier' => $request->query->getBoolean('supplier'),
208
            'ordernr' => $request->query->getBoolean('ordernr'),
209
            'manufacturer' => $request->query->getBoolean('manufacturer'),
210
            'footprint' => $request->query->getBoolean('footprint'),
211
            'tags' => $request->query->getBoolean('tags'),
212
            'regex' => $request->query->getBoolean('regex'),
213
        ];
214
215
        $table = $dataTable->createFromType(PartsDataTable::class, [
216
            'search' => $search, 'search_options' => $search_options,
217
        ])
218
            ->handleRequest($request);
219
220
        if ($table->isCallback()) {
221
            return $table->getResponse();
222
        }
223
224
        return $this->render('Parts/lists/search_list.html.twig', [
225
            'datatable' => $table,
226
            'keyword' => $search,
227
        ]);
228
    }
229
230
    /**
231
     * @Route("/parts", name="parts_show_all")
232
     *
233
     * @return JsonResponse|Response
234
     */
235
    public function showAll(Request $request, DataTableFactory $dataTable)
236
    {
237
        $table = $dataTable->createFromType(PartsDataTable::class)
238
            ->handleRequest($request);
239
240
        if ($table->isCallback()) {
241
            return $table->getResponse();
242
        }
243
244
        return $this->render('Parts/lists/all_list.html.twig', ['datatable' => $table]);
245
    }
246
}
247