Improve adding wishes without url

This commit is contained in:
grandeljay 2022-02-27 02:55:07 +01:00
parent a2956c47ec
commit 360975fc97
9 changed files with 285 additions and 305 deletions

View file

@ -1,30 +0,0 @@
<?php
/**
* Generate cache for wish
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\EmbedCache;
$api = true;
$response = array(
'success' => false,
);
require '../../index.php';
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
$cache = new EmbedCache($_GET['wish_url']);
$info = $cache->get(true);
$response['success'] = true;
$response['data'] = $info;
break;
}
header('Content-type: application/json; charset=utf-8');
echo json_encode($response);
die();

View file

@ -6,7 +6,7 @@
* @author Jay Trees <github.jay@grandel.anonaddy.me> * @author Jay Trees <github.jay@grandel.anonaddy.me>
*/ */
use wishthis\User; use wishthis\{User, Wish, EmbedCache};
$api = true; $api = true;
$response = array( $response = array(
@ -16,6 +16,31 @@ $response = array(
require '../../index.php'; require '../../index.php';
switch ($_SERVER['REQUEST_METHOD']) { switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
if (isset($_GET['wish_id'], $_GET['wishlist_user'])) {
$columns = $database
->query('SELECT *
FROM `wishes`
WHERE `id` = ' . $_GET['wish_id'] . ';')
->fetch();
$wish = new wish($columns);
$response = array(
'info' => $wish,
'html' => $wish->getCard($_GET['wishlist_user'], true)
);
} elseif (isset($_GET['wish_url'])) {
$cache = new EmbedCache($_GET['wish_url']);
$info = $cache->get(true);
$exists = $cache->exists() ? 'true' : 'false';
$response = array(
'info' => $info
);
}
break;
case 'POST': case 'POST':
if (isset($_POST['wishlist_id'], $_POST['wish_url'])) { if (isset($_POST['wishlist_id'], $_POST['wish_url'])) {
/** /**

View file

@ -7,8 +7,6 @@ $(function() {
}) })
.then(response => response.json()) .then(response => response.json())
.then(response => { .then(response => {
console.log(response);
if (response.success) { if (response.success) {
showStatistic( showStatistic(
$('#wishes .value'), $('#wishes .value'),
@ -51,7 +49,6 @@ function showStatistic(elementStatistic, amount, timeout) {
interval += interval * 0.1; interval += interval * 0.1;
} }
console.log(interval);
setTimeout(count, interval); setTimeout(count, interval);
} }
}, },

View file

@ -120,7 +120,6 @@ $(function () {
card = $(card); card = $(card);
var href = card.find('.content [href]').prop('href'); var href = card.find('.content [href]').prop('href');
var wish_id = card.data('id');
var refresh = card.find('button.refresh'); var refresh = card.find('button.refresh');
if (!href) { if (!href) {
@ -130,108 +129,26 @@ $(function () {
card.addClass('loading'); card.addClass('loading');
card.attr('data-cache', true); card.attr('data-cache', true);
fetch('/src/api/cache.php?wish_id=' + wish_id + '&wish_url=' + href, { var wishlistIndex = $('.ui.dropdown.wishlists select').prop('selectedIndex') - 1;
var wishlist_user = wishlists[wishlistIndex].user;
fetch('/src/api/wishes.php?wish_id=' + card.data('id') + '&wishlist_user=' + wishlist_user, {
method: 'GET' method: 'GET'
}) })
.then(response => response.json()) .then(handleFetchError)
.then(response => { .then(handleFetchResponse)
if (response.success) { .then(function(response) {
var info = response.data; card.html(response.html);
})
/** .catch(function(error) {
* Elements console.log(error);
*/ })
var elementImage = card.children('.image'); .finally(function() {
var elementContent = card.children('.content').first(); card.attr('data-cache', 'true');
var elementDetails = card.children('.extra.content.details');
var elementButtons = card.children('.extra.content.buttons');
/**
* Image
*/
if (info.image) {
if (!elementImage.length) {
card.prepend(
'<div class="image">' +
'<img class="preview" src="' + info.image + '" loading="lazy">' +
'</div>'
);
} else {
elementImage.children('img').attr('src', info.image);
}
}
/** Favicon */
if (info.favicon) {
var elementFavicon = elementImage.children('img.favicon');
if (!elementFavicon.length) {
elementImage.children().first().after(
'<img class="favicon" src="' + info.favicon + '" loading="lazy">'
);
} else {
elementFavicon.attr('src', info.favicon);
}
}
/** Provider name */
if (info.providerName) {
var elementProviderName = elementImage.children('span.provider');
if (!elementProviderName.length) {
$('<span class="provider">' + info.providerName + '</span>').insertBefore(elementImage.children().last());
} else {
elementProviderName.text(info.providerName);
}
}
/**
* Header
*/
var elementContentHeader = elementContent.children('.header');
var elementContentHeaderTitle = elementContentHeader.children('a');
/** Title */
if (info.title) {
elementContentHeaderTitle.text(info.title);
}
/**
* Meta
*/
var elementContentMeta = elementContent.children('.meta');
if (info.keywords.length) {
if (!elementContentMeta.length) {
elementContent.append(
'<div class="meta">' + info.keywords.join(', ') + '</div>'
);
}
}
/**
* Description
*/
var elementContentDescription = elementContent.children('.description');
if (info.description) {
if (!elementContentDescription.length) {
elementContent.append(
'<div class="description">' + info.description + '</div>' +
'<div class="description-fade"></div>'
);
}
}
/**
* Finish
*/
card.removeClass('loading'); card.removeClass('loading');
progress.progress('increment'); });
}
refresh.removeClass('working'); refresh.removeClass('working');
});
} }
/** /**
@ -427,23 +344,22 @@ $(function () {
$(document).on('click', '#wishlist-wish-add-url-validate', function () { $(document).on('click', '#wishlist-wish-add-url-validate', function () {
var buttonValidate = $(this); var buttonValidate = $(this);
var inputWishURL = buttonValidate.prev(); var inputWishURL = buttonValidate.prev();
var inputURLContainer = buttonValidate.parent();
var elementModalAdd = $('.ui.modal.wishlist-wish-add'); var elementModalAdd = $('.ui.modal.wishlist-wish-add');
var elementButtons = elementModalAdd.find('.actions .button'); var elementButtons = elementModalAdd.find('.actions .button');
var elementTitle = elementModalAdd.find('[name="wish_name"]'); var elementTitle = elementModalAdd.find('[name="wish_title"]');
var elementDescription = elementModalAdd.find('[name="wish_description"]'); var elementDescription = elementModalAdd.find('[name="wish_description"]');
buttonValidate.addClass('disabled loading'); buttonValidate.addClass('disabled loading');
elementButtons.addClass('disabled'); elementButtons.addClass('disabled');
fetch('/src/api/cache.php?wish_url=' + inputWishURL.val(), { fetch('/src/api/wishes.php?wish_url=' + inputWishURL.val(), {
method: 'GET' method: 'GET'
}) })
.then(response => response.json()) .then(handleFetchError)
.then(response => { .then(handleFetchResponse)
if (response.success) { .then(function(response) {
var info = response.data; var info = response.info;
/** /**
* Title * Title
@ -456,7 +372,7 @@ $(function () {
* Description * Description
*/ */
if (info.description && elementDescription.length) { if (info.description && elementDescription.length) {
elementDescription.text(info.description); elementDescription.val(info.description);
} }
/** /**
@ -508,10 +424,11 @@ $(function () {
buttonValidate.removeClass('disabled loading'); buttonValidate.removeClass('disabled loading');
elementButtons.removeClass('disabled'); elementButtons.removeClass('disabled');
} }
})
inputURLContainer.attr('data-validated', 'true'); .catch(function(error) {
} console.log(error);
}); });
}); });
/** /**

View file

@ -16,8 +16,6 @@ class EmbedCache
* Private * Private
*/ */
private string $directory = ROOT . '/src/cache'; private string $directory = ROOT . '/src/cache';
private string $noImage = '/src/assets/img/no-image.svg';
private string $filepath; private string $filepath;
private function getIdentifier(): string private function getIdentifier(): string
@ -60,7 +58,7 @@ class EmbedCache
$info_simplified->favicon = ''; $info_simplified->favicon = '';
$info_simplified->feeds = array(); $info_simplified->feeds = array();
$info_simplified->icon = ''; $info_simplified->icon = '';
$info_simplified->image = $this->noImage; $info_simplified->image = '';
$info_simplified->keywords = array(); $info_simplified->keywords = array();
$info_simplified->language = ''; $info_simplified->language = '';
$info_simplified->languages = array(); $info_simplified->languages = array();
@ -84,7 +82,7 @@ class EmbedCache
$info_simplified->favicon = (string) $info->favicon; $info_simplified->favicon = (string) $info->favicon;
$info_simplified->feeds = (array) $info->feeds; $info_simplified->feeds = (array) $info->feeds;
$info_simplified->icon = (string) $info->icon; $info_simplified->icon = (string) $info->icon;
$info_simplified->image = $info->image ? (string) $info->image : $this->noImage; $info_simplified->image = (string) $info->image;
$info_simplified->keywords = (array) $info->keywords; $info_simplified->keywords = (array) $info->keywords;
$info_simplified->language = (string) $info->language; $info_simplified->language = (string) $info->language;
$info_simplified->languages = (array) $info->languages; $info_simplified->languages = (array) $info->languages;
@ -98,7 +96,7 @@ class EmbedCache
} catch (\Throwable $ex) { } catch (\Throwable $ex) {
$generateCache = false; $generateCache = false;
$info_simplified->title = $ex->getMessage(); $info_simplified->description = $ex->getMessage();
} }
} }

