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>
*/
use wishthis\User;
use wishthis\{User, Wish, EmbedCache};
$api = true;
$response = array(
@ -16,6 +16,31 @@ $response = array(
require '../../index.php';
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':
if (isset($_POST['wishlist_id'], $_POST['wish_url'])) {
/**

View file

@ -20,8 +20,8 @@ $(function() {
$.fn.api.settings.api = {
'get wishlists' : '/src/api/wishlists.php',
'delete wishlist' : '/src/api/wishlists.php',
'update wish status': '/src/api/wishes.php',
'delete wish' : '/src/api/wishes.php',
'update wish status' : '/src/api/wishes.php',
'delete wish' : '/src/api/wishes.php',
};
/** Default callbacks */

View file

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

View file

@ -120,7 +120,6 @@ $(function () {
card = $(card);
var href = card.find('.content [href]').prop('href');
var wish_id = card.data('id');
var refresh = card.find('button.refresh');
if (!href) {
@ -130,108 +129,26 @@ $(function () {
card.addClass('loading');
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'
})
.then(response => response.json())
.then(response => {
if (response.success) {
var info = response.data;
/**
* Elements
*/
var elementImage = card.children('.image');
var elementContent = card.children('.content').first();
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');
progress.progress('increment');
}
refresh.removeClass('working');
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
card.html(response.html);
})
.catch(function(error) {
console.log(error);
})
.finally(function() {
card.attr('data-cache', 'true');
card.removeClass('loading');
});
refresh.removeClass('working');
}
/**
@ -427,91 +344,91 @@ $(function () {
$(document).on('click', '#wishlist-wish-add-url-validate', function () {
var buttonValidate = $(this);
var inputWishURL = buttonValidate.prev();
var inputURLContainer = buttonValidate.parent();
var elementModalAdd = $('.ui.modal.wishlist-wish-add');
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"]');
buttonValidate.addClass('disabled loading');
elementButtons.addClass('disabled');
fetch('/src/api/cache.php?wish_url=' + inputWishURL.val(), {
fetch('/src/api/wishes.php?wish_url=' + inputWishURL.val(), {
method: 'GET'
})
.then(response => response.json())
.then(response => {
if (response.success) {
var info = response.data;
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
var info = response.info;
/**
* Title
*/
if (info.title && elementTitle.length) {
elementTitle.val(info.title);
}
/**
* Description
*/
if (info.description && elementDescription.length) {
elementDescription.text(info.description);
}
/**
* URL
*/
if (info.url && info.url !== inputWishURL.val()) {
var elementModalFetch = $('.ui.modal.wishlist-wish-fetch');
elementModalFetch.find('input.current').val(inputWishURL.val());
elementModalFetch.find('input.proposed').val(info.url);
elementButtons.addClass('disabled');
elementModalFetch
.modal({
allowMultiple: true,
closable: false,
onApprove: function (buttonFetch) {
var formData = new URLSearchParams();
formData.append('wish_url_current', inputWishURL.val());
formData.append('wish_url_proposed', info.url);
buttonFetch.addClass('loading');
fetch('/src/api/wishes.php', {
method: 'PUT',
body: formData
})
.then(response => response.json())
.then(response => {
if (response.success) {
inputWishURL.val(info.url);
elementModalFetch.modal('hide');
}
buttonFetch.removeClass('loading');
});
return false;
},
onHide: function() {
buttonValidate.removeClass('disabled loading');
elementButtons.removeClass('disabled');
}
})
.modal('show');
} else {
buttonValidate.removeClass('disabled loading');
elementButtons.removeClass('disabled');
}
inputURLContainer.attr('data-validated', 'true');
/**
* Title
*/
if (info.title && elementTitle.length) {
elementTitle.val(info.title);
}
/**
* Description
*/
if (info.description && elementDescription.length) {
elementDescription.val(info.description);
}
/**
* URL
*/
if (info.url && info.url !== inputWishURL.val()) {
var elementModalFetch = $('.ui.modal.wishlist-wish-fetch');
elementModalFetch.find('input.current').val(inputWishURL.val());
elementModalFetch.find('input.proposed').val(info.url);
elementButtons.addClass('disabled');
elementModalFetch
.modal({
allowMultiple: true,
closable: false,
onApprove: function (buttonFetch) {
var formData = new URLSearchParams();
formData.append('wish_url_current', inputWishURL.val());
formData.append('wish_url_proposed', info.url);
buttonFetch.addClass('loading');
fetch('/src/api/wishes.php', {
method: 'PUT',
body: formData
})
.then(response => response.json())
.then(response => {
if (response.success) {
inputWishURL.val(info.url);
elementModalFetch.modal('hide');
}
buttonFetch.removeClass('loading');
});
return false;
},
onHide: function() {
buttonValidate.removeClass('disabled loading');
elementButtons.removeClass('disabled');
}
})
.modal('show');
} else {
buttonValidate.removeClass('disabled loading');
elementButtons.removeClass('disabled');
}
})
.catch(function(error) {
console.log(error);
});
});
/**

View file

@ -16,8 +16,6 @@ class EmbedCache
* Private
*/
private string $directory = ROOT . '/src/cache';
private string $noImage = '/src/assets/img/no-image.svg';
private string $filepath;
private function getIdentifier(): string
@ -39,9 +37,9 @@ class EmbedCache
public function get(bool $generateCache = false): mixed
{
$info = null;
$maxAge = 2592000; // 30 days
$age = file_exists($this->getFilepath()) ? time() - filemtime($this->getFilepath()) : $maxAge;
$info = null;
$maxAge = 2592000; // 30 days
$age = file_exists($this->getFilepath()) ? time() - filemtime($this->getFilepath()) : $maxAge;
if ($this->exists() && $age <= $maxAge && false === $generateCache) {
$info = json_decode(file_get_contents($this->getFilepath()));
@ -60,7 +58,7 @@ class EmbedCache
$info_simplified->favicon = '';
$info_simplified->feeds = array();
$info_simplified->icon = '';
$info_simplified->image = $this->noImage;
$info_simplified->image = '';
$info_simplified->keywords = array();
$info_simplified->language = '';
$info_simplified->languages = array();
@ -84,7 +82,7 @@ class EmbedCache
$info_simplified->favicon = (string) $info->favicon;
$info_simplified->feeds = (array) $info->feeds;
$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->language = (string) $info->language;
$info_simplified->languages = (array) $info->languages;
@ -98,7 +96,7 @@ class EmbedCache
} catch (\Throwable $ex) {
$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
*/
$this->wishes = $database->query('SELECT *
FROM `wishes`
WHERE `wishlist` = ' . $this->id . ';')
->fetchAll();
$this->wishes = $database
->query('SELECT *
FROM `wishes`
WHERE `wishlist` = ' . $this->id . ';')
->fetchAll();
foreach ($this->wishes as &$wish) {
$wish = new Wish($wish);
}
}
public function getCards($options = array()): string
@ -71,7 +76,7 @@ class Wishlist
if ($exclude) {
$wishes = array_filter($this->wishes, function ($wish) use ($exclude) {
return !in_array($wish['status'], $exclude);
return !in_array($wish->status, $exclude);
});
} else {
$wishes = $this->wishes;
@ -80,96 +85,14 @@ class Wishlist
/**
* Cards
*/
$userIsCurrent = isset($_SESSION['user']) && $this->data['user'] === $_SESSION['user']['id'];
$cardIndex = 0;
if (!empty($wishes)) { ?>
<div class="ui three column doubling stackable grid">
<?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;
?>
<?php foreach ($wishes as $wish) { ?>
<div class="column">
<div class="ui fluid card stretch" data-id="<?= $wish['id'] ?>" data-index="<?= $cardIndex ?>" data-cache="<?= $exists ?>">
<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>
<?= $wish->getCard($this->data['user'], false) ?>
</div>
<?php } ?>
</div>
<? $cardIndex++ ?>
<?php } else { ?>
<div class="ui container">
<div class="sixteen wide column">

View file

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