Completed
Pull Request — development (#809)
by
unknown
04:29
created

CachesController::get_caches_details_data()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 2
nop 2
dl 0
loc 42
rs 9.248
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
        $query = 'SELECT caches.cache_id, caches.name, caches.wp_oc, caches.wp_gc, user.username
63
                 FROM caches
64
                 INNER JOIN user ON caches.user_id = user.user_id
65
                 WHERE caches.wp_oc  =       "' . $searchtext . '"
66
                 OR caches.wp_gc     =       "' . $searchtext . '"
67
                 OR caches.name     LIKE    "%' . $searchtext . '%"
68
                 OR user.username   LIKE    "%' . $searchtext . '%"
69
                 ORDER BY caches.cache_id
70
                 ';
71
72
//        $pagination = $paginator->paginate($query, $request->query->getInt('page', 1), 10);
73
74
        if ($searchtext != "") {
75
            // search in database for the given $searchtext in wp_oc, wp_gc, wp_nc and name
76
            $fetched_caches = $connection->fetchAll($query);
77
        }
78
79
        return $fetched_caches;
80
    }
81
82
    /**
83
     *
84
     */
85
    function get_caches_details_data(Connection $connection, string $searchtext)
86
    : array {
87
        $fetched_caches = [];
88
89
        if ($searchtext != "") {
90
            $sql_string = '
91
            SELECT caches.cache_id, caches.wp_oc, caches.wp_gc, caches.wp_nc, caches.name, 
92
                   caches.date_hidden, caches.date_created, caches.is_publishdate, caches.latitude, caches.longitude,
93
                   caches.difficulty, caches.terrain, caches.size, caches.logpw,
94
                   cache_status.name as cache_status_name, cache_type.icon_large as cache_type_picture, 
95
                   cache_size.name as cache_size_name, user.username
96
            FROM caches
97
            INNER JOIN user ON caches.user_id = user.user_id
98
            INNER JOIN cache_status ON caches.status = cache_status.id
99
            INNER JOIN cache_type ON caches.type = cache_type.id
100
            INNER JOIN cache_size ON caches.size = cache_size.id
101
            WHERE caches.wp_oc         = "' . $searchtext . '"
102
            ';
103
104
            $fetched_caches = $connection->fetchAll($sql_string);
105
106
            $array_size = count($fetched_caches);
107
108
            for ($i = 0; $i < $array_size; $i ++) {
109
                // replace existing log passwords with something different
110
                // nur der Teil mit den Bilderzuweisungen müsste nochmal überdacht werden..
111
                if ($fetched_caches[$i]["logpw"] != "") {
112
                    $fetched_caches[$i]["logpw"] = 1;
113
                } else {
114
                    $fetched_caches[$i]["logpw"] = 0;
115
                }
116
117
                // replace cache type information with picture links
118
                // auch hier müsste die Bildzuweisung nochmal überarbeitet werden..
119
                $fetched_caches[$i]["cache_type_picture"] =
120
                    "https://www.opencaching.de/resource2/ocstyle/images/cacheicon/"
121
                    . $fetched_caches[$i]["cache_type_picture"];
122
            }
123
        }
124
125
        return $fetched_caches;
126
    }
127
}
128