View sourcecode

The following files exists in this folder. Click to view.

categories.php

153 lines UTF-8 Unix (LF)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
session_start
();
// Startar sessionen för att kunna använda $_SESSION

if (!isset($_SESSION["userID"])) {
    
header("location: login.php");
    exit();
    
// Om användaren inte är inloggad, omdirigera till login-sidan
}

require_once 
"functions/database.php";
// Inkluderar databashanteringsfilen

$userinfo $db->query("SELECT * FROM accountData WHERE userID=:userID", array("userID" => $_SESSION["userID"]));
// Hämtar all användardata från databasen

if ($userinfo["isAdmin"] == 0) {
    
header("location: index.php");
    exit();
    
// Om användaren inte är admin, omdirigera till startsidan
}

if (isset(
$_POST["save"]) && isset($_GET["categoryID"]) && isset($_POST["name"]) && isset($_POST["description"]))
    
$db->query("UPDATE category SET name=:name, description=:description WHERE categoryID=:categoryID", array(
        
"categoryID" => $_GET["categoryID"],
        
"name" => $_POST["name"],
        
"description" => $_POST["description"]
    ));
elseif (isset(
$_POST["delete"]) && isset($_GET["categoryID"])) {
    
$mediaFiles $db->queryAll("SELECT fileURL FROM mediaFiles WHERE categoryID=:categoryID", array("categoryID" => $_GET["categoryID"]));
    foreach (
$mediaFiles as $file)
        
unlink("mediaFiles/{$file['fileURL']}");
    
$db->query("DELETE FROM mediaFiles WHERE categoryID=:categoryID", array("categoryID" => $_GET["categoryID"]));
    
$db->query("DELETE FROM allowedIDs WHERE categoryID=:categoryID", array("categoryID" => $_GET["categoryID"]));
    
$db->query("DELETE FROM category WHERE categoryID=:categoryID", array("categoryID" => $_GET["categoryID"]));
} elseif (isset(
$_POST["add"]) && isset($_POST["name"]) && isset($_POST["description"]))
    
$db->query("INSERT INTO category(name,description) VALUES (:name, :description)", array(
        
"name" => $_POST["name"],
        
"description" => $_POST["description"]
    ));


?>

<!DOCTYPE html>
<html lang="en" data-bs-theme="dark">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- Bootstrap CSS från CDN -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-sRIl4kxILFvY47J16cr9ZwB07vP4J8+LH7qKQnuqkuIAvNWLzeN8tE5YBujZqJLB" crossorigin="anonymous">
    <!-- Bootstrap JS-bundle från CDN -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"
        integrity="sha384-FKyoEForCGlyvwx9Hj09JcYn3nv7wiPVlz7YYwJrWVcXK/BmnVDxM+D2scQbITxI"
        crossorigin="anonymous"></script>
</head>

<body>
    <?php include "page/navbar.php" ?>
    <!-- Inkluderar navigationsfältet -->

    <div class="container">
        <br>
        <h1>Kategorier</h1>
        <?php
        $categories 
$db->queryAll("SELECT * FROM category");
        
// Hämtar alla kategorier
        

        //echo "<pre>"; var_export($data); echo "</pre>";
        
if (!$categories) {
            echo 
'<h5 class="text-center" style="color: rgb(100,100,100)";>Inga kategorier finns.</h5>';
            
// Om inga kategorier finns, visa meddelande
        
}

        
$cardRowSize 2;
        
// Antal kolumner per rad för kort-layout
        
        
function print_cell($data)
        {
            
// Funktion för att skriva ut ett kort (card) med media
            
if (isset($data))
                echo <<<EOD
                    <div class="col">
                        <div class="card">
                            <div class="card-body">
                                <form method="POST" action="categories.php?categoryID=
{$data["categoryID"]}">
                                    <div class="mb-3">
                                        <label for="category-name
{$data["categoryID"]}" class="form-label">Namn</label>
                                        <div class="input-group">
                                            <input type="text" name="name" class="form-control" id="category-name
{$data["categoryID"]}" value="{$data["name"]}">
                                        </div>
                                    </div>
                                    <div class="mb-3">
                                        <label for="category-desc
{$data["categoryID"]}" class="form-label">Beskrivning</label>
                                        <div class="input-group">
                                            <textarea class="form-control" name="description" id="category-desc
{$data["categoryID"]}">{$data["description"]}</textarea>
                                        </div>
                                    </div>
                                    <input type="submit" name="save" class="btn btn-info m-1" value="Spara">
                                    <input type="submit" name="delete" class="btn btn-danger m-1" value="Ta bort">
                                </form>
                            </div>
                        </div>
                    </div>
                    EOD;
            else
                echo 
'<div class="col"></div>';
            
// Om ingen data finns, skriv ut en tom kolumn
        
}

        for (
$row 0$row ceil(sizeof($categories) / $cardRowSize); $row++) {
            echo 
'<div class="row">';
            for (
$col 0$col $cardRowSize$col++) {
                if (isset(
$categories[$row $cardRowSize $col]))
                    
print_cell($categories[$row $cardRowSize $col]);
                else
                    
print_cell(null);
                
// Loopar igenom media-data och skapar rader/kolumner med kort
            
}
            echo 
'</div><br>';


        }
        
?>
        <br>
        <h1>Lägg till ny kategori</h1>
        <div class="card">
            <div class="card-body">
                <form method="POST" action="categories.php">
                    <div class="mb-3">
                        <label for="newcategory-name" class="form-label">Namn</label>
                        <div class="input-group">
                            <input type="text" name="name" class="form-control" id="newcategory-name">
                        </div>
                    </div>
                    <div class="mb-3">
                        <label for="newcategory-desc" class="form-label">Beskrivning</label>
                        <div class="input-group">
                            <textarea class="form-control" name="description" id="newcategory-desc"></textarea>
                        </div>
                    </div>
                    <input type="submit" name="add" class="btn btn-info m-1" value="Lägg till">
                </form>
            </div>
        </div>
    </div>
</body>

</html>