#!/usr/bin/perl

# ***** NEW VERSION (RESPONSIVE) - SEPTEMBER 2020 *****

use strict;

use lib qw(/var/www/sites/ubuyne/cgi-perl/modules/);

use PW2_Functions;

use PW2_PrintBits;

&process_main();

exit;

# ---------------------------------------------------------------------------------------------------------------------------

sub process_main{

undef my %FORM;

&process_form_any2(\%FORM);

my $requestcode	=$FORM{'store'};
my $geo_dir	=lc($FORM{'dir'});

my $listing=''; my $pagetitle='';

$requestcode=~tr/A-Z/a-z/;	# Lowercase it

# ---
my $shopdirect_url='';		# This Shop Direct section is to handle old brand names which shouldn't be displayed

if ($requestcode eq'choi'){$shopdirect_url='https://price-wizard.com/cgi-perl/pw.displaystore.cgi?store=lit1';}
if ($requestcode eq'ward'){$shopdirect_url='https://price-wizard.com/cgi-perl/pw.displaystore.cgi?store=isme';}
if (($requestcode eq'emps')||($requestcode eq'gus1')||($requestcode eq'kays')){$shopdirect_url='https://price-wizard.com/cgi-perl/pw.displaystore.cgi?store=kay2';}
if (($requestcode eq'abnd')||($requestcode eq'arg2')||($requestcode eq'litt')){$shopdirect_url='https://price-wizard.com/cgi-perl/pw.displaystore.cgi?store=very';}
if ($shopdirect_url gt''){print "Location: $shopdirect_url\n\n"; exit;}
# ---

my $metadesc=''; my $metakeyw=''; my $headlines='';

&fetch_store_info(\$requestcode,\$listing,\$geo_dir,\$pagetitle,\$metadesc,\$metakeyw);

undef my $catlist; &fetch_catlist(\$catlist);

undef my $simstores; 

if (length($requestcode)==4)
{
&fetch_sim_stores(\$requestcode,\$simstores,\$geo_dir);
}
else
{
$listing=qq~
<br>
<span class="lh25">
<b>Ooops!</b> No store code was provided, so we are unable to display details of a specific online store for you. However, below you will find all of the directory categories which we currently list. Selecting any of the links will provide a list of online stores relating to those products or services.
<br><br>
You may also like to view stores by country:
&nbsp;&#8226;&nbsp;<a href="https://price-wizard.com/cgi-perl/pw.directory.cgi?g=gbp">Great Britain</a> 
&nbsp;&#8226;&nbsp;<a href="https://price-wizard.com/cgi-perl/pw.directory.cgi?g=usd">United States</a>

&nbsp;&#8226;&nbsp;<a href="https://price-wizard.com/cgi-perl/pw.directory.cgi?g=cdn">Canada</a>
</span><br><br><br><br>
~;

$pagetitle='Price-Wizard.com - Online Shopping from UK, USA and Canada';
}

&print_myheader();

# Additionally, use "-bannerkey=>\$requestcode" if we want specific banner
&html_top_of_page(-title=>\$pagetitle,-storekey=>\$requestcode,-scriptflags=>\'pwz1',-metadesc=>\$metadesc,-keywords=>\$metakeyw);

print '<main id="maincontent">';

print $listing;

print $simstores;

print '</main>';

print $catlist;

&print_footer(\0);

undef %FORM;

}1;
# ---------------------------------------------------------------------------------------------------------------------------

