$sc_errors = "";
$item_added = false;
$sc_item_id = "";
$sc_category_id = "";
$cart = get_param("cart");
if($cart)
{
$placed_ids = get_session("placed_ids");
if (!is_array($placed_ids)) {
$placed_ids = array();
}
$random_id = get_param("rnd");
//-- checking if such page has been already called
if (!isset($placed_ids[$random_id]))
{
$placed_ids[$random_id] = $random_id;
$shopping_cart = get_session("shopping_cart");
switch(strtoupper($cart))
{
case "ADD": // add item to the cart
if (!is_array($shopping_cart)) {
$shopping_cart = array();
}
$item_id = get_param("item_id");
$accessory_id = get_param("accessory_id");
$sc_item_id = $accessory_id ? $accessory_id : $item_id;
$sc_category_id = get_param("ic_id");;
$sql = " SELECT item_name,price,is_sales,sales_price,stock_level,use_stock_level,hide_out_of_stock ";
$sql .= " FROM " . $table_prefix . "items ";
$sql .= " WHERE item_id=" . $db->tosql($sc_item_id, INTEGER);
$sql .= " AND is_showing=1";
$sql .= " AND ((hide_out_of_stock=1 AND stock_level > 0) OR hide_out_of_stock=0)";
$db->query($sql);
if($db->next_record()) {
$item_name = $db->f("item_name");
$stock_level = $db->f("stock_level");
$use_stock_level = $db->f("use_stock_level");
$hide_out_of_stock = $db->f("hide_out_of_stock");
$price = calculate_price($db->f("price"), $db->f("is_sales"), $db->f("sales_price"));
} else {
// item doesn't exists or unavailable
return;
}
$properties = "";
$properties_types = array();
$sql = " SELECT property_id, property_name, control_type, required ";
$sql .= " FROM " . $table_prefix . "items_properties WHERE item_id=" . $sc_item_id;
if ($type == "list") {
$sql .= " AND use_on_list=1 ";
} else {
$sql .= " AND use_on_details=1 ";
}
$sql .= " ORDER BY property_order, property_id ";
$db->query($sql);
while($db->next_record())
{
$property_id = $db->f("property_id");
$property_name = $db->f("property_name");
$property_type = $db->f("control_type");
$property_required = $db->f("required");
$property_values = array();
if ($property_type == "CHECKBOXLIST") {
$property_total = get_param("property_total_" . $property_id);
for ($i = 1; $i <= $property_total; $i++) {
$property_value = get_param("property_" . $property_id . "_" . $i);
if ($property_value) { $property_values[] = $property_value; }
}
} else {
$property_value = get_param("property_" . $property_id);
if ($property_value) { $property_values[] = $property_value; }
}
if(sizeof($property_values) > 0) {
$properties[$property_id] = $property_values;
$properties_types[$property_id] = $property_type;
} else if ($property_required) {
$property_error = str_replace("{property_name}", get_translation($property_name), REQUIRED_PROPERTY_MSG);
$property_error = str_replace("{product_name}", get_translation($item_name), $property_error);
$sc_errors .= $property_error . "
";
}
}
if($sc_errors) {
// error occurred can't continue process
return;
}
// check if such item already in our cart than increase quantity by one
$in_cart = false;
reset($shopping_cart);
while(list($cart_id, $item) = each($shopping_cart))
{
if($item["ITEM_ID"] == $sc_item_id)
{
$item_properties = $item["PROPERTIES"];
if(!is_array($item_properties) && !is_array($properties))
{
$in_cart = true;
break;
}
else if(is_array($item_properties) && is_array($properties) && sizeof($item_properties) == sizeof($properties))
{
$identical_properties = true;
reset($properties);
while(list($property_id, $property_values) = each($properties))
{
$new_values = implode(",", $property_values);
if(isset($item_properties[$property_id])) {
$exists_values = implode(",", $item_properties[$property_id]);
} else {
$exists_values = "";
}
if ($exists_values != $new_values) {
$identical_properties = false;
break;
}
}
if($identical_properties)
{
$in_cart = true;
break;
}
}
}
}
if($in_cart && $settings["change_quantity"])
{
if($hide_out_of_stock)
{
if(!$use_stock_level || $stock_level > $shopping_cart[$cart_id]["QUANTITY"]) {
$shopping_cart[$cart_id]["QUANTITY"]++;
$item_added = true;
} else {
$property_error = str_replace("{limit_quantity}", $stock_level, PRODUCT_LIMIT_MSG);
$property_error = str_replace("{product_name}", get_translation($item_name), $property_error);
$sc_errors .= $property_error . "
";
}
}
else
{
$shopping_cart[$cart_id]["QUANTITY"]++;
$item_added = true;
}
}
else
{
//-- prepare item for adding to cart
//-- check for additional price for product
if(is_array($properties)) {
foreach($properties as $property_id => $property_values) {
if(strtoupper($properties_types[$property_id]) == "LISTBOX"
|| strtoupper($properties_types[$property_id]) == "RADIOBUTTON"
|| strtoupper($properties_types[$property_id]) == "CHECKBOXLIST") {
for($pv = 0; $pv < sizeof($property_values); $pv++) {
$sql = " SELECT additional_price, additional_weight ";
$sql .= " FROM " . $table_prefix . "items_properties_values ipv ";
$sql .= " WHERE property_id=" . $db->tosql($property_id, INTEGER);
$sql .= " AND item_property_id=" . $db->tosql($property_values[$pv], INTEGER);
$sql .= " ORDER BY item_property_id ";
$db->query($sql);
if($db->next_record()) {
$additional_price = $db->f("additional_price");
$price += $additional_price;
}
}
}
}
}
$item = array (
"ITEM_ID" => $sc_item_id,
"ITEM_NAME" => $item_name,
"PROPERTIES" => $properties,
"QUANTITY" => 1, // only one item can be placed
"PRICE" => $price
);
//-- add to cart
$shopping_cart[] = $item;
$item_added = true;
}
set_session("shopping_cart", $shopping_cart);
if($item_added && isset($settings["redirect_to_cart"]) && $settings["redirect_to_cart"]) {
$rp = get_param("rp");
$cart_page = strlen($rp) ? "basket.php?rp=" . urlencode($rp) : "basket.php";
header("Location: " . $cart_page);
exit;
}
break;
case "RM": //-- remove the item from the cart
if (is_array($shopping_cart))
{
$cart_id = get_param("cart_id");
unset($shopping_cart[$cart_id]);
if(sizeof($shopping_cart) == 0) {
unset($shopping_cart);
set_session("shopping_cart", "");
set_session("session_coupons", "");
}
else {
set_session("shopping_cart", $shopping_cart);
}
}
break;
case "QTY": // update item quantity in the cart
if (is_array($shopping_cart) && $settings["change_quantity"])
{
$cart_id = get_param("cart_id");
$new_quantity = get_param("new_quantity");
$new_quantity = abs($new_quantity);
if(isset($shopping_cart[$cart_id]))
{
$item_id = $shopping_cart[$cart_id]["ITEM_ID"];
$sql = " SELECT item_name, stock_level, use_stock_level, hide_out_of_stock FROM " . $table_prefix . "items ";
$sql .= " WHERE item_id=" . $db->tosql($item_id, INTEGER);
$db->query($sql);
if($db->next_record()) {
$item_name = $db->f("item_name");
$stock_level = $db->f("stock_level");
$use_stock_level = $db->f("use_stock_level");
$hide_out_of_stock = $db->f("hide_out_of_stock");
} else {
// item doesn't exists or unavailable
return;
}
if($hide_out_of_stock)
{
if(!$use_stock_level || $stock_level > $new_quantity)
$shopping_cart[$cart_id]["QUANTITY"] = $new_quantity;
else
$shopping_cart[$cart_id]["QUANTITY"] = $stock_level;
}
else
{
$shopping_cart[$cart_id]["QUANTITY"] = $new_quantity;
}
set_session("shopping_cart", $shopping_cart);
}
}
break;
case "CLR": //-- remove all items from the cart
if (is_array($shopping_cart)) {
set_session("shopping_cart", "");
set_session("session_coupons", "");
}
break;
}
set_session("placed_ids", $placed_ids);
}
}
function calculate_price($price, $is_sales, $sales_price)
{
if($is_sales && $sales_price)
{
$price = $sales_price;
}
return $price;
}
function remove_coupon($coupon_id)
{
global $shopping_cart, $coupons;
if (is_array($coupons) && isset($coupons[$coupon_id])) {
unset($coupons[$coupon_id]);
if (sizeof($coupons) == 0) {
set_session("session_coupons", "");
} else {
set_session("session_coupons", $coupons);
}
}
foreach ($shopping_cart as $cart_id => $item) {
if (isset($shopping_cart[$cart_id]["COUPONS"]) && isset($shopping_cart[$cart_id]["COUPONS"][$coupon_id])) {
unset($shopping_cart[$cart_id]["COUPONS"][$coupon_id]);
if (sizeof($shopping_cart[$cart_id]["COUPONS"]) == 0) {
unset($shopping_cart[$cart_id]["COUPONS"]);
}
}
}
set_session("shopping_cart", $shopping_cart);
}
// calculate fingerprint
function calculate_fp ($login_id, $trankey, $amount, $sequence, $timestamp, $currency = "")
{
return (hmac_md5 ($login_id."^".$sequence."^".$timestamp."^".$amount."^".$currency, $trankey));
}
function get_payment_rate($payment_id, $currency_rate)
{
global $db, $table_prefix;
$payment_rate = 1;
$sql = " SELECT parameter_type,parameter_source FROM " . $table_prefix . "payment_parameters ";
$sql .= " WHERE payment_id=" . $db->tosql($payment_id, INTEGER);
$sql .= " AND parameter_name IN ('currency_code', 'x_currency_code', 'currency') ";
$sql .= " AND not_passed<>1 ";
$db->query($sql);
if ($db->next_record()) {
$parameter_type = $db->f("parameter_type");
$parameter_source = $db->f("parameter_source");
if (strtoupper($parameter_type) == "VARIABLE") {
if ($parameter_source == "currency_code" || $parameter_source == "{currency_code}") {
$payment_rate = $currency_rate;
}
} else {
$sql = " SELECT exchange_rate FROM " . $table_prefix . "currencies ";
$sql .= " WHERE currency_code=" . $db->tosql($parameter_source, TEXT);
$db->query($sql);
if ($db->next_record()) {
$payment_rate = $db->f("exchange_rate");
}
}
}
return $payment_rate;
}
function get_final_message($message, $message_type)
{
if (preg_match("/\[" . $message_type . "\](.+)\[\/" . $message_type . "\]/s", $message, $match)) {
$message = $match[1];
} else {
$message = preg_replace("/\[success].*\[\/success]/s", "", $message);
$message = preg_replace("/\[pending].*\[\/pending]/s", "", $message);
$message = preg_replace("/\[failure].*\[\/failure]/s", "", $message);
}
return $message;
}
?>
function custom_block($block_name, $block_number)
{
global $t;
global $db, $table_prefix;
global $category_id;
global $page_settings;
if(get_setting_value($page_settings, $block_name . "_column_hide", 0)) {
return;
}
$sql = " SELECT block_title,block_desc FROM " . $table_prefix . "custom_blocks ";
$sql .= " WHERE block_id=" . intval($block_number);
$db->query($sql);
if($db->next_record()) {
$custom_title = get_translation($db->f("block_title"));
$custom_body = get_translation($db->f("block_desc"));
}
if(!strlen($custom_body) && !strlen($custom_title)) {
return;
}
if(strlen($custom_title)) {
$t->set_file("block_body", "block_custom.html");
} else {
$t->set_file("block_body", "block_simple.html");
}
$t->set_var("MORE_MSG", MORE_MSG);
$t->set_var("READ_MORE_MSG", READ_MORE_MSG);
$t->set_var("CLICK_HERE_MSG", CLICK_HERE_MSG);
$t->set_var("custom_title", $custom_title);
$t->set_var("custom_body", $custom_body);
$t->parse("block_body", false);
$t->parse($block_name, true);
}
?>
function small_cart($block_name)
{
global $t, $db, $table_prefix;
global $category_id;
global $page_settings, $settings;
if(get_setting_value($page_settings, $block_name . "_column_hide", 0)) {
return;
}
$t->set_file("block_body", "block_cart.html");
$t->set_var("SMALL_CART_TITLE", SMALL_CART_TITLE);
$t->set_var("GOTO_CHECKOUT_MSG", GOTO_CHECKOUT_MSG);
$t->set_var("AMEND_CART_MSG", AMEND_CART_MSG);
$t->set_var("VIEW_CART_MSG", VIEW_CART_MSG);
$shopping_cart = get_session("shopping_cart");
if(is_array($shopping_cart) && sizeof($shopping_cart) > 0) {
$t->set_var("empty_small_cart", "");
$t->set_var("small_cart_items", "");
$currency = get_currency();
$currency_left = $currency["left"];
$currency_right = $currency["right"];
$currency_rate = $currency["rate"];
$default_tax = get_setting_value($settings, "default_tax", 0);
$default_tax_note = get_setting_value($settings, "default_tax_note", "");
$t->set_var("PROD_TITLE_COLUMN", PROD_TITLE_COLUMN);
$t->set_var("PROD_QTY_COLUMN", PROD_QTY_COLUMN);
$t->set_var("PROD_PRICE_COLUMN",PROD_PRICE_COLUMN);
$t->set_var("GOODS_TOTAL_MSG", GOODS_TOTAL_MSG);
$t->set_var("CART_TOTAL_MSG", CART_TOTAL_MSG);
$t->set_var("default_tax_note", $default_tax_note);
$total_quantity = 0; $total_price = 0;
foreach($shopping_cart as $cart_id => $item)
{
$item_id = $item["ITEM_ID"];
$item_name = get_translation($item["ITEM_NAME"]);
$short_name = substr($item_name, 0, 10);
$properties = $item["PROPERTIES"];
$quantity = $item["QUANTITY"];
$price = $item["PRICE"];
$price = $price * $currency_rate;
if ($default_tax > 0) {
$price += round(($price * $default_tax) / 100, 2);
}
if (isset($item["COUPONS"]) && is_array($item["COUPONS"])) {
foreach ($item["COUPONS"] as $coupon_id => $coupon_info) {
$price -= $coupon_info["DISCOUNT_AMOUNT"];
}
}
$total_quantity += $quantity;
$total_price += ($quantity * $price);
$t->set_var("short_name", $short_name);
$t->set_var("quantity", $quantity);
$t->set_var("price", $currency_left . number_format($price, 2) . $currency_right);
$t->parse("small_cart_items", true);
}
$t->set_var("total_quantity", $total_quantity);
$t->set_var("total_price", $currency_left . number_format($total_price, 2) . $currency_right);
$t->set_var("checkout_href", "checkout.php");
$t->set_var("basket_href", "basket.php");
$t->parse("small_cart", false);
} else {
$t->set_var("EMPTY_CART_MSG", EMPTY_CART_MSG);
$t->parse("empty_small_cart", false);
$t->set_var("small_cart", "");
}
$t->parse("block_body", false);
$t->parse($block_name, true);
}
?>
function poll_form($block_name)
{
global $t, $db, $table_prefix, $language_code;
global $page_settings, $date_show_format;
if(get_setting_value($page_settings, $block_name . "_column_hide", 0)) {
return;
}
$polls = array();
$sql = " SELECT * FROM " . $table_prefix . "polls ";
$sql .= " WHERE is_active=1 ";
$sql .= " ORDER BY date_added DESC ";
$db->query($sql);
while ($db->next_record()) {
$poll_id = $db->f("poll_id");
$poll_type = $db->f("poll_type");
$question = get_translation($db->f("question"), $language_code);
$poll_date = $db->f("date_added", DATETIME);
$polls[] = array($poll_id, $poll_type, $question, $poll_date);
}
if (sizeof($polls) > 0) {
$t->set_file("block_body", "block_poll.html");
$t->set_var("poll_vote_href", "poll_vote.php");
$t->set_var("polls_href", "polls.php");
$t->set_var("POLL_TITLE", POLL_TITLE);
$t->set_var("VOTE_BUTTON", VOTE_BUTTON);
$t->set_var("VIEW_RESULTS_MSG", VIEW_RESULTS_MSG);
$t->set_var("PREVIOUS_POLLS_MSG", PREVIOUS_POLLS_MSG);
for($i = 0; $i < sizeof($polls); $i++) {
list($poll_id, $poll_type, $question, $poll_date) = $polls[$i];
$poll_control = ($poll_type == 1) ? "radio" : "checkbox";
$t->set_var("poll_id", $poll_id);
$t->set_var("question", $question);
$t->set_var("poll_date", va_date($date_show_format, $poll_date));
$t->set_var("poll_control", $poll_control);
$option_number = 0;
$t->set_var("poll_options", "");
$sql = " SELECT * FROM " . $table_prefix . "polls_options ";
$sql .= " WHERE poll_id=" . $db->tosql($poll_id, INTEGER);
$db->query($sql);
while($db->next_record()) {
$option_number++;
$is_default_value = $db->f("is_default_value");
$option_checked = ($is_default_value == 1) ? "checked" : "";
$option_name = ($poll_type == 1) ? "option_value" : "option_value_" . $option_number;
$t->set_var("poll_option_id", $db->f("poll_option_id"));
$t->set_var("option_name", $option_name);
$t->set_var("option_checked", $option_checked);
$t->set_var("option_description", get_translation($db->f("option_description"), $language_code));
$t->parse("poll_options", true);
}
$t->parse("block_body", false);
$t->parse($block_name, true);
}
}
}
?>
function search_form($block_name)
{
global $t, $db, $table_prefix, $language_code;
global $category_id;
global $page_settings;
if(get_setting_value($page_settings, $block_name . "_column_hide", 0)) {
return;
}
if($block_name) {
$t->set_file("block_body", "block_search.html");
}
$t->set_var("search_href", "products.php");
$t->set_var("search_name", PRODUCTS_TITLE);
$t->set_var("SEARCH_TITLE", SEARCH_TITLE);
$t->set_var("GO_BUTTON", GO_BUTTON);
$t->set_var("SEARCH_BUTTON", SEARCH_BUTTON);
$t->set_var("ADVANCED_SEARCH_TITLE", ADVANCED_SEARCH_TITLE);
$category_id = get_param("category_id");
$search_category_id = get_param("search_category_id");
$search_string = trim(get_param("search_string"));
$is_search = strlen($search_string);
$pq = get_param("pq");
$fq = get_param("fq");
$s_tit = get_param("s_tit");
$s_sds = get_param("s_sds");
$s_fds = get_param("s_fds");
$manf = get_param("manf");
$lprice = get_param("lprice");
$hprice = get_param("hprice");
$lweight = get_param("lweight");
$hweight = get_param("hweight");
if($is_search) $category_id = $search_category_id;
if(!strlen($category_id)) $category_id = "0";
$pass_parameters = array(
"search_string" => $search_string,
"search_category_id" => $search_category_id, "pq" => $pq, "fq" => $fq,
"s_tit" => $s_tit, "s_sds" => $s_sds, "s_fds" => $s_fds,
"manf" => $manf, "lprice" => $lprice, "hprice" => $hprice,
"lweight" => $lweight, "hweight" => $hweight
);
if ($pq > 0) {
for($pi = 1; $pi <= $pq; $pi++) {
$property_name = get_param("pn_" . $pi);
$property_value = get_param("pv_" . $pi);
if (strlen($property_name) && strlen($property_value)) {
$pass_parameters["pn_" . $pi] = $property_name;
$pass_parameters["pv_" . $pi] = $property_value;
}
}
}
if ($fq > 0) {
for($fi = 1; $fi <= $fq; $fi++) {
$feature_name = get_param("fn_" . $fi);
$feature_value = get_param("fv_" . $fi);
if (strlen($feature_name) && strlen($feature_value)) {
$pass_parameters["fn_" . $fi] = $feature_name;
$pass_parameters["fv_" . $fi] = $feature_value;
}
}
}
$query_string = get_query_string($pass_parameters, "", "", false);
$t->set_var("advanced_search_href", "search.php" . $query_string);
$t->global_parse("advanced_search", false, false, true);
$search_categories[] = array(0, SEARCH_IN_ALL_MSG);
if($category_id != 0) {
$search_categories[] = array($category_id, SEARCH_IN_CURRENT_MSG);
}
$sql = " SELECT category_id,category_name ";
$sql .= " FROM " . $table_prefix . "categories ";
$sql .= " WHERE is_showing=1";
$sql .= " AND parent_category_id = " . $db->tosql($category_id, INTEGER);
$sql .= " ORDER BY category_order ";
$db->query($sql);
while ($db->next_record())
{
$show_category_id = $db->f("category_id");
$category_name = get_translation($db->f("category_name"), $language_code);
$search_categories[] = array($show_category_id, $category_name);
}
// set up search form parameters
if (sizeof($search_categories) > 1) {
set_options($search_categories, $search_category_id, "search_category_id");
$t->global_parse("search_categories", false, false, true);
} else {
$t->set_var("search_categories", "");
}
$t->set_var("search_string", htmlspecialchars($search_string));
$t->set_var("current_category_id", htmlspecialchars($category_id));
if($block_name) {
$t->parse("block_body", false);
$t->parse($block_name, true);
}
}
?>