Completed
Pull Request — development (#809)
by
unknown
04:40
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 ContainerDz0yoSZ\getConsole_ErrorListenerService;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\ORM\EntityManagerInterface;
10
use Form\CachesFormType;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\HttpFoundation\Request;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\Routing\Annotation\Route;
15
16
class CachesController extends AbstractController
17
{
18
    /**
19
     * @Route("/caches", name="caches_index")
20
     */
21
    public function index(Connection $connection, Request $request)
22
    : Response {
23
        // declare variable to avoid render-error when $request is empty // there is surely a better method..
24
        $fetched_caches = '0';
25
26
        // create input field for caches_by_searchfield
27
        $form = $this->createForm(CachesFormType::class);
28
29
        // see: https://symfonycasts.com/screencast/symfony-forms/form-submit
30
        // handles the request (submit-button of the form), but only if there is a POST request
31
        $form->handleRequest($request);
32
        // if is true only if there is a request submitted and it is valid
33
        if ($form->isSubmitted() && $form->isValid()) {
34
            // read content of form input field
35
            $input_data = $form->getData();
36
37
            // send request to DB
38
            $fetched_caches = $this->get_caches_base_data($connection, $input_data["content_caches_searchfield"]);
39
        }
40
41
        // load all caches from database and hand over to twig page
42
        // just for initial test to learn how it works.. leave it here for later check up
43
        // $caches = $connection->fetchAll('SELECT * FROM caches');
44
45
        return $this->render(
46
            'backend/caches/index.html.twig', [
47
                                                'cachesForm' => $form->createView(),
48
                                                'caches_by_searchfield' => $fetched_caches
49
                                            ]
50
        );
51
    }
52
53
    //    /**
54
    //     * @Route("/caches/caches_by_searchfield", name="create_form_caches_by_searchfield")
55
    //     */
56
    //    public function create_form_caches_by_searchfield(EntityManagerInterface $em)
57
    //    {
58
    //        $form = $this->createForm(CachesFormType::class);
59
    //
60
    //        return $this->render('backend/caches/index.html.twig', ['cachesForm' => $form->createView()]);
61
    //    }
62
63
    /**
64
     * @Route("/cache/{wp_oc}", name="cache_by_wp_oc")
65
     */
66
    public function search_by_cache_wp(Connection $connection, string $wp_oc)
67
    : Response {
68
        $fetched_caches = $this->get_caches_details_data($connection, $wp_oc);
69
70
        return $this->render('backend/caches/index.html.twig', ['cache_by_id' => $fetched_caches]);
71
    }
72
73
    /**
74
     *
75
     */
76
    function get_caches_base_data(Connection $connection, string $searchtext)
77
    : array {
78
        // search in database for the given $searchtext in wp_oc, wp_gc, wp_nc and name
79
        $fetched_caches = $connection->fetchAll(
80
            'SELECT cache_id, name, wp_oc, wp_gc, wp_nc FROM caches
81
                 WHERE wp_oc         =       "' . $searchtext . '"
82
                 OR caches.wp_gc     =       "' . $searchtext . '"
83
                 OR caches.wp_nc     =       "' . $searchtext . '"
84
                 OR caches.name     LIKE    "%' . $searchtext . '%"'
85
        );
86
87
        return $fetched_caches;
88
    }
89
90
    /**
91
     *
92
     */
93
    function get_caches_details_data(Connection $connection, string $searchtext)
94
    {
95
        $fetched_caches = '0';
96
97
        if ($searchtext != "") {
98
            $sql_string = '
99
            SELECT caches.cache_id, caches.wp_oc, caches.wp_gc, caches.wp_nc, caches.name, 
100
                   caches.date_hidden, caches.date_created, caches.is_publishdate, caches.latitude, caches.longitude,
101
                   caches.difficulty, caches.terrain, caches.size, caches.logpw,
102
                   cache_status.name as cache_status_name, cache_type.icon_large as cache_type_picture, 
103
                   cache_size.name as cache_size_name, user.username
104
            FROM caches
105
            INNER JOIN user ON caches.user_id = user.user_id
106
            INNER JOIN cache_status ON caches.status = cache_status.id
107
            INNER JOIN cache_type ON caches.type = cache_type.id
108
            INNER JOIN cache_size ON caches.size = cache_size.id
109
            WHERE caches.wp_oc       = "' . $searchtext . '"
110
                  or caches.wp_gc    = "' . $searchtext . '"
111
                  or caches.wp_nc    = "' . $searchtext . '"
112
                  or caches.name LIKE "%' . $searchtext . '%"
113
            ';
114
115
            $fetched_caches = $connection->fetchAll($sql_string);
116
117
            for ($i = 0; $i < count($fetched_caches); $i ++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
118
                // replace existing log passwords with something different
119
                // nur der Teil mit den Bilderzuweisungen müsste nochmal überdacht werden..
120
                if ($fetched_caches[$i]["logpw"] != "") {
121
                    $fetched_caches[$i]["logpw"] =
122
                        "https://www.opencaching.de/resource2/ocstyle/images/viewcache/decrypt.png";
123
                } else {
124
                    $fetched_caches[$i]["logpw"] =
125
                        "https://www.opencaching.de/resource2/ocstyle/images/attributes/cross-35x35-round.png";
126
                }
127
128
                // replace size information with picture links
129
                // auch hier müsste die Bildzuweisung nochmal überarbeitet werden..
130
                $fetched_caches[$i]["cache_type_picture"] =
131
                    "https://www.opencaching.de/resource2/ocstyle/images/cacheicon/"
132
                    . $fetched_caches[$i]["cache_type_picture"];
133
            }
134
        }
135
136
        return $fetched_caches;
137
    }
138
}
139