Compare commits
14 Commits
4665e1706f
...
5ed539a471
Author | SHA1 | Date |
---|---|---|
tracer | 5ed539a471 | |
tracer | 5bc0b7966b | |
tracer | d0e1d2e87d | |
tracer | 1b794f775d | |
tracer | 744117d958 | |
tracer | cce18f6516 | |
tracer | a973a4362f | |
tracer | 3bf0c2b44f | |
tracer | ec022d09e4 | |
tracer | 148eff1557 | |
tracer | ab4a00d25c | |
tracer | 753b832cbc | |
tracer | e5f4656d71 | |
tracer | 117903ac7c |
|
@ -1,2 +1,3 @@
|
|||
As I was not allowed to use any framework, respectively no foreign code, most of the time was spent, well creating some kind of framework myself. :-)
|
||||
|
||||
The address book itself was then done in a few hours.
|
||||
|
|
|
@ -30,8 +30,7 @@ function editAddress(id) {
|
|||
})
|
||||
})
|
||||
.then(
|
||||
response => response.text() // .json(), etc.
|
||||
// same as function(response) {return response.text();}
|
||||
response => response.text()
|
||||
).then(
|
||||
html => console.log(html)
|
||||
);
|
||||
|
@ -58,5 +57,60 @@ function editAddress(id) {
|
|||
}
|
||||
}
|
||||
|
||||
function deleteAddress() {
|
||||
function deleteAddress(id) {
|
||||
console.log("del")
|
||||
if (confirm('Are you sure?')) {
|
||||
const url = "/address/delete";
|
||||
fetch(url, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
id: id
|
||||
})
|
||||
})
|
||||
.then(
|
||||
response => response.text()
|
||||
).then(
|
||||
html => console.log(html)
|
||||
);
|
||||
let row = document.getElementById('row_' + id)
|
||||
row.parentNode.removeChild(row)
|
||||
}
|
||||
}
|
||||
|
||||
function sortBy(column) {
|
||||
console.log("sortby: " + column)
|
||||
|
||||
const table = document.getElementById('address_table');
|
||||
let dirty = true;
|
||||
// loop until clean
|
||||
while (dirty) {
|
||||
console.log('dirty', dirty)
|
||||
// assume we are finished
|
||||
dirty = false
|
||||
const rows = table.rows;
|
||||
console.log(rows)
|
||||
for (let i = 1; i < (rows.length - 2); i++) {
|
||||
let x = rows[i]
|
||||
let rowXId = x.id
|
||||
let rowXNumber = rowXId.charAt(rowXId.length -1)
|
||||
let valueX = document.getElementById(column + '_' + rowXNumber).value
|
||||
|
||||
let y = rows[i + 1]
|
||||
let rowYId = y.id
|
||||
let rowYNumber = rowYId.charAt(rowYId.length -1)
|
||||
let valueY = document.getElementById(column + '_' + rowYNumber).value
|
||||
|
||||
console.log(valueX, valueY)
|
||||
// mind asc & desc
|
||||
let sortOrder = 1
|
||||
|
||||
console.log(valueX.localeCompare(valueY))
|
||||
if (valueX.localeCompare(valueY) === sortOrder) {
|
||||
console.log('switch A')
|
||||
// switch rows
|
||||
x.parentNode.insertBefore(y, x);
|
||||
dirty = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,5 +64,16 @@ $router->addRoute(name: 'app_admin_users_delete', route: '/admin/users/delete/{n
|
|||
$addressBookAdmin->adminUserDelete(parameters: $parameters);
|
||||
});
|
||||
|
||||
$router->addRoute(name: 'address_add', route: '/address/add', callback: function () use ($addressBook) {
|
||||
$addressBook->addAddress();
|
||||
});
|
||||
|
||||
$router->addRoute(name: 'address_add', route: '/address/update', callback: function () use ($addressBook) {
|
||||
$addressBook->updateAddress();
|
||||
});
|
||||
|
||||
$router->addRoute(name: 'address_add', route: '/address/delete', callback: function () use ($addressBook) {
|
||||
$addressBook->deleteAddress();
|
||||
});
|
||||
|
||||
$router->handleRouting();
|
|
@ -13,7 +13,6 @@ use App\Entity\User;
|
|||
use App\Service\Router;
|
||||
use App\Service\Template;
|
||||
use App\Repository\UserRepository;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
class AddressBookAdminController
|
||||
{
|
||||
|
@ -39,7 +38,7 @@ class AddressBookAdminController
|
|||
|
||||
public function admin(): never
|
||||
{
|
||||
// TODO $this->adminCheck();
|
||||
$this->adminCheck();
|
||||
$this->template->render(templateName: 'admin/index.html.php', vars: [
|
||||
'user' => $this->user,
|
||||
'router' => $this->router
|
||||
|
@ -49,7 +48,7 @@ class AddressBookAdminController
|
|||
|
||||
public function adminUser(): never
|
||||
{
|
||||
// TODO $this->adminCheck();
|
||||
$this->adminCheck();
|
||||
|
||||
$users = $this->userRepository->findAll();
|
||||
|
||||
|
@ -62,7 +61,7 @@ class AddressBookAdminController
|
|||
|
||||
public function adminUserEdit(array $parameters): never
|
||||
{
|
||||
// TODO $this->adminCheck();
|
||||
$this->adminCheck();
|
||||
|
||||
if (!empty($_POST)) {
|
||||
if (!empty($_POST['is_admin'])) {
|
||||
|
@ -108,8 +107,7 @@ class AddressBookAdminController
|
|||
// TODO currently breaks on inserting a duplicate nick
|
||||
if (!empty($_POST)) {
|
||||
$isAdmin = empty($_POST['is_admin']) ? 0 : 1;
|
||||
echo "isA: $isAdmin";
|
||||
$user = new User(nick: $_POST['nick'], password: $_POST['password'], first: $_POST['first'], last: $_POST['last'], isAdmin: $isAdmin);
|
||||
$user = new User(nick: $_POST['nick'], newPassword: $_POST['new_password'], first: $_POST['first'], last: $_POST['last'], isAdmin: $isAdmin);
|
||||
|
||||
if ($this->userRepository->insert(user: $user)) {
|
||||
$users = $this->userRepository->findAll();
|
||||
|
|
|
@ -10,30 +10,78 @@
|
|||
namespace App\Controller;
|
||||
|
||||
use App\Entity\User;
|
||||
use App\Entity\AddressBookEntry;
|
||||
use App\Enums\UserAuth;
|
||||
use App\Service\Router;
|
||||
use App\Service\Template;
|
||||
use App\Repository\UserRepository;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use App\Repository\AddressRepository;
|
||||
|
||||
class AddressBookController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Template $template,
|
||||
private readonly User $user,
|
||||
private readonly UserRepository $userRepository,
|
||||
private readonly AddressRepository $addressRepository,
|
||||
private readonly Router $router
|
||||
)
|
||||
{
|
||||
// empty body
|
||||
}
|
||||
|
||||
#[NoReturn]
|
||||
public function main(): void
|
||||
public function main(): never
|
||||
{
|
||||
if ($this->user->getAuth() != UserAuth::AUTH_ANONYMOUS) {
|
||||
$addresses = $this->addressRepository->findAll();
|
||||
}
|
||||
|
||||
$this->template->render(templateName: 'index.html.php', vars: [
|
||||
'user' => $this->user,
|
||||
'router' => $this->router,
|
||||
'addresses' => $addresses ?? []
|
||||
]);
|
||||
}
|
||||
|
||||
public function addAddress(): never
|
||||
{
|
||||
if (!empty($_POST)) {
|
||||
$address = new AddressBookEntry(owner: $_POST['owner'], first: $_POST['first'], last: $_POST['last'], street: $_POST['street'], zip: $_POST['zip'], city: $_POST['city'], phone: $_POST['phone']);
|
||||
|
||||
if ($this->addressRepository->insert(address: $address)) {
|
||||
$addresses = $this->addressRepository->findAll();
|
||||
|
||||
$this->template->render(templateName: 'index.html.php', vars: [
|
||||
'user' => $this->user,
|
||||
'addresses' => $addresses,
|
||||
'router' => $this->router
|
||||
]);
|
||||
} else {
|
||||
die("Error inserting user");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$this->template->render(templateName: 'addressbook/add_address.html.php', vars: [
|
||||
'user' => $this->user,
|
||||
'router' => $this->router
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function updateAddress(): void
|
||||
{
|
||||
$_POST = json_decode(json: file_get_contents(filename: "php://input"), associative: true);
|
||||
|
||||
$address = new AddressBookEntry(owner: $_POST['owner'], first: $_POST['first'], last: $_POST['last'], street: $_POST['street'], zip: $_POST['zip'], city: $_POST['city'], phone: $_POST['phone'], id: $_POST['id']);
|
||||
$this->addressRepository->update(address: $address);
|
||||
}
|
||||
|
||||
public function deleteAddress(): void
|
||||
{
|
||||
echo "in del";
|
||||
$_POST = json_decode(json: file_get_contents(filename: "php://input"), associative: true);
|
||||
if ($address = $this->addressRepository->findByID(id: $_POST['id'])) {
|
||||
$this->addressRepository->delete(addressBookEntry: $address);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -11,7 +11,6 @@ namespace App\Repository;
|
|||
|
||||
use App\Entity\AddressBookEntry;
|
||||
use App\Service\DatabaseConnection;
|
||||
use App\Entity\User;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
|
@ -27,7 +26,6 @@ class AddressRepository
|
|||
|
||||
public function findAll(string $orderBy = 'last'): array
|
||||
{
|
||||
$users = [];
|
||||
$sql = "
|
||||
SELECT id, owner, first, last, street, zip, city, phone
|
||||
FROM " . DatabaseConnection::TABLE_ADDRESSES . "
|
||||
|
@ -54,7 +52,7 @@ class AddressRepository
|
|||
{
|
||||
$sql = "
|
||||
SELECT id, owner, first, last, street, zip, city, phone
|
||||
FROM " . DatabaseConnection::TABLE_USERS . "
|
||||
FROM " . DatabaseConnection::TABLE_ADDRESSES . "
|
||||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
|
@ -104,39 +102,36 @@ class AddressRepository
|
|||
}
|
||||
|
||||
|
||||
public function update(Address $address): bool|int
|
||||
public function update(AddressBookEntry $address): bool|int
|
||||
{
|
||||
/*
|
||||
$id = $user->getId();
|
||||
$nick = $user->getNick();
|
||||
$first = $user->getFirst();
|
||||
$last = $user->getLast();
|
||||
$isAdmin = $user->isAdmin() ? 1 : 0;
|
||||
|
||||
if ($user->getPassword()) {
|
||||
$password = $user->getPassword();
|
||||
} else {
|
||||
$current = $this->findByID(id: $id);
|
||||
$password = $current->getPassword();
|
||||
}
|
||||
$id = $address->getId();
|
||||
$owner = $address->getOwner();
|
||||
$first = $address->getFirst();
|
||||
$last = $address->getLast();
|
||||
$street = $address->getStreet();
|
||||
$zip = $address->getZip();
|
||||
$city = $address->getCity();
|
||||
$phone = $address->getPhone();
|
||||
|
||||
$sql = "
|
||||
UPDATE " . DatabaseConnection::TABLE_USERS . " SET
|
||||
nick = :nick,
|
||||
password = :password,
|
||||
UPDATE " . DatabaseConnection::TABLE_ADDRESSES . " SET
|
||||
first = :first,
|
||||
last = :last,
|
||||
is_admin = :is_admin
|
||||
street = :street,
|
||||
zip = :zip,
|
||||
city = :city,
|
||||
phone = :phone
|
||||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
$statement = $this->databaseConnection->getConnection()->prepare(query: $sql);
|
||||
$statement->bindParam(param: 'id', var: $id);
|
||||
$statement->bindParam(param: 'nick', var: $nick);
|
||||
$statement->bindParam(param: 'password', var: $password);
|
||||
$statement->bindParam(param: 'first', var: $first);
|
||||
$statement->bindParam(param: 'last', var: $last);
|
||||
$statement->bindParam(param: 'is_admin', var: $isAdmin);
|
||||
$statement->bindParam(param: 'street', var: $street);
|
||||
$statement->bindParam(param: 'zip', var: $zip);
|
||||
$statement->bindParam(param: 'city', var: $city);
|
||||
$statement->bindParam(param: 'phone', var: $phone);
|
||||
$statement->execute();
|
||||
|
||||
return $statement->rowCount();
|
||||
|
@ -144,8 +139,6 @@ class AddressRepository
|
|||
echo $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ use App\Controller\AddressBookAdminController;
|
|||
use App\Controller\AddressBookController;
|
||||
use App\Controller\SecurityController;
|
||||
use App\Entity\User;
|
||||
use App\Repository\AddressRepository;
|
||||
use App\Repository\UserRepository;
|
||||
|
||||
/*
|
||||
|
@ -26,6 +27,7 @@ class Container
|
|||
|
||||
private AddressBookController $addressBook;
|
||||
private AddressBookAdminController $addressBookAdmin;
|
||||
private AddressRepository $addressRepository;
|
||||
private Config $config;
|
||||
private DatabaseConnection $databaseConnection;
|
||||
private Router $router;
|
||||
|
@ -41,13 +43,14 @@ class Container
|
|||
$this->template = new Template(templateDir: dirname(path: __DIR__, levels: 2) . '/templates/');
|
||||
$this->router = new Router(template: $this->template);
|
||||
$this->userRepository = new UserRepository(databaseConnection: $this->databaseConnection);
|
||||
$this->addressRepository = new AddressRepository(databaseConnection: $this->databaseConnection);
|
||||
$this->securityController = new SecurityController(template: $this->template, userRepository: $this->userRepository, router: $this->router);
|
||||
if (empty($_SESSION['user_id'])) {
|
||||
$this->user = new User(); // ANONYMOUS
|
||||
} else {
|
||||
$this->user = $this->userRepository->findByID(id: $_SESSION['user_id']);
|
||||
}
|
||||
$this->addressBook = new AddressBookController(template: $this->template, user: $this->user, userRepository: $this->userRepository, router: $this->router);
|
||||
$this->addressBook = new AddressBookController(template: $this->template, user: $this->user, addressRepository: $this->addressRepository, router: $this->router);
|
||||
$this->addressBookAdmin = new AddressBookAdminController(template: $this->template, user: $this->user, userRepository: $this->userRepository, router: $this->router);
|
||||
}
|
||||
|
||||
|
|
|
@ -36,6 +36,7 @@ class DatabaseConnection
|
|||
username: $dbUser,
|
||||
password: $dbPassword
|
||||
);
|
||||
$this->dbConnection->setAttribute(attribute: PDO::ATTR_ERRMODE, value: PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
public function getConnection(): PDO
|
||||
|
|
|
@ -107,7 +107,7 @@ class Router
|
|||
|
||||
if ($route->getName() == $routeName) {
|
||||
if ($vars) {
|
||||
// build route
|
||||
// build route for dynamic routes
|
||||
$route = $route->getRoute();
|
||||
// replace placeholder with current values
|
||||
foreach ($vars as $key => $value) {
|
||||
|
@ -120,6 +120,15 @@ class Router
|
|||
}
|
||||
}
|
||||
// no 404, this is reached only if the code is wrong
|
||||
// TODO doesn't find the last route
|
||||
/*
|
||||
foreach (array_merge($this->dynamicRoutes, $this->staticRoutes) as $route) {
|
||||
echo $route->getRoute() . '<br>';
|
||||
if ($routeName == $route->getRoute()) {
|
||||
echo "equal";
|
||||
}
|
||||
}
|
||||
die("Missing Route: $routeName");
|
||||
*/
|
||||
}
|
||||
}
|
|
@ -1,3 +1,3 @@
|
|||
<!-- mind the javascript -->
|
||||
<script src="/assets/js/functions.js"></script>
|
||||
</body>
|
||||
</html>
|
|
@ -1,10 +1,9 @@
|
|||
<html lang="en">
|
||||
<head>
|
||||
<title>Address Book
|
||||
<?php if (!empty($user)): ?>
|
||||
<?php if (!empty($user->getNick())): ?>
|
||||
- <?= $user->getNick() ?>
|
||||
<?php endif; ?>
|
||||
|
||||
</title>
|
||||
<link rel="stylesheet" href="/assets/styles/main.css">
|
||||
</head>
|
||||
|
@ -14,19 +13,12 @@
|
|||
<a href="<?= $router->path('app_main'); ?>">🏠 Home</a>
|
||||
<a href="<?= $router->path('app_admin'); ?>">⚙ Admin</a>
|
||||
<?php if (empty($user) || $user->getAuth() == \App\Enums\UserAuth::AUTH_ANONYMOUS): ?>
|
||||
<a href="/login">🚪Login</a>
|
||||
<a href="<?= $router->path('app_login'); ?>">🚪Login</a>
|
||||
<?php else: ?>
|
||||
<a href="/logout">🚪Logout</a>
|
||||
<a href="<?= $router->path('app_logout'); ?>">🚪Logout</a>
|
||||
<?php endif; ?>
|
||||
<?php
|
||||
if ($user->getAuth() == \App\Enums\UserAuth::AUTH_ANONYMOUS) {
|
||||
echo "no";
|
||||
} else {
|
||||
echo "yes";
|
||||
}
|
||||
?>
|
||||
<?php if (!empty($user) && !$user->getAuth() == \App\Enums\UserAuth::AUTH_ANONYMOUS): ?>
|
||||
<?php if (!empty($user->getNick())): ?>
|
||||
<br>
|
||||
Welcome back, <?= $user->getNick(); ?>
|
||||
<-- TODO fix for anonymous Welcome back, <?= $user->getNick(); ?> -->
|
||||
<?php endif; ?>
|
||||
<br>
|
||||
|
|
|
@ -1,9 +1,54 @@
|
|||
<?php include '_header.html.php' ?>
|
||||
|
||||
<br>
|
||||
Welcome to Address Book
|
||||
<h2>Welcome to Address Book</h2>
|
||||
|
||||
<?php if(!empty($addresses)): ?>
|
||||
<form method="POST">
|
||||
<table id="address_table">
|
||||
<tr>
|
||||
<th id="first" onclick="sortBy('first')">First</th>
|
||||
<th id="last" onclick="sortBy('last')">Last</th>
|
||||
<th id="street" onclick="sortBy('street')">Street</th>
|
||||
<th id="zip" onclick="sortBy('zip')">Zip</th>
|
||||
<th id="city" onclick="sortBy('city')">City</th>
|
||||
<th id="phone" onclick="sortBy('phone')">Phone</th>
|
||||
<th colspan="2"> </th>
|
||||
</tr>
|
||||
<?php foreach ($addresses as $address): ?>
|
||||
<?php $id = $address->getId(); ?>
|
||||
<tr id="row_<?= $id ?>">
|
||||
<td><input type="text" id="first_<?= $id ?>" value="<?= $address->getFirst(); ?>" disabled></td>
|
||||
<td><input type="text" id="last_<?= $id ?>" value="<?= $address->getLast(); ?>" disabled></td>
|
||||
<td><input type="text" id="street_<?= $id ?>" value="<?= $address->getStreet(); ?>" disabled></td>
|
||||
<td><input type="text" id="zip_<?= $id ?>" value="<?= $address->getZip(); ?>" disabled></td>
|
||||
<td><input type="text" id="city_<?= $id ?>" value="<?= $address->getCity(); ?>" disabled></td>
|
||||
<td><input type="text" id="phone_<?= $id ?>" value="<?= $address->getPhone(); ?>" disabled></td>
|
||||
<td>
|
||||
<input type="button" value="Edit" id="edit_button_<?= $id ?>" onclick="editAddress(<?= $id ?>)">
|
||||
</td>
|
||||
<td>
|
||||
<input type="button" value="Delete" onclick="deleteAddress(<?= $id ?>)">
|
||||
<input type="hidden" id="owner_<?= $id ?>" value="<?= $id ?>">
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
<tr style="display:none;">
|
||||
<td id="first_sort">asc</td>
|
||||
<td id="last_sort">asc</td>
|
||||
<td id="street_sort">asc</td>
|
||||
<td id="zip_sort">asc</td>
|
||||
<td id="city_sort">asc</td>
|
||||
<td id="phone_sort">asc</td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
|
||||
<!-- TODO why is the route not found? $addPath = $router->path('/address/add'); -->
|
||||
<input type="button" value="Add Address" onclick="addAddress('address/add')">
|
||||
<?php else: ?>
|
||||
Your addresses wil be listed soon …
|
||||
<?php endif; ?>
|
||||
|
||||
|
||||
<?php include '_footer.html.php' ?>
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
<?php include dirname(path: __DIR__) . '/_header.html.php'; ?>
|
||||
|
||||
<h2>404 Page not found</h2>
|
||||
|
||||
<?php include dirname(path: __DIR__) . '/_footer.html.php' ?>
|
||||
The requested URL cannot be found on this server.
|
||||
|
|
Loading…
Reference in New Issue