sub fetch_store_info{

my $requestcode	=shift;	# A reference
my $listing	=shift;	#	"
my $geo_dir	=shift;	#	"
my $pagetitle	=shift;	#	"
my $metadesc	=shift;	#	"
my $metakeyw	=shift;	#	"

my ($fields,$SQLQUERY,$geo_msg)='';

$fields='STOREKEY,STORENAME,TITLE,CURRENCY,DESCRIPTION,REALURL,LOGO,CATEGORY,DATAFEED,FAVORITES,SHORTNAME,WORDS';

my %countrybits=('gbp'=>'the United Kingdom',
		'usd'=>'the United States',
		'cdn'=>'Canada'
);

$SQLQUERY=join('','SELECT ',$fields,' FROM STORES WHERE STOREKEY="',$$requestcode,'"');

undef my @record; &fetch_single_record2(\@record,\$SQLQUERY);

my $storekey	=$record[0];
my $storename	=$record[1];	
my $title	=$record[2];
my $currency	=$record[3];
my $description	=$record[4];
my $realurl	=$record[5];
my $logo	=$record[6];
my $category	=$record[7];
my $datafeed	=$record[8];
my $favorites	=$record[9];
my $shortname	=$record[10];
my $keywords	=$record[11];

if (index($description,'<br>')==-1){
$description=~s/\n/<br>/g;
}else{
$description=~s#<br>#<div class="lh20">&nbsp;</div>#g;
}

my $temp_meta=$description;
$temp_meta=substr($temp_meta,0,130);
$temp_meta=substr($temp_meta,0,rindex($temp_meta,' '));
$$metadesc=$storename.'. '.$temp_meta .' &#8230;';
$$metadesc=~s/"/&#34;/g;	# swap out any quotation marks (unlikely to be any, but if there are it may screw up the page)

if ($keywords gt'')
{
$$metakeyw=$keywords; 
  if (index($$metakeyw,$storename)==-1)
  {
  $$metakeyw=$storename.', '.$$metakeyw;
  }
}
else {$$metakeyw=$storename;}

my $headlines=''; &get_related_headlines(\$headlines,\$storekey);
  if ($headlines gt''){
    $headlines=join('','<div class="lh25 cblog1 pad10"><h3>Recent News</h3>',$headlines,'</div>');
  }

my $titles_name=$storename; if ( (length($titles_name)>28)&&($shortname gt'') ){$titles_name=$shortname;};

# my $banner300=''; &get_random_banner(\$banner300,\$storekey,\'300',\'250',\'class="responsive_img img300d"');	# let's see if a 300x250 banner exists
my $banner300=''; &get_random_banner(\$banner300,\$storekey,\'300',\'',\'class="responsive_img img300d"');	# let's see if a 300x??? banner exists

$$pagetitle=join('',$storename,' - Special Discounts and Online Offers at ',$storename);

if ($storekey eq'john')
{
$$pagetitle=join('',$storename,' - Enjoy Special Value with Online Shopping at ',$storename);
}

undef my $products; &fetch_products(\$storekey,\$products,\$storename);

# We'll stick a banner there instead if there are no products, or add one even if there are products
my $prod_br=($products gt'')?'':'<br>';
$products.=qq~
$prod_br<div class="just_c">
<a href="https://www.price-wizard.com/cgi-perl/pw.direct.pl?store=$storekey" title="$storename">
<img src="https://price-wizard.com/cgi-perl/pw.iget.cgi?r=&store=$storekey&w=468&h=60" class="responsive_468" alt="$storename">
</a>
</div><br>~;

if ($$geo_dir gt'')
{
$geo_msg=join('','You may also choose to display <a href="https://price-wizard.com/cgi-perl/pw.displaystore.cgi?store=',$storekey,'&dir=">all the stores</a> in this category, regardless of country.');
}
elsif ($$geo_dir eq'')
{
$geo_msg=join('','You may also choose to display only stores in this category from <a href="https://price-wizard.com/cgi-perl/pw.displaystore.cgi?store=',$storekey,'&dir=',lc($currency),'">',$countrybits{lc($currency)},'</a>.');
}

if ($countrybits{lc($currency)} eq'')
{
$geo_msg='';	# For japan, germany, europe and other
}

my $browseprods='';
my $favcode='';	# shouldn't need this any longer ... delete it when we're through updating everything (DGB.Sep.2020)

if ($datafeed==1)
{
$browseprods=qq~
<i class="fas fa-shopping-cart"></i> &nbsp; <a href="https://price-wizard.com/cgi-perl/pw.search.cgi?c=$storekey">Browse Products</a>
~;
}

my $shopnow=qq~
<i class="fas fa-shopping-cart"></i> &nbsp; <a href="https://price-wizard.com/cgi-perl/pw.direct.pl?store=$storekey">Shop Now, at $storename</a> &nbsp; 
~;

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
my $logolink=($wday%2==0)?"https://price-wizard.com/links/$storekey.html":"https://price-wizard.com/links2/$storekey.html";
if ($wday==3){$logolink=~s/price-wizard.com/u-buy.net/;}

my $extra_row='';
if ($datafeed==1 || $favorites==1)
{
$extra_row=join('','<div class="just_r">',$favcode,' ',$shopnow,' ',$browseprods,'<p class="ptop10"></p></div>');
}

$$listing=qq~
<br>
<div id="div_dispstore1" class="cblog1 flex_container">
	<div><h1 title="$storename"><i class="fas fa-caret-right"></i> $storename</h1></div>
	<div class="w3-right-align flexy1">
	<a href="$logolink" title="$storename">
	<img src="https://price-wizard.com/images/logos/$logo" width="150" height="40" alt="$storename Official Website">
	</a>
	</div>
</div>

<br>

<div class="lh25 cblog1">

	$extra_row

	<div class="dispstore">$banner300$description</div>
	<br>
	<i class="fas fa-caret-right"></i> &nbsp; <span class="f_green">$storename Official Homepage</span>:&nbsp;
	<b><a href="https://price-wizard.com/cgi-perl/pw.direct.pl?store=$storekey">$realurl</a></b>
</div>

<br>
$headlines
$products

<div class="dispstore cblog1 pad10">
<h3>Stores which may offer similar products or services to $titles_name:</h3>
The stores listed below may be related, or may offer some similar products or services to $storename. $geo_msg
</div>
~;

$$listing=~s/`/'/g;

}1;
# ---------------------------------------------------------------------------------------------------------------------------

sub fetch_sim_stores{

my $requestkey	=shift;	# A reference
my $simstores	=shift;	#	"
my $geo_dir	=shift;	#	"	(may be empty)

my $SQLQUERY=join('','SELECT CATEGORY FROM STORES WHERE STOREKEY="',$$requestkey,'"');

undef my @record; &fetch_single_record2(\@record,\$SQLQUERY);

my $catref=$record[0];

undef my @catrefs; @catrefs=split(/,/,$catref);

$SQLQUERY=join('','SELECT STORENAME,STOREKEY,TITLE,CURRENCY FROM STORES WHERE (ACTIVE=1 AND STOREKEY NOT LIKE "',$$requestkey,'") ');

my $SUB_SQL='AND (';

for (@catrefs)
{
if (length($SUB_SQL)>5){$SUB_SQL.=' OR '};

$SUB_SQL.=join('','CATEGORY LIKE "%',$_,'%"');
}

$SUB_SQL.=') ORDER BY STORENAME';

$SQLQUERY.=$SUB_SQL if (length($SUB_SQL)>6);

undef my @records; &fetch_records2(\@records,\$SQLQUERY,'^');

$$simstores='<div class="lh25 pad10 cblog1">';

for (@records)
{
my ($storename,$storekey,$title,$currency)=split(/\^/,$_);

	if ($$geo_dir eq'' || ($$geo_dir eq lc($currency)) )
	{
	$$simstores.=qq~
	<div class="lh25 ptop10 border_dotted_bottom"> 
	<i class="fas fa-caret-right"></i> &nbsp; 
	<b><a href="https://price-wizard.com/cgi-perl/pw.displaystore.cgi?store=$storekey">$storename</a></b>
	<p class="w3-margin-left">$title</p>
	</div>
	~;
	}
}

$$simstores.='</div><br><br>';

$$simstores=~s/`/'/g;

}1;
# ---------------------------------------------------------------------------------------------------------------------------

