Merge branch 'add-custom-wish' into develop

This commit is contained in:
grandeljay 2022-02-27 02:55:33 +01:00
commit 786b1adbd6
12 changed files with 349 additions and 332 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'])) {
/**
@ -24,9 +49,13 @@ switch ($_SERVER['REQUEST_METHOD']) {
$database->query('INSERT INTO `wishes`
(
`wishlist`,
`title`,
`description`,
`url`
) VALUES ('
. $_POST['wishlist_id'] . ',
"' . $_POST['wish_title'] . '",
"' . $_POST['wish_description'] . '",
"' . $_POST['wish_url'] . '"
)
;');

View file

@ -144,17 +144,19 @@ img {
max-height: calc(2 * 1.28571429em);
overflow: hidden;
}
.ui.card > .content > .description {
max-height: calc(3em * var(--lineHeight));
overflow: hidden;
}
@media (hover: hover) {
.ui.card > .content:not(.extra) {
margin-bottom: calc(3em * var(--lineHeight));
}
.ui.card > .content > .description {
position: absolute;
left: 1em;
right: 1em;
max-height: calc(3em * var(--lineHeight));
}
}

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,114 +120,35 @@ $(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) {
return;
}
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
*/
.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');
progress.progress('increment');
}
});
refresh.removeClass('working');
});
}
/**
@ -380,24 +301,21 @@ $(function () {
var modalWishlistWishAdd = $('.ui.modal.wishlist-wish-add');
modalWishlistWishAdd.find('[name="wishlist_id"]').val($('.ui.dropdown.wishlists').dropdown('get value'));
modalWishlistWishAdd.find('.primary.approve.button').addClass('disabled');
modalWishlistWishAdd
.modal({
onApprove: function (button) {
button.addClass('loading');
onApprove: function (buttonAdd) {
buttonAdd.addClass('loading');
var form = $('.ui.form.wishlist-wish-fetch');
var formData = new URLSearchParams();
formData.append('wishlist_id', form.find('input[name="wishlist_id"]').val());
formData.append('wish_url', form.find('input[name="wish_url"]').val());
var form = $('.form.wishlist-wish-add');
var formData = new URLSearchParams(new FormData(form[0]));
fetch('/src/api/wishes.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(response => {
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
if (response.success) {
$('body').toast({
class: 'success',
@ -410,7 +328,10 @@ $(function () {
modalWishlistWishAdd.modal('hide');
}
button.removeClass('loading');
buttonAdd.removeClass('loading');
})
.catch(function(error) {
console.log(error);
});
return false;
@ -420,41 +341,47 @@ $(function () {
});
/** Fetch */
$(document).on('submit', '.wishlist-wish-fetch', function (event) {
event.preventDefault();
var form = $(event.currentTarget);
var href = form.find('[name="wish_url"]').val();
$(document).on('click', '#wishlist-wish-add-url-validate', function () {
var buttonValidate = $(this);
var inputWishURL = buttonValidate.prev();
var elementModalAdd = $('.ui.modal.wishlist-wish-add');
var elementButtons = elementModalAdd.find('.actions .button');
var elementImage = elementModalAdd.find('.image img');
var elementTitle = elementModalAdd.find('[name="wish_title"]');
var elementDescription = elementModalAdd.find('[name="wish_description"]');
form.addClass('loading');
buttonValidate.addClass('disabled loading');
elementButtons.addClass('disabled');
fetch('/src/api/cache.php?wish_url=' + href, {
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;
/**
* Image
* Title
*/
if (info.image && elementImage.length) {
elementImage.attr('src', info.image);
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 !== href) {
if (info.url && info.url !== inputWishURL.val()) {
var elementModalFetch = $('.ui.modal.wishlist-wish-fetch');
elementModalFetch.find('input.current').val(href);
elementModalFetch.find('input.current').val(inputWishURL.val());
elementModalFetch.find('input.proposed').val(info.url);
elementButtons.addClass('disabled');
@ -465,7 +392,7 @@ $(function () {
closable: false,
onApprove: function (buttonFetch) {
var formData = new URLSearchParams();
formData.append('wish_url_current', href);
formData.append('wish_url_current', inputWishURL.val());
formData.append('wish_url_proposed', info.url);
buttonFetch.addClass('loading');
@ -477,7 +404,7 @@ $(function () {
.then(response => response.json())
.then(response => {
if (response.success) {
form.find('input[type="url"]').val(info.url);
inputWishURL.val(info.url);
elementModalFetch.modal('hide');
}
@ -488,17 +415,20 @@ $(function () {
return false;
},
onHide: function() {
form.removeClass('loading');
buttonValidate.removeClass('disabled loading');
elementButtons.removeClass('disabled');
}
})
.modal('show');
} else {
form.removeClass('loading');
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
@ -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 *
$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,94 +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() ? 'true' : 'false';
?>
<?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) { ?>
<button class="ui icon button refresh">
<i class="refresh icon"></i>
</button>
<?php } ?>
</div>
<div class="content">
<?php if ($info->title) { ?>
<div class="header">
<?php if ($info->url) { ?>
<a href="<?= $info->url ?>" target="_blank"><?= $info->title ?></a>
<?php } else { ?>
<?= $info->title ?>
<?php } ?>
</div>
<?php } ?>
<?php if ($info->keywords) { ?>
<div class="meta">
<?= implode(', ', $info->keywords) ?>
</div>
<?php } ?>
<?php if ($info->description) { ?>
<div class="description">
<?= $info->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>
View
</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

@ -140,13 +140,15 @@ switch ($step) {
$database->query('CREATE TABLE `wishes` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`wishlist` INT NOT NULL,
`type` VARCHAR(32) NULL DEFAULT NULL,
`title` VARCHAR(128) NULL DEFAULT NULL,
`description` TEXT NULL DEFAULT NULL,
`url` VARCHAR(255) NULL DEFAULT NULL,
`status` VARCHAR(32) NULL DEFAULT NULL,
FOREIGN KEY (`wishlist`)
REFERENCES `wishlists` (`id`)
ON DELETE CASCADE
);');
$database->query('CREATE INDEX `idx_url` ON `wishes` (`url`);');
/**
* Options

View file

@ -111,7 +111,7 @@ $page->navigation();
<div class="column">
<h2 class="ui header">Authentication</h2>
<p>
Prove you are a Human, Lizard-person or Zuck-Like creature.
Prove you are a Human, Lizard-person or Zuck-like creature.
Please name a planet from our solar system.
</p>

View file

@ -127,28 +127,43 @@ $page->navigation();
<div class="header">
Add a wish
</div>
<div class="image content">
<div class="ui medium image">
<img src="/src/assets/img/no-image.svg" loading="lazy" />
</div>
<div class="content">
<div class="description">
<div class="ui header">Wish</div>
<p>Fill out the below fields to add your new wish.</p>
<p>Fill out any or all of the below fields to add your new wish.</p>
<form class="ui form wishlist-wish-fetch" method="post">
<form class="ui form wishlist-wish-add" method="post">
<input type="hidden" name="wishlist_id" />
<div class="field">
<label>URL</label>
<input type="url" name="wish_url" />
<label>Title</label>
<input type="text" name="wish_title" maxlength="128" />
</div>
<input class="ui button" type="submit" value="Fetch" />
<div class="field">
<label>Description</label>
<textarea name="wish_description"></textarea>
</div>
<div class="field">
<label>URL</label>
<div class="ui action input url">
<input type="url" name="wish_url" maxlength="255" />
<button class="ui button"
id="wishlist-wish-add-url-validate"
type="button"
>
Validate
</button>
</div>
</div>
</form>
</div>
</div>
<div class="actions">
<div class="ui primary approve button disabled">
<div class="ui primary approve button">
Add
</div>
<div class="ui deny button">