fix: wish details view

This commit is contained in:
grandeljay 2023-09-05 15:11:58 +02:00
parent 05cd98812b
commit b19c3221a9
4 changed files with 63 additions and 60 deletions

View file

@ -17,10 +17,12 @@ if (!isset($page)) {
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
if (isset($_GET['wish_id'])) {
$wish = new Wish($_GET['wish_id'], true);
$getWish = isset($_GET['wish_id']);
$response['info'] = $wish;
if ($getWish) {
$wish = Wish::getFromId($_GET['wish_id']);
$response['info'] = $wish->serialise();
if (isset($_GET['wishlist_user'])) {
$response['html'] = $wish->getCard($_GET['wishlist_user']);

View file

@ -170,6 +170,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
$wishlists[] = array(
'id' => $wishlistId,
'hash' => $wishlist->getHash(),
'userId' => $wishlist->getUserId(),
);
$wishlistsItems[] = array(
'name' => $wishlistName,

View file

@ -53,7 +53,7 @@ $(function () {
wish_details
.modal({
'onShow' : function() {
var user_is_current = wishlist && wishlist.user === parseInt($('[name="user-id"]').val());
var user_is_current = wishlist && wishlist.userId === parseInt($('[name="user-id"]').val());
if (user_is_current) {
$('.ui.button.wish-fulfil').remove();

View file

@ -45,10 +45,33 @@ class Wish
);
}
public static function getFromId(int $wishId): self|false
{
global $database;
$wishQuery = $database->query(
'SELECT *
FROM `wishes`
WHERE `wishes`.`id` = :wish_id',
array(
'wish_id' => $wishId,
)
);
if (false === $wishQuery) {
return false;
}
$wishData = $wishQuery->fetch();
$wish = new self($wishData);
return $wish;
}
/**
* Non-Static
*/
private Cache\Embed $cache;
private ?Cache\Embed $cache = null;
/**
* The unique wish id.
@ -132,59 +155,18 @@ class Wish
public bool $exists = false;
public function __construct(int|array $idOrColumns, bool $generateCache = false)
public function __construct(array $wishData)
{
global $database;
$columns = array();
if (is_numeric($idOrColumns)) {
$id = $idOrColumns;
$columns = $database
->query(
' SELECT ' . self::SELECT . '
FROM ' . self::FROM . '
LEFT JOIN ' . self::LEFT_JOIN . '
WHERE ' . self::WHERE,
array(
'wish_id' => $id,
)
)
->fetch();
} elseif (is_array($idOrColumns)) {
$columns = $idOrColumns;
}
if ($columns) {
$this->exists = true;
$this->id = $columns['id'];
$this->wishlist = $columns['wishlist'];
$this->title = $columns['title'] ?? '';
$this->description = $columns['description'] ?? '';
$this->image = $columns['image'] ?? '';
$this->url = $columns['url'] ?? '';
$this->priority = $columns['priority'];
$this->status = $columns['status'];
$this->is_purchasable = $columns['is_purchasable'];
$this->edited = $columns['edited'] ? \strtotime($columns['edited']) : null;
$this->info = new \stdClass();
if ($this->url) {
$this->cache = new Cache\Embed($this->url);
$this->info = $this->cache->get($generateCache);
}
foreach ($columns as $key => $value) {
if (empty($value) && isset($this->info->$key)) {
$this->$key = $this->info->$key;
}
}
$this->title = stripslashes($this->title ?? '');
$this->description = stripslashes($this->description ?? '');
}
$this->id = $wishData['id'];
$this->wishlist = $wishData['wishlist'];
$this->title = stripslashes($wishData['title'] ?? '');
$this->description = stripslashes($wishData['description'] ?? '');
$this->image = $wishData['image'] ?? '';
$this->url = $wishData['url'] ?? '';
$this->priority = $wishData['priority'];
$this->status = $wishData['status'];
$this->is_purchasable = $wishData['is_purchasable'];
$this->edited = $wishData['edited'] ? \strtotime($wishData['edited']) : null;
}
public function getCard(int $ofUser): string
@ -202,7 +184,7 @@ class Wish
/**
* Card
*/
if ($this->url) {
if ($this->url && $this->cache) {
$generateCache = $this->cache->generateCache() ? 'true' : 'false';
} else {
$generateCache = 'false';
@ -407,4 +389,22 @@ class Wish
return $buttons;
}
public function serialise(): array
{
$wishArray = array(
'id' => $this->id,
'wishlist' => $this->wishlist,
'title' => $this->title,
'description' => $this->description,
'image' => $this->image,
'url' => $this->url,
'priority' => $this->priority,
'status' => $this->status,
'is_purchasable' => $this->is_purchasable,
'edited' => $this->edited,
);
return $wishArray;
}
}