sub fetch_products{

my $storekey	=	shift;	# References
my $products	=	shift;
my $storename	=	shift;

my $lc_storekey	=lc($$storekey);

my $SQLQUERY=join('','SELECT COUNT(1) FROM PW_PRODUCTS_V2 WHERE STOREKEY="',$$storekey,'"');
undef my @record; &fetch_single_record2(\@record,\$SQLQUERY);

my $testcount=$record[0];

return if ($testcount==0);

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

$SQLQUERY='SELECT SQL_NO_CACHE PRODUCT,PRICE,SKU,MERCAT,IMAGE,URL FROM PW_PRODUCTS_V2 WHERE STOREKEY="'.$$storekey.'" LIMIT '.$mday.',100';
undef my @records; &fetch_records2(\@records,\$SQLQUERY,'^');

my $totrecs=@records; 

if ($totrecs<10){return;};

$$products=qq~
<br>
<h3 class="cblog1 inline">A Selection of Products from $$storename</h3>
<div class="w100 flex_container">
	<div class="flexy1 wmin">
	<ul class="lh25 w3-padding-16">~; 

my $count=0;
my $sku_check='';

while ($count<10)
{
my $rec=$records[rand(@records)];

my ($product,$price,$sku,$category,$image,$url)=split(/\^/,$rec);

if (index($sku_check,$sku)!=-1){next};

my $js_url = "https://$ENV{'HTTP_HOST'}/cgi-perl/pw.direct.pl?store=$lc_storekey&sku=$sku";
my $split_point = 15; # The number of characters for the first part
my $js_url_part1 = substr($js_url, 0, $split_point);
my $js_url_part2 = substr($js_url, $split_point);

# my $product_link=qq~<a href="https://$ENV{'HTTP_HOST'}/cgi-perl/pw.direct.pl?store=$lc_storekey&sku=$sku">~;
my $product_link=qq~<span class="cursor f_blue" onclick="igoto('$js_url_part1'+'$js_url_part2');">~;

if ($price eq'' || $price==0){$price='(n/a)';}

  if ($count<9)
  {
  $$products.=qq~
  <li title="$product">$product_link$product</span>, for &#163;$price~;

    if ($category gt'')
    {
    my $temp_cat=$category; my $escaped_cat=''; &encode_url_string(\$escaped_cat,\$temp_cat);

    $$products.=qq~ in <a href="https://$ENV{'HTTP_HOST'}/cgi-perl/pw.search.cgi?c=$lc_storekey&amp;scn=$escaped_cat" title="$category at $$storename">$category</a></li>~;
    }
    else
    {
    $$products.='</li>';
    }
  }
  else
  {
  $$products.=qq~</ul></div> <div class="just_c w3-padding wmin">$product_link<img src="$image" class="mb5 maxw120" alt="$product"><br></a>
  $product_link$product</a></div>~;
  }

$count++;
}

$$products.='</div><br>';

}1;
# ---------------------------------------------------------------------------------------------------------------------------

sub get_related_headlines{

my $headlines	=shift;		# A reference
my $storekey	=shift;		#	"

my $SQLQUERY=qq~SELECT SQL_NO_CACHE TITLE,DESCRIPTION,DATE_FORMAT(LOGDATE,"%D %M, %Y"),DKEY FROM PW_CATABLOGS WHERE ACTIVE=1 AND STOREKEY="$$storekey" AND DATEDIFF(CURDATE(),LOGDATE) <300 ORDER BY LOGDATE DESC LIMIT 3~;	# up to 300 days old

undef my @records; &fetch_records2(\@records,\$SQLQUERY,'^');

for (@records)
{
	my($title,$desc,$logdate,$dkey)=split(/\^/,$_);

	&numbers_only(\$dkey);

	$$headlines.='<br>' if ($$headlines gt'');

	$$headlines.=join('','<div><span class="f_green">',$logdate,' - </span><a href="https://price-wizard.com/cgi-perl/pw.catablogs.cgi?b=',$dkey,'">',$title,'</a></br>','<span>',$desc,'</span></div>');
}

undef $SQLQUERY;
undef @records;

}1;
# --------------------------------------------------------------------------------------------------------------------------






