Refactor
This commit is contained in:
parent
2a985321fc
commit
d64c0251a5
11 changed files with 85 additions and 125 deletions
|
@ -7,9 +7,7 @@
|
|||
*/
|
||||
|
||||
$api = true;
|
||||
$response = array(
|
||||
'success' => false,
|
||||
);
|
||||
$response = array();
|
||||
|
||||
ob_start();
|
||||
|
||||
|
@ -35,8 +33,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
|||
|
||||
$response['data'][$table] = $count;
|
||||
}
|
||||
|
||||
$response['success'] = true;
|
||||
} else {
|
||||
$count = $database
|
||||
->query('SELECT COUNT(`id`) AS "count"
|
||||
|
@ -44,7 +40,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
|||
->fetch();
|
||||
|
||||
$response['data'] = $count;
|
||||
$response['success'] = true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -9,9 +9,7 @@
|
|||
use wishthis\URL;
|
||||
|
||||
$api = true;
|
||||
$response = array(
|
||||
'success' => false,
|
||||
);
|
||||
$response = array();
|
||||
|
||||
ob_start();
|
||||
|
||||
|
@ -25,7 +23,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
|||
$response['data'] = array(
|
||||
'url' => $url->getPretty()
|
||||
);
|
||||
$response['success'] = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -73,7 +73,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
|||
)
|
||||
;');
|
||||
|
||||
$response['success'] = true;
|
||||
$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;
|
||||
}
|
||||
|
|
|
@ -9,9 +9,7 @@
|
|||
use wishthis\{User, Wishlist};
|
||||
|
||||
$api = true;
|
||||
$response = array(
|
||||
'success' => false,
|
||||
);
|
||||
$response = array();
|
||||
|
||||
ob_start();
|
||||
|
||||
|
@ -35,7 +33,6 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
|||
)
|
||||
;');
|
||||
|
||||
$response['success'] = true;
|
||||
$response['data'] = array(
|
||||
'lastInsertId' => $database->lastInsertId(),
|
||||
);
|
||||
|
@ -49,8 +46,10 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
|||
$wishlists = $user->getWishlists();
|
||||
$wishlists = array_map(
|
||||
function ($dataWishlist) {
|
||||
/**
|
||||
* Format wishlists to fit FUI dropdown
|
||||
*/
|
||||
$data = $dataWishlist;
|
||||
// $newFormat['name'] = $wishlist['name'];
|
||||
$data['value'] = $dataWishlist['id'];
|
||||
$data['text'] = $dataWishlist['name'];
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -5,9 +5,9 @@ $(function() {
|
|||
fetch('/src/api/statistics.php?table=all', {
|
||||
method: 'GET'
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (response.success) {
|
||||
.then(handleFetchError)
|
||||
.then(handleFetchResponse)
|
||||
.then(function(response) {
|
||||
showStatistic(
|
||||
$('#wishes .value'),
|
||||
response.data.wishes.count,
|
||||
|
@ -23,7 +23,6 @@ $(function() {
|
|||
response.data.users.count,
|
||||
0
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -87,15 +87,14 @@ $(function () {
|
|||
method: 'PUT',
|
||||
body : formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if (response.success) {
|
||||
.then(handleFetchError)
|
||||
.then(handleFetchResponse)
|
||||
.then(function(response) {
|
||||
inputURL.val(info.url);
|
||||
|
||||
elementModalFetch.modal('hide');
|
||||
|
||||
$('body').toast({ message: text.toast_wish_update });
|
||||
}
|
||||
|
||||
buttonFetch.removeClass('loading');
|
||||
});
|
||||
|
|
|
@ -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) {
|
||||
.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 });
|
||||
|
||||
wishlistsRefresh();
|
||||
|
||||
modalWishlistWishAdd.modal('hide');
|
||||
}
|
||||
|
||||
buttonAdd.removeClass('loading');
|
||||
})
|
||||
|
@ -418,9 +415,9 @@ $(function () {
|
|||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(response => {
|
||||
if(response.success) {
|
||||
.then(handleFetchError)
|
||||
.then(handleFetchResponse)
|
||||
.then(function(response) {
|
||||
modalWishlistCreate.modal('hide');
|
||||
|
||||
urlParams.set('wishlist', response.data.lastInsertId);
|
||||
|
@ -428,7 +425,6 @@ $(function () {
|
|||
$('body').toast({ message: text.toast_wish_create });
|
||||
|
||||
wishlistsRefresh();
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
formWishlistCreate.removeClass('loading');
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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)',
|
||||
)
|
||||
);
|
||||
?>
|
||||
|
|
Loading…
Reference in a new issue