refactor: wishlist class

This commit is contained in:
grandeljay 2023-08-31 11:31:02 +02:00
parent 262c67098a
commit d8e8491a18
3 changed files with 47 additions and 52 deletions

View file

@ -41,7 +41,8 @@ class User
return sha1($plainPassword); return sha1($plainPassword);
} }
public static function getCurrent(): self { public static function getCurrent(): self
{
if (!isset($_SESSION['user'])) { if (!isset($_SESSION['user'])) {
$_SESSION['user'] = new self(); $_SESSION['user'] = new self();
} }
@ -250,7 +251,6 @@ class User
/** /**
* Returns a list of the users wishlists. * Returns a list of the users wishlists.
* Defaults to the currently logged in user.
* *
* @return array * @return array
*/ */
@ -315,7 +315,8 @@ class User
* *
* @return bool Whether the log in was successful. * @return bool Whether the log in was successful.
*/ */
public function logIn(string $email = '', string $password = '', bool $user_login_is_persistent = false): bool { public function logIn(string $email = '', string $password = '', bool $user_login_is_persistent = false): bool
{
global $database; global $database;
$login_was_successful = false; $login_was_successful = false;
@ -457,19 +458,23 @@ class User
); );
} }
public function getId(): int { public function getId(): int
{
return $this->id; return $this->id;
} }
public function getEmail(): string { public function getEmail(): string
{
return $this->email; return $this->email;
} }
public function getPassword(): string { public function getPassword(): string
{
return $this->password; return $this->password;
} }
public function getPower(): int { public function getPower(): int
{
return $this->power; return $this->power;
} }
} }

View file

@ -46,54 +46,19 @@ class Wishlist
* *
* @var int * @var int
*/ */
private int $notification_send; private int $notification_sent;
public array $wishes = array(); public array $wishes = array();
public bool $exists = false; public bool $exists = false;
public function __construct(int|string $id_or_hash) public function __construct(array $wishlist_data)
{ {
global $database; $this->id = $wishlist_data['id'];
$this->user = $wishlist_data['user'];
$column; $this->name = $wishlist_data['name'];
$this->hash = $wishlist_data['hash'];
if (is_numeric($id_or_hash)) { $this->notification_sent = $wishlist_data['notification_sent'] ?? 0;
$column = 'id';
} elseif (is_string($id_or_hash)) {
$column = 'hash';
}
/**
* Get Wishlist
*/
$columns = $database
->query(
'SELECT *
FROM `wishlists`
WHERE `' . $column . '` = :id_or_hash;',
array(
'id_or_hash' => $id_or_hash,
)
)
->fetch();
if ($columns) {
$this->exists = true;
$this->id = $columns['id'];
$this->user = $columns['user'];
$this->name = html_entity_decode($columns['name']);
$this->hash = $columns['hash'];
$this->notification_send = $columns['notification_send'];
} else {
return;
}
/**
* Get Wishes
*/
// $this->wishes = $this->getWishes();
} }
public function getWishes(array $options = array('placeholders' => array())): array public function getWishes(array $options = array('placeholders' => array())): array
@ -233,4 +198,29 @@ class Wishlist
return $title; return $title;
} }
public function getId(): int
{
return $this->id;
}
public function getUserId(): int
{
return $this->user;
}
public function getName(): string
{
return $this->name;
}
public function getHash(): string
{
return $this->hash;
}
public function getNotificationSent(): int
{
return $this->notification_sent;
}
} }

View file

@ -75,8 +75,8 @@ $user = User::getCurrent();
</div> </div>
</div> </div>
<?php if (isset($wishlist->id)) { ?> <?php if (isset($wishlist)) { ?>
<div class="wishlist-cards" data-wishlist="<?= $wishlist->id ?>"></div> <div class="wishlist-cards" data-wishlist="<?= $wishlist->getId() ?>"></div>
<?php } else { ?> <?php } else { ?>
<div class="wishlist-cards"></div> <div class="wishlist-cards"></div>
<?php } ?> <?php } ?>