87 lines
2.8 KiB
PHP
87 lines
2.8 KiB
PHP
<?php
|
|
/*
|
|
* Copyright (c) 2022. Micha Espey <tracer@24unix.net>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*
|
|
*/
|
|
|
|
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;
|
|
|
|
class AddressBookController
|
|
{
|
|
public function __construct(
|
|
private readonly Template $template,
|
|
private readonly User $user,
|
|
private readonly AddressRepository $addressRepository,
|
|
private readonly Router $router
|
|
)
|
|
{
|
|
// empty body
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
} |