#!/usr/bin/perl # ------------------------------------------------------------------------ # index.mp : user-level interface for the shopping cart system. # # Copyright 2006+, DeadBunny Enterprises, Inc. all rights reserved. # # $Id: index.mp,v 1.45 2007/07/23 20:29:49 ravyn Exp $ # $RCSfile: index.mp,v $ # ------------------------------------------------------------------------ use strict; no strict 'refs'; use String::Strip; use Data::UUID; do "../../perl-bin/dbugsite.rc"; do "../../perl-bin/initialize.rc"; do "../../perl-bin/functions/load-functions.pl"; do "../../perl-bin/functions/cart-crud.pl"; do "../../perl-bin/functions/cart-do.pl"; do "../../perl-bin/functions/cart-go.pl"; $| = 1; my ($query, $dbh, $dbInfo, $dConfig, %dH) = nonmember_init(); # -- this is how we know that a user is admin for this section. $dH{'iUserIsAdmin'} = ($dH{'iMyAccessLevel'} & $site::SHOP_MGR) > 0; $dH{'szShopDefaultPhone'} = $dConfig->{szShopDefaultPhone}; $dH{'iShopPhoneNumberFlag'} = $dConfig->{iShopPhoneNumberFlag}; if ($dH{'szAction'} eq '') { $dH{'szAction'} = 'menu'; } my $routine = $dH{'szAction'} . "Go"; %dH = &$routine ($dbh, $dbInfo, $query, %dH); # -- we always have to get the cart status, so we can put that here. # $dH{'szCartStatus'} = getCartStatus ($dbh, $dbInfo, %dH); # -- even if we have no cart ID, still set the cookie. # my $cartCookie = new CGI::Cookie(-name => 'szCartID', -value=> $dH{'szCartID'}, -domain=> $site::cookieDomain, -path=> '/'); my $sessionCookie = new CGI::Cookie(-name => 'shopSessionID', -value=> $dH{'shopSessionID'}, -domain=> $site::cookieDomain, -path=> '/'); $dH{'szFeaturedItemSidebar'} = generateFeaturedSB($dbh); $dH{'szRecentItemSidebar'} = generateRecentSB($dbh, $dH{'shopSessionID'}); my $data = dbAccess::parseDBPage($dbh, $dbInfo, %dH); $dbh->commit; $dbh->disconnect; print $query->header (-type=>'text/html', -cookie=>[$cartCookie, $sessionCookie], -expires=>'-1d'); print $data; exit; # ------------- # menuGo: # # display the main shop menu. i don't really know what's going # to go here - maybe a list of new items, maybe a welcome screen with # not much on it, who knows. # sub menuGo { my ($dbh, $dbInfo, $query, %dH) = @_; return (browseByCategoryGo($dbh, $dbInfo, $query, %dH)); return (%dH); } # searchGo: # # display the shop search page. # sub searchGo { my ($dbh, $dbInfo, $query, %dH) = @_; my @catList = listAllCategory($dbh, 1, 'szCategoryName'); my @venList = listAllVendor($dbh, 1, 'szCompanyName'); $dH{'szCategoryList'} = ""; $dH{'szVendorList'} = ""; $dH{'szFileName'} = 'shoppingcart/cm-search-template.html'; return (%dH); } # doSearchGo: # # perform the actual search of the database and display the results. # sub doSearchGo { my ($dbh, $dbInfo, $query, %dH) = @_; $dH{'iStart'} = ($dH{'iStart'}+0 <= 0) ? 0 : $dH{'iStart'}; my $allItems = 1; if ($dH{'iUserIsAdmin'}) { $allItems = -1; } my ($resCode, @fullItemList) = searchForItems($dbh, $allItems, 0, 501, 'a.szItemName', $dH{'szKeyword'}, $dH{'iCategoryID'}, $dH{'iShopVendorID'}, $dH{'fMinimumPrice'}, $dH{'fMaximumPrice'}, $dH{'iFeaturedOnly'}, $dH{'iNewOnly'}, -1); my $count = $#fullItemList+1; my $i=0; my @itemList; for ($i=$dH{'iStart'}; $i<=$dH{'iStart'}+15; $i++) { if (defined $fullItemList[$i]->{iItemID}) { push(@itemList, $fullItemList[$i]); } else { last if 1; } } $dH{'iSearchShopVendorID'} = $dH{'iShopVendorID'}; $dH{'iSearchCategoryID'} = $dH{'iCategoryID'}; $dH{'encKW'} = cgiUtil::urlEscape($dH{'szKeyword'}); my $oldStart = $dH{'iStart'}; if ($#itemList == 15) { pop(@itemList); $dH{'iStart'} += 15; $dH{'szFileName'} = 'elements/cm-search-nextpage.html'; $dH{'szNextPage'} = dbAccess::parseDBPage($dbh, $dbInfo, %dH); } else { $dH{'szNextPage'} = 'At last page'; } if ($oldStart > 0) { $dH{'iStart'} = $oldStart - 15; $dH{'iStart'} = 0 if ($dH{'iStart'} < 0); $dH{'szFileName'} = 'elements/cm-search-previouspage.html'; $dH{'szPrevPage'} = dbAccess::parseDBPage($dbh, $dbInfo, %dH); } else { $dH{'szPrevPage'} = 'At first page'; } $dH{'iStart'} = $oldStart; if ($count > 500) { $dH{'szCountPhrase'} = "Over 500 items found. Please make your search more specific."; } else { my $start = $oldStart+1; my $end = $start + $#itemList; $dH{'szCountPhrase'} = "Found $count items, displaying $start to $end\."; } $dH{'szResults'} = ($resCode == $site::resultOK) ? formatCatalogList($dbh, 'searchResults', $dH{'iStart'}, $dH{'iUserIsAdmin'}, 0, 3, $dH{'iSubCategoryID'}, $dH{'iShopVendorID'}, $dH{'iCategoryID'}, $dH{'szKeyword'}, $dH{'iMyShopVendorID'}, @itemList) : $site::errorMessages{$resCode}; if ($resCode == $site::notFound) { open (FILE, $site::templateDir . 'elements/cm-no-items-found.html'); my @fileData = ; $dH{'szResults'} = join('', @fileData); close (FILE); } $dH{'szReturnTo'} = 'searchResults'; $dH{'szBreadCrumbs'} = qq|Search Results: $dH{'szCountPhrase'}|; $dH{'szFileName'} = 'shoppingcart/cm-item-list-template.html'; return (%dH); } # showWarrantyGo: # # display the pop-up window with the vendor warranty info. # sub showWarrantyGo { my ($dbh, $dbInfo, $query, %dH) = @_; my ($resCode, $dVendor) = retrieveVendorDo($dbh, $dH{'iShopVendorID'}); $dH{'szResults'} = ($resCode != $site::resultOK) ? $site::errorMessages{$resCode} : $dVendor->{szReturnPolicy}; $dH{'szFileName'} = 'shoppingcart/cm-warranty-view-template.html'; return (%dH); } # browseByCategoryGo: # # display a list of item categories that the user can shop in. # if a category is inactive, has no active sub-categories, or # has no active items in it, we won't display it. there really # isn't much to this function. # sub browseByCategoryGo { my ($dbh, $dbInfo, $query, %dH) = @_; my ($resCode, @list) = retrieveUserCategoryList($dbh, 0, 9999, 1); foreach (@list) { $dH{'szShopCategoryJump'} .= qq|\n|; } $dH{'szCategoryList'} = buildCategoryTable ($dbh, ! $dH{'iUserIsAdmin'}); $dH{'szFileName'} = 'shoppingcart/cm-category-browse-template.html'; return (%dH); } # browseByVendorGo: # # show a list of all active vendors that have at least one # active item that can be purchased. # sub browseByVendorGo { my ($dbh, $dbInfo, $query, %dH) = @_; $dH{'szVendorList'} = buildVendorTable($dbh, ! $dH{'iUserIsAdmin'}, $dH{'iMyShopVendorID'}); my ($resCode, @list) = retrieveUserVendorList($dbh, 0, 9999, 1); foreach (@list) { $dH{'szShopVendorJump'} .= qq|\n|; } $dH{'szFileName'} = 'shoppingcart/cm-vendor-browse-template.html'; return (%dH); } # browseBySubCategoryGo: # # once a user selects a category, we want to display a list of # sub-categories that he can choose from along with the number # of items available in that sub-category. # sub browseBySubCategoryGo { my ($dbh, $dbInfo, $query, %dH) = @_; my $category = retrieveCategory ($dbh, $dH{'iCategoryID'}); $dH{'szCategoryName'} = $category->{szCategoryName}; $dH{'szCategoryImage'} = $category->{szImageFile}; $dH{'szSubCategoryList'} = buildUserSubCategoryList($dbh, $dH{'iCategoryID'}, ! $dH{'iUserIsAdmin'}); $dH{'iTotalItems'} = countItemsInCategory($dbh, $dH{'iCategoryID'}, ! $dH{'iUserIsAdmin'}); if ($dH{'iTotalItems'} == 0) { $dH{'szMethod'} = 'SUBCAT'; return (showCatalogGo($dbh, $dbInfo, $query, %dH)); } if ($dH{'szSubCategoryList'} =~ /\D/) { my ($resCode, @featured) = searchForItems ($dbh, 1, 0, 9999, ' a.iItemID DESC ', '', $dH{'iCategoryID'}, 0, 0, 0, 1, 0, -1); $dH{'szFeaturedItems'} = formatCatalogList($dbh, 'SUBCAT', 0, $dH{'iUserIsAdmin'}, 1, 3, $dH{'iSubCategoryID'}, 0,0, '', $dH{'iMyShopVendorID'}, @featured) if ($resCode == $site::resultOK); } elsif ($dH{'szSubCategoryList'} == $site::goBack) { return (browseByCategoryGo($dbh, $dbInfo, $query, %dH)); } else { $dH{'szSubCategoryList'} = " $site::errorMessages{$dH{'szSubCategoryList'}} \n"; } $dH{'szFileName'} = 'shoppingcart/cm-subcategory-browse-template.html'; return (%dH); } # browseBySubCategoryVendorGo: # # once a user selects a vendor, we want to display a list of # sub-categories that he can choose from along with the number # of items available in that sub-category. # sub browseBySubCategoryVendorGo { my ($dbh, $dbInfo, $query, %dH) = @_; my $vendor = retrieveVendor ($dbh, $dH{'iShopVendorID'}); $dH{'szCompanyName'} = $vendor->{szCompanyName}; $dH{'szLogoImage'} = $vendor->{szImageFile}; $dH{'fCurrentBalance'} = sprintf("%.2f", $vendor->{fCurrentBalance}); $dH{'szSubCategoryList'} = buildUserSubCategoryListByVendor($dbh, $dH{'iShopVendorID'}, ! $dH{'iUserIsAdmin'}); $dH{'iTotalItems'} = countItemsByVendor ($dbh, $dH{'iShopVendorID'}, ! $dH{'iUserIsAdmin'}); if ($dH{'szSubCategoryList'} =~ /\D/) { my ($resCode, @featured) = searchForItems ($dbh, 1, 0, 9999, ' a.iItemID DESC ', '', 0, $dH{'iShopVendorID'}, 0, 0, 1, 0, -1); $dH{'szFeaturedItems'} = formatCatalogList($dbh, 'SUBCATV', 0, $dH{'iUserIsAdmin'}, 1, 3, $dH{'iSubCategoryID'}, 0,0, '', $dH{'iMyShopVendorID'}, @featured) if ($resCode == $site::resultOK); } elsif ($dH{'szSubCategoryList'} == $site::goBack) { $dH{'iShopVendorID'} = 0; return (browseByVendorGo($dbh, $dbInfo, $query, %dH)); } else { $dH{'szSubCategoryList'} = " $site::errorMessages{$dH{'szSubCategoryList'}} \n"; } $dH{'iShowAdminLinks'} = 1 if (($dH{'iCategoryID'}+0 > 0) && $dH{'iUserIsAdmin'}); $dH{'szFileName'} = 'shoppingcart/cm-vendor-browse-step2-template.html'; return (%dH); } # showCatalogGo: # # are we shopping by vendor or by sub-category or by something else? # once we know that, we can list the matching items. # sub showCatalogGo { my ($dbh, $dbInfo, $query, %dH) = @_; $dH{'iStart'} = ($dH{'iStart'}+0 <= 0) ? 0 : $dH{'iStart'}; # -- this is only used if we're searching by vendor # -- or subcategory. we don't need it for new/featured items. # my $key = ($dH{'szMethod'} eq 'VENDOR') ? $dH{'iShopVendorID'} : $dH{'iSubCategoryID'}; my ($resCode, @itemList) = ($dH{'szMethod'} ne 'SUBCATV') ? findItemDo($dbh, $dH{'szMethod'}, $key, $dH{'iStart'}, ! $dH{'iUserIsAdmin'}, '', 16) : findItemSubCatVendorDo($dbh, $dH{'iSubCategoryID'}, $dH{'iShopVendorID'}, $dH{'iStart'}, ! $dH{'iUserIsAdmin'}, '', 16); my $oldStart = $dH{'iStart'}; if ($#itemList == 15) { pop(@itemList); $dH{'iStart'} += 15; $dH{'szFileName'} = 'elements/cm-catalog-nextpage.html'; $dH{'szNextPage'} = dbAccess::parseDBPage($dbh, $dbInfo, %dH); } else { $dH{'szNextPage'} = 'At last page'; } if ($oldStart > 0) { $dH{'iStart'} = $oldStart - 15; $dH{'iStart'} = 0 if ($dH{'iStart'} < 0); $dH{'szFileName'} = 'elements/cm-catalog-previouspage.html'; $dH{'szPrevPage'} = dbAccess::parseDBPage($dbh, $dbInfo, %dH); } else { $dH{'szPrevPage'} = 'At first page'; } $dH{'iStart'} = $oldStart; $dH{'szResults'} = ($resCode == $site::resultOK) ? formatCatalogList($dbh, $dH{'szMethod'}, $dH{'iStart'}, $dH{'iUserIsAdmin'}, 0, 3, $dH{'iSubCategoryID'}, 0,0, '', $dH{'iMyShopVendorID'}, @itemList) : $site::errorMessages{$resCode}; if ($resCode == $site::notFound) { open (FILE, $site::templateDir . 'elements/cm-no-items-found.html'); my @fileData = ; $dH{'szResults'} = join('', @fileData); close (FILE); } # if we're in SUBCATV mode, $key will be the subcat ID $dH{'szBreadCrumbs'} = formatBreadCrumbs ($dbh, $dH{'szMethod'}, $key, $dH{'iShopVendorID'}); $dH{'szFileName'} = 'shoppingcart/cm-item-list-template.html'; return (%dH); } # viewItemGo: # # display an individual item. # sub viewItemGo { my ($dbh, $dbInfo, $query, %dH) = @_; my ($resCode, $dItem) = retrieveRealItemDo ($dbh, $dH{'iItemID'}); if (($dH{'iSubCategoryID'} > 0) && ($dH{'iSubCategoryID'} != $dItem->{iSubCategoryID})) { my $dShowSC = retrieveSubCategory($dbh, $dH{'iSubCategoryID'}); $dItem->{iCategoryID} = $dShowSC->{iCategoryID}; $dItem->{iSubCategoryID} = $dShowSC->{iSubCategoryID}; $dItem->{szCategoryName} = $dShowSC->{szCategoryName}; $dItem->{szSubCategoryName} = $dShowSC->{szSubCategoryName}; } if ( ($dItem->{iIsActive} == 0) && ($dItem->{iShopVendorID} != $dH{'iMyShopVendorID'}) && ! $dH{'iUserIsAdmin'}) { $dH{'szErrorMessage'} = $site::errorMessages{$site::notFound}; $dH{'szFileName'} = 'error-template.html'; return (%dH); } $dH{'szInactive'} = '(INACTIVE)' if (($dItem->{iIsActive} + $dItem->{iSubCatIsActive} + $dItem->{iCategoryIsActive} + $dItem->{iVendorIsActive}) < 4); $dH{'iDeleteOK'} = 1 if ($dH{'iUserIsAdmin'} && ($dItem->{iSubCategoryID} == 157)); $dH{'szPaymentTypes'} = site::mapToList($dbh, 'tblShopCardType', 'iCardTypeID', $dItem->{imvCardTypeID}, 1); $dH{'szShippingIncluded'} = ($dItem->{iPriceIncludesShipping} == 1) ? 'Note: Item price includes standard shipping' : ''; if ($dH{'shopSessionID'} eq '') { my $ug = new Data::UUID; my $uid = $ug->create(); $dH{'shopSessionID'} = $ug->to_string($uid); } updateRecentlyViewed ($dbh, $dH{'shopSessionID'}, $dH{'iItemID'}); %dH = cgiUtil::mergeHash ($dItem, {%dH}); if ($resCode == $site::resultOK) { if ( ($dItem->{iShopVendorID} == $dH{'iMyShopVendorID'}) && ($dH{'iCanUpdateProducts'} == 1) && (! $dH{'iUserIsAdmin'})) { $dH{'szFileName'} = 'elements/vendor-item-view-edit.html'; $dH{'szVendorLinks'} = dbAccess::parseDBPage($dbh, $dbInfo, %dH); } $dH{'szFileName'} = 'shoppingcart/cm-item-view-template.html'; %dH = formatItemDisplay (%dH); } else { $dH{'szErrorMessage'} = $site::errorMessages{$resCode}; $dH{'szFileName'} = 'error-template.html'; } return (%dH); }