Allow deleting a product

This commit is contained in:
Jay Trees 2022-01-20 14:33:06 +01:00
parent 1b5b503651
commit 0a79d646cc
6 changed files with 177 additions and 171 deletions

View file

@ -28,6 +28,18 @@ switch ($_SERVER['REQUEST_METHOD']) {
$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);

View file

@ -6,13 +6,140 @@ $(function() {
'get wishlists' : '/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

@ -22,66 +22,4 @@ $(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

@ -13,8 +13,8 @@ class Wishlist
private int $id;
private string $hash;
public mixed $data;
public mixed $products;
public array $data;
public array $products = array();
public bool $exists = false;
@ -57,14 +57,24 @@ class Wishlist
->fetchAll();
}
public function getCards(): void
public function getCards($options = array()): void
{
$products = array_filter($this->products, function ($product) {
if ('unavailable' !== $product['status']) {
return true;
}
});
/**
* 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">
@ -122,7 +132,11 @@ class Wishlist
<?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>

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']));
}
/**
@ -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 <?= !isset($_GET['wishlist']) ? '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>
@ -76,96 +70,11 @@ if (isset($_POST['wishlist_delete_id'])) {
</div>
</div>
<?php if (!empty($products)) { ?>
<div class="ui three column stackable 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

@ -62,7 +62,13 @@ $page->navigation();
</p>
</div>
<?php $wishlist->getCards() ?>
<?php
$wishlist->getCards(
array(
'exclude' => array('unavailable'),
)
);
?>
</div>
</main>