149
src/classes/wish.php Normal file
View file

@ -0,0 +1,149 @@
<?php
/**
* A wish
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
namespace wishthis;
class Wish
{
public int $id;
public int $wishlist;
public ?string $title;
public ?string $description;
public ?string $url;
public ?string $status;
public function __construct(int|array $wish)
{
global $database;
$columns = array();
if (is_int($wish)) {
$wish = $database
->query('SELECT *
FROM `wishes`
WHERE `id` = ' . $wish . ';')
->fetch();
$columns = $wish;
} elseif (is_array($wish)) {
$columns = $wish;
}
if ($columns) {
foreach ($columns as $key => $value) {
$this->$key = $value;
}
}
}
public function getCard(int $ofUser, bool $generateCache = false): string
{
ob_start();
/**
* Card
*/
$userIsCurrent = isset($_SESSION['user']['id']) && intval($_SESSION['user']['id']) === $ofUser;
if ($this->url) {
$cache = new EmbedCache($this->url);
$info = $cache->get($generateCache);
$exists = $cache->exists() || !$this->url ? 'true' : 'false';
} else {
$exists = 'true';
}
$title = $this->title ?? $info->title ?? null;
$description = $this->description ?? $info->description ?? null;
$url = $this->url ?? $info->url ?? null;
$image = $info->image ?? '/src/assets/img/no-image.svg';
$favicon = $info->favicon ?? null;
$providerName = $info->providerName ?? null;
$keywords = $info->keywords ?? null;
?>
<div class="ui fluid card stretch" data-id="<?= $this->id ?>" data-cache="<?= $exists ?>">
<div class="overlay"></div>
<div class="image">
<?php if ($image) { ?>
<img class="preview" src="<?= $image ?>" loading="lazy" />
<?php } ?>
<?php if ($favicon) { ?>
<img class="favicon" src="<?= $favicon ?>" loading="lazy" />
<?php } ?>
<?php if ($providerName) { ?>
<span class="provider"><?= $providerName ?></span>
<?php } ?>
<?php if ($userIsCurrent && $url) { ?>
<button class="ui icon button refresh">
<i class="refresh icon"></i>
</button>
<?php } ?>
</div>
<div class="content">
<?php if ($title) { ?>
<div class="header">
<?php if ($url) { ?>
<a href="<?= $url ?>" target="_blank"><?= $title ?></a>
<?php } else { ?>
<?= $title ?>
<?php } ?>
</div>
<?php } ?>
<?php if ($keywords) { ?>
<div class="meta">
<?= implode(', ', $keywords) ?>
</div>
<?php } ?>
<?php if ($description) { ?>
<div class="description">
<?= $description ?>
</div>
<div class="description-fade"></div>
<?php } ?>
</div>
<div class="extra content buttons">
<?php if (!$userIsCurrent) { ?>
<a class="ui small primary labeled icon button commit">
<i class="shopping cart icon"></i>
Commit
</a>
<?php } ?>
<?php if ($url) { ?>
<a class="ui small labeled icon button" href="<?= $url ?>" target="_blank">
<i class="external icon"></i>
Visit
</a>
<?php } ?>
<?php if ($userIsCurrent) { ?>
<a class="ui small labeled red icon button delete">
<i class="trash icon"></i>
Delete
</a>
<?php } ?>
</div>
</div>
<?php
$html = ob_get_clean();
return $html;
}
}

