#!/usr/bin/perl
# ------------------------------------------------------------------------
# index.mp : user-level interface for the business directory system.
#
# Copyright 2006+, DeadBunny Enterprises, Inc. all rights reserved.
#
# $Id: index.mp,v 1.9 2007/07/16 08:26:30 ravyn Exp $
# $RCSfile: index.mp,v $
# ------------------------------------------------------------------------
use strict;
no strict 'refs';
do "../../perl-bin/dbugsite.rc";
do "../../perl-bin/initialize.rc";
do "../../perl-bin/functions/bd-crud.pl";
do "../../perl-bin/functions/misc-functions.pl";
do "../../perl-bin/functions/vendor-crud.pl";
my ($query, $dbh, $dbInfo, $dConfig, %dH) = nonmember_init();
$dH{'iBDCategoryID'} += 0;
$dH{'iFreeStat'} = $dbh->selectrow_array ("SELECT iFreeListingStatus
FROM tblBDListingStatus WHERE iBDCategoryID=
$dH{'iBDCategoryID'} AND szStateCode=
'$dH{'szStateCode'}'");
if (! defined $dH{'iFreeStat'}) {
if ($dH{'szStateCode'} ne '') {
$dH{'iFreeStat'} = $dbh->selectrow_array ("SELECT iFreeListingStatus
FROM tblBDListingStatus WHERE iBDCategoryID=
$dH{'iBDCategoryID'}");
}
}
if (! defined $dH{'iFreeStat'}) {
$dH{'szFreeStat'} = 'Shown';
} else {
$dH{'szFreeStat'} = ($dH{'iFreeStat'} == 0) ? 'Hidden' : 'Shown';
}
if ($dH{'szAction'} eq '') { $dH{'szAction'} = 'menu'; }
my $routine = $dH{'szAction'} . "Go";
%dH = &$routine ($dbh, $dbInfo, $query, %dH);
my $data = dbAccess::parseDBPage($dbh, $dbInfo, %dH);
$data =~ s/Select one:/-- All locations -- /gi;
$dbh->commit;
$dbh->disconnect;
print $query->header (-type=>'text/html',
-expires=>'-1d');
print $data;
exit;
# -------------
# toggleVisibleGo:
#
# if all the free ads are hidden, make them visible, and vice versa.
#
sub toggleVisibleGo {
my ($dbh, $dbInfo, $query, %dH) = @_;
$dH{'iFreeStat'} = (! defined $dH{'iFreeStat'}) ? 0 : 1-$dH{'iFreeStat'};
# if we have a state specified, we don't really need to do anything
# special. if we have no state specified, then we're setting it
# for the whole category.
$dbh->do("REPLACE INTO tblBDListingStatus VALUES
($dH{'iBDCategoryID'}, '$dH{'szStateCode'}', $dH{'iFreeStat'})");
if ($dH{'szStateCode'} eq '') {
$dbh->do ("UPDATE tblBDListingStatus SET iFreeListingStatus=
$dH{'iFreeStat'} WHERE iBDCategoryID=$dH{'iBDCategoryID'}");
}
my $states = " AND c.szStateCode = '$dH{'szStateCode'}'"
if ($dH{'szStateCode'} ne '');
$dbh->do ("UPDATE tblBDListing AS a LEFT JOIN tblBDCategoryXREF AS
b ON a.iBDID=b.iBDID LEFT JOIN tblBDStateXREF AS c ON
a.iBDID=c.iBDID SET a.iIsActive=$dH{'iFreeStat'} WHERE
b.iBDCategoryID=$dH{'iBDCategoryID'} $states AND
a.iBDTierID=1 AND a.iIsActive >= 0");
$dbh->commit;
my $redirLink = "/directory/index.mp?szAction=browseByCategory&" .
"iBDCategoryID=$dH{'iBDCategoryID'}&szStateCode=".
"$dH{'szStateCode'}&iStart=$dH{'iStart'}";
print $query->redirect($redirLink);
}
# 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) = @_;
$dH{'szFileName'} = 'bizdirectory/index-template.html';
$dH{'szStateName'} = ($dH{'szStateCode'} ne '') ?
$dbh->selectrow_array ("SELECT szStateName FROM tblState
WHERE szStateCode='$dH{'szStateCode'}'") : 'All Locations';
$dH{'szStateName'} = 'Outside the US/Canada' if $dH{'szStateCode'} eq 'ZZ';
$dH{'szCategoryList'} = buildBDCategoryTable($dbh,
! $dH{'iUserIsAdmin'}, $dH{'szStateCode'});
return (%dH);
}
# browseByCategory:
#
# browse the directory by category, and optionally by state and such.
#
sub browseByCategoryGo {
my ($dbh, $dbInfo, $query, %dH) = @_;
$dH{'iBDCategoryID'} += 0;
$dH{'szStateName'} = ($dH{'szStateCode'} ne '') ?
$dbh->selectrow_array ("SELECT szStateName FROM tblState
WHERE szStateCode='$dH{'szStateCode'}'") : 'All Locations';
$dH{'szCategoryName'} = ($dH{'iBDCategoryID'} > 0) ?
$dbh->selectrow_array ("SELECT szCategoryName FROM
tblBDCategory WHERE iBDCategoryID=$dH{'iBDCategoryID'}") :
'All Categories';
my @dList = searchForBDListings($dbh, $dH{'iBDCategoryID'},
$dH{'szStateCode'}, '', 0, 0, 0, ! $dH{'iUserIsAdmin'},
0, 0, $dH{'szFirstLetter'}, '');
($dH{'szResults'}, $dH{'iPrevStart'}, $dH{'iNextStart'}) =
formatSearchResults($dbh, $dbInfo, {%dH}, @dList);
$dH{'szResults'} = '
No results were found matching the
parameters you selected.'
if ($dH{'szResults'} eq '');
$dH{'szPrevLink'} = '';
$dH{'szNextLink'} = '';
if ($dH{'iPrevStart'} > -1 ) {
$dH{'szFileName'} = 'bizdirectory/results-prevlink-template.html';
$dH{'szPrevLink'} = dbAccess::parseDBPage($dbh, $dbInfo, %dH);
}
if ($dH{'iNextStart'} > -1) {
$dH{'szFileName'} = 'bizdirectory/results-nextlink-template.html';
$dH{'szNextLink'} = dbAccess::parseDBPage($dbh, $dbInfo, %dH);
}
$dH{'szFileName'} = 'bizdirectory/browse-results.html';
return (%dH);
}
# doSearchGo:
#
# search the directory based on parameters we supply..
#
sub doSearchGo {
my ($dbh, $dbInfo, $query, %dH) = @_;
$dH{'iBDCategoryID'} += 0;
$dH{'szStateName'} = ($dH{'szStateCode'} ne '') ?
$dbh->selectrow_array ("SELECT szStateName FROM tblState
WHERE szStateCode='$dH{'szStateCode'}'") : 'All Locations';
$dH{'szCategoryName'} = ($dH{'iBDCategoryID'} > 0) ?
$dbh->selectrow_array ("SELECT szCategoryName FROM
tblBDCategory WHERE iBDCategoryID=$dH{'iBDCategoryID'}") :
'All Categories';
my @dList = searchForBDListings($dbh, $dH{'iBDCategoryID'},
$dH{'szStateCode'}, $dH{'szKeyword'}, $dH{'iMinimumTier'}+0,
$dH{'iShopVendor'}, $dH{'iDays'} * ($dH{'iUseDays'}+0),
! $dH{'iUserIsAdmin'}, 0, 0, $dH{'szFirstLetter'}, '');
($dH{'szResults'}, $dH{'iPrevStart'}, $dH{'iNextStart'}) =
formatSearchResults($dbh, $dbInfo, {%dH}, @dList);
$dH{'szResults'} = 'No results were found matching the
parameters you selected.'
if ($dH{'szResults'} eq '');
$dH{'szPrevLink'} = '';
$dH{'szNextLink'} = '';
if ($dH{'iPrevStart'} > -1 ) {
$dH{'szFileName'} = 'bizdirectory/results-prevlink-template.html';
$dH{'szPrevLink'} = dbAccess::parseDBPage($dbh, $dbInfo, %dH);
}
if ($dH{'iNextStart'} > -1) {
$dH{'szFileName'} = 'bizdirectory/results-nextlink-template.html';
$dH{'szNextLink'} = dbAccess::parseDBPage($dbh, $dbInfo, %dH);
}
$dH{'szExpandedOnly'} = ($dH{'iMinimumTier'} > 0) ?
'businesses with expanded listings & photos' :
'all businesses';
$dH{'szShopOnly'} = ($dH{'iShopVendor'} > 0) ?
' with items for sale in the DuneBuggy.com Shop'
: '';
$dH{'szDays'} = ($dH{'iUseDays'} > 0) ?
" that have been added in the last
$dH{'iDays'} days" : '';
$dH{'szKeyString'} = ($dH{'szKeyword'} ne '') ?
"whose names or descriptions match
the keywords $dH{'szKeyword'}." : ".";
$dH{'szFileName'} = 'bizdirectory/search-results.html';
return (%dH);
}
# listAllGo:
#
# display the entire directory in alphabetical order.
#
sub listAllGo {
my ($dbh, $dbInfo, $query, %dH) = @_;
$dH{'szStateName'} = ($dH{'szStateCode'} ne '') ?
$dbh->selectrow_array ("SELECT szStateName FROM tblState
WHERE szStateCode='$dH{'szStateCode'}'") : 'All Locations';
my @categories = retrieveBDCategoryList ($dbh, 0, 1000, ! $dH{'iUserIsAdmin'},
$dH{'szStateCode'}, '');
foreach (@categories) {
next if ($_->{iBDCategoryID}+0 == 0);
my @dList = searchForBDListings($dbh, $_->{iBDCategoryID},
$dH{'szStateCode'}, '', 0, 0, 0, ! $dH{'iUserIsAdmin'},
0, 16, '', 'd.szCategoryName, a.iBDTierID DESC, a.iWeight DESC,
a.szBusinessName');
$dH{'szFileName'} = 'bizdirectory/directory-category-list.html';
$dH{'szCategoryName'} = $_->{szCategoryName};
$dH{'iBDCategoryID'} = $_->{iBDCategoryID};
$dH{'iHasMore'} = ($#dList == 15) ? 1: 0;
$dH{'szResults1'} = $dH{'szResults2'} = $dH{'szResults3'} = '';
for (my $i=0; (($i<5) && ($i<=$#dList)); $i++) {
$dList[$i]->{szClass} = ($dList[$i]->{iBDTierID} > 1) ? 'link-12-RED': 'link-12-02';
$dList[$i]->{szFileName} = 'bizdirectory/directory-list-all-cell.html';
$dH{'szResults1'} .= dbAccess::parseDBPage($dbh, $dbInfo, %{$dList[$i]});
}
for (my $i=5; (($i<10) && ($i<=$#dList)); $i++) {
$dList[$i]->{szClass} = ($dList[$i]->{iBDTierID} > 1) ? 'link-12-RED': 'link-12-02';
$dList[$i]->{szFileName} = 'bizdirectory/directory-list-all-cell.html';
$dH{'szResults2'} .= dbAccess::parseDBPage($dbh, $dbInfo, %{$dList[$i]});
}
for (my $i=10; (($i<15) && ($i<=$#dList)); $i++) {
$dList[$i]->{szClass} = ($dList[$i]->{iBDTierID} > 1) ? 'link-12-RED': 'link-12-02';
$dList[$i]->{szFileName} = 'bizdirectory/directory-list-all-cell.html';
$dH{'szResults3'} .= dbAccess::parseDBPage($dbh, $dbInfo, %{$dList[$i]});
}
$dH{'szResults'} .= dbAccess::parseDBPage($dbh, $dbInfo, %dH);
}
$dH{'szFileName'} = 'bizdirectory/directory-list.html';
return (%dH);
}
# searchGo:
#
# display the search page.
#
sub searchGo {
my ($dbh, $dbInfo, $query, %dH) = @_;
$dH{'szCategoryList'} = buildUserBDCategoryList($dbh,1);
$dH{'szFileName'} = 'bizdirectory/search-template.html';
return (%dH);
}
# buildBDCategoryTable:
#
# build the category list as a nx3 list of items.
#
sub buildBDCategoryTable {
my ($dbh, $restrict, $stateCode) = @_;
my $results ='';
my ($resCode, @dList) = retrieveBDCategoryList($dbh, 0, 99, $restrict,
$stateCode);
if ($resCode == $site::resultOK) {
my $i=0;
my $foo;
foreach $foo (@dList) {
$foo->{szFileName} = 'bizdirectory/vendor-category-cell.html';
$foo->{szStateCode} = $stateCode;
$foo->{iBusinessCount} = countItemsInBDCategory($dbh,
$foo->{iBDCategoryID}, $stateCode,
$restrict);
if ($restrict == 0) {
$foo->{szAdminLinks} =
qq|Edit category|.
" | " . qq|Add listing|;
}
$results .= dbAccess::parseDBPage($dbh, '', %{$foo});
$i++;
if (($i % 3) == 0) { $results .= "
\n"; }
}
$results .= "
\n";
} elsif ($resCode == $site::notFound) {
$results = "| No listings were found in the area you selected. |
\n";
} else {
$results = "| $site::errorMessages{$resCode} |
\n";
}
return $results;
}
# formatSearchResults:
#
# display the search / browse results in a useful format.
#
sub formatSearchResults {
my ($dbh, $dbInfo, $dHash, @dList) = @_;
my $results = '';
my $totalWeight = 0;
my $maxWeight = $dbh->selectrow_array("SELECT iPageWeight FROM
tblBDCategory WHERE iBDCategoryID=$dHash->{iBDCategoryID}");
$maxWeight = 50 if ($maxWeight + 0 <= 0);
$dHash->{iStart} += 0;
my $i;
my $stx = $dbh->prepare ("UPDATE tblBDListing SET iTimesViewed=1
WHERE iBDID=? AND iTimesViewed IS NULL");
my $sth = $dbh->prepare ("UPDATE tblBDListing SET
iTimesViewed=iTimesViewed+1 WHERE iBDID=?");
for ($i=$dHash->{iStart}; (($totalWeight < $maxWeight)
&& ($i <= $#dList)); $i++) {
my $entry = formatBDListingEntry($dbh, $dList[$i], $dHash);
$sth->execute($entry->{iBDID});
$stx->execute($entry->{iBDID});
$entry->{szFileName} = "bizdirectory/vendor-listing-row-TIER$entry->{iBDTierID}\.html";
if (($entry->{iShowURL} == 1) && ($entry->{szURL} ne '')) {
$entry->{szWebLink} = "| Website: " . qq|Click to view|;
$entry->{szLinkURL} = qq|/members/directoryGo.mp?iBDID=$entry->{iBDID}|;
}
if ($entry->{iBDTierID} == 1) {
$entry->{szPhone} = ($entry->{iShowPhone} == 1) ?
"Phone: $entry->{szPhone} | " : "";
}
$results .= dbAccess::parseDBPage($dbh, $dbInfo, %{$entry});
$totalWeight += $entry->{iWeight};
}
$dbh->commit;
my $nextStart = ($i < $#dList) ? $i : -1;
$i = -1000; $totalWeight = 0;
# calculate the previous page's starting point.
#
for ($i = ($dHash->{iStart}-1); (($totalWeight < $maxWeight) &&
($i > 0)); $i--) {
my $entry = $dList[$i];
$totalWeight += $entry->{iWeight};
}
$sth->finish;
$stx->finish;
my $prevStart = ($i != -1000) ? $i : -1;
return ($results, $prevStart, $nextStart);
}