From d64c0251a5da4baa96cacc3fd5156ffd98caba97 Mon Sep 17 00:00:00 2001 From: Jay Trees Date: Tue, 5 Apr 2022 09:32:54 +0200 Subject: [PATCH] Refactor --- src/api/statistics.php | 9 ++----- src/api/url.php | 7 ++---- src/api/wishes.php | 9 +------ src/api/wishlists.php | 19 +++++++-------- src/assets/js/default.js | 2 +- src/assets/js/home.js | 37 ++++++++++++++--------------- src/assets/js/wish.js | 13 +++++------ src/assets/js/wishlists.js | 38 ++++++++++++++---------------- src/classes/user.php | 20 ---------------- src/classes/wishlist.php | 48 +++++++++++++++++++++----------------- src/pages/wishlist.php | 8 +++---- 11 files changed, 85 insertions(+), 125 deletions(-) diff --git a/src/api/statistics.php b/src/api/statistics.php index 9775f46d..0bfcc8bd 100644 --- a/src/api/statistics.php +++ b/src/api/statistics.php @@ -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; diff --git a/src/api/url.php b/src/api/url.php index bf9ab09c..3d8d86ab 100644 --- a/src/api/url.php +++ b/src/api/url.php @@ -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; } diff --git a/src/api/wishes.php b/src/api/wishes.php index 8a5908cb..6d94ddce 100644 --- a/src/api/wishes.php +++ b/src/api/wishes.php @@ -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; } diff --git a/src/api/wishlists.php b/src/api/wishlists.php index 7af4ddae..ec61c670 100644 --- a/src/api/wishlists.php +++ b/src/api/wishlists.php @@ -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; } diff --git a/src/assets/js/default.js b/src/assets/js/default.js index 2114b325..a6a98741 100644 --- a/src/assets/js/default.js +++ b/src/assets/js/default.js @@ -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'); diff --git a/src/assets/js/home.js b/src/assets/js/home.js index ebe53c46..ae91da61 100644 --- a/src/assets/js/home.js +++ b/src/assets/js/home.js @@ -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 + ); }); }); diff --git a/src/assets/js/wish.js b/src/assets/js/wish.js index 9597b994..f7868ba3 100644 --- a/src/assets/js/wish.js +++ b/src/assets/js/wish.js @@ -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'); }); diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index 251afac1..b70cd2be 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -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'); diff --git a/src/classes/user.php b/src/classes/user.php index aa33f904..5b852c15 100644 --- a/src/classes/user.php +++ b/src/classes/user.php @@ -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; - } } diff --git a/src/classes/wishlist.php b/src/classes/wishlist.php index 28150741..524c59ad 100644 --- a/src/classes/wishlist.php +++ b/src/classes/wishlist.php @@ -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)) { ?>
- + wishes as $wish) { ?>
- getCard($this->data['user']) ?> + getCard($this->user) ?>
diff --git a/src/pages/wishlist.php b/src/pages/wishlist.php index 787189ec..0446ca63 100644 --- a/src/pages/wishlist.php +++ b/src/pages/wishlist.php @@ -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)) { ?>
@@ -48,7 +48,7 @@ $page->navigation();
-
+

navigation(); getCards( array( - 'exclude' => array('unavailable'), + 'WHERE' => '`wishlist` = ' . $wishlist->id . ' AND (`status` != "unavailable" OR `status` IS NULL)', ) ); ?>