lollipop

A PHP-framework
Log | Files | Refs

commit 00b0053a29cf3430209ae68111919b38ab5269d8
parent fa11251b474626b5d570cee37daeb11aaf09139a
Author: Gerco van Woudenbergh <[email protected]>
Date:   Tue, 30 May 2023 15:44:55 +0200

starting to seperate php and html with the templating language

Diffstat:
Mindex.php | 11+++++++++--
Alogic/user/add_get.php | 18++++++++++++++++++
Alogic/user/add_post.php | 87+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtemplate_test.html | 2+-
Aviews/add_user.html | 37+++++++++++++++++++++++++++++++++++++
Dviews/add_user.php | 147-------------------------------------------------------------------------------
6 files changed, 152 insertions(+), 150 deletions(-)

diff --git a/index.php b/index.php @@ -3,8 +3,8 @@ require_once "utils/autoloader.php"; $router = new Lollipop\Router(); + $router->addRoute(["GET", "POST"], "/user/:email/update", "views/alter_user.php"); -$router->addRoute(["GET", "POST"], "/user/add", "views/add_user.php"); $router->addRoute(["GET", "POST"], "/user/:email/crud", "views/crud_user.php"); $router->addRoute(["GET", "POST"], "/user/search", "views/search_user.php"); $router->addRoute(["GET", "POST"], "/dashboard", "views/dashboard.php"); @@ -13,9 +13,16 @@ $router->addRoute(["GET", "POST"], "/logout", "logic/logout.php"); $router->addRoute(["GET", "POST"], "/course/search", "views/search_course.php"); $router->addRoute(["GET", "POST"], "/course/:enroll/enroll", "views/search_course.php"); $router->addRoute(["GET", "POST"], "/course/:unsubscribe/unsubscribe", "views/search_course.php"); -$router->addRoute(["GET", "POST"], "/test/template/:hello", function($vars) { +$router->addRoute(["GET"], "/test/template/:hello", function($vars) { $t = new Lollipop\Template(); echo $t->template("template_test.html", $vars); }); +$router->addRoute(["GET"], "/user/add", function($vars) { + include "logic/user/add_get.php"; + $vars = database_permissions(); + $t = new Lollipop\Template(); + echo $t->template("views/add_user.html", $vars); +}); +$router->addRoute(["POST"], "/user/add", "logic/add_user_post.php"); $router->route(); \ No newline at end of file diff --git a/logic/user/add_get.php b/logic/user/add_get.php @@ -0,0 +1,17 @@ +<?php +include "utils/autoloader.php"; +//select the available permissions from the database +function database_permissions():array{ + $db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "lollipop"); + $all_p = $db->all(Model\Permission::class); + $html = ""; + foreach ($all_p as $db_permission) { + $html .= "<div class=\"mb-3 form-check\"> + <input type=\"checkbox\" class=\"form-check-input\" name=\"permissions[]\" value=" . $db_permission->id . "\"> + <label class=\"form-check-label\" for=" . $db_permission->name . ">" . $db_permission->name . "</label> + </div>"; + } + return ["permission_radials" => $html]; +} +?> + +\ No newline at end of file diff --git a/logic/user/add_post.php b/logic/user/add_post.php @@ -0,0 +1,86 @@ +<?php + if ($_SERVER["REQUEST_METHOD"] == "POST") { + $errors = array(); // initialize an empty array to store errors + + // Check if voornaam is set and not empty + if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) { + $fname = $_POST['voornaam']; + } else { + $errors[] = "Voornaam is required"; + } + + // Check if achternaam is set and not empty + if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) { + $lname = $_POST['achternaam']; + } else { + $errors[] = "Achternaam is required"; + } + + // Check if email is set and not empty + if (isset($_POST['email']) && !empty($_POST['email'])) { + $email = $_POST['email']; + } else { + $errors[] = "E-mail is required"; + } + + // Check if password is set and not empty + if (isset($_POST['password']) && !empty($_POST['password'])) { + $password = $_POST['password']; + } else { + $errors[] = "Wachtwoord is required"; + } + + // Check if permissions is set + if (isset($_POST['permissions'])) { + $permissions = $_POST['permissions']; + } else { + $errors[] = "Permissies zijn vereist"; + } + + // Check if there are any errors + if (count($errors) > 0) { + // Print out the errors + foreach ($errors as $error) { + echo $error . "<br>"; + } + } else { + // Pass the password through a hashing function + $hashed_pwd = password_hash($password, PASSWORD_DEFAULT); + + //create a database object with table user + $u = $db->get(Model\User::class); + + //check if email already exists + if ($u->load($email)) { + echo "this email address is taken: " . $email; + } else { + $succes = false; + //set new user data + $u->email = $email; + $u->fname = $fname; + $u->lname = $lname; + $u->pwd = $hashed_pwd; + + //add user with the add function + if ($u->insert()) { + $succes = true; + } + + + //create a database object with table permission for each permission + //set the data and execute the add function + foreach ($permissions as $permission) { + $p = $db->get(Model\PermissionUser::class); + $p->email = $email; + $p->id = (int) $permission; + if ($p->insert()) { + $succes = true; + } + } + if ($succes) { + echo "succes!"; + } + } + } + } + ?> +\ No newline at end of file diff --git a/template_test.html b/template_test.html @@ -1 +1 @@ -data: {{ 3 !add }} +data: {{ 3 3 !add }} diff --git a/views/add_user.html b/views/add_user.html @@ -0,0 +1,36 @@ +<!DOCTYPE html> +<html lang="eng"> +<head> + <title>Add user</title> + <!-- Bootstrap CSS --> + <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> +</head> +<body> + <div class="container"> + <h1>Add user</h1> + + <form action="/user/add" method="post"> + <div class="mb-3"> + <label for="voornaam" class="form-label"><b>Voornaam:</b></label> + <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="Voornaam"> + </div> + <div class="mb-3"> + <label for="achternaam" class="form-label"><b>Achternaam:</b></label> + <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Achternaam"> + </div> + <div class="mb-3"> + <label for="email" class="form-label"><b>Email:</b></label> + <input type="text" class="form-control" name="email" id="email" placeholder="Email"> + </div> + <div class="mb-3"> + <label for="password" class="form-label"><b>Wachtwoord:</b></label> + <input type="password" class="form-control" name="password" id="password" placeholder="******"> + </div> + <p>Please select the user permissions:</p> + {{ permission_radials }} + <button type="submit" class="btn btn-primary" name="submit">Add user</button> + </form> + </div> +</body> + +</html> +\ No newline at end of file diff --git a/views/add_user.php b/views/add_user.php @@ -1,146 +0,0 @@ -<!DOCTYPE html> -<html lang="eng"> - -<head> - <title>Add user</title> - <!-- Bootstrap CSS --> - <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"> -</head> -<?php -include "navbar.php"; -include "utils/autoloader.php"; -if (!in_array(0, $_SESSION['permissions'])) { - header('Location: /dashboard'); - exit; -} -$db = new Lollipop\SQLDatabase("86.92.67.21", "friedel", "hailiwa", "lollipop"); -//select the available permissions from the database -$all_p = $db->all(Model\Permission::class); -$available_permissions = []; -foreach ($all_p as $tmp) { - $available_permissions[] = ['id' => $tmp->id, 'name' => $tmp->name]; -} -?> - -<body> - <div class="container"> - <h1>Add user</h1> - - <form action="/user/add" method="post"> - <div class="mb-3"> - <label for="voornaam" class="form-label"><b>Voornaam:</b></label> - <input type="text" class="form-control" name="voornaam" id="voornaam" placeholder="Voornaam"> - </div> - <div class="mb-3"> - <label for="achternaam" class="form-label"><b>Achternaam:</b></label> - <input type="text" class="form-control" name="achternaam" id="achternaam" placeholder="Achternaam"> - </div> - <div class="mb-3"> - <label for="email" class="form-label"><b>Email:</b></label> - <input type="text" class="form-control" name="email" id="email" placeholder="Email"> - </div> - <div class="mb-3"> - <label for="password" class="form-label"><b>Wachtwoord:</b></label> - <input type="password" class="form-control" name="password" id="password" placeholder="******"> - </div> - <p>Please select the user permissions:</p> - <?php - foreach ($available_permissions as $db_permission) { - echo "<div class=\"mb-3 form-check\"> - <input type=\"checkbox\" class=\"form-check-input\" name=\"permissions[]\" value=" . $db_permission['id'] . "\"> - <label class=\"form-check-label\" for=" . $db_permission['name'] . ">" . $db_permission['name'] . "</label> - </div>"; - } - ?> - <button type="submit" class="btn btn-primary" name="submit">Add user</button> - </form> - </div> - <?php - if ($_SERVER["REQUEST_METHOD"] == "POST") { - $errors = array(); // initialize an empty array to store errors - - // Check if voornaam is set and not empty - if (isset($_POST['voornaam']) && !empty($_POST['voornaam'])) { - $fname = $_POST['voornaam']; - } else { - $errors[] = "Voornaam is required"; - } - - // Check if achternaam is set and not empty - if (isset($_POST['achternaam']) && !empty($_POST['achternaam'])) { - $lname = $_POST['achternaam']; - } else { - $errors[] = "Achternaam is required"; - } - - // Check if email is set and not empty - if (isset($_POST['email']) && !empty($_POST['email'])) { - $email = $_POST['email']; - } else { - $errors[] = "E-mail is required"; - } - - // Check if password is set and not empty - if (isset($_POST['password']) && !empty($_POST['password'])) { - $password = $_POST['password']; - } else { - $errors[] = "Wachtwoord is required"; - } - - // Check if permissions is set - if (isset($_POST['permissions'])) { - $permissions = $_POST['permissions']; - } else { - $errors[] = "Permissies zijn vereist"; - } - - // Check if there are any errors - if (count($errors) > 0) { - // Print out the errors - foreach ($errors as $error) { - echo $error . "<br>"; - } - } else { - // Pass the password through a hashing function - $hashed_pwd = password_hash($password, PASSWORD_DEFAULT); - - //create a database object with table user - $u = $db->get(Model\User::class); - - //check if email already exists - if ($u->load($email)) { - echo "this email address is taken: " . $email; - } else { - $succes = false; - //set new user data - $u->email = $email; - $u->fname = $fname; - $u->lname = $lname; - $u->pwd = $hashed_pwd; - - //add user with the add function - if ($u->insert()) { - $succes = true; - } - - - //create a database object with table permission for each permission - //set the data and execute the add function - foreach ($permissions as $permission) { - $p = $db->get(Model\PermissionUser::class); - $p->email = $email; - $p->id = (int) $permission; - if ($p->insert()) { - $succes = true; - } - } - if ($succes) { - echo "succes!"; - } - } - } - } - ?> -</body> - -</html> -\ No newline at end of file