This commit is contained in:
Jay Trees 2022-04-05 09:32:54 +02:00
parent 2a985321fc
commit d64c0251a5
11 changed files with 85 additions and 125 deletions

View file

@ -7,9 +7,7 @@
*/
$api = true;
$response = array(
'success' => false,
);
$response = array();
ob_start();
@ -35,16 +33,13 @@ switch ($_SERVER['REQUEST_METHOD']) {
$response['data'][$table] = $count;
}
$response['success'] = true;
} else {
$count = $database
->query('SELECT COUNT(`id`) AS "count"
FROM `' . $_GET['table'] . '`;')
->fetch();
$response['data'] = $count;
$response['success'] = true;
$response['data'] = $count;
}
}
break;

View file

@ -9,9 +9,7 @@
use wishthis\URL;
$api = true;
$response = array(
'success' => false,
);
$response = array();
ob_start();
@ -22,10 +20,9 @@ switch ($_SERVER['REQUEST_METHOD']) {
if (isset($_GET['url'])) {
$url = new URL(base64_decode($_GET['url']));
$response['data'] = array(
$response['data'] = array(
'url' => $url->getPretty()
);
$response['success'] = true;
}
break;
}

View file

@ -73,8 +73,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
)
;');
$response['success'] = true;
$response['data'] = array(
$response['data'] = array(
'lastInsertId' => $database->lastInsertId(),
);
}
@ -91,8 +90,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
SET `status` = "' . $_PUT['wish_status'] . '"
WHERE `id` = ' . $_PUT['wish_id'] . '
;');
$response['success'] = true;
} elseif (isset($_PUT['wish_url_current'], $_PUT['wish_url_proposed'])) {
/**
* Update Wish URL
@ -101,8 +98,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
SET `url` = "' . $_PUT['wish_url_proposed'] . '"
WHERE `url` = "' . $_PUT['wish_url_current'] . '"
;');
$response['success'] = true;
}
break;
@ -113,8 +108,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
$database->query('DELETE FROM `wishes`
WHERE `id` = ' . $_DELETE['wish_id'] . '
;');
$response['success'] = true;
}
break;
}

View file

@ -9,9 +9,7 @@
use wishthis\{User, Wishlist};
$api = true;
$response = array(
'success' => false,
);
$response = array();
ob_start();
@ -35,8 +33,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
)
;');
$response['success'] = true;
$response['data'] = array(
$response['data'] = array(
'lastInsertId' => $database->lastInsertId(),
);
}
@ -49,12 +46,14 @@ switch ($_SERVER['REQUEST_METHOD']) {
$wishlists = $user->getWishlists();
$wishlists = array_map(
function ($dataWishlist) {
$data = $dataWishlist;
// $newFormat['name'] = $wishlist['name'];
/**
* Format wishlists to fit FUI dropdown
*/
$data = $dataWishlist;
$data['value'] = $dataWishlist['id'];
$data['text'] = $dataWishlist['name'];
$data['text'] = $dataWishlist['name'];
$wishlist = new Wishlist($dataWishlist['id']);
$wishlist = new Wishlist($dataWishlist['id']);
$data['cards'] = $wishlist->getCards();
return $data;
@ -63,7 +62,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
);
$response['results'] = $wishlists;
$response['success'] = true;
}
break;
@ -85,7 +83,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
WHERE `id` = ' . $_DELETE['wishlistID'] . '
;');
$response['success'] = true;
break;
}

View file

