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

View file

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

View file

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

View file

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

View file

@ -29,7 +29,7 @@ $(function() {
return response; return response;
} }
$.fn.api.settings.successTest = function(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) { $.fn.api.settings.onComplete = function(response, element, xhr) {
element.removeClass('loading'); element.removeClass('loading');

View file

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

View file

@ -87,15 +87,14 @@ $(function () {
method: 'PUT', method: 'PUT',
body : formData body : formData
}) })
.then(response => response.json()) .then(handleFetchError)
.then(response => { .then(handleFetchResponse)
if (response.success) { .then(function(response) {
inputURL.val(info.url); 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'); buttonFetch.removeClass('loading');
}); });

View file

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

View file

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

View file

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