#!/usr/local/bin/perl

# My Kinda Prices - Search for Sub Categories
# Written By : Redi Internet - Auto Built by Site Builder - http://www.redi.co.za

require "header";

$domain = $ENV{'HTTP_HOST'};

&GetCookies;
$mycountry = $Cookies{country};
$refer = $Cookies{refer};
if ($mycountry eq "") {$mycountry = "South Africa";}

print "Content-type: text/html\n\n";


$env = $ENV{'PATH_INFO'};
$env =~ s+/++;
$env =~ s+/+,+;

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
    ($names, $value) = split(/=/, $pair);

    # Un-Webify plus signs and %-encoding
    $value =~tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    # Stop people from using subshells to execute commands
    $value =~ s/~!/ ~!/g;

    #print "Setting $names to $value<P>";
    $arry{"$names"} = "$value";
}

$cat = $ARGV[0];

if ($cat eq "") {
   $cat = $arry{category};
}


   ##### Get count of entries in results #####
   $query = "select count(*) from tblcategory where name = '$cat'";

   $sth = $dbh->prepare($query);
   $sth->execute();

   $itemcount = $sth->fetchrow_array();
   $nopercolumn = $itemcount / 2;
   ###########################################

   $query = "SELECT * from tblcategory where name = '$cat'";

   $count=0;

   $sth = $dbh->prepare($query);
   $sth->execute();

   while ($ref = $sth->fetchrow_hashref()) {
      $id = $ref->{'id'};
      $name = $ref->{'name'};
      $description = $ref->{'description'};
      $subname = $ref->{'subname'};

      $showicon = "<img src=\"http://www.mykindaprices.com/images/icons/$name.gif\" align=left>"; 
      $showmain = "<b>$name Category</b><br><br><span class=\"style2\">Click on the Sub Category below to view all related <br>products in this category.</span>";

      if ($subname ne "") {
         #$prodcount = &GetProdNameCount($name,$subname);
         if ($count <= $nopercolumn) {
            $tablewidth = "175";
            $td1width = "100%";
            $td2width = "0%";
            $showsubcats1 .= "<li><a href=\"/cgi-bin/mykindaprices/searchsub.pl?$name:$subname\" class=\"nav2\"><strong>$subname</strong></a> <!--($prodcount)-->";
         }
         else {
            $tablewidth = "380";
            $td1width = "50%";
            $td2width = "50%";
            $showsubcats2 .= "<li><a href=\"/cgi-bin/mykindaprices/searchsub.pl?$name:$subname\" class=\"nav2\"><strong>$subname</strong></a> <!--($prodcount)-->";
         }
      }

      $results .= "";
      $count++; 
   }

   #$featured .= &GetFeatured($name,$subname);
   $featured .= &GetFeatured($cat);

   $sth->finish();

   $dbh->disconnect();


   if ($showsubcats1 eq "") {
      $showicon = "<img src=\"/images/icons/$name.gif\" align=left>";
      $showmain = "No matches found with your search criteria<br><br>Please try another search.";
   }
   if ($header eq "") {
      $header = "Your Search Results !";
   }


##LOG NOW
#  $noofresults = $count;
#  &WriteToLog("Category Search",$cat,$noofresults,$mycountry);

## show advert if it exists 
#if (-f "$basedir/adverts/$advert") {
#   $showadvert = "<img src=\"/adverts/$advert\">";
#}
#else {
#   $showadvert = "";
#}

$mycat = $name;

# get top 10 for this category per country
   if ($mycountry eq "United Kingdom") {
      open (top10file, "$basedir/top10/UK_$mycat.html");
         while (<top10file>) {
            $top10 .= $_;
         }
      close(top10file);
   }
   else {
      open (top10file, "$basedir/top10/$mycat.html");
         while (<top10file>) {
            $top10 .= $_;
         }
      close(top10file);
   }


# show advert if it exists
   # show adverts for various countries
   if ($mycountry eq "United Kingdom") {
      $advert = "UK_"."$name.gif";
      $name = "UK_$name";
   }
   else {
      $advert = "$name.gif";
   }

