Permalink
Browse files

- The gift details is no longer directly in the list page code.

- Added ability to move a gift to another category by drag/drop.
  • Loading branch information...
1 parent ab47f52 commit 46b2f9261d6e677a2fc38585d9cee47ba23540fb Philippe Villiers committed Jul 6, 2012
View
@@ -0,0 +1,106 @@
+<?php
+/**
+ * Show a gift
+ */
+define('BESTWISHES', true);
+
+// Load common
+require_once(dirname(__FILE__). DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR . 'BwCommon.inc.php');
+
+$autoloader = BwClassAutoloader::getInstance();
+
+if(!isset($_GET['type']) || empty($_GET['type'])) {
+ exit;
+}
+if(!isset($_GET['listId']) || empty($_GET['listId'])) {
+ exit;
+}
+
+$type = trim($_GET['type']);
+$listId = intval($_GET['listId']);
+
+$list = new BwList();
+if(!$list->loadById($listId)) {
+ exit;
+}
+
+if(BwUser::checkSession()) {
+ $sessionOk = true;
+ $user = BwUser::getInstance();
+ $disp = new BwSessionDisplay($user->getTheme()->shortName, $user);
+} else {
+ // Nobody logged
+ $sessionOk = false;
+ $disp = new BwDisplay(BwConfig::get('theme', 'default'));
+}
+switch($type) {
+ case 'gift':
+ if(!isset($_GET['id']) || empty($_GET['id'])) {
+ exit;
+ }
+ $giftId = intval($_GET['id']);
+ $gift = new BwGift($giftId);
+ if(!$gift->load()) {
+ $disp->showJSONData(null);
+ exit;
+ }
+ // Cleanup some data
+ if(isset($user)) {
+ if($user->isListOwner($list)) {
+ $gift->filterContent();
+ }
+ } else {
+ $gift->filterContent();
+ }
+ $disp->showJSONData($gift);
+ break;
+ case 'cats':
+ // Get the cats list
+ if(!$sessionOk) {
+ exit;
+ }
+ $allCats = BwCategory::getAllByListId($id);
+ foreach($allCats as $cat) {
+ unset($cat->gifts);
+ }
+ $disp->showJSONData($allCats);
+ break;
+ case 'list':
+ if($sessionOk) {
+ $user->loadParams();
+ $tipsText = BwConfig::get('list_tips_text', false);
+ $disp->assign('tipsText', $tipsText);
+ } else {
+ $user = null;
+ // Stop if not public
+ // TODO: Tweak this
+ if(!boolVal($publicLists)) {
+ $disp->header(_('This list is not public'), _('You must be logged in to view this list'));
+ echo _('You must be logged in to view this list');
+ $disp->footer();
+ exit;
+ }
+ }
+ $disp->assign('cfgMaxEdits', BwConfig::get('max_gift_name_edits', false));
+ // Translation strings
+ $disp->assignListStrings();
+
+ // Load and display the list
+ $subTitle = '';
+ // Remove some categories/gifts depending on the situation
+ $list->filterContent($sessionOk, $user);
+ $listTitle = sprintf(_('List display: %s\'s list'), $list->name);
+ $nextEventData = $list->getNearestEventData();
+ $daysLeft = intval($nextEventData['daysLeft']);
+ $eventText = sprintf(_('Next event (%s): '), $nextEventData['name']);
+ if($daysLeft > 0) {
+ $eventText .= sprintf(ngettext('%d day', '%d days', $daysLeft), $daysLeft);
+ } else {
+ $eventText .= _('today');
+ }
+ $disp->assign('list', $list);
+ $disp->assign('daysLeft', $daysLeft);
+ $disp->assign('timeLeftText', $eventText);
+ $disp->display('list_display.tpl');
+ break;
+}
View
@@ -354,5 +354,50 @@
}
$disp->showJSONStatus($status, getStatusMessage($statusCode, $statusMessages));
break;
+ case 'move':
+ if($user->canEditList($listId) || $user->isListOwner($list)) {
+ // Move a gift to another category
+ if(!isset($_POST['id']) || empty($_POST['id'])) {
+ exit;
+ }if(!isset($_POST['targetCatId']) || empty($_POST['targetCatId'])) {
+ exit;
+ }
+ $giftId = intval($_POST['id']);
+ $catId = intval($_POST['targetCatId']);
+ $gift = new BwGift($giftId);
+ $category = new BwCategory($catId);
+ $statusMessages = array(
+ 0 => _('Gift moved successfully'),
+ 1 => _('Could not move the gift to this category'),
+ 2 => _('Source and destination categories are the same!'),
+ 99 => _('Internal error')
+ );
+ $statusCode = 99;
+ $status = 'error';
+ if(!$category->load() || !$gift->load()) {
+ $disp->showJSONStatus($status, getStatusMessage($statusCode, $statusMessages));
+ exit;
+ }
+ $sourceCategory = new BwCategory($gift->getCategoryId());
+ if(!$sourceCategory->load()) {
+ $disp->showJSONStatus($status, getStatusMessage($statusCode, $statusMessages));
+ exit;
+ }
+ if($category->giftListId !== $listId || $sourceCategory->giftListId !== $listId) {
+ $disp->showJSONStatus($status, getStatusMessage($statusCode, $statusMessages));
+ exit;
+ }
+ if($gift->getCategoryId() === $category->getId()) {
+ $statusCode = 2;
+ $disp->showJSONStatus($status, getStatusMessage($statusCode, $statusMessages));
+ exit;
+ }
+ $statusCode = $gift->moveToCategory($catId, $listId);
+ if($statusCode == 0) {
+ $status = 'success';
+ }
+ $disp->showJSONStatus($status, getStatusMessage($statusCode, $statusMessages));
+ }
+ break;
}
View
@@ -1,25 +1,39 @@
-function showGiftDetailsWindow(giftDetails)
+function showGiftDetailsWindow(giftId, listId)
{
- if(typeof giftDetails !== 'undefined') {
- $('#gift_details_name').text(giftDetails.name);
- $('#gift_details_added').text(date(bwLng.dateFormat, strtotime(giftDetails.addedDate)));
- if(giftDetails.isBought) {
- $('#gift_details_buy').show();
- $('#gift_details_buy_who').text(giftDetails.boughtByName);
- if(giftDetails.purchaseDate != null) {
- $('#gift_details_buy_date').text(date(bwLng.dateFormat, strtotime(giftDetails.purchaseDate)));
- }
- if(typeof giftDetails.boughtComment !== 'undefined' && giftDetails.boughtComment != null && giftDetails.boughtComment.length > 0) {
- $('#gift_details_buy_comment').show();
- $('#gift_details_buy_comment_text').text(giftDetails.boughtComment);
+ var currlistId = parseInt(listId);
+ var currGiftId = parseInt(giftId);
+ $.ajax({
+ url: bwURL + '/a_gift_show.php?listId=' + currlistId + '&id=' + currGiftId,
+ dataType: 'json',
+ error: function(jqXHR, textStatus, errorThrown) {
+ showFlashMessage('error', 'An error occured: ' + errorThrown);
+ },
+ success: function(returnedData, textStatus, jqXHR) {
+ if(typeof returnedData !== 'undefined') {
+ var giftDetails = returnedData;
+ $('#gift_details_name').text(giftDetails.name);
+ $('#gift_details_added').text(date(bwLng.dateFormat, strtotime(giftDetails.addedDate)));
+ if(giftDetails.isBought) {
+ $('#gift_details_buy').show();
+ $('#gift_details_buy_who').text(giftDetails.boughtByName);
+ if(giftDetails.purchaseDate != null) {
+ $('#gift_details_buy_date').text(date(bwLng.dateFormat, strtotime(giftDetails.purchaseDate)));
+ }
+ if(typeof giftDetails.boughtComment !== 'undefined' && giftDetails.boughtComment != null && giftDetails.boughtComment.length > 0) {
+ $('#gift_details_buy_comment').show();
+ $('#gift_details_buy_comment_text').text(giftDetails.boughtComment);
+ } else {
+ $('#gift_details_buy_comment').hide();
+ }
+ } else {
+ $('#gift_details_buy').hide();
+ }
+ giftDetailsDialog.dialog('open');
} else {
- $('#gift_details_buy_comment').hide();
+ showFlashMessage('error', 'Internal error');
}
- } else {
- $('#gift_details_buy').hide();
}
- giftDetailsDialog.dialog('open');
- }
+ });
}
function addCat(listId)
@@ -354,6 +368,30 @@ function addSurpriseGift(listId, force) {
}
}
+function moveGift(giftId, listId, targetCatId)
+{
+ var currGiftId = parseInt(giftId);
+ var currTargetCatId = parseInt(targetCatId);
+ var currListId = parseInt(listId);
+ $.ajax({
+ type: 'POST',
+ url: bwURL + '/a_gifts_mgmt.php?listId=' + listId + '&action=move',
+ data: {type: 'gift', targetCatId: currTargetCatId, id: currGiftId},
+ dataType: 'json',
+ error: function(jqXHR, textStatus, errorThrown) {
+ showFlashMessage('error', 'An error occured: ' + errorThrown);
+ },
+ success: function(returnedData, textStatus, jqXHR) {
+ if(returnedData.status == 'success') {
+ showFlashMessage('info', returnedData.message);
+ reloadList(listId);
+ } else {
+ showFlashMessage('error', returnedData.message);
+ }
+ }
+ });
+}
+
function showBuyWindow(giftName, giftId, catId, listId)
{
currentGiftId = parseInt(giftId);
@@ -459,8 +497,9 @@ function reloadList(listId) {
currentListId = parseInt(listId);
$.ajax({
type: 'GET',
- url: bwURL + '/a_list_show.php',
- data: {id: currentListId},
+ cache: false,
+ url: bwURL + '/a_elem_show.php',
+ data: {listId: currentListId, type: 'list'},
error: function(jqXHR, textStatus, errorThrown) {
showFlashMessage('error', 'An error occured: ' + errorThrown);
},
@@ -477,8 +516,9 @@ function reloadCatsList(listId) {
currentListId = parseInt(listId);
$.ajax({
type: 'GET',
- url: bwURL + '/a_cats_show.php',
- data: {id: currentListId},
+ cache: false,
+ url: bwURL + '/a_elem_show.php',
+ data: {listId: currentListId, type: 'cats'},
dataType: 'json',
error: function(jqXHR, textStatus, errorThrown) {
showFlashMessage('error', 'An error occured: ' + errorThrown);
@@ -517,6 +557,7 @@ function updatePwd() {
// Let the server handle the rest
$.ajax({
type: 'POST',
+ cache: false,
url: bwURL + '/a_opts_mgmt.php?action=editpwd',
data: {currentPasswd: currentPwd, newPasswd: newPwd, newPasswdRepeat: newPwdRepeat},
dataType: 'json',
View
@@ -127,6 +127,7 @@ public function assignListStrings()
$this->assign('lngGiftNameP', _('Gift name:'));
$this->assign('lngCategoryNameP', _('Category name:'));
$this->assign('lngGiftCategoryP', _('Gift category:'));
+ $this->assign('lngSameMoveCategory', _('Source and destination categories are the same!'));
// Explanation texts
$this->assign('lngAddCatExplanation', _('To add a category:<br />-&nbsp;Fill its name<br />-&nbsp;Click on the &#8220;Add the category&#8221; button below'));
@@ -144,6 +145,7 @@ public function assignListStrings()
$this->assign('lngEditExplanation', sprintf(_('To edit a gift name, click on the %s icon to the left of it.<br />Once you have finished the edition, click anywhere in the page or use Enter key to validate the modification(s).<br />%s The modifications allowed are limited.'),
'<img class="icon_text" src="' . $this->themeWebDir . '/img/edit.png" alt="' . _('Edit') . '" />', '<img class="icon_text" src="' . $this->themeWebDir . '/img/exclamation.png" alt="!" />'));
}
+ $this->assign('lngShowDetailsExplanation', _('To show a gift\'s details, such as the addition date, purchase info (if applicable), ... Just double-click on its name.'));
// Possible actions
$this->assign('lngActnAddCategory', _('Add a category to the list'));
@@ -152,6 +154,7 @@ public function assignListStrings()
$this->assign('lngActnDelGift', _('Delete a gift'));
$this->assign('lngActnDelCategory', _('Delete a category'));
$this->assign('lngActnAddSurpriseGift', _('Add a surprise gift (in an already existing category) to the list'));
+ $this->assign('lngActnShowDetails', _('Show a gift\'s details'));
$this->assign('lngActnDisplayPDF', _('Display the list as PDF'));
}
View
@@ -30,6 +30,11 @@ public function getId()
return $this->id;
}
+ public function getCategoryId()
+ {
+ return $this->categoryId;
+ }
+
public function load($id = null)
{
if(!empty($id))
@@ -635,6 +640,7 @@ public function markAsBought($listId = null, $userId = null, $purchaseComment =
$result = $db->exec();
if($result) {
// Empty cache
+ BwCache::delete('gift_' . $this->id);
BwCache::delete('category_all_list_' . $listId);
BwCache::delete('category_' . $this->categoryId);
BwCache::delete('gift_all_list_' . $listId);
@@ -689,10 +695,61 @@ public function markAsReceived($listId = null) {
$result = $db->exec();
if($result) {
// Empty cache
+ BwCache::delete('gift_' . $this->id);
+ BwCache::delete('category_all_list_' . $listId);
+ BwCache::delete('category_' . $this->categoryId);
+ BwCache::delete('gift_all_list_' . $listId);
+ BwCache::delete('gift_all_cat_' . $this->categoryId);
+ $resultValue = 0;
+ } else {
+ $resultValue = 1;
+ }
+ }
+ return $resultValue;
+ }
+
+ /**
+ * Moves a gift to another category
+ */
+ public function moveToCategory($catId = null, $listId = null) {
+ $resultValue = 99;
+ if(empty($catId) || empty($listId)) {
+ return $resultValue;
+ }
+
+ $db = BwDatabase::getInstance();
+ $queryParams = array(
+ 'tableName' => 'gift',
+ 'queryType' => 'UPDATE',
+ 'queryFields' => array(
+ 'category_id' => ':category_id'
+ ),
+ 'queryCondition' => 'id = :id',
+ 'queryValues' => array(
+ array(
+ 'parameter' => ':id',
+ 'variable' => $this->id,
+ 'data_type' => PDO::PARAM_INT
+ ),
+ array(
+ 'parameter' => ':category_id',
+ 'variable' => $catId,
+ 'data_type' => PDO::PARAM_INT
+ )
+ ),
+
+ );
+ if($db->prepareQuery($queryParams)) {
+ $result = $db->exec();
+ if($result) {
+ // Empty cache
+ BwCache::delete('gift_' . $this->id);
BwCache::delete('category_all_list_' . $listId);
BwCache::delete('category_' . $this->categoryId);
+ BwCache::delete('category_' . $catId);
BwCache::delete('gift_all_list_' . $listId);
BwCache::delete('gift_all_cat_' . $this->categoryId);
+ BwCache::delete('gift_all_cat_' . $catId);
$resultValue = 0;
} else {
$resultValue = 1;
Oops, something went wrong.

0 comments on commit 46b2f92

Please sign in to comment.