[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
[phpBB Debug] PHP Warning: in file [ROOT]/includes/crs/crs_misc_functions.php on line 37: mime_content_type(): Empty filename or path
Zen Cart 源代码 functions_lookups.php

Zen Cart 源代码 functions_lookups.php




下载文件

文件名: functions_lookups.php
文件类型: PHP文件
文件大小: 33.49 KiB
MD5: ec5c8aeef8b8b306991648cdaac2014a

functions_lookups.php - 关闭高亮
  1. <?php
  2. /**
  3.  * functions_lookups.php
  4.  * Lookup Functions for various Zen Cart activities such as countries, prices, products, product types, etc
  5.  *
  6.  * @package functions
  7.  * @copyright Copyright 2003-2011 Zen Cart Development Team
  8.  * @copyright Portions Copyright 2003 osCommerce
  9.  * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
  10.  * @version $Id: functions_lookups.php 19352 2011-08-19 16:13:43Z ajeh $
  11.  */
  12.  
  13.  
  14. /**
  15.  * Returns an array with countries
  16.  *
  17.  * @param int If set limits to a single country
  18.  * @param boolean If true adds the iso codes to the array
  19. */
  20.   function zen_get_countries($countries_id = '', $with_iso_codes = false) {
  21.     global $db;
  22.     $countries_array = array();
  23.     if (zen_not_null($countries_id)) {
  24.       if ($with_iso_codes == true) {
  25.         $countries = "select countries_name, countries_iso_code_2, countries_iso_code_3
  26.                      from " . TABLE_COUNTRIES . "
  27.                      where countries_id = '" . (int)$countries_id . "'
  28.                      order by countries_name";
  29.  
  30.         $countries_values = $db->Execute($countries);
  31.  
  32.         $countries_array = array('countries_name' => $countries_values->fields['countries_name'],
  33.                                  'countries_iso_code_2' => $countries_values->fields['countries_iso_code_2'],
  34.                                  'countries_iso_code_3' => $countries_values->fields['countries_iso_code_3']);
  35.       } else {
  36.         $countries = "select countries_name
  37.                      from " . TABLE_COUNTRIES . "
  38.                      where countries_id = '" . (int)$countries_id . "'";
  39.  
  40.         $countries_values = $db->Execute($countries);
  41.  
  42.         $countries_array = array('countries_name' => $countries_values->fields['countries_name']);
  43.       }
  44.     } else {
  45.       $countries = "select countries_id, countries_name
  46.                    from " . TABLE_COUNTRIES . "
  47.                    order by countries_name";
  48.  
  49.       $countries_values = $db->Execute($countries);
  50.  
  51.       while (!$countries_values->EOF) {
  52.         $countries_array[] = array('countries_id' => $countries_values->fields['countries_id'],
  53.                                    'countries_name' => $countries_values->fields['countries_name']);
  54.  
  55.         $countries_values->MoveNext();
  56.       }
  57.     }
  58.  
  59.     return $countries_array;
  60.   }
  61.  
  62. /*
  63.  *  Alias function to zen_get_countries()
  64.  */
  65.   function zen_get_country_name($country_id) {
  66.     $country_array = zen_get_countries($country_id);
  67.  
  68.     return $country_array['countries_name'];
  69.   }
  70.  
  71. /**
  72.  * Alias function to zen_get_countries, which also returns the countries iso codes
  73.  *
  74.  * @param int If set limits to a single country
  75. */
  76.   function zen_get_countries_with_iso_codes($countries_id) {
  77.     return zen_get_countries($countries_id, true);
  78.   }
  79.  
  80. /*
  81.  * Return the zone (State/Province) name
  82.  * TABLES: zones
  83.  */
  84.   function zen_get_zone_name($country_id, $zone_id, $default_zone) {
  85.     global $db;
  86.     $zone_query = "select zone_name
  87.                   from " . TABLE_ZONES . "
  88.                   where zone_country_id = '" . (int)$country_id . "'
  89.                   and zone_id = '" . (int)$zone_id . "'";
  90.  
  91.     $zone = $db->Execute($zone_query);
  92.  
  93.     if ($zone->RecordCount()) {
  94.       return $zone->fields['zone_name'];
  95.     } else {
  96.       return $default_zone;
  97.     }
  98.   }
  99.  
  100. /*
  101.  * Returns the zone (State/Province) code
  102.  * TABLES: zones
  103.  */
  104.   function zen_get_zone_code($country_id, $zone_id, $default_zone) {
  105.     global $db;
  106.     $zone_query = "select zone_code
  107.                   from " . TABLE_ZONES . "
  108.                   where zone_country_id = '" . (int)$country_id . "'
  109.                   and zone_id = '" . (int)$zone_id . "'";
  110.  
  111.     $zone = $db->Execute($zone_query);
  112.  
  113.     if ($zone->RecordCount() > 0) {
  114.       return $zone->fields['zone_code'];
  115.     } else {
  116.       return $default_zone;
  117.     }
  118.   }
  119.  
  120.  
  121. /*
  122.  *  validate products_id
  123.  */
  124.   function zen_products_id_valid($valid_id) {
  125.     global $db;
  126.     $check_valid = $db->Execute("select p.products_id
  127.                                 from " . TABLE_PRODUCTS . " p
  128.                                 where products_id='" . (int)$valid_id . "' limit 1");
  129.     if ($check_valid->EOF) {
  130.       return false;
  131.     } else {
  132.       return true;
  133.     }
  134.   }
  135.  
  136. /**
  137.  * Return a product's name.
  138.  *
  139.  * @param int The product id of the product who's name we want
  140.  * @param int The language id to use. If this is not set then the current language is used
  141. */
  142.   function zen_get_products_name($product_id, $language = '') {
  143.     global $db;
  144.  
  145.     if (empty($language)) $language = $_SESSION['languages_id'];
  146.  
  147.     $product_query = "select products_name
  148.                      from " . TABLE_PRODUCTS_DESCRIPTION . "
  149.                      where products_id = '" . (int)$product_id . "'
  150.                      and language_id = '" . (int)$language . "'";
  151.  
  152.     $product = $db->Execute($product_query);
  153.  
  154.     return $product->fields['products_name'];
  155.   }
  156.  
  157.  
  158. /**
  159.  * Return a product's stock count.
  160.  *
  161.  * @param int The product id of the product who's stock we want
  162. */
  163.   function zen_get_products_stock($products_id) {
  164.     global $db;
  165.     $products_id = zen_get_prid($products_id);
  166.     $stock_query = "select products_quantity
  167.                    from " . TABLE_PRODUCTS . "
  168.                    where products_id = '" . (int)$products_id . "'";
  169.  
  170.     $stock_values = $db->Execute($stock_query);
  171.  
  172.     return $stock_values->fields['products_quantity'];
  173.   }
  174.  
  175. /**
  176.  * Check if the required stock is available.
  177.  *
  178.  * If insufficent stock is available return an out of stock message
  179.  *
  180.  * @param int The product id of the product whos's stock is to be checked
  181.  * @param int Is this amount of stock available
  182.  *
  183.  * @TODO naughty html in a function
  184. */
  185.   function zen_check_stock($products_id, $products_quantity) {
  186.     $stock_left = zen_get_products_stock($products_id) - $products_quantity;
  187.     $out_of_stock = '';
  188.  
  189.     if ($stock_left < 0) {
  190.       $out_of_stock = '<span class="markProductOutOfStock">' . STOCK_MARK_PRODUCT_OUT_OF_STOCK . '</span>';
  191.     }
  192.  
  193.     return $out_of_stock;
  194.   }
  195.  
  196. /*
  197.  * List manufacturers (returned in an array)
  198.  */
  199.   function zen_get_manufacturers($manufacturers_array = '', $have_products = false) {
  200.     global $db;
  201.     if (!is_array($manufacturers_array)) $manufacturers_array = array();
  202.  
  203.     if ($have_products == true) {
  204.       $manufacturers_query = "select distinct m.manufacturers_id, m.manufacturers_name
  205.                              from " . TABLE_MANUFACTURERS . " m
  206.                              left join " . TABLE_PRODUCTS . " p on m.manufacturers_id = p.manufacturers_id
  207.                              where p.manufacturers_id = m.manufacturers_id
  208.                              and (p.products_status = 1
  209.                              and p.products_quantity > 0)
  210.                              order by m.manufacturers_name";
  211.     } else {
  212.       $manufacturers_query = "select manufacturers_id, manufacturers_name
  213.                              from " . TABLE_MANUFACTURERS . " order by manufacturers_name";
  214.     }
  215.  
  216.     $manufacturers = $db->Execute($manufacturers_query);
  217.  
  218.     while (!$manufacturers->EOF) {
  219.       $manufacturers_array[] = array('id' => $manufacturers->fields['manufacturers_id'], 'text' => $manufacturers->fields['manufacturers_name']);
  220.       $manufacturers->MoveNext();
  221.     }
  222.  
  223.     return $manufacturers_array;
  224.   }
  225.  
  226. /*
  227.  *  Check if product has attributes
  228.  */
  229.   function zen_has_product_attributes($products_id, $not_readonly = 'true') {
  230.     global $db;
  231.  
  232.     if (PRODUCTS_OPTIONS_TYPE_READONLY_IGNORED == '1' and $not_readonly == 'true') {
  233.       // don't include READONLY attributes to determin if attributes must be selected to add to cart
  234.       $attributes_query = "select pa.products_attributes_id
  235.                           from " . TABLE_PRODUCTS_ATTRIBUTES . " pa left join " . TABLE_PRODUCTS_OPTIONS . " po on pa.options_id = po.products_options_id
  236.                           where pa.products_id = '" . (int)$products_id . "' and po.products_options_type != '" . PRODUCTS_OPTIONS_TYPE_READONLY . "' limit 1";
  237.     } else {
  238.       // regardless of READONLY attributes no add to cart buttons
  239.       $attributes_query = "select pa.products_attributes_id
  240.                           from " . TABLE_PRODUCTS_ATTRIBUTES . " pa
  241.                           where pa.products_id = '" . (int)$products_id . "' limit 1";
  242.     }
  243.  
  244.     $attributes = $db->Execute($attributes_query);
  245.  
  246.     if ($attributes->recordCount() > 0 && $attributes->fields['products_attributes_id'] > 0) {
  247.       return true;
  248.     } else {
  249.       return false;
  250.     }
  251.   }
  252.  
  253. /*
  254.  *  Check if product has attributes values
  255.  */
  256.   function zen_has_product_attributes_values($products_id) {
  257.     global $db;
  258.     $attributes_query = "select sum(options_values_price) as total
  259.                         from " . TABLE_PRODUCTS_ATTRIBUTES . "
  260.                         where products_id = '" . (int)$products_id . "'";
  261.  
  262.     $attributes = $db->Execute($attributes_query);
  263.  
  264.     if ($attributes->fields['total'] != 0) {
  265.       return true;
  266.     } else {
  267.       return false;
  268.     }
  269.   }
  270.  
  271. /*
  272.  * Find category name from ID, in indicated language
  273.  */
  274.   function zen_get_category_name($category_id, $fn_language_id) {
  275.     global $db;
  276.     $category_query = "select categories_name
  277.                       from " . TABLE_CATEGORIES_DESCRIPTION . "
  278.                       where categories_id = '" . $category_id . "'
  279.                       and language_id = '" . $fn_language_id . "'";
  280.  
  281.     $category = $db->Execute($category_query);
  282.  
  283.     return $category->fields['categories_name'];
  284.   }
  285.  
  286.  
  287. /*
  288.  * Find category description, from category ID, in given language
  289.  */
  290.   function zen_get_category_description($category_id, $fn_language_id) {
  291.     global $db;
  292.     $category_query = "select categories_description
  293.                       from " . TABLE_CATEGORIES_DESCRIPTION . "
  294.                       where categories_id = '" . $category_id . "'
  295.                       and language_id = '" . $fn_language_id . "'";
  296.  
  297.     $category = $db->Execute($category_query);
  298.  
  299.     return $category->fields['categories_description'];
  300.   }
  301.  
  302. /*
  303.  * Return a product's category
  304.  * TABLES: products_to_categories
  305.  */
  306.   function zen_get_products_category_id($products_id) {
  307.     global $db;
  308.  
  309.     $the_products_category_query = "select products_id, master_categories_id from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'";
  310.     $the_products_category = $db->Execute($the_products_category_query);
  311.  
  312.     return $the_products_category->fields['master_categories_id'];
  313.   }
  314.  
  315.  
  316. /*
  317.  * Return category's image
  318.  * TABLES: categories
  319.  */
  320.   function zen_get_categories_image($what_am_i) {
  321.     global $db;
  322.  
  323.     $the_categories_image_query= "select categories_image from " . TABLE_CATEGORIES . " where categories_id= '" . $what_am_i . "'";
  324.     $the_products_category = $db->Execute($the_categories_image_query);
  325.  
  326.     return $the_products_category->fields['categories_image'];
  327.   }
  328.  
  329. /*
  330.  *  Return category's name from ID, assuming current language
  331.  *  TABLES: categories_description
  332.  */
  333.   function zen_get_categories_name($who_am_i) {
  334.     global $db;
  335.     $the_categories_name_query= "select categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id= '" . $who_am_i . "' and language_id= '" . $_SESSION['languages_id'] . "'";
  336.  
  337.     $the_categories_name = $db->Execute($the_categories_name_query);
  338.  
  339.     return $the_categories_name->fields['categories_name'];
  340.   }
  341.  
  342. /*
  343.  * Return a product's manufacturer's name, from ID
  344.  * TABLES: products, manufacturers
  345.  */
  346.   function zen_get_products_manufacturers_name($product_id) {
  347.     global $db;
  348.  
  349.     $product_query = "select m.manufacturers_name
  350.                      from " . TABLE_PRODUCTS . " p, " .
  351.                             TABLE_MANUFACTURERS . " m
  352.                      where p.products_id = '" . (int)$product_id . "'
  353.                      and p.manufacturers_id = m.manufacturers_id";
  354.  
  355.     $product =$db->Execute($product_query);
  356.  
  357.     return ($product->RecordCount() > 0) ? $product->fields['manufacturers_name'] : "";
  358.   }
  359.  
  360. /*
  361.  * Return a product's manufacturer's image, from Prod ID
  362.  * TABLES: products, manufacturers
  363.  */
  364.   function zen_get_products_manufacturers_image($product_id) {
  365.     global $db;
  366.  
  367.     $product_query = "select m.manufacturers_image
  368.                      from " . TABLE_PRODUCTS . " p, " .
  369.                             TABLE_MANUFACTURERS . " m
  370.                      where p.products_id = '" . (int)$product_id . "'
  371.                      and p.manufacturers_id = m.manufacturers_id";
  372.  
  373.     $product =$db->Execute($product_query);
  374.  
  375.     return $product->fields['manufacturers_image'];
  376.   }
  377.  
  378. /*
  379.  * Return a product's manufacturer's id, from Prod ID
  380.  * TABLES: products
  381.  */
  382.   function zen_get_products_manufacturers_id($product_id) {
  383.     global $db;
  384.  
  385.     $product_query = "select p.manufacturers_id
  386.                      from " . TABLE_PRODUCTS . " p
  387.                      where p.products_id = '" . (int)$product_id . "'";
  388.  
  389.     $product =$db->Execute($product_query);
  390.  
  391.     return $product->fields['manufacturers_id'];
  392.   }
  393.  
  394. /*
  395.  * Return attributes products_options_sort_order
  396.  * TABLE: PRODUCTS_ATTRIBUTES
  397.  */
  398.   function zen_get_attributes_sort_order($products_id, $options_id, $options_values_id) {
  399.     global $db;
  400.       $check = $db->Execute("select products_options_sort_order
  401.                             from " . TABLE_PRODUCTS_ATTRIBUTES . "
  402.                             where products_id = '" . (int)$products_id . "'
  403.                             and options_id = '" . (int)$options_id . "'
  404.                             and options_values_id = '" . (int)$options_values_id . "' limit 1");
  405.  
  406.       return $check->fields['products_options_sort_order'];
  407.   }
  408.  
  409. /*
  410.  *  return attributes products_options_sort_order
  411.  *  TABLES: PRODUCTS_OPTIONS, PRODUCTS_ATTRIBUTES
  412.  */
  413.   function zen_get_attributes_options_sort_order($products_id, $options_id, $options_values_id, $lang_num = '') {
  414.     global $db;
  415.       if ($lang_num == '') $lang_num = (int)$_SESSION['languages_id'];
  416.       $check = $db->Execute("select products_options_sort_order
  417.                             from " . TABLE_PRODUCTS_OPTIONS . "
  418.                             where products_options_id = '" . (int)$options_id . "' and language_id = '" . $lang_num . "' limit 1");
  419.  
  420.       $check_options_id = $db->Execute("select products_id, options_id, options_values_id, products_options_sort_order
  421.                             from " . TABLE_PRODUCTS_ATTRIBUTES . "
  422.                             where products_id='" . (int)$products_id . "'
  423.                             and options_id='" . (int)$options_id . "'
  424.                             and options_values_id = '" . (int)$options_values_id . "' limit 1");
  425.  
  426.  
  427.       return $check->fields['products_options_sort_order'] . '.' . str_pad($check_options_id->fields['products_options_sort_order'],5,'0',STR_PAD_LEFT);
  428.   }
  429.  
  430. /*
  431.  *  check if attribute is display only
  432.  */
  433.   function zen_get_attributes_valid($product_id, $option, $value) {
  434.     global $db;
  435.  
  436. // regular attribute validation
  437.     $check_attributes = $db->Execute("select attributes_display_only, attributes_required from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id='" . (int)$product_id . "' and options_id='" . (int)$option . "' and options_values_id='" . (int)$value . "'");
  438.  
  439.     $check_valid = true;
  440.  
  441. // display only cannot be selected
  442.     if ($check_attributes->fields['attributes_display_only'] == '1') {
  443.       $check_valid = false;
  444.     }
  445.  
  446. // text required validation
  447.     if (preg_match('/^txt_/', $option)) {
  448.       $check_attributes = $db->Execute("select attributes_display_only, attributes_required from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id='" . (int)$product_id . "' and options_id='" . (int)preg_replace('/txt_/', '', $option) . "' and options_values_id='0'");
  449. // text cannot be blank
  450.       if ($check_attributes->fields['attributes_required'] == '1' && (empty($value) && !is_numeric($value))) {
  451.         $check_valid = false;
  452.       }
  453.     }
  454.  
  455.     return $check_valid;
  456.   }
  457.  
  458. /*
  459.  * Return Options_Name from ID
  460.  */
  461.  
  462.   function zen_options_name($options_id) {
  463.     global $db;
  464.  
  465.     $options_id = str_replace('txt_','',$options_id);
  466.  
  467.     $options_values = $db->Execute("select products_options_name
  468.                                    from " . TABLE_PRODUCTS_OPTIONS . "
  469.                                    where products_options_id = '" . (int)$options_id . "'
  470.                                    and language_id = '" . (int)$_SESSION['languages_id'] . "'");
  471.  
  472.     return $options_values->fields['products_options_name'];
  473.   }
  474.  
  475. /*
  476.  * Return Options_values_name from value-ID
  477.  */
  478.   function zen_values_name($values_id) {
  479.     global $db;
  480.  
  481.     $values_values = $db->Execute("select products_options_values_name
  482.                                   from " . TABLE_PRODUCTS_OPTIONS_VALUES . "
  483.                                   where products_options_values_id = '" . (int)$values_id . "'
  484.                                   and language_id = '" . (int)$_SESSION['languages_id'] . "'");
  485.  
  486.     return $values_values->fields['products_options_values_name'];
  487.   }
  488.  
  489. /*
  490.  *  configuration key value lookup
  491.  *  TABLE: configuration
  492.  */
  493.   function zen_get_configuration_key_value($lookup) {
  494.     global $db;
  495.     $configuration_query= $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key='" . $lookup . "'");
  496.     $lookup_value= $configuration_query->fields['configuration_value'];
  497.     if ( !($lookup_value) ) {
  498.       $lookup_value='<span class="lookupAttention">' . $lookup . '</span>';
  499.     }
  500.     return $lookup_value;
  501.   }
  502.  
  503. /*
  504.  *  Return products description, based on specified language (or current lang if not specified)
  505.  */
  506.   function zen_get_products_description($product_id, $language = '') {
  507.     global $db;
  508.  
  509.     if (empty($language)) $language = $_SESSION['languages_id'];
  510.  
  511.     $product_query = "select products_description
  512.                      from " . TABLE_PRODUCTS_DESCRIPTION . "
  513.                      where products_id = '" . (int)$product_id . "'
  514.                      and language_id = '" . (int)$language . "'";
  515.  
  516.     $product = $db->Execute($product_query);
  517.  
  518.     return $product->fields['products_description'];
  519.   }
  520.  
  521. /*
  522.  * look up the product type from product_id and return an info page name (for template/page handling)
  523.  */
  524.   function zen_get_info_page($zf_product_id) {
  525.     global $db;
  526.     $sql = "select products_type from " . TABLE_PRODUCTS . " where products_id = '" . (int)$zf_product_id . "'";
  527.     $zp_type = $db->Execute($sql);
  528.     if ($zp_type->RecordCount() == 0) {
  529.       return 'product_info';
  530.     } else {
  531.       $zp_product_type = $zp_type->fields['products_type'];
  532.       $sql = "select type_handler from " . TABLE_PRODUCT_TYPES . " where type_id = '" . (int)$zp_product_type . "'";
  533.       $zp_handler = $db->Execute($sql);
  534.       return $zp_handler->fields['type_handler'] . '_info';
  535.     }
  536.   }
  537.  
  538. /*
  539.  * Get accepted credit cards
  540.  * There needs to be a define on the accepted credit card in the language file credit_cards.php example: TEXT_CC_ENABLED_VISA
  541.  */
  542.   function zen_get_cc_enabled($text_image = 'TEXT_', $cc_seperate = ' ', $cc_make_columns = 0) {
  543.     global $db;
  544.     $cc_check_accepted_query = $db->Execute(SQL_CC_ENABLED);
  545.     $cc_check_accepted = '';
  546.     $cc_counter = 0;
  547.     if ($cc_make_columns == 0) {
  548.       while (!$cc_check_accepted_query->EOF) {
  549.         $check_it = $text_image . $cc_check_accepted_query->fields['configuration_key'];
  550.         if (defined($check_it)) {
  551.           $cc_check_accepted .= constant($check_it) . $cc_seperate;
  552.         }
  553.         $cc_check_accepted_query->MoveNext();
  554.       }
  555.     } else {
  556.       // build a table
  557.       $cc_check_accepted = '<table class="ccenabled">' . "\n";
  558.       $cc_check_accepted .= '<tr class="ccenabled">' . "\n";
  559.       while (!$cc_check_accepted_query->EOF) {
  560.         $check_it = $text_image . $cc_check_accepted_query->fields['configuration_key'];
  561.         if (defined($check_it)) {
  562.           $cc_check_accepted .= '<td class="ccenabled">' . constant($check_it) . '</td>' . "\n";
  563.         }
  564.         $cc_check_accepted_query->MoveNext();
  565.         $cc_counter++;
  566.         if ($cc_counter >= $cc_make_columns) {
  567.           $cc_check_accepted .= '</tr>' . "\n" . '<tr class="ccenabled">' . "\n";
  568.           $cc_counter = 0;
  569.         }
  570.       }
  571.       $cc_check_accepted .= '</tr>' . "\n" . '</table>' . "\n";
  572.     }
  573.     return $cc_check_accepted;
  574.   }
  575.  
  576. ////
  577. // TABLES: categories_name from products_id
  578.   function zen_get_categories_name_from_product($product_id) {
  579.     global $db;
  580.  
  581. //    $check_products_category= $db->Execute("select products_id, categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id='" . $product_id . "' limit 1");
  582.     $check_products_category = $db->Execute("select products_id, master_categories_id from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
  583.     $the_categories_name= $db->Execute("select categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id= '" . $check_products_category->fields['master_categories_id'] . "' and language_id= '" . $_SESSION['languages_id'] . "'");
  584.  
  585.     return $the_categories_name->fields['categories_name'];
  586.   }
  587.  
  588.  
  589. /*
  590.  * configuration key value lookup in TABLE_PRODUCT_TYPE_LAYOUT
  591.  * Used to determine keys/flags used on a per-product-type basis for template-use, etc
  592.  */
  593.   function zen_get_configuration_key_value_layout($lookup, $type=1) {
  594.     global $db;
  595.     $configuration_query= $db->Execute("select configuration_value from " . TABLE_PRODUCT_TYPE_LAYOUT . " where configuration_key='" . $lookup . "' and product_type_id='". (int)$type . "'");
  596.     $lookup_value= $configuration_query->fields['configuration_value'];
  597.     if ( !($lookup_value) ) {
  598.       $lookup_value='<span class="lookupAttention">' . $lookup . '</span>';
  599.     }
  600.     return $lookup_value;
  601.   }
  602.  
  603. /*
  604.  * look up a products image and send back the image's HTML \<IMG...\> tag
  605.  */
  606.   function zen_get_products_image($product_id, $width = SMALL_IMAGE_WIDTH, $height = SMALL_IMAGE_HEIGHT) {
  607.     global $db;
  608.  
  609.     $sql = "select p.products_image from " . TABLE_PRODUCTS . " p  where products_id='" . (int)$product_id . "'";
  610.     $look_up = $db->Execute($sql);
  611.  
  612.     return zen_image(DIR_WS_IMAGES . $look_up->fields['products_image'], zen_get_products_name($product_id), $width, $height);
  613.   }
  614.  
  615. /*
  616.  * look up whether a product is virtual
  617.  */
  618.   function zen_get_products_virtual($lookup) {
  619.     global $db;
  620.  
  621.     $sql = "select p.products_virtual from " . TABLE_PRODUCTS . " p  where p.products_id='" . (int)$lookup . "'";
  622.     $look_up = $db->Execute($sql);
  623.  
  624.     if ($look_up->fields['products_virtual'] == '1') {
  625.       return true;
  626.     } else {
  627.       return false;
  628.     }
  629.   }
  630.  
  631. /*
  632.  * Look up whether the given product ID is allowed to be added to cart, according to product-type switches set in Admin
  633.  */
  634.   function zen_get_products_allow_add_to_cart($lookup) {
  635.     global $db;
  636.  
  637.     $sql = "select products_type from " . TABLE_PRODUCTS . " where products_id='" . (int)$lookup . "'";
  638.     $type_lookup = $db->Execute($sql);
  639.  
  640.     $sql = "select allow_add_to_cart from " . TABLE_PRODUCT_TYPES . " where type_id = '" . (int)$type_lookup->fields['products_type'] . "'";
  641.     $allow_add_to_cart = $db->Execute($sql);
  642.  
  643.     return $allow_add_to_cart->fields['allow_add_to_cart'];
  644.   }
  645.  
  646. /*
  647.  * Look up SHOW_XXX_INFO switch for product ID and product type
  648.  */
  649.     function zen_get_show_product_switch_name($lookup, $field, $suffix= 'SHOW_', $prefix= '_INFO', $field_prefix= '_', $field_suffix='') {
  650.       global $db;
  651.  
  652.       $sql = "select products_type from " . TABLE_PRODUCTS . " where products_id='" . (int)$lookup . "'";
  653.       $type_lookup = $db->Execute($sql);
  654.  
  655.       $sql = "select type_handler from " . TABLE_PRODUCT_TYPES . " where type_id = '" . (int)$type_lookup->fields['products_type'] . "'";
  656.       $show_key = $db->Execute($sql);
  657.  
  658. // BOM by zen-cart.cn
  659.       $zv_key = GBcase($suffix . $show_key->fields['type_handler'] . $prefix . $field_prefix . $field . $field_suffix,"upper");
  660. // EOM by zen-cart.cn
  661.  
  662.       return $zv_key;
  663.     }
  664.  
  665. /*
  666.  * Look up SHOW_XXX_INFO switch for product ID and product type
  667.  */
  668.     function zen_get_show_product_switch($lookup, $field, $suffix= 'SHOW_', $prefix= '_INFO', $field_prefix= '_', $field_suffix='') {
  669.       global $db;
  670.  
  671.       $sql = "select products_type from " . TABLE_PRODUCTS . " where products_id='" . $lookup . "'";
  672.       $type_lookup = $db->Execute($sql);
  673.  
  674.       $sql = "select type_handler from " . TABLE_PRODUCT_TYPES . " where type_id = '" . $type_lookup->fields['products_type'] . "'";
  675.       $show_key = $db->Execute($sql);
  676.  
  677. // BOM by zen-cart.cn
  678.       $zv_key = GBcase($suffix . $show_key->fields['type_handler'] . $prefix . $field_prefix . $field . $field_suffix,"upper");
  679. // EOM by zen-cart.cn
  680.  
  681.       $sql = "select configuration_key, configuration_value from " . TABLE_PRODUCT_TYPE_LAYOUT . " where configuration_key='" . $zv_key . "'";
  682.       $zv_key_value = $db->Execute($sql);
  683.       if ($zv_key_value->RecordCount() > 0) {
  684.         return $zv_key_value->fields['configuration_value'];
  685.       } else {
  686.         $sql = "select configuration_key, configuration_value from " . TABLE_CONFIGURATION . " where configuration_key='" . $zv_key . "'";
  687.         $zv_key_value = $db->Execute($sql);
  688.         if ($zv_key_value->RecordCount() > 0) {
  689.           return $zv_key_value->fields['configuration_value'];
  690.         } else {
  691.           return false;
  692.         }
  693.       }
  694.     }
  695.  
  696. /*
  697.  *  Look up whether a product is always free shipping
  698.  */
  699.   function zen_get_product_is_always_free_shipping($lookup) {
  700.     global $db;
  701.  
  702.     $sql = "select p.product_is_always_free_shipping from " . TABLE_PRODUCTS . " p  where p.products_id='" . (int)$lookup . "'";
  703.     $look_up = $db->Execute($sql);
  704.  
  705.     if ($look_up->fields['product_is_always_free_shipping'] == '1') {
  706.       return true;
  707.     } else {
  708.       return false;
  709.     }
  710.   }
  711.  
  712. /*
  713.  *  stop regular behavior based on customer/store settings
  714.  *  Used to disable various activities if store is in an operating mode that should prevent those activities
  715.  */
  716.   function zen_run_normal() {
  717.     $zc_run = false;
  718.     switch (true) {
  719.       case (strstr(EXCLUDE_ADMIN_IP_FOR_MAINTENANCE, $_SERVER['REMOTE_ADDR'])):
  720.       // down for maintenance not for ADMIN
  721.         $zc_run = true;
  722.         break;
  723.       case (DOWN_FOR_MAINTENANCE == 'true'):
  724.       // down for maintenance
  725.         $zc_run = false;
  726.         break;
  727.       case (STORE_STATUS >= 1):
  728.       // showcase no prices
  729.         $zc_run = false;
  730.         break;
  731.       case (CUSTOMERS_APPROVAL == '1' and $_SESSION['customer_id'] == ''):
  732.       // customer must be logged in to browse
  733.         $zc_run = false;
  734.         break;
  735.       case (CUSTOMERS_APPROVAL == '2' and $_SESSION['customer_id'] == ''):
  736.       // show room only
  737.       // customer may browse but no prices
  738.         $zc_run = false;
  739.         break;
  740.       case (CUSTOMERS_APPROVAL == '3'):
  741.       // show room only
  742.         $zc_run = false;
  743.         break;
  744.       case (CUSTOMERS_APPROVAL_AUTHORIZATION != '0' and $_SESSION['customer_id'] == ''):
  745.       // customer must be logged in to browse
  746.         $zc_run = false;
  747.         break;
  748.       case (CUSTOMERS_APPROVAL_AUTHORIZATION != '0' and $_SESSION['customers_authorization'] > '0'):
  749.       // customer must be logged in to browse
  750.         $zc_run = false;
  751.         break;
  752.       default:
  753.       // proceed normally
  754.         $zc_run = true;
  755.         break;
  756.     }
  757.     return $zc_run;
  758.   }
  759.  
  760. /*
  761.  *  Look up whether to show prices, based on customer-authorization levels
  762.  */
  763.   function zen_check_show_prices() {
  764.     if (!(CUSTOMERS_APPROVAL == '2' and $_SESSION['customer_id'] == '') and !((CUSTOMERS_APPROVAL_AUTHORIZATION > 0 and CUSTOMERS_APPROVAL_AUTHORIZATION < 3) and ($_SESSION['customers_authorization'] > '0' or $_SESSION['customer_id'] == '')) and STORE_STATUS != 1) {
  765.       return true;
  766.     } else {
  767.       return false;
  768.     }
  769.   }
  770.  
  771. /*
  772.  * Return any field from products or products_description table
  773.  * Example: zen_products_lookup('3', 'products_date_added');
  774.  */
  775.   function zen_products_lookup($product_id, $what_field = 'products_name', $language = '') {
  776.     global $db;
  777.  
  778.     if (empty($language)) $language = $_SESSION['languages_id'];
  779.  
  780.     $product_lookup = $db->Execute("select " . $what_field . " as lookup_field
  781.                              from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
  782.                              where p.products_id ='" . (int)$product_id . "'
  783.                              and pd.products_id = p.products_id
  784.                              and pd.language_id = '" . (int)$language . "'");
  785.  
  786.     $return_field = $product_lookup->fields['lookup_field'];
  787.  
  788.     return $return_field;
  789.   }
  790.  
  791. /*
  792.  * Return any field from categories or categories_description table
  793.  * Example: zen_categories_lookup('10', 'parent_id');
  794.  */
  795.   function zen_categories_lookup($categories_id, $what_field = 'categories_name', $language = '') {
  796.     global $db;
  797.  
  798.     if (empty($language)) $language = $_SESSION['languages_id'];
  799.  
  800.     $category_lookup = $db->Execute("select " . $what_field . " as lookup_field
  801.                              from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
  802.                              where c.categories_id ='" . (int)$categories_id . "'
  803.                              and c.categories_id = cd.categories_id
  804.                              and cd.language_id = '" . (int)$language . "'");
  805.  
  806.     $return_field = $category_lookup->fields['lookup_field'];
  807.  
  808.     return $return_field;
  809.   }
  810.  
  811. /*
  812.  * Find index_filters directory
  813.  * suitable for including template-specific immediate /modules files, such as:
  814.  * new_products, products_new_listing, featured_products, featured_products_listing, product_listing, specials_index, upcoming,
  815.  * products_all_listing, products_discount_prices, also_purchased_products
  816.  */
  817.   function zen_get_index_filters_directory($check_file, $dir_only = 'false') {
  818.     global $template_dir;
  819.     $zv_filename = $check_file;
  820.     if (!strstr($zv_filename, '.php')) $zv_filename .= '.php';
  821.     $checkArray = array();
  822.     $checkArray[] = DIR_WS_INCLUDES . 'index_filters/' . $template_dir . '/' . $zv_filename;
  823.     $checkArray[] = DIR_WS_INCLUDES . 'index_filters/' . $zv_filename;
  824.     $checkArray[] = DIR_WS_INCLUDES . 'index_filters/' . $template_dir . '/' . 'default_filter.php';
  825.     foreach($checkArray as $key => $val) {
  826.       if (file_exists($val)) {
  827.         return ($dir_only == 'true') ? $val = substr($val, 0, strpos($val, '/')) : $val;
  828.       }
  829.     }
  830.     return DIR_WS_INCLUDES . 'index_filters/' . 'default_filter.php';
  831.   }
  832.  
  833. ////
  834. // get define of New Products
  835.   function zen_get_products_new_timelimit($time_limit = false) {
  836.     if ($time_limit == false) {
  837.       $time_limit = SHOW_NEW_PRODUCTS_LIMIT;
  838.     }
  839.     switch (true) {
  840.       case ($time_limit == '0'):
  841.         $display_limit = '';
  842.         break;
  843.       case ($time_limit == '1'):
  844.         $display_limit = " and date_format(p.products_date_added, '%Y%m') >= date_format(now(), '%Y%m')";
  845.         break;
  846.       case ($time_limit == '7'):
  847.         $display_limit = ' and TO_DAYS(NOW()) - TO_DAYS(p.products_date_added) <= 7';
  848.         break;
  849.       case ($time_limit == '14'):
  850.         $display_limit = ' and TO_DAYS(NOW()) - TO_DAYS(p.products_date_added) <= 14';
  851.         break;
  852.       case ($time_limit == '30'):
  853.         $display_limit = ' and TO_DAYS(NOW()) - TO_DAYS(p.products_date_added) <= 30';
  854.         break;
  855.       case ($time_limit == '60'):
  856.         $display_limit = ' and TO_DAYS(NOW()) - TO_DAYS(p.products_date_added) <= 60';
  857.         break;
  858.       case ($time_limit == '90'):
  859.         $display_limit = ' and TO_DAYS(NOW()) - TO_DAYS(p.products_date_added) <= 90';
  860.         break;
  861.       case ($time_limit == '120'):
  862.         $display_limit = ' and TO_DAYS(NOW()) - TO_DAYS(p.products_date_added) <= 120';
  863.         break;
  864.     }
  865.     return $display_limit;
  866.   }
  867.  
  868. ////
  869. // check if Product is set to use downloads
  870. // does not validate download filename
  871.   function zen_has_product_attributes_downloads_status($products_id) {
  872.     global $db;
  873.     if (DOWNLOAD_ENABLED == 'true') {
  874.       $download_display_query_raw ="select pa.products_attributes_id, pad.products_attributes_filename
  875.                                    from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad
  876.                                    where pa.products_id='" . (int)$products_id . "'
  877.                                      and pad.products_attributes_id= pa.products_attributes_id";
  878.  
  879.       $download_display = $db->Execute($download_display_query_raw);
  880.       if ($download_display->RecordCount() != 0) {
  881.         $valid_downloads = false;
  882.       } else {
  883.         $valid_downloads = true;
  884.       }
  885.     } else {
  886.       $valid_downloads = false;
  887.     }
  888.     return $valid_downloads;
  889.   }
  890.  
  891. // build date range for new products
  892.   function zen_get_new_date_range($time_limit = false) {
  893.     if ($time_limit == false) {
  894.       $time_limit = SHOW_NEW_PRODUCTS_LIMIT;
  895.     }
  896.     // 120 days; 24 hours; 60 mins; 60secs
  897.     $date_range = time() - ($time_limit * 24 * 60 * 60);
  898.     $upcoming_mask_range = time();
  899.     $upcoming_mask = date('Ymd', $upcoming_mask_range);
  900.  
  901. // echo 'Now:      '. date('Y-m-d') ."<br />";
  902. // echo $time_limit . ' Days: '. date('Ymd', $date_range) ."<br />";
  903.     $zc_new_date = date('Ymd', $date_range);
  904.     switch (true) {
  905.     case (SHOW_NEW_PRODUCTS_LIMIT == 0):
  906.       $new_range = '';
  907.       break;
  908.     case (SHOW_NEW_PRODUCTS_LIMIT == 1):
  909.       $zc_new_date = date('Ym', time()) . '01';
  910.       $new_range = ' and p.products_date_added >=' . $zc_new_date;
  911.       break;
  912.     default:
  913.       $new_range = ' and p.products_date_added >=' . $zc_new_date;
  914.     }
  915.  
  916.     if (SHOW_NEW_PRODUCTS_UPCOMING_MASKED == 0) {
  917.       // do nothing upcoming shows in new
  918.     } else {
  919.       // do not include upcoming in new
  920.       $new_range .= " and (p.products_date_available <=" . $upcoming_mask . " or p.products_date_available IS NULL)";
  921.     }
  922.     return $new_range;
  923.   }
  924.  
  925.  
  926. // build date range for upcoming products
  927.   function zen_get_upcoming_date_range() {
  928.     // 120 days; 24 hours; 60 mins; 60secs
  929.     $date_range = time();
  930.     $zc_new_date = date('Ymd', $date_range);
  931. // need to check speed on this for larger sites
  932. //    $new_range = ' and date_format(p.products_date_available, \'%Y%m%d\') >' . $zc_new_date;
  933.     $new_range = ' and p.products_date_available >' . $zc_new_date . '235959';
  934.  
  935.     return $new_range;
  936.   }
  937.  
  938. ?>