Allow adding and editing product prices

This commit is contained in:
grandeljay 2022-06-13 10:43:28 +02:00
parent 95bbc03f2b
commit e1ec8bd69f
7 changed files with 110 additions and 57 deletions

View file

@ -22,9 +22,11 @@ switch ($_SERVER['REQUEST_METHOD']) {
if (isset($_GET['wish_id'])) {
$columns = $database
->query(
'SELECT *
'SELECT `wishes`.*,
`products`.`price`
FROM `wishes`
WHERE `id` = ' . $_GET['wish_id'] . ';'
LEFT JOIN `products` ON `wishes`.`id` = `products`.`wish`
WHERE `wishes`.`id` = ' . $_GET['wish_id'] . ';'
)
->fetch();
@ -60,6 +62,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
$wish_title = trim($_POST['wish_title']);
$wish_description = trim($_POST['wish_description']);
$wish_image = 'NULL';
$wish_url = trim($_POST['wish_url']);
$wish_priority = isset($_POST['wish_priority']) && $_POST['wish_priority'] ? $_POST['wish_priority'] : 'NULL';
$wish_is_purchasable = isset($_POST['wish_is_purchasable']) ? 'true' : 'false';
@ -85,34 +88,36 @@ switch ($_SERVER['REQUEST_METHOD']) {
$wish_image = is_null($info->image) ? 'NULL' : "' . $info->image . '";
$response = array(
'info' => $info,
'success' => true,
'info' => $info,
);
}
$database
->query(
'REPLACE INTO `wishes`
(
`id`,
`wishlist`,
`title`,
`description`,
`image`,
`url`,
`priority`,
`is_purchasable`
) VALUES (
' . $wish_id . ',
' . $wishlist_id . ',
"' . $wish_title . '",
"' . $wish_description . '",
' . $wish_image . ',
"' . $wish_url . '",
' . $wish_priority . ',
' . $wish_is_purchasable . '
);'
'UPDATE `wishes`
SET `wishlist` = ' . $wishlist_id . ',
`title` = "' . $wish_title . '",
`description` = "' . $wish_description . '",
`image` = ' . $wish_image . ',
`url` = "' . $wish_url . '",
`priority` = ' . $wish_priority . ',
`is_purchasable` = ' . $wish_is_purchasable . '
WHERE `id` = ' . $wish_id . ';'
);
/**
* Product
*/
if (!empty($_POST['wish_price'])) {
$wish_price = $_POST['wish_price'];
$database
->query(
'UPDATE `products`
SET `price` = ' . $wish_price . '
WHERE `wish` = ' . $wish_id . ';'
);
}
} elseif (isset($_POST['wishlist_id'])) {
/** Insert wish */
$wishlist_id = $_POST['wishlist_id'];
@ -136,12 +141,29 @@ switch ($_SERVER['REQUEST_METHOD']) {
' . $wish_is_purchasable . '
);'
);
/**
* Product
*/
if (!empty($_POST['wish_price'])) {
$wish_id = $database->lastInsertId();
$wish_price = $_POST['wish_price'];
$database
->query(
'INSERT INTO `products`
(
`wish`,
`price`
) VALUES (
' . $wish_id . ',
' . $wish_price . '
);'
);
}
}
$response['data'] = array(
'lastInsertId' => $database->lastInsertId(),
'success' => true,
);
$response['lastInsertId'] = $wish_id;
}
break;

View file

@ -1,20 +0,0 @@
$(function () {
/**
* Priority
*/
$('.modal .wishlist-wish-add .dropdown.priority').dropdown();
/**
* Properties
*/
$('.checkbox').checkbox({
onChecked : function() {
$('.menu.wish-add .item[data-tab="product"]').removeClass('disabled');
},
onUnchecked : function() {
$('.menu.wish-add .item[data-tab="product"]').addClass('disabled');
},
});
});

View file

