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']) { switch ($_SERVER['REQUEST_METHOD']) {
case 'GET': case 'GET':
if (isset($_GET['wish_id'])) { $getWish = isset($_GET['wish_id']);
$wish = new Wish($_GET['wish_id'], true);
$response['info'] = $wish; if ($getWish) {
$wish = Wish::getFromId($_GET['wish_id']);
$response['info'] = $wish->serialise();
if (isset($_GET['wishlist_user'])) { if (isset($_GET['wishlist_user'])) {
$response['html'] = $wish->getCard($_GET['wishlist_user']); $response['html'] = $wish->getCard($_GET['wishlist_user']);

View file

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

View file

@ -53,7 +53,7 @@ $(function () {
wish_details wish_details
.modal({ .modal({
'onShow' : function() { '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) { if (user_is_current) {
$('.ui.button.wish-fulfil').remove(); $('.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 * Non-Static
*/ */
private Cache\Embed $cache; private ?Cache\Embed $cache = null;
/** /**
* The unique wish id. * The unique wish id.
@ -132,59 +155,18 @@ class Wish
public bool $exists = false; public bool $exists = false;
public function __construct(int|array $idOrColumns, bool $generateCache = false) public function __construct(array $wishData)
{ {
global $database; $this->id = $wishData['id'];
$this->wishlist = $wishData['wishlist'];
$columns = array(); $this->title = stripslashes($wishData['title'] ?? '');
$this->description = stripslashes($wishData['description'] ?? '');
if (is_numeric($idOrColumns)) { $this->image = $wishData['image'] ?? '';
$id = $idOrColumns; $this->url = $wishData['url'] ?? '';
$columns = $database $this->priority = $wishData['priority'];
->query( $this->status = $wishData['status'];
' SELECT ' . self::SELECT . ' $this->is_purchasable = $wishData['is_purchasable'];
FROM ' . self::FROM . ' $this->edited = $wishData['edited'] ? \strtotime($wishData['edited']) : null;
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 ?? '');
}
} }
public function getCard(int $ofUser): string public function getCard(int $ofUser): string
@ -202,7 +184,7 @@ class Wish
/** /**
* Card * Card
*/ */
if ($this->url) { if ($this->url && $this->cache) {
$generateCache = $this->cache->generateCache() ? 'true' : 'false'; $generateCache = $this->cache->generateCache() ? 'true' : 'false';
} else { } else {
$generateCache = 'false'; $generateCache = 'false';
@ -407,4 +389,22 @@ class Wish
return $buttons; 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;
}
} }