@ -29,7 +29,7 @@ $(function() {
return response;
}
$.fn.api.settings.successTest = function(response) {
return response.status == 'OK' || response.success || false;
return response.status == 'OK' || response.results || response.success || false;
}
$.fn.api.settings.onComplete = function(response, element, xhr) {
element.removeClass('loading');

View file

@ -5,25 +5,24 @@ $(function() {
fetch('/src/api/statistics.php?table=all', {
method: 'GET'
})
.then(response => response.json())
.then(response => {
if (response.success) {
showStatistic(
$('#wishes .value'),
response.data.wishes.count,
0
);
showStatistic(
$('#wishlists .value'),
response.data.wishlists.count,
0
);
showStatistic(
$('#users .value'),
response.data.users.count,
0
);
}
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
showStatistic(
$('#wishes .value'),
response.data.wishes.count,
0
);
showStatistic(
$('#wishlists .value'),
response.data.wishlists.count,
0
);
showStatistic(
$('#users .value'),
response.data.users.count,
0
);
});
});

View file

@ -87,15 +87,14 @@ $(function () {
method: 'PUT',
body : formData
})
.then(response => response.json())
.then(response => {
if (response.success) {
inputURL.val(info.url);
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
inputURL.val(info.url);
elementModalFetch.modal('hide');
elementModalFetch.modal('hide');
$('body').toast({ message: text.toast_wish_update });
}
$('body').toast({ message: text.toast_wish_update });
buttonFetch.removeClass('loading');
});

View file

@ -13,8 +13,8 @@ $(function () {
wishlists = response.results;
element.dropdown({
values : wishlists,
placeholder: text.wishlist_no_selection
values : wishlists,
placeholder : text.wishlist_no_selection
})
if ($_GET.wishlist) {
@ -58,11 +58,10 @@ $(function () {
fetch('/src/api/url.php?url=' + btoa(urlParams.toString()), {
method: 'GET'
})
.then(response => response.json())
.then(response => {
if (response.success) {
window.history.pushState({}, '', response.data.url);
}
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
window.history.pushState({}, '', response.data.url);
});
} else {
$('.button.wishlist-wish-add').addClass('disabled');
@ -156,7 +155,7 @@ $(function () {
progress.progress('increment');
$('.ui.dropdown').dropdown();
$('.ui.dropdown.options').dropdown();
});
}
@ -377,13 +376,11 @@ $(function () {
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
if (response.success) {
$('body').toast({ message: text.toast_wish_add });
$('body').toast({ message: text.toast_wish_add });
wishlistsRefresh();
wishlistsRefresh();
modalWishlistWishAdd.modal('hide');
}
modalWishlistWishAdd.modal('hide');
buttonAdd.removeClass('loading');
})
@ -418,17 +415,16 @@ $(function () {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(response => {
if(response.success) {
modalWishlistCreate.modal('hide');
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
modalWishlistCreate.modal('hide');
urlParams.set('wishlist', response.data.lastInsertId);
urlParams.set('wishlist', response.data.lastInsertId);
$('body').toast({ message: text.toast_wish_create });
$('body').toast({ message: text.toast_wish_create });
wishlistsRefresh();
}
wishlistsRefresh();
})
.finally(() => {
formWishlistCreate.removeClass('loading');

View file

@ -81,24 +81,4 @@ class User
return $wishlists;
}
/**
* Returns a list of wishes for a given wishlist.
*
* @param int $wishlist
*
* @return array
*/
public function getWishes(int $wishlist): array
{
global $database;
$wishes = $database
->query('SELECT *
FROM `wishes`
WHERE `wishlist` = ' . $wishlist . ';')
->fetchAll();
return $wishes;
}
}

View file

@ -10,10 +10,6 @@ namespace wishthis;
class Wishlist
{
private int $id;
private string $hash;
public array $data;
public array $wishes = array();
public bool $exists = false;
@ -34,17 +30,18 @@ class Wishlist
/**
* Get Wishlist
*/
$result = $database
$columns = $database
->query('SELECT *
FROM `wishlists`
WHERE `' . $column . '` = ' . $id_or_hash . ';')
->fetch();
$this->data = $result ? $result : array();
foreach ($columns as $key => $value) {
$this->$key = $value;
}
/** Exists */
if (isset($this->data['id'])) {
$this->id = $this->data['id'];
if (isset($this->id)) {
$this->exists = true;
} else {
return;
@ -53,15 +50,28 @@ class Wishlist
/**
* Get Wishes
*/
$this->wishes = $this->getWishes();
}
private function getWishes($sql = array()): array
{
global $database;
$SELECT = isset($sql['SELECT']) ? $sql['SELECT'] : '*';
$FROM = isset($sql['FROM']) ? $sql['FROM'] : '`wishes`';
$WHERE = isset($sql['WHERE']) ? $sql['WHERE'] : '`wishlist` = ' . $this->id;
$this->wishes = $database
->query('SELECT *
FROM `wishes`
WHERE `wishlist` = ' . $this->id . ';')
->query('SELECT ' . $SELECT . '
FROM ' . $FROM . '
WHERE ' . $WHERE . ';')
->fetchAll();
foreach ($this->wishes as &$wish) {
$wish = new Wish($wish, false);
}
return $this->wishes;
}
public function getCards($options = array()): string
@ -71,24 +81,18 @@ class Wishlist
/**
* Options
*/
$exclude = isset($options['exclude']) ? $options['exclude'] : array();
if ($exclude) {
$wishes = array_filter($this->wishes, function ($wish) use ($exclude) {
return !in_array($wish->status, $exclude);
});
} else {
$wishes = $this->wishes;
if (!empty($options)) {
$this->wishes = $this->getWishes($options);
}
/**
* Cards
*/
if (!empty($wishes)) { ?>
if (!empty($this->wishes)) { ?>
<div class="ui three column doubling stackable grid">
<?php foreach ($wishes as $wish) { ?>
<?php foreach ($this->wishes as $wish) { ?>
<div class="column">
<?= $wish->getCard($this->data['user']) ?>
<?= $wish->getCard($this->user) ?>
</div>
<?php } ?>
</div>

View file

@ -19,7 +19,7 @@ if (!$wishlist->exists) {
die();
}
$page = new Page(__FILE__, $wishlist->data['name']);
$page = new Page(__FILE__, $wishlist->name);
$page->header();
$page->bodyStart();
$page->navigation();
@ -33,7 +33,7 @@ $page->navigation();
/**
* Warn the wishlist creator
*/
if (isset($user->id) && $user->id === intval($wishlist->data['user']) && !empty($wishlist->wishes)) { ?>
if (isset($user->id) && $user->id === intval($wishlist->user) && !empty($wishlist->wishes)) { ?>
<div class="ui icon warning message wishlist-own">
<i class="exclamation triangle icon"></i>
<div class="content">
@ -48,7 +48,7 @@ $page->navigation();
</div>
<?php } ?>
<div class="ui horizontal stackable segments">
<div class="ui segments">
<div class="ui segment">
<h2 class="ui header"><?= __('What to do?') ?></h2>
<p><?= sprintf(
@ -64,7 +64,7 @@ $page->navigation();
<?php
echo $wishlist->getCards(
array(
'exclude' => array('unavailable'),
'WHERE' => '`wishlist` = ' . $wishlist->id . ' AND (`status` != "unavailable" OR `status` IS NULL)',
)
);
?>