Completed
Push — next ( 80f341...5537c5 )
by Thomas
08:03 queued 04:11
created

CachesController::search_by_cache_wp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
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 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
14
class CachesController extends AbstractController
15
{
16
    /**
17
     * @Route("/caches", name="caches_index")
18
     */
19
    public function index(Connection $connection, Request $request)
20
    : Response {
21
        $fetched_caches = '0';
22
23
        // create input field for caches_by_searchfield
24
        $form = $this->createForm(CachesFormType::class);
25
26
        // see: https://symfonycasts.com/screencast/symfony-forms/form-submit
27
        // handles the request (submit-button of the form), but only if there is a POST request
28
        $form->handleRequest($request);
29
        // if is true only if there is a request submitted and it is valid
30
        if ($form->isSubmitted() && $form->isValid()) {
31
            // read content of form input field
32
            $input_data = $form->getData();
33
34
            // send request to DB
35
            $fetched_caches = $this->get_caches_basic_data($connection, $input_data["content_caches_searchfield"]);
36
        }
37
38
        return $this->render(
39
            'backend/caches/index.html.twig', [
40
                                                'cachesForm' => $form->createView(),
41
                                                'caches_by_searchfield' => $fetched_caches
42
                                            ]
43
        );
44
    }
45
46
    /**
47
     * @Route("/cache/{wp_oc}", name="cache_by_wp_oc")
48
     */
49
    public function search_by_cache_wp(Connection $connection, string $wp_oc)
50
    : Response {
51
        $fetched_caches = $this->get_caches_details_data($connection, $wp_oc);
52
53
        return $this->render('backend/caches/index.html.twig', ['cache_by_id' => $fetched_caches]);
54
    }
55
56
    /**
57
     *
58
     */
59
    function get_caches_basic_data(Connection $connection, string $searchtext)
60
    : array {
61
        $fetched_caches = [];
62
63
        if ($searchtext != "") {
64
            // search in database for the given $searchtext in wp_oc, wp_gc, wp_nc and name
65
            $fetched_caches = $connection->fetchAll(
66
                'SELECT cache_id, name, wp_oc, wp_gc, wp_nc FROM caches
67
                 WHERE wp_oc         =       "' . $searchtext . '"
68
                 OR caches.wp_gc     =       "' . $searchtext . '"
69
                 OR caches.wp_nc     =       "' . $searchtext . '"
70
                 OR caches.name     LIKE    "%' . $searchtext . '%"'
71
            );
72
        }
73
74
        return $fetched_caches;
75
    }
76
77
    /**
78
     *
79
     */
80
    function get_caches_details_data(Connection $connection, string $searchtext)
81
    : array {
82
        $fetched_caches = [];
83
84
        if ($searchtext != "") {
85
            $sql_string = '
86
            SELECT caches.cache_id, caches.wp_oc, caches.wp_gc, caches.wp_nc, caches.name, 
87
                   caches.date_hidden, caches.date_created, caches.is_publishdate, caches.latitude, caches.longitude,
88
                   caches.difficulty, caches.terrain, caches.size, caches.logpw,
89
                   cache_status.name as cache_status_name, cache_type.icon_large as cache_type_picture, 
90
                   cache_size.name as cache_size_name, user.username
91
            FROM caches
92
            INNER JOIN user ON caches.user_id = user.user_id
93
            INNER JOIN cache_status ON caches.status = cache_status.id
94
            INNER JOIN cache_type ON caches.type = cache_type.id
95
            INNER JOIN cache_size ON caches.size = cache_size.id
96
            WHERE caches.wp_oc       = "' . $searchtext . '"
97
                  or caches.wp_gc    = "' . $searchtext . '"
98
                  or caches.wp_nc    = "' . $searchtext . '"
99
                  or caches.name LIKE "%' . $searchtext . '%"
100
            ';
101
102
            $fetched_caches = $connection->fetchAll($sql_string);
103
104
            $array_size = count($fetched_caches);
105
106
            for ($i = 0; $i < $array_size; $i ++) {
107
                // replace existing log passwords with something different
108
                // nur der Teil mit den Bilderzuweisungen müsste nochmal überdacht werden..
109
                if ($fetched_caches[$i]["logpw"] != "") {
110
                    $fetched_caches[$i]["logpw"] = 1;
111
                } else {
112
                    $fetched_caches[$i]["logpw"] = 0;
113
                }
114
115
                // replace cache type information with picture links
116
                // auch hier müsste die Bildzuweisung nochmal überarbeitet werden..
117
                $fetched_caches[$i]["cache_type_picture"] =
118
                    "https://www.opencaching.de/resource2/ocstyle/images/cacheicon/"
119
                    . $fetched_caches[$i]["cache_type_picture"];
120
            }
121
        }
122
123
        return $fetched_caches;
124
    }
125
}
126