Compare commits
No commits in common. "5ed539a4717ec8723f534e4270ec48c9f2962bad" and "4665e1706f5dc1037a8ff27cae43f6bbe9c19b15" have entirely different histories.
5ed539a471
...
4665e1706f
|
@ -1,3 +1,2 @@
|
|||
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,7 +30,8 @@ function editAddress(id) {
|
|||
})
|
||||
})
|
||||
.then(
|
||||
response => response.text()
|
||||
response => response.text() // .json(), etc.
|
||||
// same as function(response) {return response.text();}
|
||||
).then(
|
||||
html => console.log(html)
|
||||
);
|
||||
|
@ -57,60 +58,5 @@ function editAddress(id) {
|
|||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
function deleteAddress() {
|
||||
}
|
||||
|
|
|
@ -64,16 +64,5 @@ $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,6 +13,7 @@ use App\Entity\User;
|
|||
use App\Service\Router;
|
||||
use App\Service\Template;
|
||||
use App\Repository\UserRepository;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
class AddressBookAdminController
|
||||
{
|
||||
|
@ -38,7 +39,7 @@ class AddressBookAdminController
|
|||
|
||||
public function admin(): never
|
||||
{
|
||||
$this->adminCheck();
|
||||
// TODO $this->adminCheck();
|
||||
$this->template->render(templateName: 'admin/index.html.php', vars: [
|
||||
'user' => $this->user,
|
||||
'router' => $this->router
|
||||
|
@ -48,7 +49,7 @@ class AddressBookAdminController
|
|||
|
||||
public function adminUser(): never
|
||||
{
|
||||
$this->adminCheck();
|
||||
// TODO $this->adminCheck();
|
||||
|
||||
$users = $this->userRepository->findAll();
|
||||
|
||||
|
@ -61,7 +62,7 @@ class AddressBookAdminController
|
|||
|
||||
public function adminUserEdit(array $parameters): never
|
||||
{
|
||||
$this->adminCheck();
|
||||
// TODO $this->adminCheck();
|
||||
|
||||
if (!empty($_POST)) {
|
||||
if (!empty($_POST['is_admin'])) {
|
||||
|
@ -107,7 +108,8 @@ class AddressBookAdminController
|
|||
// TODO currently breaks on inserting a duplicate nick
|
||||
if (!empty($_POST)) {
|
||||
$isAdmin = empty($_POST['is_admin']) ? 0 : 1;
|
||||
$user = new User(nick: $_POST['nick'], newPassword: $_POST['new_password'], first: $_POST['first'], last: $_POST['last'], isAdmin: $isAdmin);
|
||||
echo "isA: $isAdmin";
|
||||
$user = new User(nick: $_POST['nick'], password: $_POST['password'], first: $_POST['first'], last: $_POST['last'], isAdmin: $isAdmin);
|
||||
|
||||
if ($this->userRepository->insert(user: $user)) {
|
||||
$users = $this->userRepository->findAll();
|
||||
|
|
|
@ -10,78 +10,30 @@
|
|||
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\AddressRepository;
|
||||
use App\Repository\UserRepository;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
class AddressBookController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Template $template,
|
||||
private readonly User $user,
|
||||
private readonly AddressRepository $addressRepository,
|
||||
private readonly UserRepository $userRepository,
|
||||
private readonly Router $router
|
||||
)
|
||||
{
|
||||
// empty body
|
||||
}
|
||||
|
||||
public function main(): never
|
||||
#[NoReturn]
|
||||
public function main(): void
|
||||
{
|
||||
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,6 +11,7 @@ namespace App\Repository;
|
|||
|
||||
use App\Entity\AddressBookEntry;
|
||||
use App\Service\DatabaseConnection;
|
||||
use App\Entity\User;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
|
||||
|
@ -26,6 +27,7 @@ class AddressRepository
|
|||
|
||||
public function findAll(string $orderBy = 'last'): array
|
||||
{
|
||||
$users = [];
|
||||
$sql = "
|
||||
SELECT id, owner, first, last, street, zip, city, phone
|
||||
FROM " . DatabaseConnection::TABLE_ADDRESSES . "
|
||||
|
@ -52,7 +54,7 @@ class AddressRepository
|
|||
{
|
||||
$sql = "
|
||||
SELECT id, owner, first, last, street, zip, city, phone
|
||||
FROM " . DatabaseConnection::TABLE_ADDRESSES . "
|
||||
FROM " . DatabaseConnection::TABLE_USERS . "
|
||||
WHERE id = :id";
|
||||
|
||||
try {
|
||||
|
@ -102,36 +104,39 @@ class AddressRepository
|
|||
}
|
||||
|
||||
|
||||
public function update(AddressBookEntry $address): bool|int
|
||||
public function update(Address $address): bool|int
|
||||
{
|
||||
$id = $address->getId();
|
||||
$owner = $address->getOwner();
|
||||
$first = $address->getFirst();
|
||||
$last = $address->getLast();
|
||||
$street = $address->getStreet();
|
||||
$zip = $address->getZip();
|
||||
$city = $address->getCity();
|
||||
$phone = $address->getPhone();
|
||||
/*
|
||||
$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();
|
||||
}
|
||||
|
||||
$sql = "
|
||||
UPDATE " . DatabaseConnection::TABLE_ADDRESSES . " SET
|
||||
UPDATE " . DatabaseConnection::TABLE_USERS . " SET
|
||||
nick = :nick,
|
||||
password = :password,
|
||||
first = :first,
|
||||
last = :last,
|
||||
street = :street,
|
||||
zip = :zip,
|
||||
city = :city,
|
||||
phone = :phone
|
||||
is_admin = :is_admin
|
||||
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: 'street', var: $street);
|
||||
$statement->bindParam(param: 'zip', var: $zip);
|
||||
$statement->bindParam(param: 'city', var: $city);
|
||||
$statement->bindParam(param: 'phone', var: $phone);
|
||||
$statement->bindParam(param: 'is_admin', var: $isAdmin);
|
||||
$statement->execute();
|
||||
|
||||
return $statement->rowCount();
|
||||
|
@ -139,6 +144,8 @@ class AddressRepository
|
|||
echo $e->getMessage();
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -13,7 +13,6 @@ use App\Controller\AddressBookAdminController;
|
|||
use App\Controller\AddressBookController;
|
||||
use App\Controller\SecurityController;
|
||||
use App\Entity\User;
|
||||
use App\Repository\AddressRepository;
|
||||
use App\Repository\UserRepository;
|
||||
|
||||
/*
|
||||
|
@ -27,7 +26,6 @@ class Container
|
|||
|
||||
private AddressBookController $addressBook;
|
||||
private AddressBookAdminController $addressBookAdmin;
|
||||
private AddressRepository $addressRepository;
|
||||
private Config $config;
|
||||
private DatabaseConnection $databaseConnection;
|
||||
private Router $router;
|
||||
|
@ -43,14 +41,13 @@ 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, addressRepository: $this->addressRepository, router: $this->router);
|
||||
$this->addressBook = new AddressBookController(template: $this->template, user: $this->user, userRepository: $this->userRepository, router: $this->router);
|
||||
$this->addressBookAdmin = new AddressBookAdminController(template: $this->template, user: $this->user, userRepository: $this->userRepository, router: $this->router);
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,6 @@ 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 for dynamic routes
|
||||
// build route
|
||||
$route = $route->getRoute();
|
||||
// replace placeholder with current values
|
||||
foreach ($vars as $key => $value) {
|
||||
|
@ -120,15 +120,6 @@ 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 @@
|
|||
<script src="/assets/js/functions.js"></script>
|
||||
<!-- mind the javascript -->
|
||||
</body>
|
||||
</html>
|
|
@ -1,24 +1,32 @@
|
|||
<html lang="en">
|
||||
<head>
|
||||
<head>
|
||||
<title>Address Book
|
||||
<?php if (!empty($user->getNick())): ?>
|
||||
<?php if (!empty($user)): ?>
|
||||
- <?= $user->getNick() ?>
|
||||
<?php endif; ?>
|
||||
|
||||
</title>
|
||||
<link rel="stylesheet" href="/assets/styles/main.css">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Address Book</h1>
|
||||
<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="<?= $router->path('app_login'); ?>">🚪Login</a>
|
||||
<?php else: ?>
|
||||
<a href="<?= $router->path('app_logout'); ?>">🚪Logout</a>
|
||||
<?php endif; ?>
|
||||
<?php if (!empty($user->getNick())): ?>
|
||||
<h1>Address Book</h1>
|
||||
<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>
|
||||
<?php else: ?>
|
||||
<a href="/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): ?>
|
||||
<br>
|
||||
<-- TODO fix for anonymous Welcome back, <?= $user->getNick(); ?> -->
|
||||
<?php endif; ?>
|
||||
Welcome back, <?= $user->getNick(); ?>
|
||||
<?php endif; ?>
|
||||
<br>
|
||||
|
|
|
@ -1,54 +1,9 @@
|
|||
<?php include '_header.html.php' ?>
|
||||
|
||||
<br>
|
||||
<h2>Welcome to Address Book</h2>
|
||||
Welcome to Address Book
|
||||
|
||||
<?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,3 +1,5 @@
|
|||
<?php include dirname(path: __DIR__) . '/_header.html.php'; ?>
|
||||
|
||||
<h2>404 Page not found</h2>
|
||||
|
||||
The requested URL cannot be found on this server.
|
||||
<?php include dirname(path: __DIR__) . '/_footer.html.php' ?>
|
||||
|
|
Loading…
Reference in New Issue