<?php
class site
{
var $mysql, $form;
//
function cat_levels($sub, $temp='')
{
$data = $this->mysql->get_array($this->mysql->query("name,sub,url_key", "fs_shop_cats", "ID = $sub LIMIT 1"));
if($data['sub'] > 0)
{
$temp .= $this->cat_levels($data['sub'], $temp);
}
$temp .= $data['url_key']."#".$data['name']."\n";
return $temp;
}
//see if a product is in stock at all
function in_stock($pid)
{
$pid = (int)$pid;
$data = $this->mysql->get_array($this->mysql->query("SUM(stock) as instock", "fs_shop_stock", "pid = $pid GROUP BY pid"));
return ($data['instock'] > 0) ? true : false;
}
//retursn number in stock for a product option
function stock($pid, $option)
{
$pid = (int)$pid;
$option = addslashes($option);
$data = $this->mysql->get_array($this->mysql->query("stock", "fs_shop_stock", "pid = $pid AND `option` = '$option' LIMIT 1"));
return (int)$data['stock'];
}
//adds or takes awy stock from a product option
function update_stock($pid, $option, $qty, $in=false)
{
$pid = (int)$pid;
$option = addslashes($option);
$qty = (int)$qty;
$current = $this->stock($pid, $option);
$new = ($current+$qty);
if($in == false)
{
$new = (($current-$qty) <= 0) ? 0 : ($current-$qty);
}
$this->mysql->update("stock", $new, "fs_shop_stock", "pid = $pid AND `option` = '$option' LIMIT 1");
}
//get all the info for a product
function productInfo($pid)
{
$pid = (int)$pid;
$data = $this->mysql->get_array($this->mysql->query("*", "fs_shop_products", "ID = $pid LIMIT 1"));
return (is_array($data)) ? array_map('stripslashes', $data) : array();
}
//returns total weight of the product
function totalProductWeight($pid, $option)
{
$pid = (int)$pid;
$option = addslashes($option);
$d = $this->mysql->get_array($this->mysql->query("weight", "fs_shop_products", "ID = $pid LIMIT 1"));
$s = $this->mysql->get_array($this->mysql->query("plus_weight", "fs_shop_stock", "pid = $pid AND `option` = '$option' LIMIT 1"));
return ($d['weight'] + $s['plus_weight']);
}
//get product ID from url_key
function productFromKey($key)
{
$key = addslashes($key);
$data = $this->mysql->get_array($this->mysql->query("ID", "fs_shop_products", "url_key = '$key' LIMIT 1"));
return (int)$data['ID'];
}
//get cat ID from url_key
function catFromKey($key)
{
$key = addslashes($key);
$data = $this->mysql->get_array($this->mysql->query("ID", "fs_shop_cats", "url_key = '$key' LIMIT 1"));
return (int)$data['ID'];
}
//get title of a page
function pageTitle($id)
{
$id = (int)$id;
$data = $this->mysql->get_array($this->mysql->query("title", "fs_pages", "ID = $id LIMIT 1"));
return stripslashes($data['title']);
}
//get shop cat name
function catName($id)
{
$id = (int)$id;
$data = $this->mysql->get_array($this->mysql->query("name", "fs_shop_cats", "ID = $id LIMIT 1"));
return stripslashes($data['name']);
}
//country list
function countryList()
{
$data = $this->shop_settings('country_list');
$list = explode("\n", $data['country_list']);
asort($list);
foreach($list as $key => $line)
{
list($name, $code) = explode("|", $line);
$return['code'][] = trim($code);
$return['name'][trim($code)] = trim($name);
}
return $return;
}
//returns shop settings
function shop_settings($name)
{
$names = explode(",", $name);
for($x = 0; $x < sizeof($names); $x++)
{
$data = $this->mysql->get_array($this->mysql->query("value", "fs_shop_settings", "`name` = '$names[$x]' LIMIT 1"));
$return[$names[$x]] = stripslashes($data['value']);
}
return $return;
}
//formats a number into currency
function currency_symbol($price)
{
$symbol = $this->shop_settings('paypal_currency,currency_symbol');
$baseCur = (!empty($_COOKIE['cur'])) ? $_COOKIE['cur'] : $symbol['paypal_currency'];
$converter = $this->mysql->get_array($this->mysql->query("value", "fs_settings", "`name` = 'currency_converter' LIMIT 1"));
$html = ($converter['value'] == 'y') ? '<span class="pgAmount" price="'.number_format($price, 2, '.', '').'">'.$symbol['currency_symbol'].number_format($price, 2).'</span>' : $symbol['currency_symbol'].number_format($price, 2).' '.$symbol['paypal_currency'];
return $html;
}
//
function best_sellers()
{
return 'show best sellers here';
}
//returns user's ip address
function ip()
{
return str_replace(",", "", addslashes($_SERVER['REMOTE_ADDR']));
}
//
function poll_results($id, $polldisplay=false)
{
$id = (int)$id;
$total_votes = $this->mysql->num_rows($this->mysql->query("ID", "fs_poll_votes" , "poll_id = $id"));
$query = $this->mysql->query("question", "fs_poll_questions", "ID = $id LIMIT 1");
if($this->mysql->num_rows($query) == 0){return '';}
$question = $this->mysql->get_array($query);
$out = '<fieldset style="border:none;margin:5px;" id="results'.$id.'"><legend style="font-weight:bold;">'.$question['question'].'</legend><div style="margin-bottom:15px;">';
$query = $this->mysql->query("fs_poll_answers.ID,fs_poll_answers.answer,fs_poll_answers.colour,COUNT(fs_poll_votes.ID) AS votes", "fs_poll_answers LEFT OUTER JOIN fs_poll_votes ON fs_poll_votes.answer_id = fs_poll_answers.ID", "fs_poll_answers.poll_id = $id GROUP BY fs_poll_answers.ID ORDER BY votes DESC");
while($answers = $this->mysql->get_array($query))
{
$percent = ($total_votes > 0) ? number_format($answers['votes']/$total_votes*100, 1) : '0.0';
$v = ($answers['votes'] == 1) ? 'vote' : 'votes';
$out .= $answers['answer'].' - '.number_format($answers['votes']).' '.$v.', '.$percent.'%
<div style="width:100%;max-width:400px;height:15px;border:1px solid #CCCCCC; background-color:#EEEEEE;padding:1px;"><div style="width:'.$percent.'%;background-color:'.$answers['colour'].';height:15px;" class="pollbar'.$id.' pollbar" id="'.$answers['ID'].'">&nbsp;</div></div><br />';
}
$out = substr($out, 0, -6);
//$polldisplay is true, include link to hide results and show the poll form
$out .= ($polldisplay == true) ? '</div><p><a href="javascript:;" onclick="$(\'poll'.$id.'\').show();$(\'results'.$id.'\').hide();">&lt;&lt; Back</a></p>' : '</div>';
$out .= '</fieldset>';
//return
return stripslashes($out);
}
//
function show_poll($id)
{
$id = (int)$id;
$query = $this->mysql->query("question,unique_votes,expire", "fs_poll_questions", "ID = $id LIMIT 1");
//if poll is not found, replace the image with blank
if($this->mysql->num_rows($query) == 0){return '';}
//if user has already voted and unique votes is true, show results
$question = $this->mysql->get_array($query);
$hasVoted = $this->mysql->num_rows($this->mysql->query("ID", "fs_poll_votes", "poll_id = $id AND ip = '".$this->ip()."' LIMIT 1"));
if($question['unique_votes'] == 'y' AND $hasVoted > 0)
{
return $this->poll_results($id);
}
//if user has voted[cookie], show results
$cookie = explode("|", $_COOKIE['poll']);
if(in_array($id, $cookie))
{
return $this->poll_results($id);
}
//if poll has expired, show results
if($question['expire'] > 0 AND $question['expire'] < time())
{
return $this->poll_results($id);
}
//show form to vote
$out = '<form id="poll'.$id.'" name="poll'.$id.'" method="post" action="">
<fieldset style="border:none;margin:5px;">
<legend style="font-weight:bold;">'.$question['question'].'</legend><div style="margin-bottom:15px;">';
$query = $this->mysql->query("ID,answer", "fs_poll_answers", "poll_id = $id ORDER BY ID ASC");
$num = $this->mysql->num_rows($query);
$x = 1;
while($answers = $this->mysql->get_array($query))
{
$class = ($x == $num) ? 'validate-one-required' : '';
$out .= '<input type="radio" id="answer'.$answers['ID'].'" name="answer" value="'.$answers['ID'].'" class="'.$class.'" style="width:auto;" /><label for="answer'.$answers['ID'].'">'.$answers['answer'].'</label><br />';
$x++;
}
$out .= '</div><input type="submit" name="submitpoll'.$id.'" id="submitpoll'.$id.'" value="Vote" style="width:auto;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:;" onclick="$(\'poll'.$id.'\').hide();$(\'results'.$id.'\').show();anibars(\''.$id.'\');">View results</a>';
$out .= '</fieldset><input type="hidden" id="poll" name="poll" value="'.$id.'" /></form><script>new Validation(\'poll'.$id.'\');</script>';
//hidden results section
$out .= '<div style="display:none;" id="results'.$id.'">'.$this->poll_results($id, true).'</div>';
//return
return stripslashes($out);
}
//returns html for form fields
function field($label, $type, $options, $required, $field_id)
{
$out = '';
$label = strtolower($label);
$label = ucwords($label);
$name = str_replace(" ", "_", $label);
$name = str_replace(":", "", $name);
if($type == 'Text')
{
$required = ($required == 'y') ? ' class="required"' : '';
$out = '<input id="'.$name.'" name="form[f'.$field_id.']" type="text" size="32" value="'.$_POST['form']['f'.$field_id].'" '.$required.' />';
}
if($type == 'Calendar')
{
$required = ($required == 'y') ? ' class="required"' : '';
$out = '<input class="calendar" id="'.$name.'" name="form[f'.$field_id.']" type="text" size="32" value="'.$_POST['form']['f'.$field_id].'" '.$required.' />';
}
if($type == 'Textarea')
{
$required = ($required == 'y') ? ' class="required"' : '';
$out = '<textarea id="'.$name.'" name="form[f'.$field_id.']" rows="4" cols="35"'.$required.'>'.$_POST['form']['f'.$field_id].'</textarea>';
}
if($type == 'Checkbox')
{
$options = explode("\n", $options);
$last_key = key(array_slice($options, -1, 1));
foreach($options as $k => $option)
{
$r = ($required == 'y' && $k == $last_key) ? ' class="validate-one-required no-width"' : ' class="no-width"';
$selected = ($_POST['form']['f'.$field_id] == trim($option)) ? ' checked="checked"' : '';
$out .= '<input name="form[f'.$field_id.'][]" type="checkbox" id="'.str_replace(" ","",trim($option)).'" value="'.trim($option).'"'.$selected.$r.' /> <label for="'.str_replace(" ","",trim($option)).'">'.trim($option).'</label><br />';
}
}
if($type == 'Radio')
{
$options = explode("\n", $options);
$last_key = key(array_slice($options, -1, 1));
foreach($options as $k => $option)
{
$r = ($required == 'y' && $k == $last_key) ? ' class="validate-one-required no-width"' : ' class="no-width"';
$selected = ($_POST['form']['f'.$field_id] == trim($option)) ? ' checked="checked"' : '';
$out .= '<input type="radio" name="form[f'.$field_id.']" id="'.str_replace(" ","",trim($option)).'" value="'.trim($option).'"'.$selected.$r.' /> <label for="'.str_replace(" ","",trim($option)).'">'.trim($option).'</label><br />';
}
}
if($type == 'Select')
{
$required = ($required == 'y') ? ' class="validate-selection"' : '';
$out = '<select id="'.$name.'" name="form[f'.$field_id.']"'.$required.'><option value="">Select one</option>';
$options = explode("\n", $options);
foreach($options as $k => $option)
{
$selected = ($_POST['form']['f'.$field_id] == trim($option)) ? ' selected' : '';
$out .= (!empty($option)) ? '<option value="'.trim($option).'"'.$selected.'>'.trim($option).'</option>' : '';
}
$out .= '</select>';
}
if($type == 'Upload')
{
$r = ($required == 'y') ? ' class="required uploadField"' : 'class="uploadField"';
$out .= '<input type="file" name="f'.$field_id.'" id="'.$name.'"'.$r.' size="35" />';
}
return $out;
}
//returns form html
function show_form($id, $ThanksMsg='')
{
$id = (int)$id;
$out = (!empty($ThanksMsg)) ? '<h3 id="thanks'.$id.'" align="center">'.$ThanksMsg.'</h3>' : '';
$out .= '<form id="formAd'.$id.'" name="formAd'.$id.'" method="post" action="" enctype="multipart/form-data">
<input type="hidden" id="formid" name="formid" value="'.$id.'" />
<table border="0" cellspacing="0" cellpadding="0" class="custom_form">';
$query = $this->mysql->query("ID,label,type,required,options", "fs_forms_fields", "form_id = $id ORDER BY `order` ASC");
if($this->mysql->num_rows($query) == 0){return '';}
while($f = $this->mysql->get_array($query))
{
if($f['type'] == 'Title')
{
$out .= '<tr><td colspan="2"><h2>'.stripslashes($f['label']).'</h2></td></tr>';
}
elseif($f['type'] == 'Paragraph')
{
$out .= '<tr><td colspan="2">'.stripslashes(nl2br($f['options'])).'</td></tr>';
}
elseif($f['type'] == 'Spacer')
{
$out .= '<tr><td colspan="2">&nbsp;</td></tr>';
}
else
{
$star = ($f['required'] == 'y') ? '<span class="formRequiredField">*</span> ' : '';
$out .= '<tr><th>'.$star.stripslashes($f['label']).'</th><td>'.$this->field($f['label'], $f['type'], $f['options'], $f['required'], $f['ID']).'</td></tr>';
}
}
//spam cap if enbled for this form
$cap = $this->mysql->num_rows($this->mysql->query("ID", "fs_forms", "ID = $id AND `use_recaptcha` = 'y' LIMIT 1"));
if($cap == 1)
{
$out .= '<tr><th>&nbsp;</th><td>'.$this->form->recaptchaShowCaptcha().'</td></tr>';
}
//submit button
$out .= '<tr><td>&nbsp;</td><td><input type="submit" id="send" name="send" value="Submit" class="button" /></td></tr>';
$out .= '</table></form>';
$out .= '<script>
new Validation(\'formAd'.$id.'\', {onsubmit:false, onFormValidate:checkCap});
function checkCap(validationPassed, form)
{
if(validationPassed)
{
var s = jQuery(\'#g-recaptcha-response\').val();
if(s==\'\')
{
event.preventDefault();
jQuery(\'#captchaErr\').show();
return false;
}
form.submit();
return true;
}
}
</script>';
return $out;
}
//
}
$site = new site();
$site->mysql = &$mysql;
$site->form = &$form;
?>