Skip to content

Commit

Permalink
Feature edit my gifts (#18)
Browse files Browse the repository at this point in the history
* Adding new line at end of CSS

* Ability to remove gift. Punting on emailing users who claimed it as wp_mail is not working. Closes #9

* Fixing the ability to claim gifts.

* You can now edit a gift. Closes #16

* Fixed bug with 'desire' not showing in claimed gifts

* Fixing small bugs throughout the code while testing.

* A few more tweaks

* Do not display link if there is no link for my list

* Removing todos
  • Loading branch information
Joshua Bryant committed Oct 26, 2018
1 parent 114982f commit 7adc1d3
Show file tree
Hide file tree
Showing 14 changed files with 359 additions and 59 deletions.
2 changes: 1 addition & 1 deletion hestia/nmbp/add-family.php
Expand Up @@ -85,7 +85,7 @@
<?php else : ?>
<div class="row">
<div class="col-md-12">
<p class="text-center">Sorry, but you do not have any more licenses...</p>
<p class="text-center">Sorry, but you either don't have access to this page or any more licenses...</p>
</div>
</div>
<?php endif; ?>
Expand Down
3 changes: 1 addition & 2 deletions hestia/nmbp/add-to-my-list.php
@@ -1,5 +1,4 @@
<?php
// @todo - Add js validation for form field submission
$iUserID = get_current_user_id();
?>

Expand All @@ -16,7 +15,7 @@
<input type="text" class="form-control" id="gift_link" name="gift_link" placeholder="Link to the gift (if there is one)">
</div>
<div class="form-group col-md-4">
<label for="gift_price">Gift</label>
<label for="gift_price">Price</label>
<input type="text" class="form-control" id="gift_price" name="gift_price" placeholder="Enter price of the gift">
</div>
<div class="form-group col-md-4">
Expand Down
97 changes: 95 additions & 2 deletions hestia/nmbp/ajax.php
Expand Up @@ -9,6 +9,14 @@
claimGift($_POST);
break;

case 'deleteGift' :
deleteGift($_POST);
break;

case 'editGift' :
editGift($_POST);
break;

case 'releaseGift' :
releaseGift($_POST);
break;
Expand Down Expand Up @@ -44,13 +52,67 @@ function claimGift($aPost)

}

function releaseGift($aPost)
function deleteGift($aPost)
{
$bContinue = helperArrayHasAllKeys($aPost, array('gift_id', 'claimed_id', 'user_id', 'quantity'));
$bContinue = helperArrayHasAllKeys($aPost, array('gift_id'));

if ($bContinue === false) {
returnJson("Error: Something went wrong on my end. Cannot delete the gift at this time.");
}

// set easy vars
$gift_id = $aPost['gift_id'];
$comment = (isset($aPost['comment'])) ? $aPost['comment'] : '';

// Remove the gift
removeGift($gift_id);

// Notify anyone who may have claimed that gift;
$sClaimedUserEmails = getUsersWhoClaimed($gift_id);
if ($sClaimedUserEmails !== '') {
// @todo: This returns false, but I don't care enough to figure out why right now.
wp_mail($sClaimedUserEmails, 'A gift you claimed has been deleted!', $comment);
}
returnJson("Success: The gift has been removed from your list!");
}

function editGift($aPost)
{
$bContinue = helperArrayHasAllKeys($aPost, array('gift_id', 'gift_title', 'gift_price', 'gift_link', 'gift_notes', 'gift_quantity', 'gift_desire'));
if ($bContinue === false) {
returnJson("Error: The required fields were not set correctly.");
}

// set easy vars
$gift_id = $aPost['gift_id'];
$title = $aPost['gift_title'];
$price = $aPost['gift_price'];
$link = $aPost['gift_link'];
$notes = $aPost['gift_notes'];
$quantity = $aPost['gift_quantity'];
$desire = $aPost['gift_desire'];

$aUpdate = array(
'title' => $title,
'price' => $price,
'link' => $link,
'notes' => $notes,
'quantity' => $quantity,
'desire' => $desire,
);

updateGift($aUpdate, $gift_id);
returnJson("Success: Your gift has been updated.");

}

function releaseGift($aPost)
{
// $bContinue = helperArrayHasAllKeys($aPost, array('gift_id', 'claimed_id', 'user_id', 'quantity'));
// if ($bContinue === false) {
// returnJson("Error: The required fields were not set correctly.");
// }

// set easy vars
$user_id = $aPost['user_id'];
$gift_id = $aPost['gift_id'];
Expand Down Expand Up @@ -91,6 +153,14 @@ function releaseClaim($claimed_id)
$wpdb->update('wp_claimed', $data, $where);
}

function removeGift($gift_id)
{
global $wpdb;
$data = array('active' => 0);
$where = array('id' => $gift_id);
$wpdb->update('wp_gift', $data, $where);
}

function getQuantityRemaining($gift_id)
{
global $wpdb;
Expand All @@ -113,6 +183,29 @@ function getQuantityClaimedByID($claimed_id)
return 0;
}