if (-f "$basedir/adverts/$advert") {
    #$name =~ s/ /_/gi;
   $showadvert = "<a href=\"/cgi-bin/mykindaprices/adclick.pl?$name\" target=\"_blank\"><img src=\"http://www.mykindaprices.com/adverts/$advert\" border=0></a><br><br>";
}
else {
   $showadvert = "";
}


   $template = "categories.html";

   if ($domain =~ /msn/) {
      $template = "msn/categories.html";
   }
   if ($domain =~ /iafrica/) {
      $template = "iafrica/categories.html";
   }
   if ($domain =~ /cbn/) {
      $template = "cbn/categories.html";
   }

   open(maintemplate, "$basedir/$template");
      while (<maintemplate>) {

         s/"images/"\/images/gi;
         s/'images/'\/images/gi;
         s/\(images/\(\/images/gi;
         s/style.css/\/style.css/gi;

         s/--CATEGORY--/$mycat/gi;
         s/--MAINCATEGORY--/$showmain/gi;
         s/--TABLEWIDTH--/$tablewidth/gi;
         s/--TD1WIDTH--/$td1width/gi;
         s/--TD2WIDTH--/$td2width/gi;
         s/--ICON--/$showicon/gi;
         s/--SUBCATEGORIES1--/$showsubcats1/gi;
         s/--SUBCATEGORIES2--/$showsubcats2/gi;
         #s/--FEATURED--/Featured products for '$name' coming soon.../gi;
         s/--FEATURED--/$featured/gi;
         s/--ADVERT--/$showadvert/gi;
         s/--TOP10--/$top10/gi;

         s/name=\"search\"/name=\"search\" value=\"$arry{search}\"/gi;

         print "$_\n";
      }
   close(maintemplate);


exit;

sub GetFeatured {
   #local($xcat,$xsubcat) = @_;
   local($xcat) = @_;

   # $query2 = "SELECT productname from tblproductname where categoryname = '$xcat' and subcategoryname = '$xsubcat' order by categoryname,subcategoryname,productname";
   $query2 = "SELECT DISTINCT productname from tblproductname where categoryname = '$xcat' and status = 'A' order by categoryname,subcategoryname,productname";

   $sth2 = $dbh->prepare($query2);
   $sth2->execute();

   while ($ref2 = $sth2->fetchrow_hashref()) {
      $productname2 = $ref2->{'productname'};

      push @allProdNames, "$productname2";
   }
   $sth2->finish();

   # then get featured products only...

   $xfeatured = "";



      $query2 = "SELECT productname from tblproducts where featured = 'Y' and status = 'A' order by productname";

      $sth2 = $dbh->prepare($query2);
      $sth2->execute();

      $xcount=0;

      while ($ref2 = $sth2->fetchrow_hashref()) {
         $productname2 = $ref2->{'productname'};
         $allfeatured .= "$productname2:";
      }

      $sth2->finish();

      foreach $prodname (@allProdNames) {
         $prodname =~ s/\(/\\(/gi;
         $prodname =~ s/\)/\\)/gi;

         $productnamelink = $prodname;
         $productnamelink =~ s/\'/\\\'/gi;

         if ($allfeatured =~ /:$prodname:/gi) {
            $prodname =~ s/\\\(/(/gi;
            $prodname =~ s/\\\)/)/gi;
           # $prodname =~ s/\'/\\'/gi;

            $showprodname = substr($prodname,0,35);

            $thumb = &GetThumb($prodname);
            $thumb = "<img src=\"http://www.mykindaprices.com/productimages/thumbs/$thumb\" border=0 alt=\"$prodname\">";

            # $xfeatured .= "<li> <a href=\"/cgi-bin/mykindaprices/compare.pl?$prodname\">$showprodname...</a>\n";
            $xfeatured .= "<a href=\"/cgi-bin/mykindaprices/compare.pl?$productnamelink\" alt=\"$prodname\">$thumb</a>\n";
         }

      }


#   foreach $prodname (@allProdNames) {
#
#      $prodname =~ s/'/\\'/gi;
#
#      $query2 = "SELECT productname from tblproducts where productname = '$prodname' and featured = 'Y' order by productname";
#
#      $sth2 = $dbh->prepare($query2);
#      $sth2->execute();
#
#      while ($ref2 = $sth2->fetchrow_hashref()) {
#         $productname2 = $ref2->{'productname'};
#
#         $showprodname = substr($productname2,0,35);
#
#         $xfeatured .= "<li> <a href=\"/cgi-bin/mykindaprices/compare.pl?$productname2\">$showprodname...</a>\n";
#      }
#      $sth2->finish();
#   }

   if ($xfeatured eq "") {
      $xfeatured = "Sorry, there are currently no products featured in the '$xcat' category.<br><br>Please select a sub-category on the left to view all the products in that sub-category.";
   }

   return $xfeatured;

}