@ -332,9 +332,18 @@ $(function () {
formEdit.addClass('loading');
formEdit.trigger('reset');
formEdit.find('.dropdown').dropdown('restore defaults');
formEdit.find('.item[data-tab="product"]').addClass('disabled');
formEdit.find('.item').tab('change tab', 'general');
/** Checkbox */
formEdit.find('.checkbox').checkbox({
onChecked : function() {
formEdit.find('.item[data-tab="product"]').removeClass('disabled');
},
onUnchecked : function() {
formEdit.find('.item[data-tab="product"]').addClass('disabled');
},
});
/** Get Wish */
var wishID = $(this).attr('data-id');
@ -350,12 +359,21 @@ $(function () {
.then(function(response) {
var wish = response.info;
/** General */
$('[name="wish_id"]').val(wish.id);
$('[name="wish_title"]').val(wish.title);
$('[name="wish_description"]').val(wish.description);
$('[name="wish_url"]').val(wish.url);
$('.ui.selection.dropdown.priority').dropdown('set selected', wish.priority);
$('[name="wish_is_purchasable"]').prop('checked', wish.is_purchasable);
if (wish.is_purchasable) {
formEdit.find('.checkbox').checkbox('check');
} else {
formEdit.find('.checkbox').checkbox('uncheck');
}
/** Product */
$('[name="wish_price"]').val(wish.price);
})
.catch(handleFetchCatch)
.finally(function() {
@ -450,9 +468,18 @@ $(function () {
var formAdd = $('.form.wishlist-wish-add');
formAdd.trigger('reset');
formAdd.find('.dropdown').dropdown('restore defaults');
formAdd.find('.item[data-tab="product"]').addClass('disabled');
formAdd.find('.item').tab('change tab', 'general');
/** Checkbox */
formAdd.find('.checkbox').checkbox({
onChecked : function() {
formAdd.find('.item[data-tab="product"]').removeClass('disabled');
},
onUnchecked : function() {
formAdd.find('.item[data-tab="product"]').addClass('disabled');
},
});
/** Modal */
var modalWishlistWishAdd = $('.ui.modal.wishlist-wish-add');
modalWishlistWishAdd.find('[name="wishlist_id"]').val($('.ui.dropdown.wishlists').dropdown('get value'));
@ -602,7 +629,7 @@ $(function () {
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
if (true !== response.success) {
if (!response.lastInsertId) {
return;
}
@ -633,7 +660,7 @@ $(function () {
.then(handleFetchError)
.then(handleFetchResponse)
.then(function(response) {
if (true !== response.success) {
if (!response.lastInsertId) {
return;
}

View file

@ -190,6 +190,21 @@ switch ($step) {
);');
$database->query('CREATE INDEX `idx_url` ON `wishes` (`url`);');
/**
* Products
*/
$database->query(
'CREATE TABLE `products` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`wish` INT NOT NULL,
`price` FLOAT NOT NULL DEFAULT 0,
FOREIGN KEY (`wish`)
REFERENCES `wishes` (`id`)
ON DELETE CASCADE
);'
);
$database->query('CREATE INDEX `idx_wish` ON `products` (`wish`);');
/**
* Options
*/

View file

@ -5,10 +5,7 @@
*/
namespace wishthis;
$scriptPart = '/src/assets/js/parts/wish-add.js';
?>
<script defer src="<?= $scriptPart ?>?m=<?= filemtime(ROOT . $scriptPart) ?>"></script>
<div class="ui secondary menu wish-add">
<a class="item active" data-tab="general"><?= __('General') ?></a>

View file

@ -16,5 +16,5 @@ CREATE INDEX `idx_wishlist` ON `wishlists_saved` (`wishlist`);
* Wishes
*/
ALTER TABLE `wishes`
ADD COLUMN `is_purchasable` BOOLEAN NOT NULL DEFAULT FALSE
ADD COLUMN `is_purchasable` BOOLEAN NOT NULL DEFAULT FALSE
;

12
src/update/0-7-0.sql Normal file
View file

@ -0,0 +1,12 @@
/**
* Products
*/
CREATE TABLE `products` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`wish` INT NOT NULL,
`price` FLOAT NOT NULL DEFAULT 0,
FOREIGN KEY (`wish`)
REFERENCES `wishes` (`id`)
ON DELETE CASCADE
);
CREATE INDEX `idx_wish` ON `products` (`wish`);