📄 general.php
字号:
<?php
/*
$Id: general.php,v 1.181 2002/07/09 18:15:09 dgw_ Exp $
网络商店 - 吉鑫网络
http://www.chinaifc.com
Copyright (c) 2000,2001 网络商店
汗化版权所有吉鑫网络
*/
////
// Stop from parsing any further PHP code
function tep_exit() {
tep_session_close();
exit();
}
////
// Redirect to another page or site
function tep_redirect($url) {
header('Location: ' . $url);
tep_exit();
}
////
// Error message wrapper
// When optional parameters are provided, it closes the application
// (ie, halts the current application execution task)
function tep_error_message($error_message, $close_application = false, $close_application_error = '') {
echo $error_message;
if ($close_application) {
die($close_application_error);
}
}
////
// Return a random row from a database query
function tep_random_select($query) {
srand((double)microtime()*1000000); // seed the random number generator
$random_product = '';
$random_query = tep_db_query($query);
$num_rows = tep_db_num_rows($random_query);
if ($num_rows > 0) {
$random_row = @rand(0, ($num_rows - 1));
tep_db_data_seek($random_query, $random_row);
$random_product = tep_db_fetch_array($random_query);
}
return $random_product;
}
////
// Return a product's name
// TABLES: products
function tep_get_products_name($product_id) {
global $languages_id;
$product_query = tep_db_query("select products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . $product_id . "' and language_id = '" . $languages_id . "'");
$product = tep_db_fetch_array($product_query);
return $product['products_name'];
}
////
// Return a product's special price (returns nothing if there is no offer)
// TABLES: products
function tep_get_products_special_price($product_id) {
$product_query = tep_db_query("select specials_new_products_price from " . TABLE_SPECIALS . " where products_id = '" . $product_id . "' and status");
$product = tep_db_fetch_array($product_query);
return $product['specials_new_products_price'];
}
////
// Return a product's stock
// TABLES: products
function tep_get_products_stock($products_id) {
$products_id = tep_get_prid($products_id);
$stock_query = tep_db_query("select products_quantity from " . TABLE_PRODUCTS . " where products_id = '" . $products_id . "'");
$stock_values = tep_db_fetch_array($stock_query);
return $stock_values['products_quantity'];
}
////
// Check if the required stock is available
// If insufficent stock is available return an out of stock message
function tep_check_stock($products_id, $products_quantity) {
$stock_left = tep_get_products_stock($products_id) - $products_quantity;
$out_of_stock = '';
if ($stock_left < 0) {
$out_of_stock = '<span class="markProductOutOfStock">' . STOCK_MARK_PRODUCT_OUT_OF_STOCK . '</span>';
}
return $out_of_stock;
}
////
// Break a word in a string if it is longer than a specified length ($len)
function tep_break_string($string, $len, $break_char = '-') {
$l = 0;
$output = '';
for ($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
if ($char != ' ') {
$l++;
} else {
$l = 0;
}
if ($l > $len) {
$l = 1;
$output .= $break_char;
}
$output .= $char;
}
return $output;
}
////
// Return all HTTP GET variables, except those passed as a parameter
function tep_get_all_get_params($exclude_array = '') {
global $HTTP_GET_VARS;
if ($exclude_array == '') $exclude_array = array();
$get_url = '';
if (is_array($HTTP_GET_VARS)) {
reset($HTTP_GET_VARS);
while (list($key, $value) = each($HTTP_GET_VARS)) {
if ((strlen($value) > 0) && ($key != session_name()) && ($key != 'error') && (!in_array($key, $exclude_array))) {
$get_url .= $key . '=' . rawurlencode(stripslashes($value)) . '&';
}
}
}
return $get_url;
}
////
// Returns an array with countries
// TABLES: countries
function tep_get_countries($countries_id = '', $with_iso_codes = false) {
$countries_array = array();
if ($countries_id) {
if ($with_iso_codes) {
$countries = tep_db_query("select countries_name, countries_iso_code_2, countries_iso_code_3 from " . TABLE_COUNTRIES . " where countries_id = '" . $countries_id . "' order by countries_name");
$countries_values = tep_db_fetch_array($countries);
$countries_array = array('countries_name' => $countries_values['countries_name'],
'countries_iso_code_2' => $countries_values['countries_iso_code_2'],
'countries_iso_code_3' => $countries_values['countries_iso_code_3']);
} else {
$countries = tep_db_query("select countries_name from " . TABLE_COUNTRIES . " where countries_id = '" . $countries_id . "'");
$countries_values = tep_db_fetch_array($countries);
$countries_array = array('countries_name' => $countries_values['countries_name']);
}
} else {
$countries = tep_db_query("select countries_id, countries_name from " . TABLE_COUNTRIES . " order by countries_name");
while ($countries_values = tep_db_fetch_array($countries)) {
$countries_array[] = array('countries_id' => $countries_values['countries_id'],
'countries_name' => $countries_values['countries_name']);
}
}
return $countries_array;
}
////
// Alias function to tep_get_countries, which also returns the countries iso codes
function tep_get_countries_with_iso_codes($countries_id) {
return tep_get_countries($countries_id, true);
}
////
// Generate a path to categories
function tep_get_path($current_category_id = '') {
global $cPath_array;
if ($current_category_id) {
if (sizeof($cPath_array) == 0) {
$cPath_new = $current_category_id;
} else {
$cPath_new = '';
$last_category_query = tep_db_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . $cPath_array[(sizeof($cPath_array)-1)] . "'");
$last_category = tep_db_fetch_array($last_category_query);
$current_category_query = tep_db_query("select parent_id from " . TABLE_CATEGORIES . " where categories_id = '" . $current_category_id . "'");
$current_category = tep_db_fetch_array($current_category_query);
if ($last_category['parent_id'] == $current_category['parent_id']) {
for ($i=0; $i<(sizeof($cPath_array)-1); $i++) {
$cPath_new .= '_' . $cPath_array[$i];
}
} else {
for ($i=0; $i<sizeof($cPath_array); $i++) {
$cPath_new .= '_' . $cPath_array[$i];
}
}
$cPath_new .= '_' . $current_category_id;
if (substr($cPath_new, 0, 1) == '_') {
$cPath_new = substr($cPath_new, 1);
}
}
} else {
$cPath_new = implode('_', $cPath_array);
}
return 'cPath=' . $cPath_new;
}
////
// Returns the clients browser
function tep_browser_detect($component) {
global $HTTP_USER_AGENT;
$result = stristr($HTTP_USER_AGENT, $component);
return $result;
}
////
// Alias function to tep_get_countries()
function tep_get_country_name($country_id) {
$country_array = tep_get_countries($country_id);
return $country_array['countries_name'];
}
////
// Returns the zone (State/Province) name
// TABLES: zones
function tep_get_zone_name($country_id, $zone_id, $default_zone) {
$zone_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . $country_id . "' and zone_id = '" . $zone_id . "'");
if (tep_db_num_rows($zone_query)) {
$zone = tep_db_fetch_array($zone_query);
return $zone['zone_name'];
} else {
return $default_zone;
}
}
////
// Returns the zone (State/Province) code
// TABLES: zones
function tep_get_zone_code($country_id, $zone_id, $default_zone) {
$zone_query = tep_db_query("select zone_code from " . TABLE_ZONES . " where zone_country_id = '" . $country_id . "' and zone_id = '" . $zone_id . "'");
if (tep_db_num_rows($zone_query)) {
$zone = tep_db_fetch_array($zone_query);
return $zone['zone_code'];
} else {
return $default_zone;
}
}
////
// Returns the tax rate for a zone / class
// TABLES: tax_rates, zones_to_geo_zones
function tep_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) {
global $customer_zone_id, $customer_country_id;
if ( ($country_id == -1) && ($zone_id == -1) ) {
if (!tep_session_is_registered('customer_id')) {
$country_id = STORE_COUNTRY;
$zone_id = STORE_ZONE;
} else {
$country_id = $customer_country_id;
$zone_id = $customer_zone_id;
}
}
$tax_query = tep_db_query("select SUM(tax_rate) as tax_rate from " . TABLE_TAX_RATES . " tr left join " . TABLE_ZONES_TO_GEO_ZONES . " za ON tr.tax_zone_id = za.geo_zone_id left join " . TABLE_GEO_ZONES . " tz ON tz.geo_zone_id = tr.tax_zone_id WHERE (za.zone_country_id IS NULL OR za.zone_country_id = '0' OR za.zone_country_id = '" . $country_id . "') AND (za.zone_id IS NULL OR za.zone_id = '0' OR za.zone_id = '" . $zone_id . "') AND tr.tax_class_id = '" . $class_id . "' GROUP BY tr.tax_priority");
if (tep_db_num_rows($tax_query)) {
$tax_multiplier = 0;
while ($tax = tep_db_fetch_array($tax_query)) {
$tax_multiplier += $tax['tax_rate'];
}
return $tax_multiplier;
} else {
return 0;
}
}
////
// Return the tax description for a zone / class
// TABLES: tax_rates;
function tep_get_tax_description($zone_id, $class_id) {
$tax_query = tep_db_query("select tax_description from " . TABLE_TAX_RATES . " where tax_zone_id = '" . $zone_id . "' and tax_class_id = '" . $class_id . "'");
if (tep_db_num_rows($tax_query)) {
$tax = tep_db_fetch_array($tax_query);
return $tax['tax_description'];
} else {
return TEXT_UNKNOWN_TAX_RATE;
}
}
////
// Add tax to a products price
function tep_add_tax($price, $tax) {
global $currencies;
if (DISPLAY_PRICE_WITH_TAX == true) {
return round($price, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']) + tep_calculate_tax($price, $tax);
} else {
return round($price, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']);
}
}
// Calculates Tax rounding the result
function tep_calculate_tax($price, $tax) {
global $currencies;
return round($price * $tax / 100, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']);
}
////
// Return the number of products in a category
// TABLES: products, products_to_categories, categories
function tep_count_products_in_category($category_id, $include_inactive = false) {
$products_count = 0;
if ($include_inactive) {
$products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = p2c.products_id and p2c.categories_id = '" . $category_id . "'");
} else {
$products_query = tep_db_query("select count(*) as total from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c where p.products_id = p2c.products_id and p.products_status = 1 and p2c.categories_id = '" . $category_id . "'");
}
$products = tep_db_fetch_array($products_query);
$products_count += $products['total'];
if (USE_RECURSIVE_COUNT == 'true') {
$child_categories_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . $category_id . "'");
if (tep_db_num_rows($child_categories_query)) {
while ($child_categories = tep_db_fetch_array($child_categories_query)) {
$products_count += tep_count_products_in_category($child_categories['categories_id'], $include_inactive);
}
}
}
return $products_count;
}
////
// Return true if the category has subcategories
// TABLES: categories
function tep_has_category_subcategories($category_id) {
$child_category_query = tep_db_query("select count(*) as count from " . TABLE_CATEGORIES . " where parent_id = '" . $category_id . "'");
$child_category = tep_db_fetch_array($child_category_query);
if ($child_category['count'] > 0) {
return true;
} else {
return false;
}
}
////
// Returns the address_format_id for the given country
// TABLES: countries;
function tep_get_address_format_id($country_id) {
$address_format_query = tep_db_query("select address_format_id as format_id from " . TABLE_COUNTRIES . " where countries_id = '" . $country_id . "'");
if (tep_db_num_rows($address_format_query)) {
$address_format = tep_db_fetch_array($address_format_query);
return $address_format['format_id'];
} else {
return '1';
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -