Merge branch 'develop' into stable

This commit is contained in:
Jay Trees 2022-01-21 08:15:22 +01:00
commit 5fda4637a9
15 changed files with 467 additions and 225 deletions

47
includes/api/products.php Normal file
View file

@ -0,0 +1,47 @@
<?php
/**
* products.php
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\User;
$api = true;
$response = array(
'success' => false,
);
require '../../index.php';
switch ($_SERVER['REQUEST_METHOD']) {
case 'PUT':
parse_str(file_get_contents("php://input"), $_PUT);
if (isset($_PUT['productID'], $_PUT['productStatus'])) {
$database->query('UPDATE `products`
SET `status` = "' . $_PUT['productStatus'] . '"
WHERE `id` = ' . $_PUT['productID'] . '
;');
$response['success'] = true;
}
break;
case 'DELETE':
parse_str(file_get_contents("php://input"), $_DELETE);
if (isset($_DELETE['productID'])) {
$database->query('DELETE FROM `products`
WHERE `id` = ' . $_DELETE['productID'] . '
;');
$response['success'] = true;
}
break;
}
echo json_encode($response);
header('Content-type: application/json; charset=utf-8');
die();

View file

@ -19,3 +19,19 @@
.ui.progress.nolabel:last-child {
margin: 0;
}
/**
* Card
*/
.ui.card .header > img {
height: 1.25em;
}
/**
* Label
*/
p .ui.horizontal.label {
margin: 0;
cursor: default;
user-select: none;
}

View file

