1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Oc\Controller\Backend; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Oc\Form\CachesFormType; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
13
|
|
|
use Oc\Repository\Exception\RecordNotFoundException; |
14
|
|
|
use Oc\Repository\CachesRepository; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class CachesController |
18
|
|
|
* |
19
|
|
|
* @package Oc\Controller\Backend |
20
|
|
|
*/ |
21
|
|
|
class CachesController extends AbstractController |
22
|
|
|
{ |
23
|
|
|
private $connection; |
24
|
|
|
|
25
|
|
|
private $cachesRepository; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* CachesController constructor. |
29
|
|
|
* |
30
|
|
|
* @param Connection $connection |
31
|
|
|
* @param CachesRepository $cachesRepository |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Connection $connection, CachesRepository $cachesRepository) |
34
|
|
|
{ |
35
|
|
|
$this->connection = $connection; |
36
|
|
|
$this->cachesRepository = $cachesRepository; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Request $request |
41
|
|
|
* @Route("/caches", name="caches_index") |
42
|
|
|
* |
43
|
|
|
* @return Response |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function cachesController_index(Request $request) |
|
|
|
|
46
|
|
|
: Response { |
47
|
|
|
$fetchedCaches = ''; |
48
|
|
|
|
49
|
|
|
// create input field for caches_by_searchfield |
50
|
|
|
$form = $this->createForm(CachesFormType::class); |
51
|
|
|
|
52
|
|
|
// see: https://symfonycasts.com/screencast/symfony-forms/form-submit |
53
|
|
|
// handles the request (submit-button of the form), but only if there is a POST request |
54
|
|
|
$form->handleRequest($request); |
55
|
|
|
// if is true only if there is a request submitted and it is valid |
56
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
57
|
|
|
// read content of form input field |
58
|
|
|
$inputData = $form->getData(); |
59
|
|
|
|
60
|
|
|
// send request to DB |
61
|
|
|
$fetchedCaches = $this->getCachesForSearchField($inputData['content_caches_searchfield']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($fetchedCaches === '') { |
65
|
|
|
return $this->render( |
66
|
|
|
'backend/caches/index.html.twig', [ |
67
|
|
|
'cachesForm' => $form->createView(), |
68
|
|
|
] |
69
|
|
|
); |
70
|
|
|
} else { |
71
|
|
|
return $this->render( |
72
|
|
|
'backend/caches/basicview.html.twig', [ |
73
|
|
|
'cachesForm' => $form->createView(), |
74
|
|
|
'caches_by_searchfield' => $fetchedCaches |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $wpID |
82
|
|
|
* |
83
|
|
|
* @return Response |
84
|
|
|
* @Route("/cache/{wpID}", name="cache_by_wp_oc_gc") |
85
|
|
|
*/ |
86
|
|
|
public function search_by_cache_wp(string $wpID) |
87
|
|
|
: Response { |
88
|
|
|
$fetchedCaches = []; |
89
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$fetchedCaches = $this->getCacheDetailsByWayPoint($wpID); |
92
|
|
|
} catch (\Exception $e) { |
93
|
|
|
// tue was.. (status_not_found = true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->render('backend/caches/detailview.html.twig', ['cache_by_id' => $fetchedCaches]); //+ status_not_found + abfragen in twig, Z.B. |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $searchtext |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
public function getCachesForSearchField(string $searchtext) |
105
|
|
|
: array { |
106
|
|
|
// so sieht die SQL-Vorlage aus.. |
107
|
|
|
// SELECT cache_id, name, wp_oc, user.username |
108
|
|
|
// FROM caches |
109
|
|
|
// INNER JOIN user ON caches.user_id = user.user_id |
110
|
|
|
// WHERE wp_oc = "' . $searchtext . '" |
111
|
|
|
// OR wp_gc = "' . $searchtext . '" |
112
|
|
|
// OR caches.name LIKE "%' . $searchtext . '%"' |
113
|
|
|
// OR user.username LIKE "%' . $searchtext . '%"' |
114
|
|
|
$qb = $this->connection->createQueryBuilder(); |
115
|
|
|
$qb->select('caches.cache_id', 'caches.name', 'caches.wp_oc', 'caches.wp_gc', 'user.username') |
116
|
|
|
->from('caches') |
117
|
|
|
->innerJoin('caches', 'user', 'user', 'caches.user_id = user.user_id') |
118
|
|
|
->where('caches.wp_oc = :searchTerm') |
119
|
|
|
->orWhere('caches.wp_gc = :searchTerm') |
120
|
|
|
->orWhere('caches.name LIKE :searchTermLIKE') |
121
|
|
|
->orWhere('user.username LIKE :searchTermLIKE') |
122
|
|
|
->setParameters(['searchTerm' => $searchtext, 'searchTermLIKE' => '%' . $searchtext . '%']) |
123
|
|
|
->orderBy('caches.wp_oc', 'ASC'); |
124
|
|
|
|
125
|
|
|
return $qb->execute()->fetchAll(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param int $id |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
* @throws RecordNotFoundException |
133
|
|
|
*/ |
134
|
|
|
public function getCacheDetailsById(int $id) : array { |
135
|
|
|
$fetchedCache = $this->cachesRepository->fetchOneBy(['cache_id' => $id]); |
136
|
|
|
|
137
|
|
|
return [$this->cachesRepository->getDatabaseArrayFromEntity($fetchedCache)]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $wayPoint |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
* @throws RecordNotFoundException |
145
|
|
|
*/ |
146
|
|
|
public function getCacheDetailsByWayPoint(string $wayPoint) : array { |
147
|
|
|
$fetchedCache = $this->cachesRepository->fetchOneBy(['wp_oc' => $wayPoint]); |
148
|
|
|
|
149
|
|
|
return [$this->cachesRepository->getDatabaseArrayFromEntity($fetchedCache)]; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.