function getUsersWhoClaimed($gift_id)
{
global $wpdb;
$sSQL = "SELECT u.user_email FROM wp_claimed c
LEFT JOIN wp_users u on c.user_id = u.ID
WHERE c.gift_id = {$gift_id} AND c.active = 1;";
$aResults = $wpdb->get_results($sSQL);
$sReturn = '';
foreach ($aResults as $oResult) {
if (isset($oResult->user_email)) {
$sReturn .= $oResult->user_email . ',';
}
}
return rtrim($sReturn, ',');
}

function updateGift($aUpdateArray, $gift_id)
{
global $wpdb;
$where = array('id' => $gift_id);
$wpdb->update('wp_gift', $aUpdateArray, $where);
}

function updateQuantityRemaining($gift_id, $iQuantityRemaining)
{
global $wpdb;
Expand Down
8 changes: 5 additions & 3 deletions hestia/nmbp/css/common.css
Expand Up @@ -46,7 +46,9 @@ div.row.individual-gift:last-child {
.individual-gift span.dashicons-minus:hover,
.individual-gift span.dashicons-plus:hover,
span.dashicons-arrow-up-alt2:hover,
span.dashicons-arrow-down-alt2:hover {
span.dashicons-arrow-down-alt2:hover,
span.dashicons-dismiss:hover,
span.dashicons-edit:hover {
cursor: pointer;
}
span.dashicons-arrow-up-alt2, span.dashicons-arrow-down-alt2 {
Expand All @@ -57,9 +59,9 @@ span.dashicons-arrow-up-alt2, span.dashicons-arrow-down-alt2 {
.dashicons-plus {
color: #075600;
}
.dashicons-minus {
.dashicons-minus, .dashicons-dismiss {
color: darkred;
}
.dashicons-lock {
color: lightgray;
}
}
1 change: 1 addition & 0 deletions hestia/nmbp/display-family-list.php
Expand Up @@ -108,6 +108,7 @@
<select class="form-control" id="claim-gift_quantity" name="claim-gift_quantity"></select>
<input type="hidden" name="user_id" value="<?php echo $iUserID; ?>" />
<input type="hidden" name="gift_id" id="claim-gift-gift_id" value="" />
<input type="hidden" id="claim-gift-base_url" value="<?php echo get_template_directory_uri(); ?>" />
</div>

<div class="modal-footer">
Expand Down
5 changes: 3 additions & 2 deletions hestia/nmbp/display-my-claimed.php
Expand Up @@ -11,7 +11,7 @@
$iUserID = get_current_user_id();

// Get my gifts, thx
$sSQL = "SELECT c.*, g.link, g.price, g.title as 'gift', g.notes, u.*
$sSQL = "SELECT c.*, g.link, g.price, g.title as 'gift', g.notes, g.desire, u.*
FROM wp_claimed c
LEFT JOIN wp_gift g on c.gift_id = g.id
LEFT JOIN wp_users u on g.user_id = u.ID
Expand Down Expand Up @@ -88,11 +88,12 @@
<input type="hidden" name="user_id" value="<?php echo $iUserID; ?>" />
<input type="hidden" name="gift_id" id="release-gift-gift_id" value="" />
<input type="hidden" name="claim_id" id="release-gift-claimed_id" value="" />
<input type="hidden" name="base_url" id="claim-gift-base_url" value="<?php echo get_template_directory_uri(); ?>" />
</div>

<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="claim-submit">Release</button>
<button type="button" class="btn btn-primary" id="release-submit">Release</button>
</div>
</div>
</div>
Expand Down
105 changes: 102 additions & 3 deletions hestia/nmbp/display-my-list.php
Expand Up @@ -14,7 +14,6 @@
$sSQL = "SELECT * FROM wp_gift g
WHERE g.user_id = {$iUserID} AND g.active = 1;";
$aResults = $wpdb->get_results($sSQL);

?>
<h2>My Christmas List</h2>
<div class="my-gift-container">
Expand All @@ -33,9 +32,30 @@
<div class="col-md-4">
<p>
<span class="visible-sm visible-xs" style="font-weight:bold;">Gift: </span>
<a href="<?php echo $oGift->link; ?>" target="_blank">
<span class="dashicons dashicons-edit"
data-toggle="modal"
data-target="#editGiftModal"
data-gift_title="<?php echo $oGift->title; ?>"
data-gift_price="<?php echo $oGift->price; ?>"
data-gift_quantity="<?php echo $oGift->quantity; ?>"
data-gift_desire="<?php echo $oGift->desire; ?>"
data-gift_link="<?php echo $oGift->link; ?>"
data-gift_notes="<?php echo $oGift->notes; ?>"
data-gift_id="<?php echo $oGift->id; ?>"
></span>&nbsp;
<span class="dashicons dashicons-dismiss"
data-toggle="modal"
data-target="#deleteGiftModal"
data-gift_id="<?php echo $oGift->id; ?>"
data-gift_title="<?php echo $oGift->title; ?>"
></span>&nbsp;
<?php if (trim($oGift->link) !== '') : ?>
<a href="<?php echo $oGift->link; ?>" target="_blank">
<?php echo $oGift->title; ?>
</a>
<?php else : ?>
<?php echo $oGift->title; ?>
</a>
<?php endif; ?>
</p>
</div>
<div class="col-md-1">
Expand Down Expand Up @@ -66,3 +86,82 @@

<?php endforeach; ?>
</div>

<!-- Delete Modal -->
<div class="modal fade" id="deleteGiftModal" tabindex="-1" role="dialog" aria-labelledby="delete-gift-modal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<!-- Title -->
<h5 class="modal-title" id="release-gift-modal">Delete Gift: <span id="delete-gift-title"></span></h5>
</div>

<div class="modal-body" id="delete-gift-modal-body">
<p style="color:red;"><strong>You are about to completely remove this gift from your Christmas list.</strong></p>
<label for="delete-comment">Reason for removing this gift:</label>
<textarea class="form-control" rows="2" id="delete-comment"></textarea>
<input type="hidden" name="gift_id" id="delete-gift-gift_id" value="" />
<input type="hidden" name="base_url" id="delete-gift-base_url" value="<?php echo get_template_directory_uri(); ?>" />
</div>

<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="delete-submit">Release</button>
</div>
</div>
</div>
</div>

<!-- Edit Modal -->
<div class="modal fade" id="editGiftModal" tabindex="-1" role="dialog" aria-labelledby="edit-gift-modal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<!-- Title -->
<h5 class="modal-title" id="release-gift-modal">Edit Gift: <span id="edit-gift-title"></span></h5>
</div>

<div class="modal-body" id="edit-gift-modal-body">
<div class="form-group col-md-6">
<label for="edit-gift_title">Gift</label>
<input type="text" class="form-control" id="edit-gift_title" name="edit-gift_title">
</div>
<div class="form-group col-md-6">
<label for="edit-gift_price">Price</label>
<input type="text" class="form-control" id="edit-gift_price" name="edit-gift_price">
</div>
<div class="form-group col-md-6">
<label for="edit-gift_quantity">Quantity</label>
<select class="form-control" id="edit-gift_quantity" name="edit-gift_quantity">
<?php for ($i = 0; $i <= 10; $i++) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div>
<div class="form-group col-md-6">
<label for="edit-gift_desire">Desire (10 = most)</label>
<select class="form-control" id="edit-gift_desire" name="edit-gift_desire">
<?php for ($i = 0; $i <= 10; $i++) : ?>
<option value="<?php echo $i; ?>"><?php echo $i; ?></option>
<?php endfor; ?>
</select>
</div>
<div class="form-group col-md-12">
<label for="edit-gift_link">Link (optional)</label>
<input type="text" class="form-control" id="edit-gift_link" name="edit-gift_link">
</div>
<div class="form-group col-md-12">
<label for="edit-gift_notes">Notes for gift</label>
<textarea class="form-control" id="edit-gift_notes" name="edit-gift_notes" rows="3"></textarea>
</div>
<input type="hidden" name="gift_id" id="edit-gift-gift_id" value="" />
<input type="hidden" name="base_url" id="edit-gift-base_url" value="<?php echo get_template_directory_uri(); ?>" />
</div>

<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="edit-submit">Edit</button>
</div>
</div>
</div>
</div>
14 changes: 13 additions & 1 deletion hestia/nmbp/functions.php
Expand Up @@ -21,4 +21,16 @@ function echo_pre($vVariable, $bDie = false)
if ($bDie) {
die;
}
}
}

function is_josh()
{
if (get_current_user_id() == 2) {
return true;
}
return false;
}

if (!current_user_can('manage_options')) {
show_admin_bar(false);
}
9 changes: 0 additions & 9 deletions hestia/nmbp/home.php

This file was deleted.

6 changes: 4 additions & 2 deletions hestia/nmbp/js/claim.js
Expand Up @@ -20,15 +20,17 @@ jQuery(document).ready(function(){
});

$oClaimSubmit.click(function(){

var sBaseURL = jQuery('#claim-gift-base_url').val();
var data = {
"user_id" : iUserID,
"gift_id" : jQuery('#claim-gift-gift_id').val(),
"quantity" : jQuery('#claim-gift_quantity').val(),
"action" : "claimGift"
};

jQuery.post( "<?php echo get_template_directory_uri(); ?>/nmbp/ajax.php", data, function(data){
console.log(sBaseURL);

jQuery.post( sBaseURL + "/nmbp/ajax.php", data, function(data){
var sHTML = '<p>' + data + '.. This page will refresh in 10 seconds.';
sHTML += '<br/><br/> -OR- <br/><br/>';
sHTML += '<a href="/christmas-list/">REFRESH NOW!</a></p>';
Expand Down

0 comments on commit 7adc1d3

Please sign in to comment.