@ -4,14 +4,142 @@ $(function() {
*/
$.fn.api.settings.api = {
'get wishlists' : '/includes/api/wishlists.php',
'delete wishlist' : '/includes/api/wishlists.php'
'delete wishlist' : '/includes/api/wishlists.php',
'update product status': '/includes/api/products.php',
'delete product' : '/includes/api/products.php',
};
$('.ui.dropdown.wishlists').dropdown({
filterRemoteData: true
});
/**
* Refresh Wishlist
*/
wishlistRefresh();
/**
* Commit to Product
*/
$('.ui.button.commit').on('click', function() {
var button = $(this);
var card = button.closest('.ui.card');
var column = card.closest('.column');
$('body')
.modal({
title: 'Really commit?',
content: 'Would you really like to commit to this purchase? It will no longer appear in the wishlist anymore.',
class: 'tiny',
actions: [
{
text: 'Yes, commit',
class: 'approve primary'
},
{
text: 'Cancel',
class: ''
}
],
onApprove: function() {
/**
* Update product status
*/
button.api({
action: 'update product status',
method: 'PUT',
data: {
productID: card.data('id'),
productStatus: 'unavailable'
},
on: 'now',
onResponse: function(response) {
return response;
},
successTest: function(response) {
return response.success || false;
},
onComplete: function(response, element, xhr) {
},
onSuccess: function(response, element, xhr) {
column.fadeOut();
},
onFailure: function(response, element, xhr) {
},
onError: function(errorMessage, element, xhr) {
},
onAbort: function(errorMessage, element, xhr) {
}
});
}
})
.modal('show');
});
/**
* Delete Product
*/
$('.ui.button.delete').on('click', function() {
var button = $(this);
var card = button.closest('.ui.card');
var column = card.closest('.column');
$('body')
.modal({
title: 'Really delete?',
content: 'Would you really like to delete to this product? It will be gone forever.',
class: 'tiny',
actions: [
{
text: 'Yes, delete',
class: 'approve primary'
},
{
text: 'Cancel',
class: ''
}
],
onApprove: function() {
/**
* Delete product
*/
button.api({
action: 'delete product',
method: 'DELETE',
data: {
productID: card.data('id'),
},
on: 'now',
onResponse: function(response) {
return response;
},
successTest: function(response) {
return response.success || false;
},
onComplete: function(response, element, xhr) {
},
onSuccess: function(response, element, xhr) {
column.fadeOut();
},
onFailure: function(response, element, xhr) {
},
onError: function(errorMessage, element, xhr) {
},
onAbort: function(errorMessage, element, xhr) {
}
});
}
})
.modal('show');
});
});
function wishlistRefresh() {

View file

@ -9,10 +9,16 @@ $(function() {
if (wishlistValue) {
$('.wishlist-view').removeClass('disabled');
} else {
$('.wishlist-view').addClass('disabled');
}
const urlParams = new URLSearchParams(window.location.search);
if (wishlistValue === urlParams.get('wishlist')) {
$('.wishlist-share').removeClass('disabled');
$('.wishlist-delete button').removeClass('disabled');
} else {
$('.wishlist-view').addClass('disabled');
$('.wishlist-share').addClass('disabled');
$('.wishlist-delete button').addClass('disabled');
}

View file

@ -0,0 +1,25 @@
$(function() {
/**
* User Warning
*/
if ($('.wishlist-own').length) {
$('body')
.modal({
title: $('.wishlist-own .header').text(),
content: $('.wishlist-own .text').html(),
class: '',
blurring: true,
closable: false,
actions: [
{
text: 'Show wishlist anyway',
class: 'approve primary'
}
],
onApprove: function() {
$('.wishlist-own').slideUp();
}
})
.modal('show');
}
});

View file

@ -43,15 +43,9 @@ class Options
public function setOption(string $key, string $value): void
{
try {
$option = $this->database->query('UPDATE `options`
SET `value`
WHERE `key` = ' . $key . '
$this->database->query('UPDATE `options`
SET `value` = "' . $value . '"
WHERE `key` = "' . $key . '"
;');
$value = $option['value'] ?? '';
} catch (\Throwable $th) {
//throw $th;
}
}
}

View file

@ -177,9 +177,6 @@ class Page
<i class="upload icon"></i> Update
</a>
<?php } ?>
<div class="right item">
<div class="ui input"><input type="text" placeholder="Search..."></div>
</div>
</div>
</div>
<div class="ui hidden divider"></div>

View file

@ -0,0 +1,161 @@
<?php
/**
* A wishlist.
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
namespace wishthis;
class Wishlist
{
private int $id;
private string $hash;
public array $data;
public array $products = array();
public bool $exists = false;
public function __construct(int|string $id_or_hash)
{
global $database;
$column;
if (is_int($id_or_hash)) {
$column = 'id';
}
if (is_string($id_or_hash)) {
$column = 'hash';
$id_or_hash = '"' . $id_or_hash . '"';
}
/**
* Get Wishlist
*/
$this->data = $database->query('SELECT *
FROM `wishlists`
WHERE `' . $column . '` = ' . $id_or_hash . ';')
->fetch();
/** Exists */
if (isset($this->data['id'])) {
$this->id = $this->data['id'];
$this->exists = true;
} else {
return;
}
/**
* Get Products
*/
$this->products = $database->query('SELECT *
FROM `products`
WHERE `wishlist` = ' . $this->id . ';')
->fetchAll();
}
public function getCards($options = array()): void
{
/**
* Exclude
*/
$exclude = isset($options['exclude']) ? $options['exclude'] : array();
if ($exclude) {
$products = array_filter($this->products, function ($product) use ($exclude) {
return !in_array($product['status'], $exclude);
});
} else {
$products = $this->products;
}
/**
* Cards
*/
if (!empty($products)) { ?>
<div class="ui three column stackable grid wishlist-cards">
<?php foreach ($products as $product) {
/**
* @link https://github.com/oscarotero/Embed
*/
$embed = new \Embed\Embed();
$info = $embed->get($product['url']);
?>
<div class="column">
<div class="ui fluid card" data-id="<?= $product['id'] ?>">
<?php if ($info->image) { ?>
<div class="image">
<img src="<?= $info->image ?>" />
</div>
<?php } ?>
<div class="content">
<?php if ($info->title) { ?>
<div class="header">
<?php if ($info->favicon) { ?>
<img src="<?= $info->favicon ?>" />
<?php } ?>
<?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">
<?= $info->keywords ?>
</div>
<?php } ?>
<?php if ($info->description) { ?>
<div class="description">
<?= $info->description ?>
</div>
<?php } ?>
</div>
<div class="extra content">
<?php if ($info->publishedTime) { ?>
<span class="right floated">
<?= $info->publishedTime ?>
</span>
<?php } ?>
<?php if ($info->providerName) { ?>
<?= $info->providerName ?>
<?php } ?>
</div>
<div class="extra content">
<?php if ($this->data['user'] === $_SESSION['user']['id']) { ?>
<a class="ui tiny red button delete">Delete</a>
<?php } else { ?>
<a class="ui tiny button commit">Commit</a>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } else { ?>
<?php if (isset($_GET['wishlist'])) { ?>
<div class="ui icon message">
<i class="info circle icon"></i>
<div class="content">
<div class="header">
Empty
</div>
<p>This wishlist seems to be empty.</p>
</div>
</div><?php
}
}
}
}

View file

@ -34,11 +34,18 @@ if ('POST' === $_SERVER['REQUEST_METHOD']) {
$database->query('ALTER TABLE `wishlists` ADD INDEX(`hash`);');
$database->query('INSERT INTO `options` (`key`, `value`) VALUES ("version", "' . VERSION . '");');
// Use this for future versions since it didn't existsin 0.1.0
// $options->setOption('version', VERSION);
}
/** Current version is below 0.3.0 */
if (-1 === version_compare($options->version, '0.3.0')) {
$database->query('ALTER TABLE `products`
ADD `status` VARCHAR(32) NOT NULL AFTER `url`
;');
}
/** Update version */
$options->setOption('version', VERSION);
header('Location: /?page=home');
die();
}

View file

@ -19,7 +19,7 @@ if (isset($_POST['name'])) {
) VALUES (
' . $_SESSION['user']['id'] . ',
"' . $_POST['name'] . '",
"' . time() . $_SESSION['user']['id'] . $_POST['name'] . '"
"' . sha1(time() . $_SESSION['user']['id'] . $_POST['name']) . '"
)
;');

View file

@ -31,6 +31,7 @@ $page->navigation();
</div>
<div class="field">
<label>Wishlist</label>
<select class="ui search selection dropdown loading wishlists" name="wishlist">
<option value="">Loading your wishlists...</option>
</select>

View file

@ -6,23 +6,17 @@
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\{Page, User};
use Embed\Embed;
use wishthis\{Page, User, Wishlist};
$page = new page(__FILE__, 'View wishlist');
$page->header();
$page->navigation();
$products = array();
/**
* Get wishlist products
*/
if (isset($_GET['wishlist'])) {
$wishlist = $database->query('SELECT * FROM `wishlists`
WHERE `id` = "' . $_GET['wishlist'] . '"')
->fetch();
$products = $user->getProducts($_GET['wishlist']);
$wishlist = new Wishlist(intval($_GET['wishlist']));
}
/**
@ -38,7 +32,7 @@ if (isset($_POST['wishlist_delete_id'])) {
<div class="ui container">
<h1 class="ui header"><?= $page->title ?></h1>
<div class="ui horizontal segments">
<div class="ui horizontal stackable segments">
<div class="ui segment">
<h2 class="ui header">Wishlists</h2>
<p>Please select a wishlist to view.</p>
@ -60,7 +54,7 @@ if (isset($_POST['wishlist_delete_id'])) {
<h2 class="ui header">Options</h1>
<p>Wishlist related options.</p>
<a class="ui small labeled icon button wishlist-share disabled" href="/?wishlist=<?= $wishlist['hash'] ?? '' ?>" target="_blank">
<a class="ui small labeled icon button wishlist-share <?= !isset($_GET['wishlist']) ? 'disabled' : '' ?>" href="/?wishlist=<?= $wishlist->data['hash'] ?? '' ?>" target="_blank">
<i class="share icon"></i>
Share
</a>
@ -68,7 +62,7 @@ if (isset($_POST['wishlist_delete_id'])) {
<form class="ui form wishlist-delete" method="post" style="display: inline-block;">
<input type="hidden" name="wishlist_delete_id" />
<button class="ui small labeled red icon button disabled" type="submit">
<button class="ui small labeled red icon button <?= !isset($_GET['wishlist']) ? 'disabled' : '' ?>" type="submit">
<i class="trash icon"></i>
Delete
</button>
@ -76,96 +70,11 @@ if (isset($_POST['wishlist_delete_id'])) {
</div>
</div>
<?php if (!empty($products)) { ?>
<div class="ui three column grid">
<?php foreach ($products as $product) { ?>
<?php
/**
* @link https://github.com/oscarotero/Embed
*/
$embed = new Embed();
$info = $embed->get($product['url']);
if (isset($_GET['wishlist'])) {
$wishlist->getCards();
}
?>
<div class="column">
<div class="ui fluid card">
<?php if ($info->image) { ?>
<div class="image">
<img src="<?= $info->image ?>" />
</div>
<?php } ?>
<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">
<?= $info->keywords ?>
</div>
<?php } ?>
<?php if ($info->description) { ?>
<div class="description">
<?= $info->description ?>
</div>
<?php } ?>
</div>
<div class="extra content">
<?php if ($info->publishedTime) { ?>
<span class="right floated">
<?= $info->publishedTime ?>
</span>
<?php } ?>
<?php if ($info->favicon) { ?>
<?php if ($info->providerName) { ?>
<img src="<?= $info->favicon ?>"
title="<?= $info->providerName ?>"
alt="<?= $info->providerName ?>"
/>
<?php } else { ?>
<img src="<?= $info->favicon ?>" />
<?php } ?>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } else { ?>
<?php if (isset($_GET['wishlist'])) { ?>
<div class="ui icon message">
<i class="info circle icon"></i>
<div class="content">
<div class="header">
Empty
</div>
<p>The selected wishlist seems to be empty.</p>
<a class="ui mini button" href="/?page=wishlist-product-add">Add a product</a>
</div>
</div>
<?php } else { ?>
<div class="ui icon message">
<i class="info circle icon"></i>
<div class="content">
<div class="header">
No wishlist selected
</div>
<p>Select a wishlist to see it's products.</p>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
</main>

View file

@ -1,123 +1,74 @@
<?php
/**
* wishlist.php
* Template for viewing a wishlist directly via its link.
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
use wishthis\{Page, User};
use Embed\Embed;
use wishthis\{Page, User, Wishlist};
$page = new page(__FILE__, 'Wishlist');
$wishlist = new Wishlist($_GET['wishlist']);
if (!$wishlist->exists) {
http_response_code(404);
?>
<h1>Not found</h1>
<p>The requested Wishlist was not found and likely deleted by its creator.</p>
<?php
die();
}
$page = new page(__FILE__, $wishlist->data['name']);
$page->header();
$page->navigation();
$wishlist = $database->query('SELECT * FROM `wishlists`
WHERE `hash` = "' . $_GET['wishlist'] . '"')
->fetch();
$products = $user->getProducts($wishlist['id']);
?>
<main>
<div class="ui container">
<h1 class="ui header"><?= $page->title ?></h1>
<div class="ui segment">
<h2 class="ui header"><?= $wishlist['name'] ?></h2>
</div>
<?php if (!empty($products)) { ?>
<div class="ui three column grid">
<?php foreach ($products as $product) { ?>
<?php
/**
* @link https://github.com/oscarotero/Embed
* Warn the wishlist creator
*/
$embed = new Embed();
$info = $embed->get($product['url']);
if (isset($user->id) && $user->id === intval($wishlist->data['user'])) { ?>
<div class="ui icon warning message wishlist-own">
<i class="exclamation triangle icon"></i>
<div class="content">
<div class="header">
Careful
</div>
<div class="text">
<p>
You are viewing your own wishlist!
You will be able to see which products have already been bought for you.
Don't you want to be surprised?
</p>
<p>
It's probably best to just close this tab.
</p>
</div>
</div>
</div>
<?php } ?>
<div class="ui segment">
<h2 class="ui header">What to do?</h2>
<p>
If you found something you would like to buy,
click the <span class="ui tiny horizontal label">Commit</span> button
and it will become unavailable for others.
</p>
</div>
<?php
$wishlist->getCards(
array(
'exclude' => array('unavailable'),
)
);
?>
<div class="column">
<div class="ui fluid card">
<?php if ($info->image) { ?>
<div class="image">
<img src="<?= $info->image ?>" />
</div>
<?php } ?>
<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">
<?= $info->keywords ?>
</div>
<?php } ?>
<?php if ($info->description) { ?>
<div class="description">
<?= $info->description ?>
</div>
<?php } ?>
</div>
<div class="extra content">
<?php if ($info->publishedTime) { ?>
<span class="right floated">
<?= $info->publishedTime ?>
</span>
<?php } ?>
<?php if ($info->favicon) { ?>
<?php if ($info->providerName) { ?>
<img src="<?= $info->favicon ?>"
title="<?= $info->providerName ?>"
alt="<?= $info->providerName ?>"
/>
<?php } else { ?>
<img src="<?= $info->favicon ?>" />
<?php } ?>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } else { ?>
<?php if (isset($_GET['wishlist'])) { ?>
<div class="ui icon message">
<i class="info circle icon"></i>
<div class="content">
<div class="header">
Empty
</div>
<p>The selected wishlist seems to be empty.</p>
<a class="ui mini button" href="/?page=wishlist-product-add">Add a product</a>
</div>
</div>
<?php } else { ?>
<div class="ui icon message">
<i class="info circle icon"></i>
<div class="content">
<div class="header">
No wishlist selected
</div>
<p>Select a wishlist to see it's products.</p>
</div>
</div>
<?php } ?>
<?php } ?>
</div>
</main>

View file

@ -77,7 +77,7 @@ if ($options) {
/**
* Update
*/
define('VERSION', '0.2.0');
define('VERSION', '0.3.0');
if ($options) {
if (-1 === version_compare($options->version, VERSION)) {