View file

@ -54,10 +54,15 @@ class Wishlist
/** /**
* Get Wishes * Get Wishes
*/ */
$this->wishes = $database->query('SELECT * $this->wishes = $database
->query('SELECT *
FROM `wishes` FROM `wishes`
WHERE `wishlist` = ' . $this->id . ';') WHERE `wishlist` = ' . $this->id . ';')
->fetchAll(); ->fetchAll();
foreach ($this->wishes as &$wish) {
$wish = new Wish($wish);
}
} }
public function getCards($options = array()): string public function getCards($options = array()): string
@ -71,7 +76,7 @@ class Wishlist
if ($exclude) { if ($exclude) {
$wishes = array_filter($this->wishes, function ($wish) use ($exclude) { $wishes = array_filter($this->wishes, function ($wish) use ($exclude) {
return !in_array($wish['status'], $exclude); return !in_array($wish->status, $exclude);
}); });
} else { } else {
$wishes = $this->wishes; $wishes = $this->wishes;
@ -80,96 +85,14 @@ class Wishlist
/** /**
* Cards * Cards
*/ */
$userIsCurrent = isset($_SESSION['user']) && $this->data['user'] === $_SESSION['user']['id'];
$cardIndex = 0;
if (!empty($wishes)) { ?> if (!empty($wishes)) { ?>
<div class="ui three column doubling stackable grid"> <div class="ui three column doubling stackable grid">
<?php foreach ($wishes as $wish) { <?php foreach ($wishes as $wish) { ?>
$cache = new EmbedCache($wish['url']);
$info = $cache->get(false);
$exists = $cache->exists() || !$info->url ? 'true' : 'false';
$title = $wish['title'] ?? $info->title;
$description = $wish['description'] ?? $info->description;
?>
<div class="column"> <div class="column">
<div class="ui fluid card stretch" data-id="<?= $wish['id'] ?>" data-index="<?= $cardIndex ?>" data-cache="<?= $exists ?>"> <?= $wish->getCard($this->data['user'], false) ?>
<div class="overlay"></div>
<div class="image">
<?php if ($info->image) { ?>
<img class="preview" src="<?= $info->image ?>" loading="lazy"/>
<?php } ?>
<?php if ($info->favicon) { ?>
<img class="favicon" src="<?= $info->favicon ?>" loading="lazy"/>
<?php } ?>
<?php if ($info->providerName) { ?>
<span class="provider"><?= $info->providerName ?></span>
<?php } ?>
<?php if ($userIsCurrent && $info->url) { ?>
<button class="ui icon button refresh">
<i class="refresh icon"></i>
</button>
<?php } ?>
</div>
<div class="content">
<?php if ($title) { ?>
<div class="header">
<?php if ($info->url) { ?>
<a href="<?= $info->url ?>" target="_blank"><?= $title ?></a>
<?php } else { ?>
<?= $title ?>
<?php } ?>
</div>
<?php } ?>
<?php if ($info->keywords) { ?>
<div class="meta">
<?= implode(', ', $info->keywords) ?>
</div>
<?php } ?>
<?php if ($description) { ?>
<div class="description">
<?= $description ?>
</div>
<div class="description-fade"></div>
<?php } ?>
</div>
<div class="extra content buttons">
<?php if (!$userIsCurrent) { ?>
<a class="ui small primary labeled icon button commit">
<i class="shopping cart icon"></i>
Commit
</a>
<?php } ?>
<?php if ($info->url) { ?>
<a class="ui small labeled icon button" href="<?= $info->url ?>" target="_blank">
<i class="external icon"></i>
Visit
</a>
<?php } ?>
<?php if ($userIsCurrent) { ?>
<a class="ui small labeled red icon button delete">
<i class="trash icon"></i>
Delete
</a>
<?php } ?>
</div>
</div>
</div> </div>
<?php } ?> <?php } ?>
</div> </div>
<? $cardIndex++ ?>
<?php } else { ?> <?php } else { ?>
<div class="ui container"> <div class="ui container">
<div class="sixteen wide column"> <div class="sixteen wide column">

View file

@ -148,6 +148,7 @@ switch ($step) {
REFERENCES `wishlists` (`id`) REFERENCES `wishlists` (`id`)
ON DELETE CASCADE ON DELETE CASCADE
);'); );');
$database->query('CREATE INDEX `idx_url` ON `wishes` (`url`);');
/** /**
* Options * Options