Completed
Pull Request — development (#829)
by
unknown
04:57
created

SupportController::executeSQL_flexible()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Oc\Controller\Backend;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\DBALException;
9
use Oc\Form\SupportAdminComment;
10
use Oc\Form\SupportSearchCaches;
11
use Oc\Form\SupportSQLFlexForm;
12
use Oc\Repository\CacheAdoptionsRepository;
13
use Oc\Repository\CacheCoordinatesRepository;
14
use Oc\Repository\CacheLogsArchivedRepository;
15
use Oc\Repository\CacheReportsRepository;
16
use Oc\Repository\CachesRepository;
17
use Oc\Repository\CacheStatusModifiedRepository;
18
use Oc\Repository\CacheStatusRepository;
19
use Oc\Repository\Exception\RecordNotFoundException;
20
use Oc\Repository\Exception\RecordNotPersistedException;
21
use Oc\Repository\Exception\RecordsNotFoundException;
22
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\Routing\Annotation\Route;
26
27
/**
28
 * Class SupportController
29
 *
30
 * @package Oc\Controller\Backend
31
 */
32
class SupportController extends AbstractController
33
{
34
    /** @var Connection */
35
    private $connection;
36
37
    /** @var CacheAdoptionsRepository */
38
    private $cacheAdoptionsRepository;
39
40
    /** @var CacheCoordinatesRepository */
41
    private $cacheCoordinatesRepository;
42
43
    /** @var CacheLogsArchivedRepository */
44
    private $cacheLogsArchivedRepository;
45
46
    /** @var CachesRepository */
47
    private $cachesRepository;
48
49
    /** @var CacheReportsRepository */
50
    private $cacheReportsRepository;
51
52
    /** @var CacheStatusModifiedRepository */
53
    private $cacheStatusModifiedRepository;
54
55
    /** @var CacheStatusRepository */
56
    private $cacheStatusRepository;
57
58
    /**
59
     * SupportController constructor.
60
     *
61
     * @param Connection $connection
62
     * @param CacheAdoptionsRepository $cacheAdoptionsRepository
63
     * @param CacheCoordinatesRepository $cacheCoordinatesRepository
64
     * @param CacheLogsArchivedRepository $cacheLogsArchivedRepository
65
     * @param CachesRepository $cachesRepository
66
     * @param CacheReportsRepository $cacheReportsRepository
67
     * @param CacheStatusModifiedRepository $cacheStatusModifiedRepository
68
     * @param CacheStatusRepository $cacheStatusRepository
69
     */
70
    public function __construct(
71
        Connection $connection,
72
        CacheAdoptionsRepository $cacheAdoptionsRepository,
73
        CacheCoordinatesRepository $cacheCoordinatesRepository,
74
        CacheLogsArchivedRepository $cacheLogsArchivedRepository,
75
        CachesRepository $cachesRepository,
76
        CacheReportsRepository $cacheReportsRepository,
77
        CacheStatusModifiedRepository $cacheStatusModifiedRepository,
78
        CacheStatusRepository $cacheStatusRepository
79
    ) {
80
        $this->connection = $connection;
81
        $this->cacheAdoptionsRepository = $cacheAdoptionsRepository;
82
        $this->cacheCoordinatesRepository = $cacheCoordinatesRepository;
83
        $this->cacheLogsArchivedRepository = $cacheLogsArchivedRepository;
84
        $this->cachesRepository = $cachesRepository;
85
        $this->cacheReportsRepository = $cacheReportsRepository;
86
        $this->cacheStatusModifiedRepository = $cacheStatusModifiedRepository;
87
        $this->cacheStatusRepository = $cacheStatusRepository;
88
    }
89
90
    /**
91
     * @return Response
92
     * @Route("/support", name="support_index")
93
     */
94
    public function index()
95
    : Response
96
    {
97
        return $this->render('backend/support/index.html.twig');
98
    }
99
100
    /**
101
     * @param Request $request
102
     *
103
     * @return Response
104
     * @Route("/supportSearch", name="support_search")
105
     */
106
    public function searchCachesAndUser(Request $request)
107
    : Response {
108
        $fetchedCaches = '';
109
        $limit = false;
110
111
        $formSearch = $this->createForm(SupportSearchCaches::class);
112
113
        $formSearch->handleRequest($request);
114
        if ($formSearch->isSubmitted() && $formSearch->isValid()) {
115
            $inputData = $formSearch->getData();
116
117
            if ($formSearch->getClickedButton() === $formSearch->get('search_One')) {
118
                $limit = true;
119
            }
120
121
            $fetchedCaches = $this->getCachesForSearchField($inputData['content_support_searchfield'], $limit);
122
        }
123
124
        return $this->render(
125
            'backend/support/searchedCaches.html.twig', [
126
                                                          'supportCachesForm' => $formSearch->createView(),
127
                                                          'foundCaches' => $fetchedCaches
128
                                                      ]
129
        );
130
    }
131
132
    /**
133
     * @return Response
134
     * @throws RecordNotFoundException
135
     * @throws RecordsNotFoundException
136
     * @Route("/reportedCaches", name="support_reported_caches")
137
     */
138
    public function listReportedCaches()
139
    : Response
140
    {
141
        $fetchedReports = $this->getReportedCaches();
142
143
        $formSearch = $this->createForm(SupportSearchCaches::class);
144
145
        return $this->render(
146
            'backend/support/reportedCaches.html.twig', [
147
                                                          'supportCachesForm' => $formSearch->createView(),
148
                                                          'reportedCaches_by_id' => $fetchedReports
149
                                                      ]
150
        );
151
    }
152
153
    /**
154
     * @param Request $request
155
     *
156
     * @return Response
157
     * @Route("/dbQueries", name="support_db_queries")
158
     */
159
    public function listDbQueries(Request $request)
160
    : Response {
161
        $fetchedInformation = [];
162
163
        $formSearch = $this->createForm(SupportSearchCaches::class);
164
165
        $form = $this->createForm(SupportSQLFlexForm::class);
166
167
        $form->handleRequest($request);
168
169
        if ($form->isSubmitted() && $form->isValid()) {
170
            $inputData = $form->getData();
171
172
            $fetchedInformation = $this->executeSQL_flexible($inputData['content_WHAT'], $inputData['content_TABLE']);
173
174
            $countFetched = count($fetchedInformation);
175
            for ($i = 0; $i < $countFetched; $i ++) {
176
                if (array_key_exists('password', $fetchedInformation[$i])) {
177
                    $fetchedInformation[$i]['password'] = '-';
178
                }
179
                if (array_key_exists('logpw', $fetchedInformation[$i])) {
180
                    $fetchedInformation[$i]['logpw'] = '-';
181
                }
182
                if (array_key_exists('admin_password', $fetchedInformation[$i])) {
183
                    $fetchedInformation[$i]['admin_password'] = '-';
184
                }
185
            }
186
        }
187
188
        return $this->render(
189
            'backend/support/databaseQueries.html.twig', [
190
                                                           'supportCachesForm' => $formSearch->createView(),
191
                                                           'SQLFlexForm' => $form->createView(),
192
                                                           'suppSQLqueryFlex' => $fetchedInformation
193
                                                       ]
194
        );
195
    }
196
197
    /**
198
     * @param int $repID
199
     *
200
     * @return Response
201
     * @throws RecordNotFoundException
202
     * @throws RecordsNotFoundException
203
     * @Route("/repCaches/{repID}", name="support_reported_cache")
204
     */
205
    public function list_reported_cache_details(int $repID)
206
    : Response {
207
        $formSearch = $this->createForm(SupportSearchCaches::class);
208
        $formComment = $this->createForm(SupportAdminComment::class);
209
210
        $fetchedReport = $this->cacheReportsRepository->fetchOneBy(['id' => $repID]);
211
212
        $fetchedStatus = $this->cacheStatusRepository->fetchAll();
213
214
        $fetchedStatusModfied = $this->cacheStatusModifiedRepository->fetchBy(['cache_id' => $fetchedReport->cacheid]);
215
216
        return $this->render(
217
            'backend/support/reportedCacheDetails.html.twig', [
218
                                                                'supportCachesForm' => $formSearch->createView(),
219
                                                                'supportAdminCommentForm' => $formComment->createView(),
220
                                                                'reported_cache_by_id' => $fetchedReport,
221
                                                                'cache_status' => $fetchedStatus,
222
                                                                'report_status_modified' => $fetchedStatusModfied
223
                                                            ]
224
        );
225
    }
226
227
    /**
228
     * @param Request $request
229
     *
230
     * @return Response
231
     * @throws DBALException
232
     * @throws RecordNotFoundException
233
     * @throws RecordNotPersistedException
234
     * @Route("/repCachesSaveText", name="support_reported_cache_save_text")
235
     */
236
    public function repCaches_saveTextArea(Request $request)
237
    : Response {
238
        $form = $this->createForm(SupportAdminComment::class)->handleRequest($request);
239
240
        if ($form->isSubmitted() && $form->isValid()) {
241
            $inputData = $form->getData();
242
243
            $entity = $this->cacheReportsRepository->fetchOneBy(['id' => (int) $inputData['hidden_repID']]);
244
            $entity->comment = $inputData['support_admin_comment'];
245
246
            $this->cacheReportsRepository->update($entity);
247
248
            return $this->redirectToRoute('backend_support_reported_cache', ['repID' => $entity->id]);
249
        }
250
251
        return $this->redirectToRoute('backend_support_reported_caches');
252
    }
253
254
    /**
255
     * @param int $repID
256
     * @param int $adminId
257
     * @param string $route
258
     *
259
     * @return Response
260
     * @throws DBALException
261
     * @throws RecordNotFoundException
262
     * @throws RecordNotPersistedException
263
     * @route("/repCachesAssignSupportuser/{repID}&{adminId}&{route}", name="support_reported_cache_supportuser_assignment")
264
     */
265 View Code Duplication
    public function repCaches_supportuser_assignment(int $repID, int $adminId, string $route)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
266
    : Response {
267
        $entity = $this->cacheReportsRepository->fetchOneBy(['id' => $repID]);
268
        $entity->adminid = $adminId;
269
270
        $this->cacheReportsRepository->update($entity);
271
272
        return $this->redirectToRoute($route, ['repID' => $repID]);
273
    }
274
275
    /**
276
     * @param int $repID
277
     * @param string $route
278
     *
279
     * @return Response
280
     * @throws DBALException
281
     * @throws RecordNotFoundException
282
     * @throws RecordNotPersistedException
283
     * @route("/repCachesAssignSupportuser/{repID}&{route}", name="support_reported_cache_set_status")
284
     */
285 View Code Duplication
    public function repCaches_setReportStatus(int $repID, string $route)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
286
    : Response {
287
        $entity = $this->cacheReportsRepository->fetchOneBy(['id' => $repID]);
288
        $entity->status = 3; // ToDo: die '3' hart vorgeben? Oder irgendwie
289
290
        $this->cacheReportsRepository->update($entity);
291
292
        return $this->redirectToRoute($route, ['repID' => $repID]);
293
    }
294
295
    /**
296
     * @param string $wpID
297
     *
298
     * @return Response
299
     * @throws RecordNotFoundException
300
     * @throws RecordsNotFoundException
301
     * @Route("/cacheHistory/{wpID}", name="support_cache_history")
302
     */
303
    public function list_cache_history(string $wpID)
304
    : Response {
305
        $formSearch = $this->createForm(SupportSearchCaches::class);
306
307
        $fetchedId = $this->cachesRepository->getIdByWP($wpID);
308
309
        $fetchedReports = $this->cacheReportsRepository->fetchBy(['cacheid' => $fetchedId]);
310
311
        $fetchedLogDeletes = $this->cacheLogsArchivedRepository->fetchBy(['cache_id' => $fetchedId]);
312
313
        $fetchedStatusModfied = $this->cacheStatusModifiedRepository->fetchBy(['cache_id' => $fetchedId]);
314
315
        $fetchedCoordinates = $this->cacheCoordinatesRepository->fetchBy(['cache_id' => $fetchedId]);
316
317
        $fetchedAdoptions = $this->cacheAdoptionsRepository->fetchBy(['cache_id' => $fetchedId]);
318
319
        return $this->render(
320
            'backend/support/cacheHistory.html.twig', [
321
                                                        'supportCachesForm' => $formSearch->createView(),
322
                                                        'cache_reports' => $fetchedReports,
323
                                                        'deleted_logs' => $fetchedLogDeletes,
324
                                                        'report_status_modified' => $fetchedStatusModfied,
325
                                                        'changed_coordinates' => $fetchedCoordinates,
326
                                                        'cache_adoptions' => $fetchedAdoptions,
327
                                                    ]
328
        );
329
    }
330
331
    /**
332
     * @param string $searchtext
333
     * @param bool $limit
334
     *
335
     * @return array
336
     */
337
    public function getCachesForSearchField(string $searchtext, bool $limit = false)
338
    : array {
339
        //      so sieht die SQL-Vorlage aus..
340
        //        SELECT name, wp_oc, wp_gc, wp_gc_maintained, user.username, user.email
341
        //        FROM caches
342
        //        INNER JOIN user ON caches.user_id = user.user_id
343
        //        WHERE wp_oc         =       "' . $searchtext . '"
344
        //        OR wp_gc            =       "' . $searchtext . '"
345
        //        OR wp_gc_maintained =       "' . $searchtext . '"
346
        //        OR caches.name     LIKE    "%' . $searchtext . '%"'
347
        //        OR user.username   LIKE    "%' . $searchtext . '%"'
348
        //        OR user.email      LIKE    "%' . $searchtext . '%"'
349
        //        LIMIT $limit
350
        $qb = $this->connection->createQueryBuilder();
351
        $qb->select('caches.name', 'caches.wp_oc', 'caches.wp_gc', 'caches.wp_gc_maintained', 'user.username', 'user.email')
352
            ->from('caches')
353
            ->innerJoin('caches', 'user', 'user', 'caches.user_id = user.user_id')
354
            ->where('caches.wp_oc = :searchTerm')
355
            ->orWhere('caches.wp_gc = :searchTerm')
356
            ->orWhere('caches.wp_gc_maintained = :searchTerm')
357
            ->orWhere('caches.name LIKE :searchTermLIKE')
358
            ->orWhere('user.username LIKE :searchTermLIKE')
359
            ->orWhere('user.email LIKE :searchTermLIKE')
360
            ->setParameters(['searchTerm' => $searchtext, 'searchTermLIKE' => '%' . $searchtext . '%'])
361
            ->orderBy('caches.wp_oc', 'ASC');
362
363
        if ($limit === true) {
364
            $qb->setMaxResults(1);
365
        }
366
367
        return $qb->execute()->fetchAll();
368
    }
369
370
    /**
371
     * @return array
372
     * @throws RecordNotFoundException
373
     * @throws RecordsNotFoundException
374
     */
375
    public function getReportedCaches()
376
    : array {
377
        return $this->cacheReportsRepository->fetchAll();
378
    }
379
380
    /**
381
     * @param int $days
382
     *
383
     * @return Response
384
     * @Route("/dbQueries1/{days}", name="support_db_queries_1")
385
     */
386 View Code Duplication
    public function executeSQL_caches_old_reg_date(int $days = 31) // List caches from users whose registration date is not older than x days.
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
387
    : Response
388
    {
389
        $formSearch = $this->createForm(SupportSearchCaches::class);
390
391
        $qb = $this->connection->createQueryBuilder();
392
        $qb->select('caches.name', 'user.username', 'user.date_created', 'user.last_login')
393
            ->from('caches')
394
            ->innerJoin('caches', 'user', 'user', 'caches.user_id = user.user_id')
395
            ->where('user.date_created > now() - interval :searchTerm DAY')
396
            ->andWhere('caches.user_id = user.user_id')
397
            ->setParameters(['searchTerm' => $days])
398
            ->orderBy('date_created', 'DESC');
399
400
        return $this->render(
401
            'backend/support/databaseQueries.html.twig', [
402
                                                           'supportCachesForm' => $formSearch->createView(),
403
                                                           'suppSQLquery1' => $qb->execute()->fetchAll()
404
                                                       ]
405
        );
406
    }
407
408
    /**
409
     * @param int $days
410
     *
411
     * @return Response
412
     * @Route("/dbQueries2/{days}", name="support_db_queries_2")
413
     */
414
    public function executeSQL_old_reg_date(int $days) // List user whose registration date is no older than x days.
415
    : Response
416
    {
417
        $formSearch = $this->createForm(SupportSearchCaches::class);
418
419
        $qb = $this->connection->createQueryBuilder();
420
        $qb->select('username', 'date_created', 'last_login')
421
            ->from('user')
422
            ->where('date_created > now() - interval :searchTerm DAY')
423
            ->setParameters(['searchTerm' => $days])
424
            ->orderBy('date_created', 'DESC');
425
426
        return $this->render(
427
            'backend/support/databaseQueries.html.twig', [
428
                                                           'supportCachesForm' => $formSearch->createView(),
429
                                                           'suppSQLquery2' => $qb->execute()->fetchAll()
430
                                                       ]
431
        );
432
    }
433
434
    /**
435
     * @return Response
436
     * @Route("/dbQueries4", name="support_db_queries_4")
437
     */
438 View Code Duplication
    public function executeSQL_caches_old_login_date(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
439
    ) // List (non-archived, non-locked) caches from users whose last login date is older than one year.
440
    : Response
441
    {
442
        $formSearch = $this->createForm(SupportSearchCaches::class);
443
444
        $qb = $this->connection->createQueryBuilder();
445
        $qb->select('caches.name', 'caches.cache_id', 'caches.status', 'user.username', 'user.last_login')
446
            ->from('caches')
447
            ->innerJoin('caches', 'user', 'user', 'caches.user_id = user.user_id')
448
            ->where('user.last_login < now() - interval :searchTerm YEAR')
449
            ->andWhere('caches.status <= 2')
450
            ->andWhere(('caches.user_id = user.user_id'))
451
            ->setParameters(['searchTerm' => 1])
452
            ->orderBy('user.last_login', 'ASC');
453
454
        return $this->render(
455
            'backend/support/databaseQueries.html.twig', [
456
                                                           'supportCachesForm' => $formSearch->createView(),
457
                                                           'suppSQLquery4' => $qb->execute()->fetchAll()
458
                                                       ]
459
        );
460
    }
461
462
    /**
463
     * @param string $what
464
     * @param string $table
465
     *
466
     * @return array
467
     */
468
    public function executeSQL_flexible(string $what, string $table)
469
    : array {
470
        $qb = $this->connection->createQueryBuilder();
471
        $qb->select($what)
472
            ->from($table);
473
474
        return ($qb->execute()->fetchAll());
475
    }
476
}
477