#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use lib qw(/home/u484518444/domains/aklaat.com/public_html/cgi-bin);
use CGI qw(:standard escapeHTML);
use AklaatDB;
use AklaatUser;
use AklaatDB::Session;
use Recipe;
use Rate;
my $basedir = '/home/u484518444/domains/aklaat.com/public_html/';
my $rate_base_url = 'http://www.aklaat.com/cgi-bin/rates/ShowRecipeRate.pl';
my $summary_file = '/home/u484518444/domains/aklaat.com/public_html/hidden/summaries.htm';
my $summary_terms = '';
my $baseurl = '../';
my $link_url = '../index.htm';
my ($name, $value, $page_title, $pair, @pairs, $rec_to_display, $total_rec, %FORM, $page, $buffer);
my %rates;
$total_rec = 0;
$page = 0;
$rec_to_display = 25;
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/,$buffer);
foreach $pair (@pairs)
{
($name,$value) = split(/=/,$pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$page_title = "���";
my ($ALL, $APPT, $ARTC, $DSRT, $DIET, $DRNK, $DISH, $SALD, $PSTR, $SOUP);
&resetListSelection;
&assign_selected_flag;
&return_html;
#############################################################
sub assign_selected_flag
{
if ($FORM{'lstSearch'} eq 'ALL') {
$ALL = 'selected';
} elsif ($FORM{'lstSearch'} eq 'APPT') {
$APPT = 'selected';
} elsif ($FORM{'lstSearch'} eq 'ARTC') {
$ARTC = 'selected';
} elsif ($FORM{'lstSearch'} eq 'DSRT') {
$DSRT = 'selected';
} elsif ($FORM{'lstSearch'} eq 'DIET') {
$DIET = 'selected';
} elsif ($FORM{'lstSearch'} eq 'DRNK') {
$DRNK = 'selected';
} elsif ($FORM{'lstSearch'} eq 'DISH') {
$DISH = 'selected';
} elsif ($FORM{'lstSearch'} eq 'SALD') {
$SALD = 'selected';
} elsif ($FORM{'lstSearch'} eq 'PSTR') {
$PSTR = 'selected';
} elsif ($FORM{'lstSearch'} eq 'SOUP') {
$SOUP = 'selected';
}
}
#############################################################
sub is_search_criteria_exist
{
if (validateSearchTerm($FORM{'terms1'}) ne '') {
return ('true');
} elsif (validateSearchTerm($FORM{'terms2'}) ne '') {
return ('true');
} elsif (validateSearchTerm($FORM{'terms3'}) ne '') {
return ('true');
} elsif (validateSearchTerm($FORM{'terms4'}) ne '') {
return ('true');
} elsif (validateSearchTerm($FORM{'noterms1'}) ne '') {
return ('true');
} elsif (validateSearchTerm($FORM{'noterms2'}) ne '') {
return ('true');
} elsif (validateSearchTerm($FORM{'noterms3'}) ne '') {
return ('true');
} elsif (validateSearchTerm($FORM{'noterms4'}) ne '') {
return ('true');
} elsif (validateSearchTerm($FORM{'terms'}) ne '') {
return ('true');
}
return ('false');
}
#############################################################
sub get_search_query_all
{
my $sql = "SELECT * FROM recipes WHERE ";
$sql .= AklaatDB::trim(get_search_query());
$sql =~ s/WHERE AND/WHERE/g;
$sql .= " ORDER BY re_title";
return ("$sql");
}
#############################################################
sub get_search_query
{
#used to check if the query is for count result or record result
chomp $FORM{'terms1'};
$FORM{'terms1'} =~ s/\cM\n/ /g;
my $terms1 = AklaatDB::trim($FORM{'terms1'});
my $sqlTitle = '';
my $sql = '';
my $sqlDetail = '';
my $sqlType = '';
$summary_terms = '';
if ($FORM{'lstSearch'} eq 'APPT') {
$sqlType = " re_type = 'appetizers'";
} elsif ($FORM{'lstSearch'} eq 'ARTC') {
$sqlType = " re_type = 'articles'";
} elsif ($FORM{'lstSearch'} eq 'DSRT') {
$sqlType = " re_type = 'deserts'";
} elsif ($FORM{'lstSearch'} eq 'DIET') {
$sqlType = " re_type = 'diet'";
} elsif ($FORM{'lstSearch'} eq 'DRNK') {
$sqlType = " re_type = 'drinks'";
} elsif ($FORM{'lstSearch'} eq 'DISH') {
$sqlType = " re_type = 'maindishes'";
} elsif ($FORM{'lstSearch'} eq 'SALD') {
$sqlType = " re_type = 'salads'";
} elsif ($FORM{'lstSearch'} eq 'PSTR') {
$sqlType = " re_type = 'pastry'";
} elsif ($FORM{'lstSearch'} eq 'SOUP') {
$sqlType = " re_type = 'soups'";
} else {
$sqlType = "";
}
if ($FORM{'type'} eq 'advance') {
chomp $FORM{'terms2'};
$FORM{'terms2'} =~ s/\cM\n/ /g;
my $terms2 = AklaatDB::trim($FORM{'terms2'});
chomp $FORM{'terms3'};
$FORM{'terms3'} =~ s/\cM\n/ /g;
my $terms3 = AklaatDB::trim($FORM{'terms3'});
chomp $FORM{'terms4'};
$FORM{'terms4'} =~ s/\cM\n/ /g;
my $terms4 = AklaatDB::trim($FORM{'terms4'});
chomp $FORM{'noterms1'};
$FORM{'noterms1'} =~ s/\cM\n/ /g;
my $noterms1 = AklaatDB::trim($FORM{'noterms1'});
chomp $FORM{'noterms2'};
$FORM{'noterms2'} =~ s/\cM\n/ /g;
my $noterms2 = AklaatDB::trim($FORM{'noterms2'});
chomp $FORM{'noterms3'};
$FORM{'noterms3'} =~ s/\cM\n/ /g;
my $noterms3 = AklaatDB::trim($FORM{'noterms3'});
chomp $FORM{'noterms4'};
$FORM{'noterms4'} =~ s/\cM\n/ /g;
my $noterms4 = AklaatDB::trim($FORM{'noterms4'});
if ($terms1 ne '') {
$sqlTitle .= get_search_all_criteria($terms1, 'true');
$sqlDetail .= get_search_all_criteria($terms1, 'false');
$summary_terms = "$summary_terms $terms1";
}
if ($terms2 ne '') {
$sqlTitle .= get_search_all_criteria($terms2, 'true');
$sqlDetail .= get_search_all_criteria($terms2, 'false');
$summary_terms = "$summary_terms $terms2";
}
if ($terms3 ne '') {
$sqlTitle .= get_search_all_criteria($terms3, 'true');
$sqlDetail .= get_search_all_criteria($terms3, 'false');
$summary_terms = "$summary_terms $terms3";
}
if ($terms4 ne '') {
$sqlTitle .= get_search_all_criteria($terms4, 'true');
$sqlDetail .= get_search_all_criteria($terms4, 'false');
$summary_terms = "$summary_terms $terms4";
}
if ($noterms1 ne '') {
$sqlTitle .= get_search_not_all_criteria($noterms1, 'true');
$sqlDetail .= get_search_not_all_criteria($noterms1, 'false');
$summary_terms = "$summary_terms - $noterms1";
}
if ($noterms2 ne '') {
$sqlTitle .= get_search_not_all_criteria($noterms2, 'true');
$sqlDetail .= get_search_not_all_criteria($noterms2, 'false');
$summary_terms = "$summary_terms - $noterms2";
}
if ($noterms3 ne '') {
$sqlTitle .= get_search_not_all_criteria($noterms3, 'true');
$sqlDetail .= get_search_not_all_criteria($noterms3, 'false');
$summary_terms = "$summary_terms - $noterms3";
}
if ($noterms4 ne '') {
$sqlTitle .= get_search_not_all_criteria($noterms4, 'true');
$sqlDetail .= get_search_not_all_criteria($noterms4, 'false');
$summary_terms = "$summary_terms - $noterms4";
}
$sql = "$sqlType $sqlTitle UNION SELECT * FROM recipes WHERE ";
$sql .= AklaatDB::trim("$sqlType $sqlDetail");
} elsif ($FORM{'type'} eq 'submitter') {
if ($terms1 ne '') {
$sql .= " $sqlType AND re_submitted_by LIKE '%$terms1%' ";
$summary_terms = "$summary_terms $terms1";
}
#$sql .= " ORDER BY re_title";
} elsif ($FORM{'type'} eq 'quick') {
if ($terms1 ne '') {
$sql .= get_search_part_criteria($terms1, $sqlType);
$summary_terms = "$summary_terms $terms1";
}
} else {
chomp $FORM{'terms'};
$FORM{'terms'} =~ s/\cM\n/ /g;
my $terms = AklaatDB::trim($FORM{'terms'});
if ($terms ne '') {
$sql .= get_search_part_criteria($terms, $sqlType);
$FORM{'terms1'} = $terms;
$summary_terms = "$summary_terms $terms";
}
}
return ($sql);
}
#############################################################
sub get_search_part_criteria
{
my ($terms, $sqlType) = @_;
$terms = AklaatDB::trim($terms);
my @words = split(/ /,$terms);
my $word;
my @chars;
my $sql = "";
my $sql1 = "";
my $sql2 = "";
my $sqlTitle = "";
my $count = 0;
foreach $word (@words) {
$word = filterSearchTerm($word);
if ($word ne "") {
$sql1 .= " AND (re_keywords LIKE '%$word%') ";
$sql2 .= " OR (re_keywords LIKE '%$word%') ";
$sqlTitle .= " AND (re_title LIKE '%$word%') ";
++$count;
}
}
$sql .= " $sqlType $sqlTitle ";
$sql .= " UNION SELECT * FROM recipes WHERE ";
$sql .= AklaatDB::trim("$sqlType $sql1");
#disable the "OR" search for quick and more efficent search
$count = 0;
if ($count > 1) {
$sql .= " UNION SELECT * FROM recipes WHERE (";
#if (AklaatDB::trim($sqlType) eq "1=1") {
# $sql .= substr($sql2, 4);
# $sql .= " ) ";
#} else {
$sql .= " $sqlType AND (";
$sql .= substr($sql2, 4);
$sql .= " ) )";
# }
}
return ($sql);
}
#############################################################
sub get_search_all_criteria
{
my ($terms, $isTitle) = @_;
$terms = AklaatDB::trim($terms);
my @words = split(/ /,$terms);
my $word;
my @chars;
my $sql = "";
foreach $word (@words) {
$word = filterSearchTerm($word);
if ($word ne "") {
if ($isTitle eq 'true') {
$sql .= " AND (re_title LIKE '%$word%') ";
} else {
$sql .= " AND (re_keywords LIKE '%$word%') ";
}
}
}
return ($sql);
}
#############################################################
sub get_search_not_all_criteria
{
my ($terms, $isTitle) = @_;
$terms = AklaatDB::trim($terms);
my @words = split(/ /,$terms);
my $word;
my @chars;
my $sql = "";
foreach $word (@words) {
$word = filterSearchTerm($word);
if ($word ne "") {
if ($isTitle eq 'true') {
$sql .= " AND (re_title NOT LIKE '%$word%') ";
} else {
$sql .= " AND (re_keywords NOT LIKE '%$word%') ";
}
}
}
return ($sql);
}
#############################################################
sub return_html
{
print "Content-type: text/html\n\n";
# First we build a summary file for the webmaster and the visitor:
my ($dbh, $sth, $ref);
my @recipes = ();
my $hitcount = 0;
my $search_criteria_exist = is_search_criteria_exist();
if ($search_criteria_exist eq 'true') {
&init_recipe_rate();
$dbh = AklaatDB::connect ();
$dbh->do ('SET NAMES CP1256');
$dbh->do ('SET COLLATION_CONNECTION=CP1256_GENERAL_CI');
my $sqlTemp = get_search_query_all();
$sth = $dbh->prepare ($sqlTemp);
$sth->execute ();
while ($ref = $sth->fetchrow_hashref ())
{
my $rec = new Recipe();
#set object attributes
$rec->setFileName($ref->{re_filename});
$rec->setTitle($ref->{re_title});
$rec->setSubmittedBy($ref->{re_submitted_by});
$rec->setDescription($ref->{re_description});
$hitcount = push(@recipes, $rec);
}
}
my $docstring = "���� $hitcount";
$docstring = "����� �����: ���� �����" if ($hitcount == 1);
$docstring = "����� �����: ������" if ($hitcount == 2);
$docstring = "����� �����: $hitcount �����" if ($hitcount > 2 && $hitcount < 11);
$docstring = "����� �����: $hitcount ����" if ($hitcount > 10);
$docstring = "�� ���� ����� �����" unless ($hitcount);
if ($summary_terms ne '') {
my ($sec,$min,$hr,$mday,$mon,$wday,$yday,$isdst) = localtime(time);
my $day = (localtime)[3];
my $month = ((localtime)[4]) + 1;
my $year = (localtime)[5] + 1900;
my $date = "$day/$month/$year";
my $summary = "
$date ����� ��: $summary_terms ";
$summary = "$summary $docstring
\n";
open(SUMMARY,">>$summary_file");
print SUMMARY $summary;
close(SUMMARY);
}
print <
$page_title
EOM
my $data_file="includes/style.htm";
my @raw_data = OpenFile("$basedir$data_file","read");
my $line;
foreach $line (@raw_data) {
print "$line";
}
$data_file="includes/script.htm";
@raw_data = OpenFile("$basedir$data_file","read");
foreach $line (@raw_data) {
print "$line";
}
print <
function callRateScreen(fileName, title) {
document.frmPageDetail.title.value = title;
document.frmPageDetail.fileName.value = fileName;
document.frmPageDetail.submit();
}
EOM
require "ui/top.pl";
print <
EOM
if ($FORM{'type'} eq 'advance') {
&search_advanced;
} elsif ($FORM{'type'} eq 'submitter') {
&search_submitter;
} else {
&search_quick;
}
if ($FORM{'clear'} ne 'y') {
my $bgColor = 0;
print <
|
|
EOM
if ($search_criteria_exist eq 'true') {
my $recipes = \@recipes;
for (my $i=0; $i<$hitcount; $i++)
{
my $rec = $recipes[$i];
if ($bgColor == 0) {
print "| \n";
$bgColor = 1;
} else {
print " | \n";
$bgColor = 0;
}
if ($FORM{'action'} ne 'articles') {
print <
|
EOM
&show_recipe_rate($rec->getFileName());
print " | ";
print "";
print "";
print "getFileName();
print "\">";
print $rec->getTitle();
print " ";
if ($rec->getSubmittedBy() ne '') {
print " ����� �� ";
print $rec->getSubmittedBy();
print " ";
}
print " ";
print $rec->getDescription();
print " ";
print <
|
EOM
} else {
print "";
print "{re_filename}\">";
print $rec->getTitle();
print "";
if ($rec->getDescription() ne '') {
print " ";
print $rec->getDescription();
print " ";
}
}
print " | \n";
}
}
print "";
}
print " | ";
require "ui/nav.pl";
print "
";
$data_file="includes/key_press.htm";
@raw_data = OpenFile("$basedir$data_file","read");
foreach $line (@raw_data) {
print "$line";
}
print "";
if ($search_criteria_exist eq 'true') {
$sth->finish ();
$dbh->disconnect ();
}
exit(0);
}
#############################################################
sub OpenFile
{
my (@temp, $filename, $action, @data);
($filename, $action, @data) = @_;
if(lc $action eq "read") # Read File specified by $filename
{
open DATA,"<$filename" or dienice("Error Opening File, $filename","$!");
@temp = ;
close DATA;
return @temp;
}
if(lc $action eq "write") # Write array @data over $filename
{
open DATA,">$filename" or dienice("Error Opening File, $filename","$!");
print DATA @data;
close DATA;
}
if(lc $action eq "append") # Append array @data on end of $filename
{
open DATA,">>$filename" or dienice("Error Opening File, $filename","$!");
print DATA @data;
close DATA;
}
}
#############################################################
sub init_recipe_rate
{
my $dbh = AklaatDB::connect ();
$dbh->do ('SET NAMES CP1256');
$dbh->do ('SET COLLATION_CONNECTION=CP1256_GENERAL_CI');
my $sth = $dbh->prepare ("SELECT recipe, COUNT(*) AS recCount FROM rates GROUP BY recipe");
$sth->execute ();
my $ref;
my $new_rate;
my $rate;
while ($ref = $sth->fetchrow_hashref ())
{
$new_rate = new Rate();
$new_rate->setTotalRates($ref->{recCount});
$rates{$ref->{recipe}} = $new_rate;
}
$sth = $dbh->prepare ("SELECT recipe, COUNT(*) AS recCount FROM rates WHERE comments != '' GROUP BY recipe");
$sth->execute ();
while ($ref = $sth->fetchrow_hashref ())
{
$rate = $rates{$ref->{recipe}};
$rate->setTotalReviews($ref->{recCount});
$rates{$ref->{recipe}} = $rate;
}
$sth = $dbh->prepare ("SELECT recipe, SUM( genrating ) AS sum, COUNT( recipe ) AS count FROM rates WHERE genrating > 0 GROUP BY recipe");
$sth->execute ();
while ($ref = $sth->fetchrow_hashref ())
{
$rate = $rates{$ref->{recipe}};
$rate->setGeneralRateSum($ref->{sum});
$rate->setGeneralRateCount($ref->{count});
$rates{$ref->{recipe}} = $rate;
}
}
#############################################################
sub show_recipe_rate
{
my($filename) = shift;
my $rate = $rates{$filename};
my $total = 0;
my $reviews = 0;
my $count = 0;
my $sum = 0;
if ($rate != '') {
$total = ($rate->getTotalRates() eq ''? 0 : $rate->getTotalRates());
$reviews = ($rate->getTotalReviews() eq ''? 0 : $rate->getTotalReviews());
$sum = ($rate->getGeneralRateSum() eq ''? 0 : $rate->getGeneralRateSum());
$count = ($rate->getGeneralRateCount() eq ''? 0 : $rate->getGeneralRateCount());
}
print <
EOM
my $red = 0;
my $gray = 0;
if ($count != 0) {
$red = AklaatDB::round($sum / $count);
}
$gray = 5 - $red;
print "";
for($count=0; $count<$gray; $count++) {
print "*";
}
print "";
print "";
for($count=0; $count<$red; $count++) {
print "*";
}
print <
������: $total �������: $reviews
|
EOM
}
#######################################################################
sub search_quick {
print <
EOM
}
#######################################################################
sub search_advanced {
print <
EOM
}
#######################################################################
sub search_submitter {
print <
EOM
}
#############################################################
sub resetListSelection {
$ALL = '';
$SOUP = '';
$APPT = '';
$SALD = '';
$PSTR = '';
$DISH = '';
$DSRT = '';
$DRNK = '';
$DIET = '';
$ARTC = '';
}
#############################################################
sub filterSearchTerm {
my($word) = shift;
my @skipWordList = ('���','���','�����','�����',
'�����','�����','����','����','�����','�����','���','������','�����',
'����','���','�����','�����','����','����','���','���','����','��','��','���','���',
'��','��','���','����','����','��',
'����','����','����','�����',' �����','������');
my $key;
my %skipWords = ();
foreach $key (@skipWordList) {
$skipWords{$key}='';
}
$word = AklaatDB::trim($word);
my @chars=split//,$word;
if (($chars[0] eq '�') && ($chars[1] eq '�')) {
$chars[0]=" ";
$chars[1]=" ";
} else {
if (($chars[0] eq '�') && ($chars[1] eq '�') && ($chars[2] eq '�')) {
$chars[0]=" ";
$chars[1]=" ";
$chars[2]=" ";
}
}
$word = join '',@chars;
$word = AklaatDB::trim($word);
if (exists($skipWords{$word})) {
return "";
}
return $word;
}
#############################################################
sub validateSearchTerm {
my($terms) = shift;
$terms = AklaatDB::trim($terms);
my @words = split(/ /,$terms);
my $word;
my $filteredTerms = "";
foreach $word (@words) {
$word = filterSearchTerm($word);
$word = AklaatDB::trim($word);
$filteredTerms .= $word ." ";
}
$filteredTerms = AklaatDB::trim($filteredTerms);
return $filteredTerms;
}