[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.63 KiB
MD5: 7b1e889d885e21cdca61697e93d4de4e

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