Allow committing to products

This commit is contained in:
Jay Trees 2022-01-20 13:45:09 +01:00
parent f8715c4d54
commit 1b5b503651
8 changed files with 292 additions and 98 deletions

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

@ -0,0 +1,35 @@
<?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;
}
echo json_encode($response);
header('Content-type: application/json; charset=utf-8');
die();

View file

@ -8,7 +8,7 @@
use wishthis\User;
$api = true;
$api = true;
$response = array(
'success' => false,
);

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

@ -3,8 +3,9 @@ $(function() {
* Fomantic UI
*/
$.fn.api.settings.api = {
'get wishlists' : '/includes/api/wishlists.php',
'delete wishlist' : '/includes/api/wishlists.php'
'get wishlists' : '/includes/api/wishlists.php',
'delete wishlist' : '/includes/api/wishlists.php',
'update product status': '/includes/api/products.php',
};
$('.ui.dropdown.wishlists').dropdown({

View file

@ -1,4 +1,7 @@
$(function() {
/**
* User Warning
*/
if ($('.wishlist-own').length) {
$('body')
.modal({
@ -19,4 +22,66 @@ $(function() {
})
.modal('show');
}
/**
* Commit
*/
$('.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');
});
});

View file

@ -0,0 +1,147 @@
<?php
/**
* A wishlist.
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
namespace wishthis;
class Wishlist
{
private int $id;
private string $hash;
public mixed $data;
public mixed $products;
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(): void
{
$products = array_filter($this->products, function ($product) {
if ('unavailable' !== $product['status']) {
return true;
}
});
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">
<a class="ui tiny button commit">Commit</a>
</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

@ -29,13 +29,21 @@ if ('POST' === $_SERVER['REQUEST_METHOD']) {
$database->query('ALTER TABLE `users` ADD INDEX(`password`);');
$database->query('ALTER TABLE `wishlists`
ADD `hash` VARCHAR(128) NOT NULL AFTER `name`
ADD `hash` VARCHAR(128) NOT NULL AFTER `name`
;');
$database->query('ALTER TABLE `wishlists` ADD INDEX(`hash`);');
$database->query('INSERT INTO `options` (`key`, `value`) VALUES ("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');

View file

@ -1,25 +1,16 @@
<?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');
$page->header();
$page->navigation();
$wishlist = new Wishlist($_GET['wishlist']);
$wishlist = $database->query('SELECT * FROM `wishlists`
WHERE `hash` = "' . $_GET['wishlist'] . '"')
->fetch();
if ($wishlist) {
$products = $user->getProducts($wishlist['id']);
} else {
if (!$wishlist->exists) {
http_response_code(404);
?>
<h1>Not found</h1>
@ -27,6 +18,10 @@ if ($wishlist) {
<?php
die();
}
$page = new page(__FILE__, $wishlist->data['name']);
$page->header();
$page->navigation();
?>
<main>
@ -37,7 +32,7 @@ if ($wishlist) {
/**
* Warn the wishlist creator
*/
if (isset($user->id) && $user->id === intval($wishlist['user'])) { ?>
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">
@ -59,88 +54,15 @@ if ($wishlist) {
<?php } ?>
<div class="ui segment">
<h2 class="ui header"><?= $wishlist['name'] ?></h2>
<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 if (!empty($products)) { ?>
<div class="ui three column stackable grid wishlist-cards">
<?php foreach ($products as $product) { ?>
<?php
/**
* @link https://github.com/oscarotero/Embed
*/
$embed = new Embed();
$info = $embed->get($product['url']);
?>
<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>This wishlist seems to be empty.</p>
</div>
</div>
<?php } ?>
<?php } ?>
<?php $wishlist->getCards() ?>
</div>
</main>