ZenCart_Documentation  1.5.0
http://www.collinsharper.com
C:/xampp/htdocs/zen-cart/admin/includes/functions/general.php
Go to the documentation of this file.
00001 <?php
00010 
00011 // Redirect to another page or site
00012   function zen_redirect($url) {
00013     global $logger;
00014 
00015 // clean up URL before executing it
00016     while (strstr($url, '&&')) $url = str_replace('&&', '&', $url);
00017     while (strstr($url, '&amp;&amp;')) $url = str_replace('&amp;&amp;', '&amp;', $url);
00018     // header locates should not have the &amp; in the address it breaks things
00019     while (strstr($url, '&amp;')) $url = str_replace('&amp;', '&', $url);
00020 
00021     header('Location: ' . $url);
00022     session_write_close();
00023     if (STORE_PAGE_PARSE_TIME == 'true') {
00024       if (!is_object($logger)) $logger = new logger;
00025       $logger->timer_stop();
00026     }
00027     exit;
00028   }
00029 
00031 // Parse the data used in the html tags to ensure the tags will not break
00032   function zen_parse_input_field_data($data, $parse) {
00033     return strtr(trim($data), $parse);
00034   }
00035 
00036 
00037   function zen_output_string($string, $translate = false, $protected = false) {
00038     if ($protected == true) {
00039       return htmlspecialchars($string, ENT_COMPAT, CHARSET, FALSE);
00040     } else {
00041       if ($translate == false) {
00042         return zen_parse_input_field_data($string, array('"' => '&quot;'));
00043       } else {
00044         return zen_parse_input_field_data($string, $translate);
00045       }
00046     }
00047   }
00048 
00049 
00050   function zen_output_string_protected($string) {
00051     return zen_output_string($string, false, true);
00052   }
00053 
00054 
00055   function zen_sanitize_string($string) {
00056     $string = preg_replace('/ +/', ' ', $string);
00057 
00058     return preg_replace("/[<>]/", '_', $string);
00059   }
00060 
00061 
00062   function zen_customers_name($customers_id) {
00063     global $db;
00064     $customers_values = $db->Execute("select customers_firstname, customers_lastname
00065                                from " . TABLE_CUSTOMERS . "
00066                                where customers_id = '" . (int)$customers_id . "'");
00067 
00068     return $customers_values->fields['customers_firstname'] . ' ' . $customers_values->fields['customers_lastname'];
00069   }
00070 
00071 
00072   function zen_get_path($current_category_id = '') {
00073     global $cPath_array, $db;
00074 // set to 0 if Top Level
00075     if ($current_category_id == '') {
00076       if (empty($cPath_array)) {
00077         $cPath_new= '';
00078       } else {
00079         $cPath_new = implode('_', $cPath_array);
00080       }
00081     } else {
00082       if (sizeof($cPath_array) == 0) {
00083         $cPath_new = $current_category_id;
00084       } else {
00085         $cPath_new = '';
00086         $last_category = $db->Execute("select parent_id
00087                                        from " . TABLE_CATEGORIES . "
00088                                        where categories_id = '" . (int)$cPath_array[(sizeof($cPath_array)-1)] . "'");
00089 
00090         $current_category = $db->Execute("select parent_id
00091                                           from " . TABLE_CATEGORIES . "
00092                                            where categories_id = '" . (int)$current_category_id . "'");
00093 
00094         if ($last_category->fields['parent_id'] == $current_category->fields['parent_id']) {
00095           for ($i = 0, $n = sizeof($cPath_array) - 1; $i < $n; $i++) {
00096             $cPath_new .= '_' . $cPath_array[$i];
00097           }
00098         } else {
00099           for ($i = 0, $n = sizeof($cPath_array); $i < $n; $i++) {
00100             $cPath_new .= '_' . $cPath_array[$i];
00101           }
00102         }
00103 
00104         $cPath_new .= '_' . $current_category_id;
00105 
00106         if (substr($cPath_new, 0, 1) == '_') {
00107           $cPath_new = substr($cPath_new, 1);
00108         }
00109       }
00110     }
00111 
00112     return 'cPath=' . $cPath_new;
00113   }
00114 
00115 
00116   function zen_get_all_get_params($exclude_array = '') {
00117     global $_GET;
00118 
00119     if ($exclude_array == '') $exclude_array = array();
00120 
00121     $get_url = '';
00122 
00123     reset($_GET);
00124     while (list($key, $value) = each($_GET)) {
00125       if (($key != zen_session_name()) && ($key != 'error') && (!in_array($key, $exclude_array))) $get_url .= $key . '=' . $value . '&';
00126     }
00127 
00128     return $get_url;
00129   }
00130 
00131 
00132   function zen_date_long($raw_date) {
00133     if ( ($raw_date == '0001-01-01 00:00:00') || ($raw_date == '') ) return false;
00134 
00135     $year = (int)substr($raw_date, 0, 4);
00136     $month = (int)substr($raw_date, 5, 2);
00137     $day = (int)substr($raw_date, 8, 2);
00138     $hour = (int)substr($raw_date, 11, 2);
00139     $minute = (int)substr($raw_date, 14, 2);
00140     $second = (int)substr($raw_date, 17, 2);
00141 
00142     return strftime(DATE_FORMAT_LONG, mktime($hour, $minute, $second, $month, $day, $year));
00143   }
00144 
00145 
00147 // Output a raw date string in the selected locale date format
00148 // $raw_date needs to be in this format: YYYY-MM-DD HH:MM:SS
00149 // NOTE: Includes a workaround for dates before 01/01/1970 that fail on windows servers
00150   function zen_date_short($raw_date) {
00151     if ( ($raw_date == '0001-01-01 00:00:00') || ($raw_date == '') ) return false;
00152 
00153     $year = substr($raw_date, 0, 4);
00154     $month = (int)substr($raw_date, 5, 2);
00155     $day = (int)substr($raw_date, 8, 2);
00156     $hour = (int)substr($raw_date, 11, 2);
00157     $minute = (int)substr($raw_date, 14, 2);
00158     $second = (int)substr($raw_date, 17, 2);
00159 
00160 // error on 1969 only allows for leap year
00161     if ($year != 1969 && @date('Y', mktime($hour, $minute, $second, $month, $day, $year)) == $year) {
00162       return date(DATE_FORMAT, mktime($hour, $minute, $second, $month, $day, $year));
00163     } else {
00164       return preg_replace('/2037$/', $year, date(DATE_FORMAT, mktime($hour, $minute, $second, $month, $day, 2037)));
00165     }
00166 
00167   }
00168 
00169 
00170   function zen_datetime_short($raw_datetime) {
00171     if ( ($raw_datetime == '0001-01-01 00:00:00') || ($raw_datetime == '') ) return false;
00172 
00173     $year = (int)substr($raw_datetime, 0, 4);
00174     $month = (int)substr($raw_datetime, 5, 2);
00175     $day = (int)substr($raw_datetime, 8, 2);
00176     $hour = (int)substr($raw_datetime, 11, 2);
00177     $minute = (int)substr($raw_datetime, 14, 2);
00178     $second = (int)substr($raw_datetime, 17, 2);
00179 
00180     return strftime(DATE_TIME_FORMAT, mktime($hour, $minute, $second, $month, $day, $year));
00181   }
00182 
00183 
00184   function zen_get_category_tree($parent_id = '0', $spacing = '', $exclude = '', $category_tree_array = '', $include_itself = false, $category_has_products = false, $limit = false) {
00185     global $db;
00186 
00187     if ($limit) {
00188       $limit_count = " limit 1";
00189     } else {
00190       $limit_count = '';
00191     }
00192 
00193     if (!is_array($category_tree_array)) $category_tree_array = array();
00194     if ( (sizeof($category_tree_array) < 1) && ($exclude != '0') ) $category_tree_array[] = array('id' => '0', 'text' => TEXT_TOP);
00195 
00196     if ($include_itself) {
00197       $category = $db->Execute("select cd.categories_name
00198                                 from " . TABLE_CATEGORIES_DESCRIPTION . " cd
00199                                 where cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
00200                                 and cd.categories_id = '" . (int)$parent_id . "'");
00201 
00202       $category_tree_array[] = array('id' => $parent_id, 'text' => $category->fields['categories_name']);
00203     }
00204 
00205     $categories = $db->Execute("select c.categories_id, cd.categories_name, c.parent_id
00206                                 from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
00207                                 where c.categories_id = cd.categories_id
00208                                 and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
00209                                 and c.parent_id = '" . (int)$parent_id . "'
00210                                 order by c.sort_order, cd.categories_name");
00211 
00212     while (!$categories->EOF) {
00213       if ($category_has_products == true and zen_products_in_category_count($categories->fields['categories_id'], '', false, true) >= 1) {
00214         $mark = '*';
00215       } else {
00216         $mark = '&nbsp;&nbsp;';
00217       }
00218       if ($exclude != $categories->fields['categories_id']) $category_tree_array[] = array('id' => $categories->fields['categories_id'], 'text' => $spacing . $categories->fields['categories_name'] . $mark);
00219       $category_tree_array = zen_get_category_tree($categories->fields['categories_id'], $spacing . '&nbsp;&nbsp;&nbsp;', $exclude, $category_tree_array, '', $category_has_products);
00220       $categories->MoveNext();
00221     }
00222 
00223     return $category_tree_array;
00224   }
00225 
00226 
00228 // products with name, model and price pulldown
00229   function zen_draw_products_pull_down($name, $parameters = '', $exclude = '', $show_id = false, $set_selected = false, $show_model = false, $show_current_category = false) {
00230     global $currencies, $db, $current_category_id;
00231 
00232     if ($exclude == '') {
00233       $exclude = array();
00234     }
00235 
00236     $select_string = '<select name="' . $name . '"';
00237 
00238     if ($parameters) {
00239       $select_string .= ' ' . $parameters;
00240     }
00241 
00242     $select_string .= '>';
00243 
00244     if ($show_current_category) {
00245 // only show $current_categories_id
00246       $products = $db->Execute("select p.products_id, pd.products_name, p.products_price, p.products_model, ptc.categories_id
00247                                 from " . TABLE_PRODUCTS . " p
00248                                 left join " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc on ptc.products_id = p.products_id, " .
00249                                 TABLE_PRODUCTS_DESCRIPTION . " pd
00250                                 where p.products_id = pd.products_id
00251                                 and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
00252                                 and ptc.categories_id = '" . (int)$current_category_id . "'
00253                                 order by products_name");
00254     } else {
00255       $products = $db->Execute("select p.products_id, pd.products_name, p.products_price, p.products_model
00256                                 from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
00257                                 where p.products_id = pd.products_id
00258                                 and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
00259                                 order by products_name");
00260     }
00261 
00262     while (!$products->EOF) {
00263       if (!in_array($products->fields['products_id'], $exclude)) {
00264         $display_price = zen_get_products_base_price($products->fields['products_id']);
00265         $select_string .= '<option value="' . $products->fields['products_id'] . '"';
00266         if ($set_selected == $products->fields['products_id']) $select_string .= ' SELECTED';
00267         $select_string .= '>' . $products->fields['products_name'] . ' (' . $currencies->format($display_price) . ')' . ($show_model ? ' [' . $products->fields['products_model'] . '] ' : '') . ($show_id ? ' - ID# ' . $products->fields['products_id'] : '') . '</option>';
00268       }
00269       $products->MoveNext();
00270     }
00271 
00272     $select_string .= '</select>';
00273 
00274     return $select_string;
00275   }
00276 
00277 
00278   function zen_options_name($options_id) {
00279     global $db;
00280 
00281     $options_id = str_replace('txt_','',$options_id);
00282 
00283     $options_values = $db->Execute("select products_options_name
00284                                     from " . TABLE_PRODUCTS_OPTIONS . "
00285                                     where products_options_id = '" . (int)$options_id . "'
00286                                     and language_id = '" . (int)$_SESSION['languages_id'] . "'");
00287 
00288     return $options_values->fields['products_options_name'];
00289   }
00290 
00291 
00292   function zen_values_name($values_id) {
00293     global $db;
00294 
00295     $values_values = $db->Execute("select products_options_values_name
00296                                    from " . TABLE_PRODUCTS_OPTIONS_VALUES . "
00297                                    where products_options_values_id = '" . (int)$values_id . "'
00298                                    and language_id = '" . (int)$_SESSION['languages_id'] . "'");
00299 
00300     return $values_values->fields['products_options_values_name'];
00301   }
00302 
00303 
00304   function zen_info_image($image, $alt, $width = '', $height = '') {
00305     if (zen_not_null($image) && (file_exists(DIR_FS_CATALOG_IMAGES . $image)) ) {
00306       $image = zen_image(DIR_WS_CATALOG_IMAGES . $image, $alt, $width, $height);
00307     } else {
00308       $image = TEXT_IMAGE_NONEXISTENT;
00309     }
00310 
00311     return $image;
00312   }
00313 
00314 
00315   function zen_break_string($string, $len, $break_char = '-') {
00316     $l = 0;
00317     $output = '';
00318     for ($i=0, $n=strlen($string); $i<$n; $i++) {
00319       $char = substr($string, $i, 1);
00320       if ($char != ' ') {
00321         $l++;
00322       } else {
00323         $l = 0;
00324       }
00325       if ($l > $len) {
00326         $l = 1;
00327         $output .= $break_char;
00328       }
00329       $output .= $char;
00330     }
00331 
00332     return $output;
00333   }
00334 
00335 
00336   function zen_get_country_name($country_id) {
00337     global $db;
00338     $country = $db->Execute("select countries_name
00339                              from " . TABLE_COUNTRIES . "
00340                              where countries_id = '" . (int)$country_id . "'");
00341 
00342     if ($country->RecordCount() < 1) {
00343       return $country_id;
00344     } else {
00345       return $country->fields['countries_name'];
00346     }
00347   }
00348 
00349 
00350   function zen_get_country_name_cfg() {
00351     global $db;
00352     $country = $db->Execute("select countries_name
00353                              from " . TABLE_COUNTRIES . "
00354                              where countries_id = '" . (int)$country_id . "'");
00355 
00356     if ($country->RecordCount() < 1) {
00357       return $country_id;
00358     } else {
00359       return $country->fields['countries_name'];
00360     }
00361   }
00362 
00363 
00364   function zen_get_zone_name($country_id, $zone_id, $default_zone) {
00365     global $db;
00366     $zone = $db->Execute("select zone_name
00367                                 from " . TABLE_ZONES . "
00368                                 where zone_country_id = '" . (int)$country_id . "'
00369                                 and zone_id = '" . (int)$zone_id . "'");
00370 
00371     if ($zone->RecordCount() > 0) {
00372       return $zone->fields['zone_name'];
00373     } else {
00374       return $default_zone;
00375     }
00376   }
00377 
00378 
00379   function zen_not_null($value) {
00380     if (is_array($value)) {
00381       if (sizeof($value) > 0) {
00382         return true;
00383       } else {
00384         return false;
00385       }
00386     } elseif( is_a( $value, 'queryFactoryResult' ) ) {
00387       if (sizeof($value->result) > 0) {
00388         return true;
00389       } else {
00390         return false;
00391       }
00392     } else {
00393       if ( (is_string($value) || is_int($value)) && ($value != '') && ($value != 'NULL') && (strlen(trim($value)) > 0)) {
00394         return true;
00395       } else {
00396         return false;
00397       }
00398     }
00399   }
00400 
00401 
00402   function zen_browser_detect($component) {
00403 
00404     return stristr($_SERVER['HTTP_USER_AGENT'], $component);
00405   }
00406 
00407 
00408   function zen_tax_classes_pull_down($parameters, $selected = '') {
00409     global $db;
00410     $select_string = '<select ' . $parameters . '>';
00411     $classes = $db->Execute("select tax_class_id, tax_class_title
00412                              from " . TABLE_TAX_CLASS . "
00413                              order by tax_class_title");
00414 
00415     while (!$classes->EOF) {
00416       $select_string .= '<option value="' . $classes->fields['tax_class_id'] . '"';
00417       if ($selected == $classes->fields['tax_class_id']) $select_string .= ' SELECTED';
00418       $select_string .= '>' . $classes->fields['tax_class_title'] . '</option>';
00419       $classes->MoveNext();
00420     }
00421     $select_string .= '</select>';
00422 
00423     return $select_string;
00424   }
00425 
00426 
00427   function zen_geo_zones_pull_down($parameters, $selected = '') {
00428     global $db;
00429     $select_string = '<select ' . $parameters . '>';
00430     $zones = $db->Execute("select geo_zone_id, geo_zone_name
00431                                  from " . TABLE_GEO_ZONES . "
00432                                  order by geo_zone_name");
00433 
00434     while (!$zones->EOF) {
00435       $select_string .= '<option value="' . $zones->fields['geo_zone_id'] . '"';
00436       if ($selected == $zones->fields['geo_zone_id']) $select_string .= ' SELECTED';
00437       $select_string .= '>' . $zones->fields['geo_zone_name'] . '</option>';
00438       $zones->MoveNext();
00439     }
00440     $select_string .= '</select>';
00441 
00442     return $select_string;
00443   }
00444 
00445 
00446   function zen_get_geo_zone_name($geo_zone_id) {
00447     global $db;
00448     $zones = $db->Execute("select geo_zone_name
00449                            from " . TABLE_GEO_ZONES . "
00450                            where geo_zone_id = '" . (int)$geo_zone_id . "'");
00451 
00452     if ($zones->RecordCount() < 1) {
00453       $geo_zone_name = $geo_zone_id;
00454     } else {
00455       $geo_zone_name = $zones->fields['geo_zone_name'];
00456     }
00457 
00458     return $geo_zone_name;
00459   }
00460 
00461 
00462 // USED FROM functions_customers
00463 /*
00464   function zen_address_format($address_format_id, $address, $html, $boln, $eoln) {
00465     global $db;
00466     $address_format = $db->Execute("select address_format as format
00467                              from " . TABLE_ADDRESS_FORMAT . "
00468                              where address_format_id = '" . (int)$address_format_id . "'");
00469 
00470     $company = zen_output_string_protected($address['company']);
00471     if (isset($address['firstname']) && zen_not_null($address['firstname'])) {
00472       $firstname = zen_output_string_protected($address['firstname']);
00473       $lastname = zen_output_string_protected($address['lastname']);
00474     } elseif (isset($address['name']) && zen_not_null($address['name'])) {
00475       $firstname = zen_output_string_protected($address['name']);
00476       $lastname = '';
00477     } else {
00478       $firstname = '';
00479       $lastname = '';
00480     }
00481     $street = zen_output_string_protected($address['street_address']);
00482     $suburb = zen_output_string_protected($address['suburb']);
00483     $city = zen_output_string_protected($address['city']);
00484     $state = zen_output_string_protected($address['state']);
00485     if (isset($address['country_id']) && zen_not_null($address['country_id'])) {
00486       $country = zen_get_country_name($address['country_id']);
00487 
00488       if (isset($address['zone_id']) && zen_not_null($address['zone_id'])) {
00489         $state = zen_get_zone_code($address['country_id'], $address['zone_id'], $state);
00490       }
00491     } elseif (isset($address['country']) && zen_not_null($address['country'])) {
00492       $country = zen_output_string_protected($address['country']);
00493     } else {
00494       $country = '';
00495     }
00496     $postcode = zen_output_string_protected($address['postcode']);
00497     $zip = $postcode;
00498 
00499     if ($html) {
00500 // HTML Mode
00501       $HR = '<hr />';
00502       $hr = '<hr />';
00503       if ( ($boln == '') && ($eoln == "\n") ) { // Values not specified, use rational defaults
00504         $CR = '<br />';
00505         $cr = '<br />';
00506         $eoln = $cr;
00507       } else { // Use values supplied
00508         $CR = $eoln . $boln;
00509         $cr = $CR;
00510       }
00511     } else {
00512 // Text Mode
00513       $CR = $eoln;
00514       $cr = $CR;
00515       $HR = '----------------------------------------';
00516       $hr = '----------------------------------------';
00517     }
00518 
00519     $statecomma = '';
00520     $streets = $street;
00521     if ($suburb != '') $streets = $street . $cr . $suburb;
00522     if ($country == '') $country = zen_output_string_protected($address['country']);
00523     if ($state != '') $statecomma = $state . ', ';
00524 
00525     $fmt = $address_format->fields['format'];
00526     eval("\$address = \"$fmt\";");
00527 
00528     if ( (ACCOUNT_COMPANY == 'true') && (zen_not_null($company)) ) {
00529       $address = $company . $cr . $address;
00530     }
00531 
00532     return $address;
00533   }
00534 */
00535 
00537   //
00538   // Function    : zen_get_zone_code
00539   //
00540   // Arguments   : country_id           country code string
00541   //               zone_id              state/province zone_id
00542   //               default_zone         default string if zone==0
00543   //
00544   // Return      : state_prov_code   s  tate/province code
00545   //
00546   // Description : Function to retrieve the state/province code (as in FL for Florida etc)
00547   //
00549   function zen_get_zone_code($country_id, $zone_id, $default_zone) {
00550     global $db;
00551     $zone_query = "select zone_code
00552                    from " . TABLE_ZONES . "
00553                    where zone_country_id = '" . (int)$country_id . "'
00554                    and zone_id = '" . (int)$zone_id . "'";
00555 
00556     $zone = $db->Execute($zone_query);
00557 
00558     if ($zone->RecordCount() > 0) {
00559       return $zone->fields['zone_code'];
00560     } else {
00561       return $default_zone;
00562     }
00563   }
00564 
00565   function zen_get_uprid($prid, $params) {
00566     $uprid = $prid;
00567     if ( (is_array($params)) && (!strstr($prid, '{')) ) {
00568       while (list($option, $value) = each($params)) {
00569         $uprid = $uprid . '{' . $option . '}' . $value;
00570       }
00571     }
00572 
00573     return $uprid;
00574   }
00575 
00576 
00577   function zen_get_prid($uprid) {
00578     $pieces = explode('{', $uprid);
00579 
00580     return $pieces[0];
00581   }
00582 
00583 
00584   function zen_get_languages() {
00585     global $db;
00586     $languages = $db->Execute("select languages_id, name, code, image, directory
00587                                from " . TABLE_LANGUAGES . " order by sort_order");
00588 
00589     while (!$languages->EOF) {
00590       $languages_array[] = array('id' => $languages->fields['languages_id'],
00591                                  'name' => $languages->fields['name'],
00592                                  'code' => $languages->fields['code'],
00593                                  'image' => $languages->fields['image'],
00594                                  'directory' => $languages->fields['directory']);
00595       $languages->MoveNext();
00596     }
00597 
00598     return $languages_array;
00599   }
00600 
00601 
00602   function zen_get_category_name($category_id, $language_id) {
00603     global $db;
00604     $category = $db->Execute("select categories_name
00605                               from " . TABLE_CATEGORIES_DESCRIPTION . "
00606                               where categories_id = '" . (int)$category_id . "'
00607                               and language_id = '" . (int)$language_id . "'");
00608 
00609     return $category->fields['categories_name'];
00610   }
00611 
00612 
00613   function zen_get_category_description($category_id, $language_id) {
00614     global $db;
00615     $category = $db->Execute("select categories_description
00616                               from " . TABLE_CATEGORIES_DESCRIPTION . "
00617                               where categories_id = '" . (int)$category_id . "'
00618                               and language_id = '" . (int)$language_id . "'");
00619 
00620     return $category->fields['categories_description'];
00621   }
00622 
00623 
00624   function zen_get_orders_status_name($orders_status_id, $language_id = '') {
00625     global $db;
00626 
00627     if (!$language_id) $language_id = $_SESSION['languages_id'];
00628     $orders_status = $db->Execute("select orders_status_name
00629                                    from " . TABLE_ORDERS_STATUS . "
00630                                    where orders_status_id = '" . (int)$orders_status_id . "'
00631                                    and language_id = '" . (int)$language_id . "'");
00632 
00633     return $orders_status->fields['orders_status_name'];
00634   }
00635 
00636 
00637   function zen_get_orders_status() {
00638     global $db;
00639 
00640     $orders_status_array = array();
00641     $orders_status = $db->Execute("select orders_status_id, orders_status_name
00642                                    from " . TABLE_ORDERS_STATUS . "
00643                                    where language_id = '" . (int)$_SESSION['languages_id'] . "'
00644                                    order by orders_status_id");
00645 
00646     while (!$orders_status->EOF) {
00647       $orders_status_array[] = array('id' => $orders_status->fields['orders_status_id'],
00648                                      'text' => $orders_status->fields['orders_status_name']);
00649       $orders_status->MoveNext();
00650     }
00651 
00652     return $orders_status_array;
00653   }
00654 
00655 
00656   function zen_get_products_name($product_id, $language_id = 0) {
00657     global $db;
00658 
00659     if ($language_id == 0) $language_id = $_SESSION['languages_id'];
00660     $product = $db->Execute("select products_name
00661                              from " . TABLE_PRODUCTS_DESCRIPTION . "
00662                              where products_id = '" . (int)$product_id . "'
00663                              and language_id = '" . (int)$language_id . "'");
00664 
00665     return $product->fields['products_name'];
00666   }
00667 
00668 
00669   function zen_get_products_description($product_id, $language_id) {
00670     global $db;
00671     $product = $db->Execute("select products_description
00672                              from " . TABLE_PRODUCTS_DESCRIPTION . "
00673                              where products_id = '" . (int)$product_id . "'
00674                              and language_id = '" . (int)$language_id . "'");
00675 
00676     return $product->fields['products_description'];
00677   }
00678 
00679 
00680   function zen_get_products_url($product_id, $language_id) {
00681     global $db;
00682     $product = $db->Execute("select products_url
00683                              from " . TABLE_PRODUCTS_DESCRIPTION . "
00684                              where products_id = '" . (int)$product_id . "'
00685                              and language_id = '" . (int)$language_id . "'");
00686 
00687     return $product->fields['products_url'];
00688   }
00689 
00690 
00692 // Return the manufacturers URL in the needed language
00693 // TABLES: manufacturers_info
00694   function zen_get_manufacturer_url($manufacturer_id, $language_id) {
00695     global $db;
00696     $manufacturer = $db->Execute("select manufacturers_url
00697                                   from " . TABLE_MANUFACTURERS_INFO . "
00698                                   where manufacturers_id = '" . (int)$manufacturer_id . "'
00699                                   and languages_id = '" . (int)$language_id . "'");
00700 
00701     return $manufacturer->fields['manufacturers_url'];
00702   }
00703 
00704 
00706 // Wrapper for class_exists() function
00707 // This function is not available in all PHP versions so we test it before using it.
00708   function zen_class_exists($class_name) {
00709     if (function_exists('class_exists')) {
00710       return class_exists($class_name);
00711     } else {
00712       return true;
00713     }
00714   }
00715 
00716 
00718 // Count how many products exist in a category
00719 // TABLES: products, products_to_categories, categories
00720   function zen_products_in_category_count($categories_id, $include_deactivated = false, $include_child = true, $limit = false) {
00721     global $db;
00722     $products_count = 0;
00723 
00724     if ($limit) {
00725       $limit_count = ' limit 1';
00726     } else {
00727       $limit_count = '';
00728     }
00729 
00730     if ($include_deactivated) {
00731 
00732       $products = $db->Execute("select count(*) as total
00733                                 from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
00734                                 where p.products_id = p2c.products_id
00735                                 and p2c.categories_id = '" . (int)$categories_id . "'" . $limit_count);
00736     } else {
00737       $products = $db->Execute("select count(*) as total
00738                                 from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
00739                                 where p.products_id = p2c.products_id
00740                                 and p.products_status = 1
00741                                 and p2c.categories_id = '" . (int)$categories_id . "'" . $limit_count);
00742 
00743     }
00744 
00745     $products_count += $products->fields['total'];
00746 
00747     if ($include_child) {
00748       $childs = $db->Execute("select categories_id from " . TABLE_CATEGORIES . "
00749                               where parent_id = '" . (int)$categories_id . "'");
00750       if ($childs->RecordCount() > 0 ) {
00751         while (!$childs->EOF) {
00752           $products_count += zen_products_in_category_count($childs->fields['categories_id'], $include_deactivated);
00753           $childs->MoveNext();
00754         }
00755       }
00756     }
00757     return $products_count;
00758   }
00759 
00760 
00762 // Count how many subcategories exist in a category
00763 // TABLES: categories
00764   function zen_childs_in_category_count($categories_id) {
00765     global $db;
00766     $categories_count = 0;
00767 
00768     $categories = $db->Execute("select categories_id
00769                                 from " . TABLE_CATEGORIES . "
00770                                 where parent_id = '" . (int)$categories_id . "'");
00771 
00772     while (!$categories->EOF) {
00773       $categories_count++;
00774       $categories_count += zen_childs_in_category_count($categories->fields['categories_id']);
00775       $categories->MoveNext();
00776     }
00777 
00778     return $categories_count;
00779   }
00780 
00781 
00783 // Returns an array with countries
00784 // TABLES: countries
00785   function zen_get_countries($default = '') {
00786     global $db;
00787     $countries_array = array();
00788     if ($default) {
00789       $countries_array[] = array('id' => '',
00790                                  'text' => $default);
00791     }
00792     $countries = $db->Execute("select countries_id, countries_name
00793                                from " . TABLE_COUNTRIES . "
00794                                order by countries_name");
00795 
00796     while (!$countries->EOF) {
00797       $countries_array[] = array('id' => $countries->fields['countries_id'],
00798                                  'text' => $countries->fields['countries_name']);
00799       $countries->MoveNext();
00800     }
00801 
00802     return $countries_array;
00803   }
00804 
00805 
00807 // return an array with country zones
00808   function zen_get_country_zones($country_id) {
00809     global $db;
00810     $zones_array = array();
00811     $zones = $db->Execute("select zone_id, zone_name
00812                            from " . TABLE_ZONES . "
00813                            where zone_country_id = '" . (int)$country_id . "'
00814                            order by zone_name");
00815 
00816     while (!$zones->EOF) {
00817       $zones_array[] = array('id' => $zones->fields['zone_id'],
00818                              'text' => $zones->fields['zone_name']);
00819       $zones->MoveNext();
00820     }
00821 
00822     return $zones_array;
00823   }
00824 
00825 
00826   function zen_prepare_country_zones_pull_down($country_id = '') {
00827 // preset the width of the drop-down for Netscape
00828     $pre = '';
00829     if ( (!zen_browser_detect('MSIE')) && (zen_browser_detect('Mozilla/4')) ) {
00830       for ($i=0; $i<45; $i++) $pre .= '&nbsp;';
00831     }
00832 
00833     $zones = zen_get_country_zones($country_id);
00834 
00835     if (sizeof($zones) > 0) {
00836       $zones_select = array(array('id' => '', 'text' => PLEASE_SELECT));
00837       $zones = array_merge($zones_select, $zones);
00838     } else {
00839       $zones = array(array('id' => '', 'text' => TYPE_BELOW));
00840 // create dummy options for Netscape to preset the height of the drop-down
00841       if ( (!zen_browser_detect('MSIE')) && (zen_browser_detect('Mozilla/4')) ) {
00842         for ($i=0; $i<9; $i++) {
00843           $zones[] = array('id' => '', 'text' => $pre);
00844         }
00845       }
00846     }
00847 
00848     return $zones;
00849   }
00850 
00851 
00853 // Get list of address_format_id's
00854   function zen_get_address_formats() {
00855     global $db;
00856     $address_format_values = $db->Execute("select address_format_id
00857                                            from " . TABLE_ADDRESS_FORMAT . "
00858                                            order by address_format_id");
00859 
00860     $address_format_array = array();
00861     while (!$address_format_values->EOF) {
00862       $address_format_array[] = array('id' => $address_format_values->fields['address_format_id'],
00863                                       'text' => $address_format_values->fields['address_format_id']);
00864       $address_format_values->MoveNext();
00865     }
00866     return $address_format_array;
00867   }
00868 
00869 
00871   function zen_cfg_select_coupon_id($coupon_id, $key = '') {
00872     global $db;
00873     $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
00874     $coupons = $db->execute("select cd.coupon_name, c.coupon_id from " . TABLE_COUPONS ." c, ". TABLE_COUPONS_DESCRIPTION . " cd where cd.coupon_id = c.coupon_id and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'");
00875     $coupon_array[] = array('id' => '0',
00876                             'text' => 'None');
00877 
00878     while (!$coupons->EOF) {
00879       $coupon_array[] = array('id' => $coupons->fields['coupon_id'],
00880                               'text' => $coupons->fields['coupon_name']);
00881       $coupons->MoveNext();
00882     }
00883     return zen_draw_pull_down_menu($name, $coupon_array, $coupon_id);
00884   }
00885 
00886 
00888 // Alias function for Store configuration values in the Administration Tool
00889   function zen_cfg_pull_down_country_list($country_id, $key = '') {
00890     $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
00891     return zen_draw_pull_down_menu($name, zen_get_countries(), $country_id);
00892   }
00893 
00894 
00896   function zen_cfg_pull_down_country_list_none($country_id, $key = '') {
00897     $country_array = zen_get_countries('None');
00898     $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
00899     return zen_draw_pull_down_menu($name, $country_array, $country_id);
00900   }
00901 
00902 
00904   function zen_cfg_pull_down_zone_list($zone_id, $key = '') {
00905     $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
00906     return zen_draw_pull_down_menu($name, zen_get_country_zones(STORE_COUNTRY), $zone_id);
00907   }
00908 
00909 
00911   function zen_cfg_pull_down_tax_classes($tax_class_id, $key = '') {
00912     global $db;
00913     $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
00914 
00915     $tax_class_array = array(array('id' => '0', 'text' => TEXT_NONE));
00916     $tax_class = $db->Execute("select tax_class_id, tax_class_title
00917                                from " . TABLE_TAX_CLASS . "
00918                                order by tax_class_title");
00919 
00920     while (!$tax_class->EOF) {
00921       $tax_class_array[] = array('id' => $tax_class->fields['tax_class_id'],
00922                                  'text' => $tax_class->fields['tax_class_title']);
00923       $tax_class->MoveNext();
00924     }
00925 
00926     return zen_draw_pull_down_menu($name, $tax_class_array, $tax_class_id);
00927   }
00928 
00929 
00931 // Function to read in text area in admin
00932  function zen_cfg_textarea($text, $key = '') {
00933     $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
00934     return zen_draw_textarea_field($name, false, 60, 5, htmlspecialchars($text, ENT_COMPAT, CHARSET, TRUE));
00935   }
00936 
00937 
00939 // Function to read in text area in admin
00940  function zen_cfg_textarea_small($text, $key = '') {
00941     $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
00942     return zen_draw_textarea_field($name, false, 35, 1, htmlspecialchars($text, ENT_COMPAT, CHARSET, TRUE));
00943   }
00944 
00945 
00946   function zen_cfg_get_zone_name($zone_id) {
00947     global $db;
00948     $zone = $db->Execute("select zone_name
00949                           from " . TABLE_ZONES . "
00950                           where zone_id = '" . (int)$zone_id . "'");
00951 
00952     if ($zone->RecordCount() < 1) {
00953       return $zone_id;
00954     } else {
00955       return $zone->fields['zone_name'];
00956     }
00957   }
00958 
00959   function zen_cfg_pull_down_htmleditors($html_editor, $key = '') {
00960     global $editors_list;
00961     $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
00962 
00963     $editors_pulldown = array();
00964     foreach($editors_list as $key=>$value) {
00965       $editors_pulldown[] = array('id' => $key, 'text' => $value['desc']);
00966     }
00967     return zen_draw_pull_down_menu($name, $editors_pulldown, $html_editor);
00968   }
00969 
00970   function zen_cfg_password_input($value, $key = '') {
00971     return zen_draw_password_field('configuration[' . $key . ']', $value);
00972   }
00973 
00974   function zen_cfg_password_display($value) {
00975     $length = strlen($value);
00976     return str_repeat('*', ($length > 16 ? 16 : $length));
00977   }
00978 
00980 // Sets the status of a product
00981   function zen_set_product_status($products_id, $status) {
00982     global $db;
00983     if ($status == '1') {
00984       return $db->Execute("update " . TABLE_PRODUCTS . "
00985                            set products_status = 1, products_last_modified = now()
00986                            where products_id = '" . (int)$products_id . "'");
00987 
00988     } elseif ($status == '0') {
00989       return $db->Execute("update " . TABLE_PRODUCTS . "
00990                            set products_status = 0, products_last_modified = now()
00991                            where products_id = '" . (int)$products_id . "'");
00992 
00993     } else {
00994       return -1;
00995     }
00996   }
00997 
00998 
01000 // Sets timeout for the current script.
01001 // Cant be used in safe mode.
01002   function zen_set_time_limit($limit) {
01003     if (version_compare(PHP_VERSION, 5.4, '>=') || !get_cfg_var('safe_mode')) {
01004       @set_time_limit($limit);
01005     }
01006   }
01007 
01008 
01010 // Alias function for Store configuration values in the Administration Tool
01011   function zen_cfg_select_option($select_array, $key_value, $key = '') {
01012     $string = '';
01013 
01014     for ($i=0, $n=sizeof($select_array); $i<$n; $i++) {
01015       $name = ((zen_not_null($key)) ? 'configuration[' . $key . ']' : 'configuration_value');
01016 
01017       $string .= '<br><input type="radio" name="' . $name . '" value="' . $select_array[$i] . '"';
01018 
01019       if ($key_value == $select_array[$i]) $string .= ' CHECKED';
01020 
01021       $string .= ' id="' . strtolower($select_array[$i] . '-' . $name) . '"> ' . '<label for="' . strtolower($select_array[$i] . '-' . $name) . '" class="inputSelect">' . $select_array[$i] . '</label>';
01022     }
01023 
01024     return $string;
01025   }
01026 
01027 
01028   function zen_cfg_select_drop_down($select_array, $key_value, $key = '') {
01029     $string = '';
01030 
01031     $name = ((zen_not_null($key)) ? 'configuration[' . $key . ']' : 'configuration_value');
01032     return zen_draw_pull_down_menu($name, $select_array, (int)$key_value);
01033   }
01034 
01036 // Alias function for module configuration keys
01037   function zen_mod_select_option($select_array, $key_name, $key_value) {
01038     reset($select_array);
01039     while (list($key, $value) = each($select_array)) {
01040       if (is_int($key)) $key = $value;
01041       $string .= '<br><input type="radio" name="configuration[' . $key_name . ']" value="' . $key . '"';
01042       if ($key_value == $key) $string .= ' CHECKED';
01043       $string .= '> ' . $value;
01044     }
01045 
01046     return $string;
01047   }
01048 
01050 // Retreive server information
01051   function zen_get_system_information() {
01052     global $db;
01053 
01054     // determine database size stats
01055     $indsize = 0;
01056     $datsize = 0;
01057     $result = $db->Execute("SHOW TABLE STATUS" . (DB_PREFIX == '' ? '' : " LIKE '" . str_replace('_', '\_', DB_PREFIX) . "%'"));
01058     while (!$result->EOF) {
01059       $datsize += $result->fields['Data_length'];
01060       $indsize += $result->fields['Index_length'];
01061       $result->MoveNext();
01062     }
01063     $mysql_in_strict_mode = false;
01064     $result = $db->Execute("SHOW VARIABLES LIKE 'sql\_mode'");
01065     while (!$result->EOF) {
01066       if (strstr($result->fields['Value'], 'strict_')) $mysql_in_strict_mode = true;
01067       $result->MoveNext();
01068     }
01069 
01070     $db_query = $db->Execute("select now() as datetime");
01071 
01072     $errnum = 0;
01073     $system = $host = $kernel = $output = '';
01074     list($system, $host, $kernel) = array('', $_SERVER['SERVER_NAME'], php_uname());
01075     $uptime = (DISPLAY_SERVER_UPTIME == 'true') ? 'Unsupported' : 'Disabled/Unavailable';
01076 
01077     // check to see if "exec()" is disabled in PHP -- if not, get additional info via command line
01078     $php_disabled_functions = '';
01079     $exec_disabled = false;
01080     $php_disabled_functions = @ini_get("disable_functions");
01081     if ($php_disabled_functions != '') {
01082       if (in_array('exec', preg_split('/,/', str_replace(' ', '', $php_disabled_functions)))) {
01083         $exec_disabled = true;
01084       }
01085     }
01086     if (!$exec_disabled) {
01087       @exec('uname -a 2>&1', $output, $errnum);
01088       if ($errnum == 0 && sizeof($output)) list($system, $host, $kernel) = preg_split('/[\s,]+/', $output[0], 5);
01089       $output = '';
01090       if (DISPLAY_SERVER_UPTIME == 'true') {
01091         @exec('uptime 2>&1', $output, $errnum);
01092         if ($errnum == 0) {
01093           $uptime = $output[0];
01094         }
01095       }
01096     }
01097 
01098     return array('date' => zen_datetime_short(date('Y-m-d H:i:s')),
01099                  'system' => $system,
01100                  'kernel' => $kernel,
01101                  'host' => $host,
01102                  'ip' => gethostbyname($host),
01103                  'uptime' => $uptime,
01104                  'http_server' => $_SERVER['SERVER_SOFTWARE'],
01105                  'php' => PHP_VERSION,
01106                  'zend' => (function_exists('zend_version') ? zend_version() : ''),
01107                  'db_server' => DB_SERVER,
01108                  'db_ip' => gethostbyname(DB_SERVER),
01109                  'db_version' => 'MySQL ' . (function_exists('mysql_get_server_info') ? mysql_get_server_info() : ''),
01110                  'db_date' => zen_datetime_short($db_query->fields['datetime']),
01111                  'php_memlimit' => @ini_get('memory_limit'),
01112                  'php_safemode' => version_compare(PHP_VERSION, 5.4, '<') ? strtolower(@ini_get('safe_mode')) : '',
01113                  'php_file_uploads' => strtolower(@ini_get('file_uploads')),
01114                  'php_uploadmaxsize' => @ini_get('upload_max_filesize'),
01115                  'php_postmaxsize' => @ini_get('post_max_size'),
01116                  'database_size' => $datsize,
01117                  'index_size' => $indsize,
01118                  'mysql_strict_mode' => $mysql_in_strict_mode,
01119                  );
01120   }
01121 
01122   function zen_generate_category_path($id, $from = 'category', $categories_array = '', $index = 0) {
01123     global $db;
01124 
01125     if (!is_array($categories_array)) $categories_array = array();
01126 
01127     if ($from == 'product') {
01128       $categories = $db->Execute("select categories_id
01129                                   from " . TABLE_PRODUCTS_TO_CATEGORIES . "
01130                                   where products_id = '" . (int)$id . "'");
01131 
01132       while (!$categories->EOF) {
01133         if ($categories->fields['categories_id'] == '0') {
01134           $categories_array[$index][] = array('id' => '0', 'text' => TEXT_TOP);
01135         } else {
01136           $category = $db->Execute("select cd.categories_name, c.parent_id
01137                                     from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
01138                                     where c.categories_id = '" . (int)$categories->fields['categories_id'] . "'
01139                                     and c.categories_id = cd.categories_id
01140                                     and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'");
01141 
01142           $categories_array[$index][] = array('id' => $categories->fields['categories_id'], 'text' => $category->fields['categories_name']);
01143           if ( (zen_not_null($category->fields['parent_id'])) && ($category->fields['parent_id'] != '0') ) $categories_array = zen_generate_category_path($category->fields['parent_id'], 'category', $categories_array, $index);
01144           $categories_array[$index] = array_reverse($categories_array[$index]);
01145         }
01146         $index++;
01147         $categories->MoveNext();
01148       }
01149     } elseif ($from == 'category') {
01150       $category = $db->Execute("select cd.categories_name, c.parent_id
01151                                 from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
01152                                 where c.categories_id = '" . (int)$id . "'
01153                                 and c.categories_id = cd.categories_id
01154                                 and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'");
01155       if (!$category->EOF) {
01156         $categories_array[$index][] = array('id' => $id, 'text' => $category->fields['categories_name']);
01157         if ( (zen_not_null($category->fields['parent_id'])) && ($category->fields['parent_id'] != '0') ) $categories_array = zen_generate_category_path($category->fields['parent_id'], 'category', $categories_array, $index);
01158       }
01159     }
01160 
01161     return $categories_array;
01162   }
01163 
01164   function zen_output_generated_category_path($id, $from = 'category') {
01165     $calculated_category_path_string = '';
01166     $calculated_category_path = zen_generate_category_path($id, $from);
01167     for ($i=0, $n=sizeof($calculated_category_path); $i<$n; $i++) {
01168       for ($j=0, $k=sizeof($calculated_category_path[$i]); $j<$k; $j++) {
01169 //        $calculated_category_path_string .= $calculated_category_path[$i][$j]['text'] . '&nbsp;&gt;&nbsp;';
01170         $calculated_category_path_string = $calculated_category_path[$i][$j]['text'] . '&nbsp;&gt;&nbsp;' . $calculated_category_path_string;
01171       }
01172       $calculated_category_path_string = substr($calculated_category_path_string, 0, -16) . '<br>';
01173     }
01174     $calculated_category_path_string = substr($calculated_category_path_string, 0, -4);
01175 
01176     if (strlen($calculated_category_path_string) < 1) $calculated_category_path_string = TEXT_TOP;
01177 
01178     return $calculated_category_path_string;
01179   }
01180 
01181   function zen_get_generated_category_path_ids($id, $from = 'category') {
01182     global $db;
01183     $calculated_category_path_string = '';
01184     $calculated_category_path = zen_generate_category_path($id, $from);
01185     for ($i=0, $n=sizeof($calculated_category_path); $i<$n; $i++) {
01186       for ($j=0, $k=sizeof($calculated_category_path[$i]); $j<$k; $j++) {
01187         $calculated_category_path_string .= $calculated_category_path[$i][$j]['id'] . '_';
01188       }
01189       $calculated_category_path_string = substr($calculated_category_path_string, 0, -1) . '<br>';
01190     }
01191     $calculated_category_path_string = substr($calculated_category_path_string, 0, -4);
01192 
01193     if (strlen($calculated_category_path_string) < 1) $calculated_category_path_string = TEXT_TOP;
01194 
01195     return $calculated_category_path_string;
01196   }
01197 
01198   function zen_remove_category($category_id) {
01199     if ((int)$category_id == 0) return;
01200     global $db;
01201 
01202     // delete from salemaker - sale_categories_selected
01203     $chk_sale_categories_selected = $db->Execute("select * from " . TABLE_SALEMAKER_SALES . "
01204     WHERE
01205     sale_categories_selected = '" . (int)$category_id . "'
01206     OR sale_categories_selected LIKE '%," . (int)$category_id . ",%'
01207     OR sale_categories_selected LIKE '%," . (int)$category_id . "'
01208     OR sale_categories_selected LIKE '" . (int)$category_id . ",%'");
01209 
01210     // delete from salemaker - sale_categories_all
01211     $chk_sale_categories_all = $db->Execute("select * from " . TABLE_SALEMAKER_SALES . "
01212     WHERE
01213     sale_categories_all = '" . (int)$category_id . "'
01214     OR sale_categories_all LIKE '%," . (int)$category_id . ",%'
01215     OR sale_categories_all LIKE '%," . (int)$category_id . "'
01216     OR sale_categories_all LIKE '" . (int)$category_id . ",%'");
01217 
01218 //echo 'WORKING ON: ' . (int)$category_id . ' chk_sale_categories_selected: ' . $chk_sale_categories_selected->RecordCount() . ' chk_sale_categories_all: ' . $chk_sale_categories_all->RecordCount() . '<br>';
01219 while (!$chk_sale_categories_selected->EOF) {
01220   $skip_cats = false; // used when deleting
01221   $skip_sale_id = 0;
01222 //echo '<br>FIRST LOOP: sale_id ' . $chk_sale_categories_selected->fields['sale_id'] . ' sale_categories_selected: ' . $chk_sale_categories_selected->fields['sale_categories_selected'] . '<br>';
01223   // 9 or ,9 or 9,
01224   // delete record if sale_categories_selected = 9 and  sale_categories_all = ,9,
01225   if ($chk_sale_categories_selected->fields['sale_categories_selected'] == (int)$category_id and $chk_sale_categories_selected->fields['sale_categories_all'] == ',' . (int)$category_id . ',') { // delete record
01226 //echo 'A: I should delete this record sale_id: ' . $chk_sale_categories_selected->fields['sale_id'] . '<br><br>';
01227     $skip_cats = true;
01228     $skip_sale_id = $chk_sale_categories_selected->fields['sale_id'];
01229     $salemakerdelete = "DELETE from " . TABLE_SALEMAKER_SALES . " WHERE sale_id='"  . $skip_sale_id . "'";
01230   }
01231 
01232   // if in the front - remove 9,
01233   //  if ($chk_sale_categories_selected->fields['sale_categories_selected'] == (int)$category_id . ',') { // front
01234   if (!$skip_cats && (preg_match('/^' . (int)$category_id . ',/', $chk_sale_categories_selected->fields['sale_categories_selected'])) ) { // front
01235 //echo 'B: I need to remove - ' . (int)$category_id . ', - from the front of ' . $chk_sale_categories_selected->fields['sale_categories_selected'] . '<br>';
01236     $new_sale_categories_selected = substr($chk_sale_categories_selected->fields['sale_categories_selected'], strlen((int)$category_id . ','));
01237 //echo 'B: new_sale_categories_selected: ' . $new_sale_categories_selected . '<br><br>';
01238   }
01239 
01240   // if in the middle or end - remove ,9,
01241   if (!$skip_cats && (strpos($chk_sale_categories_selected->fields['sale_categories_selected'], ',' . (int)$category_id . ',')) ) { // middle or end
01242 //echo 'C: I need to remove - ,' . (int)$category_id . ', - from the middle or end ' . $chk_sale_categories_selected->fields['sale_categories_selected'] . '<br>';
01243     $start_cat = (int)strpos($chk_sale_categories_selected->fields['sale_categories_selected'], ',' . (int)$category_id . ',') + strlen(',' . (int)$category_id . ',');
01244     $end_cat = (int)strpos($chk_sale_categories_selected->fields['sale_categories_selected'], ',' . (int)$category_id . ',', $start_cat+strlen(',' . (int)$category_id . ','));
01245     $new_sale_categories_selected = substr($chk_sale_categories_selected->fields['sale_categories_selected'], 0, $start_cat - (strlen(',' . (int)$category_id . ',') - 1)) . substr($chk_sale_categories_selected->fields['sale_categories_selected'], $start_cat);
01246 //echo 'C: new_sale_categories_selected: ' . $new_sale_categories_selected. '<br><br>';
01247     $skip_cat_last = true;
01248   }
01249 
01250 
01251 // not needed in loop 1 if middle does end
01252   // if on the end - remove ,9 skip if middle cleaned it
01253   if (!$skip_cats && !$skip_cat_last && (strripos($chk_sale_categories_selected->fields['sale_categories_selected'], ',' . (int)$category_id)) ) { // end
01254     $start_cat = (int)strpos($chk_sale_categories_selected->fields['sale_categories_selected'], ',' . (int)$category_id) + strlen(',' . (int)$category_id);
01255 //echo 'D: I need to remove - ,' . (int)$category_id . ' - from the end ' . $chk_sale_categories_selected->fields['sale_categories_selected'] . '<br>';
01256     $new_sale_categories_selected = substr($chk_sale_categories_selected->fields['sale_categories_selected'], 0, $start_cat - (strlen(',' . (int)$category_id . ',') - 1));
01257 //echo 'D: new_sale_categories_selected: ' . $new_sale_categories_selected. '<br><br>';
01258   }
01259 
01260   if (!$skip_cats) {
01261     $salemakerupdate =
01262     "UPDATE " . TABLE_SALEMAKER_SALES . "
01263     SET sale_categories_selected='" . $new_sale_categories_selected . "'
01264     WHERE sale_id = '" . $chk_sale_categories_selected->fields['sale_id'] . "'";
01265 //echo 'Update new_sale_categories_selected: ' . $salemakerupdate . '<br>';
01266     $db->Execute($salemakerupdate);
01267   } else {
01268 //echo 'Record was deleted sale_id ' . $skip_sale_id . '<br>' . $salemakerdelete;
01269     $db->Execute($salemakerdelete);
01270   }
01271 
01272   $chk_sale_categories_selected->MoveNext();
01273 }
01274 
01275 while (!$chk_sale_categories_all->EOF) {
01276 //echo '<br><br>SECOND LOOP: sale_id ' . $chk_sale_categories_all->fields['sale_id'] . ' sale_categories_all: ' . $chk_sale_categories_all->fields['sale_categories_all'] . '<br><br>';
01277   // remove ,9 if on front as ,9, - remove ,9 if in the middle as ,9, - remove ,9 if on the end as ,9,
01278   // beware of ,79, or ,98, or ,99, when cleaning 9
01279   // if ($chk_sale_categories_all->fields['sale_categories_all'] == ',9') { // front
01280   // if (something for the middle) { // middle
01281   // if (right($chk_sale_categories_all->fields['sale_categories_all']) == ',9,') { // end
01282 
01283   $skip_cats = false;
01284   if ($skip_sale_id == $chk_sale_categories_all->fields['sale_id']) { // was deleted
01285 //echo 'A: I should delete this record sale_id: ' . $chk_sale_categories_all->fields['sale_id'] . ' but already done' . '<br><br>';
01286     $skip_cats = true;
01287   }
01288 
01289   // if in the front - remove 9,
01290   //  if ($chk_sale_categories_all->fields['sale_categories_all'] == (int)$category_id . ',') { // front
01291   if (!$skip_cats && (preg_match('/^' . ',' . (int)$category_id . ',/', $chk_sale_categories_all->fields['sale_categories_all'])) ) { // front
01292 //echo 'B: I need to remove - ' . (int)$category_id . ', - from the front of ' . $chk_sale_categories_all->fields['sale_categories_all'] . '<br>';
01293     $new_sale_categories_all = substr($chk_sale_categories_all->fields['sale_categories_all'], strlen(',' . (int)$category_id));
01294 //echo 'B: new_sale_categories_all: ' . $new_sale_categories_all . '<br><br>';
01295   }
01296 
01297   // if in the middle or end - remove ,9,
01298   if (!$skip_cats && (strpos($chk_sale_categories_all->fields['sale_categories_all'], ',' . (int)$category_id . ',')) ) { // middle
01299 //echo 'C: I need to remove - ,' . (int)$category_id . ', - from the middle or end ' . $chk_sale_categories_all->fields['sale_categories_all'] . '<br>';
01300     $start_cat = (int)strpos($chk_sale_categories_all->fields['sale_categories_all'], ',' . (int)$category_id . ',') + strlen(',' . (int)$category_id . ',');
01301     $end_cat = (int)strpos($chk_sale_categories_all->fields['sale_categories_all'], ',' . (int)$category_id . ',', $start_cat+strlen(',' . (int)$category_id . ','));
01302     $new_sale_categories_all = substr($chk_sale_categories_all->fields['sale_categories_all'], 0, $start_cat - (strlen(',' . (int)$category_id . ',') - 1)) . substr($chk_sale_categories_all->fields['sale_categories_all'], $start_cat);
01303 //echo 'C: new_sale_categories_all: ' . $new_sale_categories_all. '<br><br>';
01304   }
01305 
01306 /*
01307 // not needed in loop 2
01308   // if on the end - remove ,9,
01309   if (!$skip_cats && (strripos($chk_sale_categories_all->fields['sale_categories_all'], ',' . (int)$category_id . ',')) ) { // end
01310     $start_cat = (int)strpos($chk_sale_categories_all->fields['sale_categories_all'], ',' . (int)$category_id) + strlen(',' . (int)$category_id . ',');
01311     echo 'D: I need to remove from the end - ,' . (int)$category_id . ', - from the end ' . $chk_sale_categories_all->fields['sale_categories_all'] . '<br>';
01312     $new_sale_categories_all = substr($chk_sale_categories_all->fields['sale_categories_all'], 0, $start_cat - (strlen(',' . (int)$category_id . ',') - 1));
01313     echo 'D: new_sale_categories_all: ' . $new_sale_categories_all. '<br><br>';
01314   }
01315 */
01316       $salemakerupdate = "UPDATE " . TABLE_SALEMAKER_SALES . " SET sale_categories_all='" . $new_sale_categories_all . "' WHERE sale_id = '" . $chk_sale_categories_all->fields['sale_id'] . "'";
01317 
01318 //echo 'Update sale_categories_all: ' . $salemakerupdate . '<br>';
01319 
01320       $db->Execute($salemakerupdate);
01321 
01322       $chk_sale_categories_all->MoveNext();
01323 }
01324 
01325 //die('DONE TESTING');
01326 
01327     $category_image = $db->Execute("select categories_image
01328                                     from " . TABLE_CATEGORIES . "
01329                                     where categories_id = '" . (int)$category_id . "'");
01330 
01331     $duplicate_image = $db->Execute("select count(*) as total
01332                                      from " . TABLE_CATEGORIES . "
01333                                      where categories_image = '" .
01334                                            zen_db_input($category_image->fields['categories_image']) . "'");
01335     if ($duplicate_image->fields['total'] < 2) {
01336       if (file_exists(DIR_FS_CATALOG_IMAGES . $category_image->fields['categories_image'])) {
01337         @unlink(DIR_FS_CATALOG_IMAGES . $category_image->fields['categories_image']);
01338       }
01339     }
01340 
01341     $db->Execute("delete from " . TABLE_CATEGORIES . "
01342                   where categories_id = '" . (int)$category_id . "'");
01343 
01344     $db->Execute("delete from " . TABLE_CATEGORIES_DESCRIPTION . "
01345                   where categories_id = '" . (int)$category_id . "'");
01346 
01347     $db->Execute("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . "
01348                   where categories_id = '" . (int)$category_id . "'");
01349 
01350     $db->Execute("delete from " . TABLE_METATAGS_CATEGORIES_DESCRIPTION . "
01351                   where categories_id = '" . (int)$category_id . "'");
01352 
01353     $db->Execute("delete from " . TABLE_COUPON_RESTRICT . "
01354                   where category_id = '" . (int)$category_id . "'");
01355 
01356 
01357   }
01358 
01359   function zen_remove_product($product_id, $ptc = 'true') {
01360     global $db;
01361     $product_image = $db->Execute("select products_image
01362                                    from " . TABLE_PRODUCTS . "
01363                                    where products_id = '" . (int)$product_id . "'");
01364 
01365     $duplicate_image = $db->Execute("select count(*) as total
01366                                      from " . TABLE_PRODUCTS . "
01367                                      where products_image = '" . zen_db_input($product_image->fields['products_image']) . "'");
01368 
01369     if ($duplicate_image->fields['total'] < 2 and $product_image->fields['products_image'] != '' && PRODUCTS_IMAGE_NO_IMAGE != substr($product_image->fields['products_image'], strrpos($product_image->fields['products_image'], '/')+1)) {
01370       $products_image = $product_image->fields['products_image'];
01371       $products_image_extension = substr($products_image, strrpos($products_image, '.'));
01372       $products_image_base = preg_replace('/' . $products_image_extension . '/', '', $products_image);
01373 
01374       $filename_medium = 'medium/' . $products_image_base . IMAGE_SUFFIX_MEDIUM . $products_image_extension;
01375       $filename_large = 'large/' . $products_image_base . IMAGE_SUFFIX_LARGE . $products_image_extension;
01376 
01377       if (file_exists(DIR_FS_CATALOG_IMAGES . $product_image->fields['products_image'])) {
01378         @unlink(DIR_FS_CATALOG_IMAGES . $product_image->fields['products_image']);
01379       }
01380       if (file_exists(DIR_FS_CATALOG_IMAGES . $filename_medium)) {
01381         @unlink(DIR_FS_CATALOG_IMAGES . $filename_medium);
01382       }
01383       if (file_exists(DIR_FS_CATALOG_IMAGES . $filename_large)) {
01384         @unlink(DIR_FS_CATALOG_IMAGES . $filename_large);
01385       }
01386     }
01387 
01388     $db->Execute("delete from " . TABLE_SPECIALS . "
01389                   where products_id = '" . (int)$product_id . "'");
01390 
01391     $db->Execute("delete from " . TABLE_PRODUCTS . "
01392                   where products_id = '" . (int)$product_id . "'");
01393 
01394 //    if ($ptc == 'true') {
01395       $db->Execute("delete from " . TABLE_PRODUCTS_TO_CATEGORIES . "
01396                     where products_id = '" . (int)$product_id . "'");
01397 //    }
01398 
01399     $db->Execute("delete from " . TABLE_PRODUCTS_DESCRIPTION . "
01400                   where products_id = '" . (int)$product_id . "'");
01401 
01402     $db->Execute("delete from " . TABLE_META_TAGS_PRODUCTS_DESCRIPTION . "
01403                   where products_id = '" . (int)$product_id . "'");
01404 
01405     zen_products_attributes_download_delete($product_id);
01406 
01407     $db->Execute("delete from " . TABLE_PRODUCTS_ATTRIBUTES . "
01408                   where products_id = '" . (int)$product_id . "'");
01409 
01410     $db->Execute("delete from " . TABLE_CUSTOMERS_BASKET . "
01411                   where products_id = '" . (int)$product_id . "'");
01412 
01413     $db->Execute("delete from " . TABLE_CUSTOMERS_BASKET_ATTRIBUTES . "
01414                   where products_id = '" . (int)$product_id . "'");
01415 
01416 
01417     $product_reviews = $db->Execute("select reviews_id
01418                                      from " . TABLE_REVIEWS . "
01419                                      where products_id = '" . (int)$product_id . "'");
01420 
01421     while (!$product_reviews->EOF) {
01422       $db->Execute("delete from " . TABLE_REVIEWS_DESCRIPTION . "
01423                     where reviews_id = '" . (int)$product_reviews->fields['reviews_id'] . "'");
01424       $product_reviews->MoveNext();
01425     }
01426     $db->Execute("delete from " . TABLE_REVIEWS . "
01427                   where products_id = '" . (int)$product_id . "'");
01428 
01429     $db->Execute("delete from " . TABLE_FEATURED . "
01430                   where products_id = '" . (int)$product_id . "'");
01431 
01432     $db->Execute("delete from " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . "
01433                   where products_id = '" . (int)$product_id . "'");
01434 
01435     $db->Execute("delete from " . TABLE_COUPON_RESTRICT . "
01436                   where product_id = '" . (int)$product_id . "'");
01437 
01438   }
01439 
01440   function zen_products_attributes_download_delete($product_id) {
01441     global $db;
01442   // remove downloads if they exist
01443     $remove_downloads= $db->Execute("select products_attributes_id from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id= '" . (int)$product_id . "'");
01444     while (!$remove_downloads->EOF) {
01445       $db->Execute("delete from " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " where products_attributes_id= '" . $remove_downloads->fields['products_attributes_id'] . "'");
01446       $remove_downloads->MoveNext();
01447     }
01448   }
01449 
01450   function zen_remove_order($order_id, $restock = false) {
01451     global $db;
01452     if ($restock == 'on') {
01453       $order = $db->Execute("select products_id, products_quantity
01454                              from " . TABLE_ORDERS_PRODUCTS . "
01455                              where orders_id = '" . (int)$order_id . "'");
01456 
01457       while (!$order->EOF) {
01458         $db->Execute("update " . TABLE_PRODUCTS . "
01459                       set products_quantity = products_quantity + " . $order->fields['products_quantity'] . ", products_ordered = products_ordered - " . $order->fields['products_quantity'] . " where products_id = '" . (int)$order->fields['products_id'] . "'");
01460         $order->MoveNext();
01461       }
01462     }
01463 
01464     $db->Execute("delete from " . TABLE_ORDERS . " where orders_id = '" . (int)$order_id . "'");
01465     $db->Execute("delete from " . TABLE_ORDERS_PRODUCTS . "
01466                   where orders_id = '" . (int)$order_id . "'");
01467 
01468     $db->Execute("delete from " . TABLE_ORDERS_PRODUCTS_ATTRIBUTES . "
01469                   where orders_id = '" . (int)$order_id . "'");
01470 
01471     $db->Execute("delete from " . TABLE_ORDERS_PRODUCTS_DOWNLOAD . "
01472                   where orders_id = '" . (int)$order_id . "'");
01473 
01474     $db->Execute("delete from " . TABLE_ORDERS_STATUS_HISTORY . "
01475                   where orders_id = '" . (int)$order_id . "'");
01476 
01477     $db->Execute("delete from " . TABLE_ORDERS_TOTAL . "
01478                   where orders_id = '" . (int)$order_id . "'");
01479 
01480     $db->Execute("delete from " . TABLE_COUPON_GV_QUEUE . "
01481                   where order_id = '" . (int)$order_id . "' and release_flag = 'N'");
01482   }
01483 
01484   function zen_get_file_permissions($mode) {
01485 // determine type
01486     if ( ($mode & 0xC000) == 0xC000) { // unix domain socket
01487       $type = 's';
01488     } elseif ( ($mode & 0x4000) == 0x4000) { // directory
01489       $type = 'd';
01490     } elseif ( ($mode & 0xA000) == 0xA000) { // symbolic link
01491       $type = 'l';
01492     } elseif ( ($mode & 0x8000) == 0x8000) { // regular file
01493       $type = '-';
01494     } elseif ( ($mode & 0x6000) == 0x6000) { //bBlock special file
01495       $type = 'b';
01496     } elseif ( ($mode & 0x2000) == 0x2000) { // character special file
01497       $type = 'c';
01498     } elseif ( ($mode & 0x1000) == 0x1000) { // named pipe
01499       $type = 'p';
01500     } else { // unknown
01501       $type = '?';
01502     }
01503 
01504 // determine permissions
01505     $owner['read']    = ($mode & 00400) ? 'r' : '-';
01506     $owner['write']   = ($mode & 00200) ? 'w' : '-';
01507     $owner['execute'] = ($mode & 00100) ? 'x' : '-';
01508     $group['read']    = ($mode & 00040) ? 'r' : '-';
01509     $group['write']   = ($mode & 00020) ? 'w' : '-';
01510     $group['execute'] = ($mode & 00010) ? 'x' : '-';
01511     $world['read']    = ($mode & 00004) ? 'r' : '-';
01512     $world['write']   = ($mode & 00002) ? 'w' : '-';
01513     $world['execute'] = ($mode & 00001) ? 'x' : '-';
01514 
01515 // adjust for SUID, SGID and sticky bit
01516     if ($mode & 0x800 ) $owner['execute'] = ($owner['execute'] == 'x') ? 's' : 'S';
01517     if ($mode & 0x400 ) $group['execute'] = ($group['execute'] == 'x') ? 's' : 'S';
01518     if ($mode & 0x200 ) $world['execute'] = ($world['execute'] == 'x') ? 't' : 'T';
01519 
01520     return $type .
01521            $owner['read'] . $owner['write'] . $owner['execute'] .
01522            $group['read'] . $group['write'] . $group['execute'] .
01523            $world['read'] . $world['write'] . $world['execute'];
01524   }
01525 
01526   function zen_remove($source) {
01527     global $messageStack, $zen_remove_error;
01528 
01529     if (isset($zen_remove_error)) $zen_remove_error = false;
01530 
01531     if (is_dir($source)) {
01532       $dir = dir($source);
01533       while ($file = $dir->read()) {
01534         if ( ($file != '.') && ($file != '..') ) {
01535           if (is_writeable($source . '/' . $file)) {
01536             zen_remove($source . '/' . $file);
01537           } else {
01538             $messageStack->add(sprintf(ERROR_FILE_NOT_REMOVEABLE, $source . '/' . $file), 'error');
01539             $zen_remove_error = true;
01540           }
01541         }
01542       }
01543       $dir->close();
01544 
01545       if (is_writeable($source)) {
01546         rmdir($source);
01547       } else {
01548         $messageStack->add(sprintf(ERROR_DIRECTORY_NOT_REMOVEABLE, $source), 'error');
01549         $zen_remove_error = true;
01550       }
01551     } else {
01552       if (is_writeable($source)) {
01553         unlink($source);
01554       } else {
01555         $messageStack->add(sprintf(ERROR_FILE_NOT_REMOVEABLE, $source), 'error');
01556         $zen_remove_error = true;
01557       }
01558     }
01559   }
01560 
01562 // Output the tax percentage with optional padded decimals
01563   function zen_display_tax_value($value, $padding = TAX_DECIMAL_PLACES) {
01564     if (strpos($value, '.')) {
01565       $loop = true;
01566       while ($loop) {
01567         if (substr($value, -1) == '0') {
01568           $value = substr($value, 0, -1);
01569         } else {
01570           $loop = false;
01571           if (substr($value, -1) == '.') {
01572             $value = substr($value, 0, -1);
01573           }
01574         }
01575       }
01576     }
01577 
01578     if ($padding > 0) {
01579       if ($decimal_pos = strpos($value, '.')) {
01580         $decimals = strlen(substr($value, ($decimal_pos+1)));
01581         for ($i=$decimals; $i<$padding; $i++) {
01582           $value .= '0';
01583         }
01584       } else {
01585         $value .= '.';
01586         for ($i=0; $i<$padding; $i++) {
01587           $value .= '0';
01588         }
01589       }
01590     }
01591 
01592     return $value;
01593   }
01594 
01595 
01596   function zen_get_tax_class_title($tax_class_id) {
01597     global $db;
01598     if ($tax_class_id == '0') {
01599       return TEXT_NONE;
01600     } else {
01601       $classes = $db->Execute("select tax_class_title
01602                                from " . TABLE_TAX_CLASS . "
01603                                where tax_class_id = '" . (int)$tax_class_id . "'");
01604 
01605       return $classes->fields['tax_class_title'];
01606     }
01607   }
01608 
01609   function zen_banner_image_extension() {
01610     if (function_exists('imagetypes')) {
01611       if (imagetypes() & IMG_PNG) {
01612         return 'png';
01613       } elseif (imagetypes() & IMG_JPG) {
01614         return 'jpg';
01615       } elseif (imagetypes() & IMG_GIF) {
01616         return 'gif';
01617       }
01618     } elseif (function_exists('imagecreatefrompng') && function_exists('imagepng')) {
01619       return 'png';
01620     } elseif (function_exists('imagecreatefromjpeg') && function_exists('imagejpeg')) {
01621       return 'jpg';
01622     } elseif (function_exists('imagecreatefromgif') && function_exists('imagegif')) {
01623       return 'gif';
01624     }
01625 
01626     return false;
01627   }
01628 
01629   function zen_round($value, $precision) {
01630     $value =  round($value *pow(10,$precision),0);
01631     $value = $value/pow(10,$precision);
01632     return $value;
01633   }
01634 
01636 // Add tax to a products price
01637   function zen_add_tax($price, $tax) {
01638     global $currencies;
01639 
01640     if (DISPLAY_PRICE_WITH_TAX_ADMIN == 'true') {
01641       return zen_round($price, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']) + zen_calculate_tax($price, $tax);
01642     } else {
01643       return zen_round($price, $currencies->currencies[DEFAULT_CURRENCY]['decimal_places']);
01644     }
01645   }
01646 
01647 // Calculates Tax rounding the result
01648   function zen_calculate_tax($price, $tax) {
01649     return $price * $tax / 100;
01650   }
01651 
01653 // Returns the tax rate for a zone / class
01654 // TABLES: tax_rates, zones_to_geo_zones
01655   function zen_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) {
01656     global $db;
01657     global $customer_zone_id, $customer_country_id;
01658 
01659     if ( ($country_id == -1) && ($zone_id == -1) ) {
01660       if (!$_SESSION['customer_id']) {
01661         $country_id = STORE_COUNTRY;
01662         $zone_id = STORE_ZONE;
01663       } else {
01664         $country_id = $customer_country_id;
01665         $zone_id = $customer_zone_id;
01666       }
01667     }
01668 
01669     $tax = $db->Execute("select SUM(tax_rate) as tax_rate
01670                          from (" . TABLE_TAX_RATES . " tr
01671                          left join " . TABLE_ZONES_TO_GEO_ZONES . " za
01672                          ON tr.tax_zone_id = za.geo_zone_id
01673                          left join " . TABLE_GEO_ZONES . " tz ON tz.geo_zone_id = tr.tax_zone_id )
01674                          WHERE (za.zone_country_id IS NULL
01675                          OR za.zone_country_id = 0
01676                          OR za.zone_country_id = '" . (int)$country_id . "')
01677                          AND (za.zone_id IS NULL OR za.zone_id = 0
01678                          OR za.zone_id = '" . (int)$zone_id . "')
01679                          AND tr.tax_class_id = '" . (int)$class_id . "'
01680                          GROUP BY tr.tax_priority");
01681 
01682     if ($tax->RecordCount() > 0) {
01683       $tax_multiplier = 0;
01684       while (!$tax->EOF) {
01685         $tax_multiplier += $tax->fields['tax_rate'];
01686     $tax->MoveNext();
01687       }
01688       return $tax_multiplier;
01689     } else {
01690       return 0;
01691     }
01692   }
01693 
01695 // Returns the tax rate for a tax class
01696 // TABLES: tax_rates
01697   function zen_get_tax_rate_value($class_id) {
01698     return zen_get_tax_rate($class_id);
01699   }
01700 
01701   function zen_call_function($function, $parameter, $object = '') {
01702     if ($object == '') {
01703       return call_user_func($function, $parameter);
01704     } else {
01705       return call_user_func(array($object, $function), $parameter);
01706     }
01707   }
01708 
01709   function zen_get_zone_class_title($zone_class_id) {
01710     global $db;
01711     if ($zone_class_id == '0') {
01712       return TEXT_NONE;
01713     } else {
01714       $classes = $db->Execute("select geo_zone_name
01715                                from " . TABLE_GEO_ZONES . "
01716                                where geo_zone_id = '" . (int)$zone_class_id . "'");
01717 
01718       return $classes->fields['geo_zone_name'];
01719     }
01720   }
01721 
01723   function zen_cfg_pull_down_zone_classes($zone_class_id, $key = '') {
01724     global $db;
01725     $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
01726 
01727     $zone_class_array = array(array('id' => '0', 'text' => TEXT_NONE));
01728     $zone_class = $db->Execute("select geo_zone_id, geo_zone_name
01729                                 from " . TABLE_GEO_ZONES . "
01730                                 order by geo_zone_name");
01731 
01732     while (!$zone_class->EOF) {
01733       $zone_class_array[] = array('id' => $zone_class->fields['geo_zone_id'],
01734                                   'text' => $zone_class->fields['geo_zone_name']);
01735       $zone_class->MoveNext();
01736     }
01737 
01738     return zen_draw_pull_down_menu($name, $zone_class_array, $zone_class_id);
01739   }
01740 
01741 
01743   function zen_cfg_pull_down_order_statuses($order_status_id, $key = '') {
01744     global $db;
01745     $name = (($key) ? 'configuration[' . $key . ']' : 'configuration_value');
01746 
01747     $statuses_array = array(array('id' => '0', 'text' => TEXT_DEFAULT));
01748     $statuses = $db->Execute("select orders_status_id, orders_status_name
01749                               from " . TABLE_ORDERS_STATUS . "
01750                               where language_id = '" . (int)$_SESSION['languages_id'] . "'
01751                               order by orders_status_id");
01752 
01753     while (!$statuses->EOF) {
01754       $statuses_array[] = array('id' => $statuses->fields['orders_status_id'],
01755                                 'text' => $statuses->fields['orders_status_name'] . ' [' . $statuses->fields['orders_status_id'] . ']');
01756       $statuses->MoveNext();
01757     }
01758 
01759     return zen_draw_pull_down_menu($name, $statuses_array, $order_status_id);
01760   }
01761 
01762   function zen_get_order_status_name($order_status_id, $language_id = '') {
01763     global $db;
01764 
01765     if ($order_status_id < 1) return TEXT_DEFAULT;
01766 
01767     if (!is_numeric($language_id)) $language_id = $_SESSION['languages_id'];
01768 
01769     $status = $db->Execute("select orders_status_name
01770                             from " . TABLE_ORDERS_STATUS . "
01771                             where orders_status_id = '" . (int)$order_status_id . "'
01772                             and language_id = '" . (int)$language_id . "'");
01773 
01774     return $status->fields['orders_status_name'] . ' [' . (int)$order_status_id . ']';
01775   }
01776 
01778 // Return a random value
01779   function zen_rand($min = null, $max = null) {
01780     static $seeded;
01781 
01782     if (!$seeded) {
01783       mt_srand((double)microtime()*1000000);
01784       $seeded = true;
01785     }
01786 
01787     if (isset($min) && isset($max)) {
01788       if ($min >= $max) {
01789         return $min;
01790       } else {
01791         return mt_rand($min, $max);
01792       }
01793     } else {
01794       return mt_rand();
01795     }
01796   }
01797 
01798 // nl2br() prior PHP 4.2.0 did not convert linefeeds on all OSs (it only converted \n)
01799   function zen_convert_linefeeds($from, $to, $string) {
01800     if ((PHP_VERSION < "4.0.5") && is_array($from)) {
01801       return preg_replace('/(' . implode('|', $from) . ')/', $to, $string);
01802     } else {
01803       return str_replace($from, $to, $string);
01804     }
01805   }
01806 
01807   function zen_string_to_int($string) {
01808     return (int)$string;
01809   }
01810 
01812 // Parse and secure the cPath parameter values
01813   function zen_parse_category_path($cPath) {
01814 // make sure the category IDs are integers
01815     $cPath_array = array_map('zen_string_to_int', explode('_', $cPath));
01816 
01817 // make sure no duplicate category IDs exist which could lock the server in a loop
01818     $tmp_array = array();
01819     $n = sizeof($cPath_array);
01820     for ($i=0; $i<$n; $i++) {
01821       if (!in_array($cPath_array[$i], $tmp_array)) {
01822         $tmp_array[] = $cPath_array[$i];
01823       }
01824     }
01825 
01826     return $tmp_array;
01827   }
01829 // Create a Coupon Code. length may be between 1 and 16 Characters
01830 // $salt needs some thought.
01831 
01832   function create_coupon_code($salt="secret", $length=SECURITY_CODE_LENGTH) {
01833     global $db;
01834     $ccid = md5(uniqid("","salt"));
01835     $ccid .= md5(uniqid("","salt"));
01836     $ccid .= md5(uniqid("","salt"));
01837     $ccid .= md5(uniqid("","salt"));
01838     srand((double)microtime()*1000000); // seed the random number generator
01839     $random_start = @rand(0, (128-$length));
01840     $good_result = 0;
01841     while ($good_result == 0) {
01842       $id1=substr($ccid, $random_start,$length);
01843       $query = $db->Execute("select coupon_code
01844                              from " . TABLE_COUPONS . "
01845                              where coupon_code = '" . $id1 . "'");
01846 
01847       if ($query->RecordCount() < 1 ) $good_result = 1;
01848     }
01849     return $id1;
01850   }
01852 // Update the Customers GV account
01853   function zen_gv_account_update($customer_id, $gv_id) {
01854     global $db;
01855     $customer_gv = $db->Execute("select amount
01856                                  from " . TABLE_COUPON_GV_CUSTOMER . "
01857                                  where customer_id = '" . (int)$customer_id . "'");
01858 
01859     $coupon_gv = $db->Execute("select coupon_amount
01860                                from " . TABLE_COUPONS . "
01861                                where coupon_id = '" . (int)$gv_id . "'");
01862 
01863     if ($customer_gv->RecordCount() > 0) {
01864       $new_gv_amount = $customer_gv->fields['amount'] + $coupon_gv->fields['coupon_amount'];
01865       $gv_query = $db->Execute("update " . TABLE_COUPON_GV_CUSTOMER . "
01866                                 set amount = '" . $new_gv_amount . "'
01867                                 where customer_id = '" . (int)$customer_id . "'");
01868 
01869     } else {
01870       $db->Execute("insert into " . TABLE_COUPON_GV_CUSTOMER . " (customer_id, amount) values ('" . (int)$customer_id . "', '" . $coupon_gv->fields['coupon_amount'] . "')");
01871     }
01872   }
01874 // Output a day/month/year dropdown selector
01875   function zen_draw_date_selector($prefix, $date='') {
01876     $month_array = array();
01877     $month_array[1] =_JANUARY;
01878     $month_array[2] =_FEBRUARY;
01879     $month_array[3] =_MARCH;
01880     $month_array[4] =_APRIL;
01881     $month_array[5] =_MAY;
01882     $month_array[6] =_JUNE;
01883     $month_array[7] =_JULY;
01884     $month_array[8] =_AUGUST;
01885     $month_array[9] =_SEPTEMBER;
01886     $month_array[10] =_OCTOBER;
01887     $month_array[11] =_NOVEMBER;
01888     $month_array[12] =_DECEMBER;
01889     $usedate = getdate($date);
01890     $day = $usedate['mday'];
01891     $month = $usedate['mon'];
01892     $year = $usedate['year'];
01893     $date_selector = '<select name="'. $prefix .'_day">';
01894     for ($i=1;$i<32;$i++){
01895       $date_selector .= '<option value="' . $i . '"';
01896       if ($i==$day) $date_selector .= 'selected';
01897       $date_selector .= '>' . $i . '</option>';
01898     }
01899     $date_selector .= '</select>';
01900     $date_selector .= '<select name="'. $prefix .'_month">';
01901     for ($i=1;$i<13;$i++){
01902       $date_selector .= '<option value="' . $i . '"';
01903       if ($i==$month) $date_selector .= 'selected';
01904       $date_selector .= '>' . $month_array[$i] . '</option>';
01905     }
01906     $date_selector .= '</select>';
01907     $date_selector .= '<select name="'. $prefix .'_year">';
01908     for ($i=2001;$i<2019;$i++){
01909       $date_selector .= '<option value="' . $i . '"';
01910       if ($i==$year) $date_selector .= 'selected';
01911       $date_selector .= '>' . $i . '</option>';
01912     }
01913     $date_selector .= '</select>';
01914     return $date_selector;
01915   }
01916 
01918 // Validate Option Name and Option Type Match
01919   function zen_validate_options_to_options_value($products_options_id, $products_options_values_id) {
01920     global $db;
01921     $check_options_to_values_query= $db->Execute("select products_options_id
01922                                                   from " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . "
01923                                                   where products_options_id= '" . (int)$products_options_id . "'
01924                                                   and products_options_values_id='" . (int)$products_options_values_id .
01925                                                   "' limit 1");
01926 
01927     if ($check_options_to_values_query->RecordCount() != 1) {
01928       return false;
01929     } else {
01930       return true;
01931     }
01932   }
01933 
01935 // look-up Attributues Options Name products_options_values_to_products_options
01936   function zen_get_products_options_name_from_value($lookup) {
01937     global $db;
01938 
01939     if ($lookup==0) {
01940       return 'RESERVED FOR TEXT/FILES ONLY ATTRIBUTES';
01941     }
01942 
01943     $check_options_to_values = $db->Execute("select products_options_id
01944                     from " . TABLE_PRODUCTS_OPTIONS_VALUES_TO_PRODUCTS_OPTIONS . "
01945                     where products_options_values_id='" . (int)$lookup . "'");
01946 
01947     $check_options = $db->Execute("select products_options_name
01948                       from " . TABLE_PRODUCTS_OPTIONS . "
01949                       where products_options_id='" . (int)$check_options_to_values->fields['products_options_id']
01950                       . "' and language_id='" . (int)$_SESSION['languages_id'] . "'");
01951 
01952     return $check_options->fields['products_options_name'];
01953   }
01954 
01955 
01957 // lookup attributes model
01958   function zen_get_products_model($products_id) {
01959     global $db;
01960     $check = $db->Execute("select products_model
01961                     from " . TABLE_PRODUCTS . "
01962                     where products_id='" . (int)$products_id . "'");
01963 
01964     return $check->fields['products_model'];
01965   }
01966 
01967 
01969 // Check if product has attributes
01970   function zen_has_product_attributes_OLD($products_id) {
01971     global $db;
01972     $attributes = $db->Execute("select count(*) as count
01973                          from " . TABLE_PRODUCTS_ATTRIBUTES . "
01974                          where products_id = '" . (int)$products_id . "'");
01975 
01976     if ($attributes->fields['count'] > 0) {
01977       return true;
01978     } else {
01979       return false;
01980     }
01981   }
01982 
01984 // Check if product has attributes
01985   function zen_has_product_attributes($products_id, $not_readonly = 'true') {
01986     global $db;
01987 
01988     if (PRODUCTS_OPTIONS_TYPE_READONLY_IGNORED == '1' and $not_readonly == 'true') {
01989       // don't include READONLY attributes to determin if attributes must be selected to add to cart
01990       $attributes_query = "select pa.products_attributes_id
01991                            from " . TABLE_PRODUCTS_ATTRIBUTES . " pa left join " . TABLE_PRODUCTS_OPTIONS . " po on pa.options_id = po.products_options_id
01992                            where pa.products_id = '" . (int)$products_id . "' and po.products_options_type != '" . PRODUCTS_OPTIONS_TYPE_READONLY . "' limit 1";
01993     } else {
01994       // regardless of READONLY attributes no add to cart buttons
01995       $attributes_query = "select pa.products_attributes_id
01996                            from " . TABLE_PRODUCTS_ATTRIBUTES . " pa
01997                            where pa.products_id = '" . (int)$products_id . "' limit 1";
01998     }
01999 
02000     $attributes = $db->Execute($attributes_query);
02001 
02002     if ($attributes->fields['products_attributes_id'] > 0) {
02003       return true;
02004     } else {
02005       return false;
02006     }
02007   }
02008 
02010 // Check if product_id is valid
02011   function zen_products_id_valid($products_id) {
02012     global $db;
02013     $products_valid_query = "select count(*) as count
02014                          from " . TABLE_PRODUCTS . "
02015                          where products_id = '" . (int)$products_id . "'";
02016 
02017     $products_valid = $db->Execute($products_valid_query);
02018 
02019     if ($products_valid->fields['count'] > 0) {
02020       return true;
02021     } else {
02022       return false;
02023     }
02024   }
02025 
02026 function zen_copy_products_attributes($products_id_from, $products_id_to) {
02027   global $db;
02028   global $messageStack;
02029   global $copy_attributes_delete_first, $copy_attributes_duplicates_skipped, $copy_attributes_duplicates_overwrite, $copy_attributes_include_downloads, $copy_attributes_include_filename;
02030 
02031 // Check for errors in copy request
02032   if ( (!zen_has_product_attributes($products_id_from, 'false') or !zen_products_id_valid($products_id_to)) or $products_id_to == $products_id_from ) {
02033     if ($products_id_to == $products_id_from) {
02034       // same products_id
02035       $messageStack->add_session('<b>WARNING: Cannot copy from Product ID #' . $products_id_from . ' to Product ID # ' . $products_id_to . ' ... No copy was made' . '</b>', 'caution');
02036     } else {
02037       if (!zen_has_product_attributes($products_id_from, 'false')) {
02038         // no attributes found to copy
02039         $messageStack->add_session('<b>WARNING: No Attributes to copy from Product ID #' . $products_id_from . ' for: ' . zen_get_products_name($products_id_from) . ' ... No copy was made' . '</b>', 'caution');
02040       } else {
02041         // invalid products_id
02042         $messageStack->add_session('<b>WARNING: There is no Product ID #' . $products_id_to . ' ... No copy was made' . '</b>', 'caution');
02043       }
02044     }
02045   } else {
02046 // FIX HERE - remove once working
02047 
02048 // check if product already has attributes
02049     $check_attributes = zen_has_product_attributes($products_id_to, 'false');
02050 
02051     if ($copy_attributes_delete_first=='1' and $check_attributes == true) {
02052 // die('DELETE FIRST - Copying from ' . $products_id_from . ' to ' . $products_id_to . ' Do I delete first? ' . $copy_attributes_delete_first);
02053       // delete all attributes first from products_id_to
02054       zen_products_attributes_download_delete($products_id_to);
02055       $db->Execute("delete from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$products_id_to . "'");
02056     }
02057 
02058 // get attributes to copy from
02059     $products_copy_from= $db->Execute("select * from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id='" . (int)$products_id_from . "'" . " order by products_attributes_id");
02060 
02061     while ( !$products_copy_from->EOF ) {
02062 // This must match the structure of your products_attributes table
02063 
02064       $update_attribute = false;
02065       $add_attribute = true;
02066       $check_duplicate = $db->Execute("select * from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id='" . (int)$products_id_to . "'" . " and options_id= '" . (int)$products_copy_from->fields['options_id'] . "' and options_values_id='" . (int)$products_copy_from->fields['options_values_id'] .  "'");
02067       if ($check_attributes == true) {
02068         if ($check_duplicate->RecordCount() == 0) {
02069           $update_attribute = false;
02070           $add_attribute = true;
02071         } else {
02072           if ($check_duplicate->RecordCount() == 0) {
02073             $update_attribute = false;
02074             $add_attribute = true;
02075           } else {
02076             $update_attribute = true;
02077             $add_attribute = false;
02078           }
02079         }
02080       } else {
02081         $update_attribute = false;
02082         $add_attribute = true;
02083       }
02084 
02085 // die('UPDATE/IGNORE - Checking Copying from ' . $products_id_from . ' to ' . $products_id_to . ' Do I delete first? ' . ($copy_attributes_delete_first == '1' ? TEXT_YES : TEXT_NO) . ' Do I add? ' . ($add_attribute == true ? TEXT_YES : TEXT_NO) . ' Do I Update? ' . ($update_attribute == true ? TEXT_YES : TEXT_NO) . ' Do I skip it? ' . ($copy_attributes_duplicates_skipped=='1' ? TEXT_YES : TEXT_NO) . ' Found attributes in From: ' . $check_duplicate->RecordCount());
02086 
02087       if ($copy_attributes_duplicates_skipped == '1' and $check_duplicate->RecordCount() != 0) {
02088         // skip it
02089           $messageStack->add_session(TEXT_ATTRIBUTE_COPY_SKIPPING . $products_copy_from->fields['products_attributes_id'] . ' for Products ID#' . $products_id_to, 'caution');
02090       } else {
02091         if ($add_attribute == true) {
02092           // New attribute - insert it
02093           $db->Execute("insert into " . TABLE_PRODUCTS_ATTRIBUTES . " (products_attributes_id, products_id, options_id, options_values_id, options_values_price, price_prefix, products_options_sort_order, product_attribute_is_free, products_attributes_weight, products_attributes_weight_prefix, attributes_display_only, attributes_default, attributes_discounted, attributes_image, attributes_price_base_included, attributes_price_onetime, attributes_price_factor, attributes_price_factor_offset, attributes_price_factor_onetime, attributes_price_factor_onetime_offset, attributes_qty_prices, attributes_qty_prices_onetime, attributes_price_words, attributes_price_words_free, attributes_price_letters, attributes_price_letters_free, attributes_required) values (0, '" . (int)$products_id_to . "',
02094           '" . $products_copy_from->fields['options_id'] . "',
02095           '" . $products_copy_from->fields['options_values_id'] . "',
02096           '" . $products_copy_from->fields['options_values_price'] . "',
02097           '" . $products_copy_from->fields['price_prefix'] . "',
02098           '" . $products_copy_from->fields['products_options_sort_order'] . "',
02099           '" . $products_copy_from->fields['product_attribute_is_free'] . "',
02100           '" . $products_copy_from->fields['products_attributes_weight'] . "',
02101           '" . $products_copy_from->fields['products_attributes_weight_prefix'] . "',
02102           '" . $products_copy_from->fields['attributes_display_only'] . "',
02103           '" . $products_copy_from->fields['attributes_default'] . "',
02104           '" . $products_copy_from->fields['attributes_discounted'] . "',
02105           '" . $products_copy_from->fields['attributes_image'] . "',
02106           '" . $products_copy_from->fields['attributes_price_base_included'] . "',
02107           '" . $products_copy_from->fields['attributes_price_onetime'] . "',
02108           '" . $products_copy_from->fields['attributes_price_factor'] . "',
02109           '" . $products_copy_from->fields['attributes_price_factor_offset'] . "',
02110           '" . $products_copy_from->fields['attributes_price_factor_onetime'] . "',
02111           '" . $products_copy_from->fields['attributes_price_factor_onetime_offset'] . "',
02112           '" . $products_copy_from->fields['attributes_qty_prices'] . "',
02113           '" . $products_copy_from->fields['attributes_qty_prices_onetime'] . "',
02114           '" . $products_copy_from->fields['attributes_price_words'] . "',
02115           '" . $products_copy_from->fields['attributes_price_words_free'] . "',
02116           '" . $products_copy_from->fields['attributes_price_letters'] . "',
02117           '" . $products_copy_from->fields['attributes_price_letters_free'] . "',
02118           '" . $products_copy_from->fields['attributes_required'] . "')");
02119           $messageStack->add_session(TEXT_ATTRIBUTE_COPY_INSERTING . $products_copy_from->fields['products_attributes_id'] . ' for Products ID#' . $products_id_to, 'caution');
02120         }
02121         if ($update_attribute == true) {
02122           // Update attribute - Just attribute settings not ids
02123           $db->Execute("update " . TABLE_PRODUCTS_ATTRIBUTES . " set
02124           options_values_price='" . $products_copy_from->fields['options_values_price'] . "',
02125           price_prefix='" . $products_copy_from->fields['price_prefix'] . "',
02126           products_options_sort_order='" . $products_copy_from->fields['products_options_sort_order'] . "',
02127           product_attribute_is_free='" . $products_copy_from->fields['product_attribute_is_free'] . "',
02128           products_attributes_weight='" . $products_copy_from->fields['products_attributes_weight'] . "',
02129           products_attributes_weight_prefix='" . $products_copy_from->fields['products_attributes_weight_prefix'] . "',
02130           attributes_display_only='" . $products_copy_from->fields['attributes_display_only'] . "',
02131           attributes_default='" . $products_copy_from->fields['attributes_default'] . "',
02132           attributes_discounted='" . $products_copy_from->fields['attributes_discounted'] . "',
02133           attributes_image='" . $products_copy_from->fields['attributes_image'] . "',
02134           attributes_price_base_included='" . $products_copy_from->fields['attributes_price_base_included'] . "',
02135           attributes_price_onetime='" . $products_copy_from->fields['attributes_price_onetime'] . "',
02136           attributes_price_factor='" . $products_copy_from->fields['attributes_price_factor'] . "',
02137           attributes_price_factor_offset='" . $products_copy_from->fields['attributes_price_factor_offset'] . "',
02138           attributes_price_factor_onetime='" . $products_copy_from->fields['attributes_price_factor_onetime'] . "',
02139           attributes_price_factor_onetime_offset='" . $products_copy_from->fields['attributes_price_factor_onetime_offset'] . "',
02140           attributes_qty_prices='" . $products_copy_from->fields['attributes_qty_prices'] . "',
02141           attributes_qty_prices_onetime='" . $products_copy_from->fields['attributes_qty_prices_onetime'] . "',
02142           attributes_price_words='" . $products_copy_from->fields['attributes_price_words'] . "',
02143           attributes_price_words_free='" . $products_copy_from->fields['attributes_price_words_free'] . "',
02144           attributes_price_letters='" . $products_copy_from->fields['attributes_price_letters'] . "',
02145           attributes_price_letters_free='" . $products_copy_from->fields['attributes_price_letters_free'] . "',
02146           attributes_required='" . $products_copy_from->fields['attributes_required'] . "'"
02147            . " where products_id='" . (int)$products_id_to . "'" . " and options_id= '" . $products_copy_from->fields['options_id'] . "' and options_values_id='" . $products_copy_from->fields['options_values_id'] . "'");
02148 //           . " where products_id='" . $products_id_to . "'" . " and options_id= '" . $products_copy_from->fields['options_id'] . "' and options_values_id='" . $products_copy_from->fields['options_values_id'] . "' and attributes_image='" . $products_copy_from->fields['attributes_image'] . "' and attributes_price_base_included='" . $products_copy_from->fields['attributes_price_base_included'] .  "'");
02149           $messageStack->add_session(TEXT_ATTRIBUTE_COPY_UPDATING . $products_copy_from->fields['products_attributes_id'] . ' for Products ID#' . $products_id_to, 'caution');
02150         }
02151       }
02152 
02153       $products_copy_from->MoveNext();
02154     } // end of products attributes while loop
02155 
02156      // reset products_price_sorter for searches etc.
02157      zen_update_products_price_sorter($products_id_to);
02158   } // end of no attributes or other errors
02159 } // eof: zen_copy_products_attributes
02160 
02162 // warning message
02163   function zen_output_warning($warning) {
02164     new errorBox(array(array('text' => zen_image(DIR_WS_ICONS . 'warning.gif', ICON_WARNING) . ' ' . $warning)));
02165   }
02166 
02167 
02168 // function to return field type
02169 // uses $tbl = table name, $fld = field name
02170 
02171   function zen_field_type($tbl, $fld) {
02172     global $db;
02173     $rs = $db->MetaColumns($tbl);
02174     $type = $rs[strtoupper($fld)]->type;
02175     return $type;
02176   }
02177 
02178 // function to return field length
02179 // uses $tbl = table name, $fld = field name
02180   function zen_field_length($tbl, $fld) {
02181     global $db;
02182     $rs = $db->MetaColumns($tbl);
02183     $length = $rs[strtoupper($fld)]->max_length;
02184     return $length;
02185   }
02186 
02188 // return the size and maxlength settings in the form size="blah" maxlength="blah" based on maximum size being 50
02189 // uses $tbl = table name, $fld = field name
02190 // example: zen_set_field_length(TABLE_CATEGORIES_DESCRIPTION, 'categories_name')
02191   function zen_set_field_length($tbl, $fld, $max=50, $override=false) {
02192     $field_length= zen_field_length($tbl, $fld);
02193     switch (true) {
02194       case (($override == false and $field_length > $max)):
02195         $length= 'size = "' . ($max+1) . '" maxlength= "' . $field_length . '"';
02196         break;
02197       default:
02198         $length= 'size = "' . ($field_length+1) . '" maxlength = "' . $field_length . '"';
02199         break;
02200     }
02201     return $length;
02202   }
02203 
02204 
02206 // Lookup Languages Icon
02207   function zen_get_language_icon($lookup) {
02208     global $db;
02209     $languages_icon = $db->Execute("select directory, image from " . TABLE_LANGUAGES . " where languages_id = '" . zen_db_input($lookup) . "'");
02210     $icon= zen_image(DIR_WS_CATALOG_LANGUAGES . $languages_icon->fields['directory'] . '/images/' . $languages_icon->fields['image']);
02211     return $icon;
02212   }
02213 
02215 // Get the Option Name for a particular language
02216   function zen_get_option_name_language($option, $language) {
02217     global $db;
02218     $lookup = $db->Execute("select products_options_id, products_options_name from " . TABLE_PRODUCTS_OPTIONS . " where products_options_id= '" . (int)$option . "' and language_id = '" . (int)$language . "'");
02219     return $lookup->fields['products_options_name'];
02220   }
02221 
02223 // Get the Option Name for a particular language
02224   function zen_get_option_name_language_sort_order($option, $language) {
02225     global $db;
02226     $lookup = $db->Execute("select products_options_id, products_options_name, products_options_sort_order from " . TABLE_PRODUCTS_OPTIONS . " where products_options_id= '" . (int)$option . "' and language_id = '" . (int)$language . "'");
02227     return $lookup->fields['products_options_sort_order'];
02228   }
02229 
02231 // lookup attributes model
02232   function zen_get_language_name($lookup) {
02233     global $db;
02234     $check_language= $db->Execute("select directory from " . TABLE_LANGUAGES . " where languages_id = '" . (int)$lookup . "'");
02235     return $check_language->fields['directory'];
02236   }
02237 
02238 
02240 // Delete all product attributes
02241   function zen_delete_products_attributes($delete_product_id) {
02242     global $db;
02243     // delete associated downloads
02244     $products_delete_from = $db->Execute("select pa.products_id, pad.products_attributes_id from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad  where pa.products_id='" . (int)$delete_product_id . "' and pad.products_attributes_id= pa.products_attributes_id");
02245     while (!$products_delete_from->EOF) {
02246       $db->Execute("delete from " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " where products_attributes_id = '" . $products_delete_from->fields['products_attributes_id'] . "'");
02247       $products_delete_from->MoveNext();
02248     }
02249 
02250     $db->Execute("delete from " . TABLE_PRODUCTS_ATTRIBUTES . " where products_id = '" . (int)$delete_product_id . "'");
02251 }
02252 
02253 
02255 // Set Product Attributes Sort Order to Products Option Value Sort Order
02256   function zen_update_attributes_products_option_values_sort_order($products_id) {
02257     global $db;
02258     $attributes_sort_order = $db->Execute("select distinct pa.products_attributes_id, pa.options_id, pa.options_values_id, pa.products_options_sort_order, pov.products_options_values_sort_order from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_OPTIONS_VALUES . " pov where pa.products_id = '" . (int)$products_id . "' and pa.options_values_id = pov.products_options_values_id");
02259     while (!$attributes_sort_order->EOF) {
02260       $db->Execute("update " . TABLE_PRODUCTS_ATTRIBUTES . " set products_options_sort_order = '" . $attributes_sort_order->fields['products_options_values_sort_order'] . "' where products_id = '" . (int)$products_id . "' and products_attributes_id = '" . $attributes_sort_order->fields['products_attributes_id'] . "'");
02261       $attributes_sort_order->MoveNext();
02262     }
02263   }
02264 
02266 // product pulldown with attributes
02267   function zen_draw_products_pull_down_attributes($name, $parameters = '', $exclude = '') {
02268     global $db, $currencies;
02269 
02270     if ($exclude == '') {
02271       $exclude = array();
02272     }
02273 
02274     $select_string = '<select name="' . $name . '"';
02275 
02276     if ($parameters) {
02277       $select_string .= ' ' . $parameters;
02278     }
02279 
02280     $select_string .= '>';
02281 
02282     $new_fields=', p.products_model';
02283 
02284     $products = $db->Execute("select distinct p.products_id, pd.products_name, p.products_price" . $new_fields .
02285         " from " . TABLE_PRODUCTS . " p, " .
02286         TABLE_PRODUCTS_DESCRIPTION . " pd, " .
02287         TABLE_PRODUCTS_ATTRIBUTES . " pa " .
02288         " where p.products_id= pa.products_id and p.products_id = pd.products_id and pd.language_id = '" . (int)$_SESSION['languages_id'] . "' order by products_name");
02289 
02290     while (!$products->EOF) {
02291       if (!in_array($products->fields['products_id'], $exclude)) {
02292         $display_price = zen_get_products_base_price($products->fields['products_id']);
02293         $select_string .= '<option value="' . $products->fields['products_id'] . '">' . $products->fields['products_name'] . ' (' . TEXT_MODEL . ' ' . $products->fields['products_model'] . ') (' . $currencies->format($display_price) . ')</option>';
02294       }
02295       $products->MoveNext();
02296     }
02297 
02298     $select_string .= '</select>';
02299 
02300     return $select_string;
02301   }
02302 
02303 
02305 // categories pulldown with products
02306   function zen_draw_products_pull_down_categories($name, $parameters = '', $exclude = '', $show_id = false, $show_parent = false) {
02307     global $db, $currencies;
02308 
02309     if ($exclude == '') {
02310       $exclude = array();
02311     }
02312 
02313     $select_string = '<select name="' . $name . '"';
02314 
02315     if ($parameters) {
02316       $select_string .= ' ' . $parameters;
02317     }
02318 
02319     $select_string .= '>';
02320 
02321     $categories = $db->Execute("select distinct c.categories_id, cd.categories_name " .
02322         " from " . TABLE_CATEGORIES . " c, " .
02323         TABLE_CATEGORIES_DESCRIPTION . " cd, " .
02324         TABLE_PRODUCTS_TO_CATEGORIES . " ptoc " .
02325         " where ptoc.categories_id = c.categories_id and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$_SESSION['languages_id'] . "' order by categories_name");
02326 
02327     while (!$categories->EOF) {
02328       if (!in_array($categories->fields['categories_id'], $exclude)) {
02329         if ($show_parent == true) {
02330           $parent = zen_get_products_master_categories_name($categories->fields['categories_id']);
02331           if ($parent != '') {
02332             $parent = ' : in ' . $parent;
02333           }
02334         } else {
02335           $parent = '';
02336         }
02337         $select_string .= '<option value="' . $categories->fields['categories_id'] . '">' . $categories->fields['categories_name'] . $parent . ($show_id ? ' - ID#' . $categories->fields['categories_id'] : '') . '</option>';
02338       }
02339       $categories->MoveNext();
02340     }
02341 
02342     $select_string .= '</select>';
02343 
02344     return $select_string;
02345   }
02346 
02348 // categories pulldown with products with attributes
02349   function zen_draw_products_pull_down_categories_attributes($name, $parameters = '', $exclude = '') {
02350     global $db, $currencies;
02351 
02352     if ($exclude == '') {
02353       $exclude = array();
02354     }
02355 
02356     $select_string = '<select name="' . $name . '"';
02357 
02358     if ($parameters) {
02359       $select_string .= ' ' . $parameters;
02360     }
02361 
02362     $select_string .= '>';
02363 
02364     $categories = $db->Execute("select distinct c.categories_id, cd.categories_name " .
02365         " from " . TABLE_CATEGORIES . " c, " .
02366         TABLE_CATEGORIES_DESCRIPTION . " cd, " .
02367         TABLE_PRODUCTS_TO_CATEGORIES . " ptoc, " .
02368         TABLE_PRODUCTS_ATTRIBUTES . " pa " .
02369         " where pa.products_id= ptoc.products_id and ptoc.categories_id= c.categories_id and c.categories_id = cd.categories_id and cd.language_id = '" . (int)$_SESSION['languages_id'] . "' order by categories_name");
02370     while (!$categories->EOF) {
02371       if (!in_array($categories->fields['categories_id'], $exclude)) {
02372         $select_string .= '<option value="' . $categories->fields['categories_id'] . '">' . $categories->fields['categories_name'] . '</option>';
02373       }
02374       $categories->MoveNext();
02375     }
02376 
02377     $select_string .= '</select>';
02378 
02379     return $select_string;
02380   }
02381 
02382   function zen_get_top_level_domain($url) {
02383     if (strpos($url, '://')) {
02384       $url = parse_url($url);
02385       $url = $url['host'];
02386     }
02387     $domain_array = explode('.', $url);
02388     $domain_size = sizeof($domain_array);
02389     if ($domain_size > 1) {
02390       if (SESSION_USE_FQDN == 'True') return $url;
02391       if (is_numeric($domain_array[$domain_size-2]) && is_numeric($domain_array[$domain_size-1])) {
02392         return false;
02393       } else {
02394         $tld = "";
02395         foreach ($domain_array as $dPart)
02396         {
02397           if ($dPart != "www") $tld = $tld . "." . $dPart;
02398         }
02399         return substr($tld, 1);
02400       }
02401     } else {
02402       return false;
02403     }
02404   }
02405 
02407 // Check if a demo is active
02408   function zen_admin_demo() {
02409     return (ADMIN_DEMO == '1') ? TRUE : FALSE;
02410   }
02411 
02413 //
02414   function zen_has_product_attributes_downloads($products_id, $check_valid=false) {
02415     global $db;
02416     if (DOWNLOAD_ENABLED == 'true') {
02417       $download_display_query_raw ="select pa.products_attributes_id, pad.products_attributes_filename
02418                                     from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad
02419                                     where pa.products_id='" . (int)$products_id . "'
02420                                       and pad.products_attributes_id= pa.products_attributes_id";
02421       $download_display = $db->Execute($download_display_query_raw);
02422       if ($check_valid == true) {
02423         $valid_downloads = '';
02424         while (!$download_display->EOF) {
02425           if (!file_exists(DIR_FS_DOWNLOAD . $download_display->fields['products_attributes_filename'])) {
02426             $valid_downloads .= '<br />&nbsp;&nbsp;' . zen_image(DIR_WS_IMAGES . 'icon_status_red.gif') . ' Invalid: ' . $download_display->fields['products_attributes_filename'];
02427             // break;
02428           } else {
02429             $valid_downloads .= '<br />&nbsp;&nbsp;' . zen_image(DIR_WS_IMAGES . 'icon_status_green.gif') . ' Valid&nbsp;&nbsp;: ' . $download_display->fields['products_attributes_filename'];
02430           }
02431           $download_display->MoveNext();
02432         }
02433       } else {
02434         if ($download_display->RecordCount() != 0) {
02435           $valid_downloads = $download_display->RecordCount() . ' files';
02436         } else {
02437           $valid_downloads = 'none';
02438         }
02439       }
02440     } else {
02441       $valid_downloads = 'disabled';
02442     }
02443     return $valid_downloads;
02444   }
02445 
02447 // check if Product is set to use downloads
02448 // does not validate download filename
02449   function zen_has_product_attributes_downloads_status($products_id) {
02450     global $db;
02451     if (DOWNLOAD_ENABLED == 'true') {
02452       $download_display_query_raw ="select pa.products_attributes_id, pad.products_attributes_filename
02453                                     from " . TABLE_PRODUCTS_ATTRIBUTES . " pa, " . TABLE_PRODUCTS_ATTRIBUTES_DOWNLOAD . " pad
02454                                     where pa.products_id='" . (int)$products_id . "'
02455                                       and pad.products_attributes_id= pa.products_attributes_id";
02456 
02457       $download_display = $db->Execute($download_display_query_raw);
02458       if ($download_display->RecordCount() != 0) {
02459         $valid_downloads = false;
02460       } else {
02461         $valid_downloads = true;
02462       }
02463     } else {
02464       $valid_downloads = false;
02465     }
02466     return $valid_downloads;
02467   }
02468 
02470 // Construct a category path to the product
02471 // TABLES: products_to_categories
02472   function zen_get_product_path($products_id, $status_override = '1') {
02473     global $db;
02474     $cPath = '';
02475 
02476 /*
02477     $category_query = "select p2c.categories_id
02478                        from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
02479                        where p.products_id = '" . (int)$products_id . "' " .
02480                        ($status_override == 1 ? " and p.products_status = 1 " : '') . "
02481                        and p.products_id = p2c.products_id limit 1";
02482 */
02483 
02484     $category_query = "select p.products_id, p.master_categories_id
02485                        from " . TABLE_PRODUCTS . " p
02486                        where p.products_id = '" . (int)$products_id . "' limit 1";
02487 
02488 
02489     $category = $db->Execute($category_query);
02490 
02491     if ($category->RecordCount() > 0) {
02492 
02493       $categories = array();
02494       zen_get_parent_categories($categories, $category->fields['master_categories_id']);
02495 
02496       $categories = array_reverse($categories);
02497 
02498       $cPath = implode('_', $categories);
02499 
02500       if (zen_not_null($cPath)) $cPath .= '_';
02501       $cPath .= $category->fields['master_categories_id'];
02502     }
02503 
02504     return $cPath;
02505   }
02506 
02508 // Recursively go through the categories and retreive all parent categories IDs
02509 // TABLES: categories
02510   function zen_get_parent_categories(&$categories, $categories_id) {
02511     global $db;
02512     $parent_categories_query = "select parent_id
02513                                 from " . TABLE_CATEGORIES . "
02514                                 where categories_id = '" . (int)$categories_id . "'";
02515 
02516     $parent_categories = $db->Execute($parent_categories_query);
02517 
02518     while (!$parent_categories->EOF) {
02519       if ($parent_categories->fields['parent_id'] == 0) return true;
02520       $categories[sizeof($categories)] = $parent_categories->fields['parent_id'];
02521       if ($parent_categories->fields['parent_id'] != $categories_id) {
02522         zen_get_parent_categories($categories, $parent_categories->fields['parent_id']);
02523       }
02524       $parent_categories->MoveNext();
02525     }
02526   }
02527 
02529 // Return a product's category
02530 // TABLES: products_to_categories
02531   function zen_get_products_category_id($products_id) {
02532     global $db;
02533 
02534     $the_products_category_query = "select products_id, master_categories_id from " . TABLE_PRODUCTS . " where products_id = '" . (int)$products_id . "'";
02535     $the_products_category = $db->Execute($the_products_category_query);
02536 
02537     return $the_products_category->fields['master_categories_id'];
02538   }
02539 
02540 
02542 // Count how many subcategories exist in a category
02543 // TABLES: categories
02544 // old name zen_get_parent_category_name
02545   function zen_get_products_master_categories_name($categories_id) {
02546     global $db;
02547 
02548     $categories_lookup = $db->Execute("select parent_id
02549                                 from " . TABLE_CATEGORIES . "
02550                                 where categories_id = '" . (int)$categories_id . "'");
02551 
02552     $parent_name = zen_get_category_name($categories_lookup->fields['parent_id'], (int)$_SESSION['languages_id']);
02553 
02554     return $parent_name;
02555   }
02556 
02557 
02559 // configuration key value lookup
02560   function zen_get_configuration_key_value($lookup) {
02561     global $db;
02562     $configuration_query= $db->Execute("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key='" . zen_db_input($lookup) . "'");
02563     $lookup_value= $configuration_query->fields['configuration_value'];
02564     if ( $configuration_query->RecordCount() == 0 ) {
02565       $lookup_value='<span class="lookupAttention">' . $lookup . '</span>';
02566     }
02567     return $lookup_value;
02568   }
02569 
02570 
02572 // enable shipping
02573   function zen_get_shipping_enabled($shipping_module) {
02574     global $PHP_SELF, $cart, $order;
02575 
02576     // for admin always true if installed
02577     if (strstr($PHP_SELF, FILENAME_MODULES)) {
02578       return true;
02579     }
02580 
02581     $check_cart_free = $_SESSION['cart']->in_cart_check('product_is_always_free_shipping','1');
02582     $check_cart_cnt = $_SESSION['cart']->count_contents();
02583     $check_cart_weight = $_SESSION['cart']->show_weight();
02584 
02585     switch(true) {
02586       // for admin always true if installed
02587       case (strstr($PHP_SELF, FILENAME_MODULES)):
02588         return true;
02589         break;
02590       // Free Shipping when 0 weight - enable freeshipper - ORDER_WEIGHT_ZERO_STATUS must be on
02591       case (ORDER_WEIGHT_ZERO_STATUS == '1' and ($check_cart_weight == 0 and $shipping_module == 'freeshipper')):
02592         return true;
02593         break;
02594       // Free Shipping when 0 weight - disable everyone - ORDER_WEIGHT_ZERO_STATUS must be on
02595       case (ORDER_WEIGHT_ZERO_STATUS == '1' and ($check_cart_weight == 0 and $shipping_module != 'freeshipper')):
02596         return false;
02597         break;
02598       // Always free shipping only true - enable freeshipper
02599       case (($check_cart_free == $check_cart_cnt) and $shipping_module == 'freeshipper'):
02600         return true;
02601         break;
02602       // Always free shipping only true - disable everyone
02603       case (($check_cart_free == $check_cart_cnt) and $shipping_module != 'freeshipper'):
02604         return false;
02605         break;
02606       // Always free shipping only is false - disable freeshipper
02607       case (($check_cart_free != $check_cart_cnt) and $shipping_module == 'freeshipper'):
02608         return false;
02609         break;
02610       default:
02611         return true;
02612         break;
02613     }
02614   }
02615 
02616   function zen_get_handler_from_type($product_type) {
02617     global $db;
02618 
02619     $sql = "select type_handler from " . TABLE_PRODUCT_TYPES . " where type_id = '" . (int)$product_type . "'";
02620     $handler = $db->Execute($sql);
02621   return $handler->fields['type_handler'];
02622   }
02623 
02624 /*
02626 // Sets the status of a featured product
02627   function zen_set_featured_status($featured_id, $status) {
02628     global $db;
02629     if ($status == '1') {
02630       return $db->Execute("update " . TABLE_FEATURED . "
02631                            set status = '1', expires_date = NULL, date_status_change = NULL
02632                            where featured_id = '" . (int)$featured_id . "'");
02633 
02634     } elseif ($status == '0') {
02635       return $db->Execute("update " . TABLE_FEATURED . "
02636                            set status = '0', date_status_change = now()
02637                            where featured_id = '" . (int)$featured_id . "'");
02638 
02639     } else {
02640       return -1;
02641     }
02642   }
02643 */
02644 
02646 // Sets the status of a product review
02647   function zen_set_reviews_status($review_id, $status) {
02648     global $db;
02649     if ($status == '1') {
02650       return $db->Execute("update " . TABLE_REVIEWS . "
02651                            set status = 1
02652                            where reviews_id = '" . (int)$review_id . "'");
02653 
02654     } elseif ($status == '0') {
02655       return $db->Execute("update " . TABLE_REVIEWS . "
02656                            set status = 0
02657                            where reviews_id = '" . (int)$review_id . "'");
02658 
02659     } else {
02660       return -1;
02661     }
02662   }
02663 
02664 
02665 
02666 
02667 
02668 
02670 // set the products_price_sorter
02671   function zen_update_products_price_sorter($product_id) {
02672     global $db;
02673 
02674     $products_price_sorter = zen_get_products_actual_price($product_id);
02675 
02676     $db->Execute("update " . TABLE_PRODUCTS . " set
02677          products_price_sorter='" . zen_db_prepare_input($products_price_sorter) . "'
02678          where products_id='" . (int)$product_id . "'");
02679 
02680   }
02681 
02683 // configuration key value lookup in TABLE_PRODUCT_TYPE_LAYOUT
02684   function zen_get_configuration_key_value_layout($lookup, $type=1) {
02685     global $db;
02686     $configuration_query= $db->Execute("select configuration_value from " . TABLE_PRODUCT_TYPE_LAYOUT . " where configuration_key='" . zen_db_input($lookup) . "' and product_type_id='". (int)$type . "'");
02687     $lookup_value= $configuration_query->fields['configuration_value'];
02688     if ( !($lookup_value) ) {
02689       $lookup_value='<span class="lookupAttention">' . $lookup . '</span>';
02690     }
02691     return $lookup_value;
02692   }
02693 
02695 // Return true if the category has subcategories
02696 // TABLES: categories
02697   function zen_has_category_subcategories($category_id) {
02698     global $db;
02699     $child_category_query = "select count(*) as count
02700                              from " . TABLE_CATEGORIES . "
02701                              where parent_id = '" . (int)$category_id . "'";
02702 
02703     $child_category = $db->Execute($child_category_query);
02704 
02705     if ($child_category->fields['count'] > 0) {
02706       return true;
02707     } else {
02708       return false;
02709     }
02710   }
02711 
02713   function zen_get_categories($categories_array = '', $parent_id = '0', $indent = '') {
02714     global $db;
02715 
02716     if (!is_array($categories_array)) $categories_array = array();
02717 
02718     $categories_query = "select c.categories_id, cd.categories_name
02719                          from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd
02720                          where parent_id = '" . (int)$parent_id . "'
02721                          and c.categories_id = cd.categories_id
02722                          and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
02723                          order by sort_order, cd.categories_name";
02724 
02725     $categories = $db->Execute($categories_query);
02726 
02727     while (!$categories->EOF) {
02728       $categories_array[] = array('id' => $categories->fields['categories_id'],
02729                                   'text' => $indent . $categories->fields['categories_name']);
02730 
02731       if ($categories->fields['categories_id'] != $parent_id) {
02732         $categories_array = zen_get_categories($categories_array, $categories->fields['categories_id'], $indent . '&nbsp;&nbsp;');
02733       }
02734       $categories->MoveNext();
02735     }
02736 
02737     return $categories_array;
02738   }
02739 
02740 
02742 // Get the status of a category
02743   function zen_get_categories_status($categories_id) {
02744     global $db;
02745     $sql = "select categories_status from " . TABLE_CATEGORIES . (zen_not_null($categories_id) ? " where categories_id=" . (int)$categories_id : "");
02746     $check_status = $db->Execute($sql);
02747     return $check_status->fields['categories_status'];
02748   }
02749 
02751 // Get the status of a product
02752   function zen_get_products_status($product_id) {
02753     global $db;
02754     $sql = "select products_status from " . TABLE_PRODUCTS . (zen_not_null($product_id) ? " where products_id=" . (int)$product_id : "");
02755     $check_status = $db->Execute($sql);
02756     return $check_status->fields['products_status'];
02757   }
02758 
02760 // check if linked
02761   function zen_get_product_is_linked($product_id, $show_count = 'false') {
02762     global $db;
02763 
02764     $sql = "select * from " . TABLE_PRODUCTS_TO_CATEGORIES . (zen_not_null($product_id) ? " where products_id=" . (int)$product_id : "");
02765     $check_linked = $db->Execute($sql);
02766     if ($check_linked->RecordCount() > 1) {
02767       if ($show_count == 'true') {
02768         return $check_linked->RecordCount();
02769       } else {
02770         return 'true';
02771       }
02772     } else {
02773       return 'false';
02774     }
02775   }
02776 
02777 
02779 // TABLES: categories_name from products_id
02780   function zen_get_categories_name_from_product($product_id) {
02781     global $db;
02782 
02783 //    $check_products_category= $db->Execute("select products_id, categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id='" . $product_id . "' limit 1");
02784     $check_products_category = $db->Execute("select products_id, master_categories_id from " . TABLE_PRODUCTS . " where products_id = '" . (int)$product_id . "'");
02785     $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= '" . (int)$_SESSION['languages_id'] . "'");
02786 
02787     return $the_categories_name->fields['categories_name'];
02788   }
02789 
02790   function zen_count_products_in_cats($category_id) {
02791     global $db;
02792     $cat_products_query = "select count(if (p.products_status=1,1,NULL)) as pr_on, count(*) as total
02793                            from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
02794                            where p.products_id = p2c.products_id
02795                            and p2c.categories_id = '" . (int)$category_id . "'";
02796 
02797     $pr_count = $db->Execute($cat_products_query);
02798 //    echo $pr_count->RecordCount();
02799     $c_array['this_count'] += $pr_count->fields['total'];
02800     $c_array['this_count_on'] += $pr_count->fields['pr_on'];
02801 
02802     $cat_child_categories_query = "select categories_id
02803                                from " . TABLE_CATEGORIES . "
02804                                where parent_id = '" . (int)$category_id . "'";
02805 
02806     $cat_child_categories = $db->Execute($cat_child_categories_query);
02807 
02808     if ($cat_child_categories->RecordCount() > 0) {
02809       while (!$cat_child_categories->EOF) {
02810           $m_array = zen_count_products_in_cats($cat_child_categories->fields['categories_id']);
02811           $c_array['this_count'] += $m_array['this_count'];
02812           $c_array['this_count_on'] += $m_array['this_count_on'];
02813 
02814 //          $this_count_on += $pr_count->fields['pr_on'];
02815         $cat_child_categories->MoveNext();
02816       }
02817     }
02818     return $c_array;
02819  }
02820 
02822 // Return the number of products in a category
02823 // TABLES: products, products_to_categories, categories
02824 // syntax for count: zen_get_products_to_categories($categories->fields['categories_id'], true)
02825 // syntax for linked products: zen_get_products_to_categories($categories->fields['categories_id'], true, 'products_active')
02826   function zen_get_products_to_categories($category_id, $include_inactive = false, $counts_what = 'products') {
02827     global $db;
02828 
02829     $products_count = 0;
02830     if ($include_inactive == true) {
02831       switch ($counts_what) {
02832         case ('products'):
02833         $cat_products_query = "select count(*) as total
02834                            from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
02835                            where p.products_id = p2c.products_id
02836                            and p2c.categories_id = '" . (int)$category_id . "'";
02837         break;
02838         case ('products_active'):
02839         $cat_products_query = "select p.products_id
02840                            from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
02841                            where p.products_id = p2c.products_id
02842                            and p2c.categories_id = '" . (int)$category_id . "'";
02843         break;
02844       }
02845 
02846     } else {
02847       switch ($counts_what) {
02848         case ('products'):
02849           $cat_products_query = "select count(*) as total
02850                              from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
02851                              where p.products_id = p2c.products_id
02852                              and p.products_status = 1
02853                              and p2c.categories_id = '" . (int)$category_id . "'";
02854         break;
02855         case ('products_active'):
02856           $cat_products_query = "select p.products_id
02857                              from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
02858                              where p.products_id = p2c.products_id
02859                              and p.products_status = 1
02860                              and p2c.categories_id = '" . (int)$category_id . "'";
02861         break;
02862       }
02863 
02864     }
02865     $cat_products = $db->Execute($cat_products_query);
02866       switch ($counts_what) {
02867         case ('products'):
02868           $cat_products_count += $cat_products->fields['total'];
02869           break;
02870         case ('products_active'):
02871         while (!$cat_products->EOF) {
02872           if (zen_get_product_is_linked($cat_products->fields['products_id']) == 'true') {
02873             return $products_linked = 'true';
02874           }
02875           $cat_products->MoveNext();
02876         }
02877           break;
02878       }
02879 
02880     $cat_child_categories_query = "select categories_id
02881                                from " . TABLE_CATEGORIES . "
02882                                where parent_id = '" . (int)$category_id . "'";
02883 
02884     $cat_child_categories = $db->Execute($cat_child_categories_query);
02885 
02886     if ($cat_child_categories->RecordCount() > 0) {
02887       while (!$cat_child_categories->EOF) {
02888       switch ($counts_what) {
02889         case ('products'):
02890           $cat_products_count += zen_get_products_to_categories($cat_child_categories->fields['categories_id'], $include_inactive);
02891           break;
02892         case ('products_active'):
02893           if (zen_get_products_to_categories($cat_child_categories->fields['categories_id'], true, 'products_active') == 'true') {
02894             return $products_linked = 'true';
02895           }
02896           break;
02897         }
02898         $cat_child_categories->MoveNext();
02899       }
02900     }
02901 
02902 
02903       switch ($counts_what) {
02904         case ('products'):
02905           return $cat_products_count;
02906           break;
02907         case ('products_active'):
02908           return $products_linked;
02909           break;
02910       }
02911   }
02912 
02914 // master category selection
02915   function zen_get_master_categories_pulldown($product_id) {
02916     global $db;
02917 
02918     $master_category_array = array();
02919 
02920     $master_categories_query = $db->Execute("select ptc.products_id, cd.categories_name, cd.categories_id
02921                                     from " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc
02922                                     left join " . TABLE_CATEGORIES_DESCRIPTION . " cd
02923                                     on cd.categories_id = ptc.categories_id
02924                                     where ptc.products_id='" . (int)$product_id . "'
02925                                     and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
02926                                     ");
02927 
02928     $master_category_array[] = array('id' => '0', 'text' => TEXT_INFO_SET_MASTER_CATEGORIES_ID);
02929     while (!$master_categories_query->EOF) {
02930       $master_category_array[] = array('id' => $master_categories_query->fields['categories_id'], 'text' => $master_categories_query->fields['categories_name'] . TEXT_INFO_ID . $master_categories_query->fields['categories_id']);
02931       $master_categories_query->MoveNext();
02932     }
02933 
02934     return $master_category_array;
02935   }
02936 
02938 // get products type
02939   function zen_get_products_type($product_id) {
02940     global $db;
02941 
02942     $check_products_type = $db->Execute("select products_type from " . TABLE_PRODUCTS . " where products_id='" . (int)$product_id . "'");
02943     return $check_products_type->fields['products_type'];
02944   }
02945 
02947 //  ++++ modified for UPS Choice 1.8 and USPS Methods 2.5 by Brad Waite and Fritz Clapp ++++
02948 //  ++++ modified for USPS Methods 2.5 08/02/03 by Brad Waite and Fritz Clapp ++++
02949 // USPS Methods 2.5
02950 // Alias function for Store configuration values in the Administration Tool
02951   function zen_cfg_select_multioption($select_array, $key_value, $key = '') {
02952     for ($i=0; $i<sizeof($select_array); $i++) {
02953       $name = (($key) ? 'configuration[' . $key . '][]' : 'configuration_value');
02954       $string .= '<br><input type="checkbox" name="' . $name . '" value="' . $select_array[$i] . '"';
02955       $key_values = explode( ", ", $key_value);
02956       if ( in_array($select_array[$i], $key_values) ) $string .= ' CHECKED';
02957       $string .= ' id="' . strtolower($select_array[$i] . '-' . $name) . '"> ' . '<label for="' . strtolower($select_array[$i] . '-' . $name) . '" class="inputSelect">' . $select_array[$i] . '</label>' . "\n";
02958     }
02959     $string .= '<input type="hidden" name="' . $name . '" value="--none--">';
02960     return $string;
02961   }
02962 
02964 // get products image
02965   function zen_get_products_image($product_id) {
02966     global $db;
02967     $product_image = $db->Execute("select products_image
02968                                    from " . TABLE_PRODUCTS . "
02969                                    where products_id = '" . (int)$product_id . "'");
02970 
02971     return $product_image->fields['products_image'];
02972   }
02973 
02974 
02976 // remove common HTML from text for display as paragraph
02977   function zen_clean_html($clean_it) {
02978 
02979     $clean_it = preg_replace('/\r/', ' ', $clean_it);
02980     $clean_it = preg_replace('/\t/', ' ', $clean_it);
02981     $clean_it = preg_replace('/\n/', ' ', $clean_it);
02982 
02983     $clean_it= nl2br($clean_it);
02984 
02985 // update breaks with a space for text displays in all listings with descriptions
02986     while (strstr($clean_it, '<br>')) $clean_it = str_replace('<br>', ' ', $clean_it);
02987     while (strstr($clean_it, '<br />')) $clean_it = str_replace('<br />', ' ', $clean_it);
02988     while (strstr($clean_it, '<br/>')) $clean_it = str_replace('<br/>', ' ', $clean_it);
02989     while (strstr($clean_it, '<p>')) $clean_it = str_replace('<p>', ' ', $clean_it);
02990     while (strstr($clean_it, '</p>')) $clean_it = str_replace('</p>', ' ', $clean_it);
02991 
02992     while (strstr($clean_it, '  ')) $clean_it = str_replace('  ', ' ', $clean_it);
02993 
02994 // remove other html code to prevent problems on display of text
02995     $clean_it = strip_tags($clean_it);
02996     return $clean_it;
02997   }
02998 
02999 
03001 // find template or default file
03002   function zen_get_file_directory($check_directory, $check_file, $dir_only = 'false') {
03003     global $template_dir;
03004 
03005     $zv_filename = $check_file;
03006     if (!strstr($zv_filename, '.php')) $zv_filename .= '.php';
03007 
03008     if (file_exists($check_directory . $template_dir . '/' . $zv_filename)) {
03009       $zv_directory = $check_directory . $template_dir . '/';
03010     } else {
03011       $zv_directory = $check_directory;
03012     }
03013 
03014     if ($dir_only == 'true') {
03015       return $zv_directory;
03016     } else {
03017       return $zv_directory . $zv_filename;
03018     }
03019   }
03020 
03022 // Recursive algorithim to restrict all sub_categories to a rpoduct type
03023   function zen_restrict_sub_categories($zf_cat_id, $zf_type) {
03024     global $db;
03025     $zp_sql = "select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$zf_cat_id . "'";
03026     $zq_sub_cats = $db->Execute($zp_sql);
03027     while (!$zq_sub_cats->EOF) {
03028       $zp_sql = "select * from " . TABLE_PRODUCT_TYPES_TO_CATEGORY . "
03029                          where category_id = '" . (int)$zq_sub_cats->fields['categories_id'] . "'
03030                          and product_type_id = '" . (int)$zf_type . "'";
03031 
03032       $zq_type_to_cat = $db->Execute($zp_sql);
03033 
03034       if ($zq_type_to_cat->RecordCount() < 1) {
03035         $za_insert_sql_data = array('category_id' => (int)$zq_sub_cats->fields['categories_id'],
03036                                     'product_type_id' => (int)$zf_type);
03037         zen_db_perform(TABLE_PRODUCT_TYPES_TO_CATEGORY, $za_insert_sql_data);
03038       }
03039       zen_restrict_sub_categories($zq_sub_cats->fields['categories_id'], $zf_type);
03040       $zq_sub_cats->MoveNext();
03041     }
03042   }
03043 
03044 
03046 // Recursive algorithim to restrict all sub_categories to a rpoduct type
03047   function zen_remove_restrict_sub_categories($zf_cat_id, $zf_type) {
03048     global $db;
03049     $zp_sql = "select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . (int)$zf_cat_id . "'";
03050     $zq_sub_cats = $db->Execute($zp_sql);
03051     while (!$zq_sub_cats->EOF) {
03052         $sql = "delete from " .  TABLE_PRODUCT_TYPES_TO_CATEGORY . "
03053                 where category_id = '" . (int)$zq_sub_cats->fields['categories_id'] . "'
03054                 and product_type_id = '" . (int)$zf_type . "'";
03055 
03056         $db->Execute($sql);
03057       zen_remove_restrict_sub_categories($zq_sub_cats->fields['categories_id'], $zf_type);
03058       $zq_sub_cats->MoveNext();
03059     }
03060   }
03061 
03062 
03063 // build configuration_key based on product type and return its value
03064 // example: To get the settings for metatags_products_name_status for a product use:
03065 // zen_get_show_product_switch($_GET['pID'], 'metatags_products_name_status')
03066 // the product is looked up for the products_type which then builds the configuration_key example:
03067 // SHOW_PRODUCT_INFO_METATAGS_PRODUCTS_NAME_STATUS
03068 // the value of the configuration_key is then returned
03069 // NOTE: keys are looked up first in the product_type_layout table and if not found looked up in the configuration table.
03070     function zen_get_show_product_switch($lookup, $field, $suffix= 'SHOW_', $prefix= '_INFO', $field_prefix= '_', $field_suffix='') {
03071       global $db;
03072 
03073       $sql = "select products_type from " . TABLE_PRODUCTS . " where products_id='" . (int)$lookup . "'";
03074       $type_lookup = $db->Execute($sql);
03075 
03076       $sql = "select type_handler from " . TABLE_PRODUCT_TYPES . " where type_id = '" . (int)$type_lookup->fields['products_type'] . "'";
03077       $show_key = $db->Execute($sql);
03078 
03079       $zv_key = strtoupper($suffix . $show_key->fields['type_handler'] . $prefix . $field_prefix . $field . $field_suffix);
03080 
03081       $sql = "select configuration_key, configuration_value from " . TABLE_PRODUCT_TYPE_LAYOUT . " where configuration_key='" . zen_db_input($zv_key) . "'";
03082       $zv_key_value = $db->Execute($sql);
03083 //echo 'I CAN SEE - look ' . $lookup . ' - field ' . $field . ' - key ' . $zv_key . ' value ' . $zv_key_value->fields['configuration_value'] .'<br>';
03084 
03085       if ($zv_key_value->RecordCount() > 0) {
03086         return $zv_key_value->fields['configuration_value'];
03087       } else {
03088         $sql = "select configuration_key, configuration_value from " . TABLE_CONFIGURATION . " where configuration_key='" . zen_db_input($zv_key) . "'";
03089         $zv_key_value = $db->Execute($sql);
03090         if ($zv_key_value->RecordCount() > 0) {
03091           return $zv_key_value->fields['configuration_value'];
03092         } else {
03093           return $zv_key_value->fields['configuration_value'];
03094         }
03095       }
03096     }
03097 
03098 
03100 // return switch name
03101     function zen_get_show_product_switch_name($lookup, $field, $suffix= 'SHOW_', $prefix= '_INFO', $field_prefix= '_', $field_suffix='') {
03102       global $db;
03103 
03104       $sql = "select products_type from " . TABLE_PRODUCTS . " where products_id='" . (int)$lookup . "'";
03105       $type_lookup = $db->Execute($sql);
03106 
03107       $sql = "select type_handler from " . TABLE_PRODUCT_TYPES . " where type_id = '" . (int)$type_lookup->fields['products_type'] . "'";
03108       $show_key = $db->Execute($sql);
03109 
03110 
03111       $zv_key = strtoupper($suffix . $show_key->fields['type_handler'] . $prefix . $field_prefix . $field . $field_suffix);
03112 
03113       return $zv_key;
03114     }
03115 
03116 
03118 // compute the days between two dates
03119   function zen_date_diff($date1, $date2) {
03120   //$date1  today, or any other day
03121   //$date2  date to check against
03122 
03123     $d1 = explode("-", $date1);
03124     $y1 = $d1[0];
03125     $m1 = $d1[1];
03126     $d1 = $d1[2];
03127 
03128     $d2 = explode("-", $date2);
03129     $y2 = $d2[0];
03130     $m2 = $d2[1];
03131     $d2 = $d2[2];
03132 
03133     $date1_set = mktime(0,0,0, $m1, $d1, $y1);
03134     $date2_set = mktime(0,0,0, $m2, $d2, $y2);
03135 
03136     return(round(($date2_set-$date1_set)/(60*60*24)));
03137   }
03138 
03140 // check that a download filename exists
03141   function zen_orders_products_downloads($check_filename) {
03142     global $db;
03143 
03144     $valid_downloads = true;
03145 
03146     // Moved to /admin/includes/configure.php
03147     if (!defined('DIR_FS_DOWNLOAD')) define('DIR_FS_DOWNLOAD', DIR_FS_CATALOG . 'download/');
03148 
03149     if (!file_exists(DIR_FS_DOWNLOAD . $check_filename)) {
03150       $valid_downloads = false;
03151     // break;
03152     } else {
03153       $valid_downloads = true;
03154     }
03155 
03156     return $valid_downloads;
03157   }
03158 
03160 // salemaker categories array
03161   function zen_parse_salemaker_categories($clist) {
03162     $clist_array = explode(',', $clist);
03163 
03164 // make sure no duplicate category IDs exist which could lock the server in a loop
03165     $tmp_array = array();
03166     $n = sizeof($clist_array);
03167     for ($i=0; $i<$n; $i++) {
03168       if (!in_array($clist_array[$i], $tmp_array)) {
03169         $tmp_array[] = $clist_array[$i];
03170       }
03171     }
03172     return $tmp_array;
03173   }
03174 
03176 // update salemaker product prices per category per product
03177   function zen_update_salemaker_product_prices($salemaker_id) {
03178     global $db;
03179     $zv_categories = $db->Execute("select sale_categories_selected from " . TABLE_SALEMAKER_SALES . " where sale_id = '" . (int)$salemaker_id . "'");
03180 
03181     $za_salemaker_categories = zen_parse_salemaker_categories($zv_categories->fields['sale_categories_selected']);
03182     $n = sizeof($za_salemaker_categories);
03183     for ($i=0; $i<$n; $i++) {
03184       $update_products_price = $db->Execute("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id='" . (int)$za_salemaker_categories[$i] . "'");
03185       while (!$update_products_price->EOF) {
03186         zen_update_products_price_sorter($update_products_price->fields['products_id']);
03187         $update_products_price->MoveNext();
03188       }
03189     }
03190   }
03191 
03193 // check if products has discounts
03194   function zen_has_product_discounts($look_up) {
03195     global $db;
03196 
03197     $check_discount_query = "select products_id from " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . " where products_id='" . (int)$look_up . "'";
03198     $check_discount = $db->Execute($check_discount_query);
03199 
03200     if ($check_discount->RecordCount() > 0) {
03201       return 'true';
03202     } else {
03203       return 'false';
03204     }
03205   }
03206 
03208 //copy discounts from product to another
03209   function zen_copy_discounts_to_product($copy_from, $copy_to) {
03210     global $db;
03211 
03212     $check_discount_type_query = "select products_discount_type, products_discount_type_from, products_mixed_discount_quantity from " . TABLE_PRODUCTS . " where products_id='" . (int)$copy_from . "'";
03213     $check_discount_type = $db->Execute($check_discount_type_query);
03214 
03215     $db->Execute("update " . TABLE_PRODUCTS . " set products_discount_type='" . $check_discount_type->fields['products_discount_type'] . "', products_discount_type_from='" . $check_discount_type->fields['products_discount_type_from'] . "', products_mixed_discount_quantity='" . $check_discount_type->fields['products_mixed_discount_quantity'] . "' where products_id='" . (int)$copy_to . "'");
03216 
03217     $check_discount_query = "select * from " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . " where products_id='" . (int)$copy_from . "' order by discount_id";
03218     $check_discount = $db->Execute($check_discount_query);
03219     $cnt_discount=1;
03220     while (!$check_discount->EOF) {
03221       $db->Execute("insert into " . TABLE_PRODUCTS_DISCOUNT_QUANTITY . "
03222                   (discount_id, products_id, discount_qty, discount_price )
03223                   values ('" . (int)$cnt_discount . "', '" . (int)$copy_to . "', '" . $check_discount->fields['discount_qty'] . "', '" . $check_discount->fields['discount_price'] . "')");
03224       $cnt_discount++;
03225       $check_discount->MoveNext();
03226     }
03227   }
03228 
03229 
03231 // return products master_categories_id
03232 // TABLES: categories
03233   function zen_get_parent_category_id($product_id) {
03234     global $db;
03235 
03236     $categories_lookup = $db->Execute("select master_categories_id
03237                                 from " . TABLE_PRODUCTS . "
03238                                 where products_id = '" . (int)$product_id . "'");
03239 
03240     $parent_id = $categories_lookup->fields['master_categories_id'];
03241 
03242     return $parent_id;
03243   }
03244 
03245 // replacement for fmod to manage values < 1
03246   function fmod_round($x, $y) {
03247     $x = strval($x);
03248     $y = strval($y);
03249     $zc_round = ($x*1000)/($y*1000);
03250     $zc_round_ceil = (int)($zc_round);
03251     $multiplier = $zc_round_ceil * $y;
03252     $results = abs(round($x - $multiplier, 6));
03253      return $results;
03254   }
03255 
03257 // return any field from products or products_description table
03258 // Example: zen_products_lookup('3', 'products_date_added');
03259 //  function zen_products_lookup($product_id, $what_field = 'products_name', $language = $_SESSION['languages_id']) {
03260   function zen_products_lookup($product_id, $what_field = 'products_name', $language = '') {
03261     global $db;
03262 
03263     if (empty($language)) $language = $_SESSION['languages_id'];
03264 
03265     $product_lookup = $db->Execute("select " . zen_db_input($what_field) . " as lookup_field
03266                               from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd
03267                               where  p.products_id ='" . (int)$product_id . "'
03268                               and pd.products_id = p.products_id
03269                               and pd.language_id = '" . (int)$language . "'");
03270 
03271     $return_field = $product_lookup->fields['lookup_field'];
03272 
03273     return $return_field;
03274   }
03275 
03276   function zen_count_days($start_date, $end_date, $lookup = 'm') {
03277     if ($lookup == 'd') {
03278     // Returns number of days
03279       $start_datetime = gmmktime (0, 0, 0, substr ($start_date, 5, 2), substr ($start_date, 8, 2), substr ($start_date, 0, 4));
03280       $end_datetime = gmmktime (0, 0, 0, substr ($end_date, 5, 2), substr ($end_date, 8, 2), substr ($end_date, 0, 4));
03281       $days = (($end_datetime - $start_datetime) / 86400) + 1;
03282       $d = $days % 7;
03283       $w = date("w", $start_datetime);
03284       $result = floor ($days / 7) * 5;
03285       $counter = $result + $d - (($d + $w) >= 7) - (($d + $w) >= 8) - ($w == 0);
03286     }
03287     if ($lookup == 'm') {
03288     // Returns whole-month-count between two dates
03289     // courtesy of websafe<at>partybitchez<dot>org
03290       $start_date_unixtimestamp = strtotime($start_date);
03291       $start_date_month = date("m", $start_date_unixtimestamp);
03292       $end_date_unixtimestamp = strtotime($end_date);
03293       $end_date_month = date("m", $end_date_unixtimestamp);
03294       $calculated_date_unixtimestamp = $start_date_unixtimestamp;
03295       $counter=0;
03296       while ($calculated_date_unixtimestamp < $end_date_unixtimestamp) {
03297         $counter++;
03298         $calculated_date_unixtimestamp = strtotime($start_date . " +{$counter} months");
03299       }
03300       if ( ($counter==1) && ($end_date_month==$start_date_month)) $counter=($counter-1);
03301     }
03302     return $counter;
03303   }
03304 
03306 // Get all products_id in a Category and its SubCategories
03307 // use as:
03308 // $my_products_id_list = array();
03309 // $my_products_id_list = zen_get_categories_products_list($categories_id)
03310   function zen_get_categories_products_list($categories_id, $include_deactivated = false, $include_child = true) {
03311     global $db;
03312     global $categories_products_id_list;
03313 
03314     if ($include_deactivated) {
03315 
03316       $products = $db->Execute("select p.products_id
03317                                 from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
03318                                 where p.products_id = p2c.products_id
03319                                 and p2c.categories_id = '" . (int)$categories_id . "'");
03320     } else {
03321       $products = $db->Execute("select p.products_id
03322                                 from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c
03323                                 where p.products_id = p2c.products_id
03324                                 and p.products_status = '1'
03325                                 and p2c.categories_id = '" . (int)$categories_id . "'");
03326     }
03327 
03328     while (!$products->EOF) {
03329 // categories_products_id_list keeps resetting when category changes ...
03330 //      echo 'Products ID: ' . $products->fields['products_id'] . '<br>';
03331       $categories_products_id_list[] = $products->fields['products_id'];
03332       $products->MoveNext();
03333     }
03334 
03335     if ($include_child) {
03336       $childs = $db->Execute("select categories_id from " . TABLE_CATEGORIES . "
03337                               where parent_id = '" . (int)$categories_id . "'");
03338       if ($childs->RecordCount() > 0 ) {
03339         while (!$childs->EOF) {
03340           zen_get_categories_products_list($childs->fields['categories_id'], $include_deactivated);
03341           $childs->MoveNext();
03342         }
03343       }
03344     }
03345     $products_id_listing = $categories_products_id_list;
03346     return $products_id_listing;
03347   }
03348 
03349   function zen_geo_zones_pull_down_coupon($parameters, $selected = '') {
03350     global $db;
03351     $select_string = '<select ' . $parameters . '>';
03352     $zones = $db->Execute("select geo_zone_id, geo_zone_name
03353                                  from " . TABLE_GEO_ZONES . "
03354                                  order by geo_zone_name");
03355 
03356     if ($selected == 0) {
03357       $select_string .= '<option value=0 SELECTED>' . TEXT_NONE . '</option>';
03358     } else {
03359       $select_string .= '<option value=0>' . TEXT_NONE . '</option>';
03360     }
03361 
03362     while (!$zones->EOF) {
03363       $select_string .= '<option value="' . $zones->fields['geo_zone_id'] . '"';
03364       if ($selected == $zones->fields['geo_zone_id']) $select_string .= ' SELECTED';
03365       $select_string .= '>' . $zones->fields['geo_zone_name'] . '</option>';
03366       $zones->MoveNext();
03367     }
03368     $select_string .= '</select>';
03369 
03370     return $select_string;
03371   }
03372 
03373 // customer lookup of address book
03374   function zen_get_customers_address_book($customer_id) {
03375     global $db;
03376 
03377     $customer_address_book_count_query = "SELECT c.*, ab.* from " .
03378                                           TABLE_CUSTOMERS . " c
03379                                           left join " . TABLE_ADDRESS_BOOK . " ab on c.customers_id = ab.customers_id
03380                                           WHERE c.customers_id = '" . (int)$customer_id . "'";
03381 
03382     $customer_address_book_count = $db->Execute($customer_address_book_count_query);
03383     return $customer_address_book_count;
03384   }
03385 
03386 // get customer comments
03387   function zen_get_orders_comments($orders_id) {
03388     global $db;
03389     $orders_comments_query = "SELECT osh.comments from " .
03390                               TABLE_ORDERS_STATUS_HISTORY . " osh
03391                               where osh.orders_id = '" . (int)$orders_id . "'
03392                               order by osh.orders_status_history_id
03393                               limit 1";
03394 
03395     $orders_comments = $db->Execute($orders_comments_query);
03396     return $orders_comments->fields['comments'];
03397   }
03398 
03399 // manufacturers name
03400   function zen_get_products_manufacturers_name($product_id) {
03401     global $db;
03402 
03403     $product_query = "select m.manufacturers_name
03404                       from " . TABLE_PRODUCTS . " p, " .
03405                             TABLE_MANUFACTURERS . " m
03406                       where p.products_id = '" . (int)$product_id . "'
03407                       and p.manufacturers_id = m.manufacturers_id";
03408 
03409     $product =$db->Execute($product_query);
03410 
03411     return ($product->RecordCount() > 0) ? $product->fields['manufacturers_name'] : "";
03412   }
03413 
03414     function zen_user_has_gv_balance($c_id) {
03415       global $db;
03416         $gv_result = $db->Execute("select amount from " . TABLE_COUPON_GV_CUSTOMER . " where customer_id = '" . (int)$c_id . "'");
03417         if ($gv_result->RecordCount() > 0) {
03418           if ($gv_result->fields['amount'] > 0) {
03419             return $gv_result->fields['amount'];
03420           }
03421         }
03422         return 0;
03423     }
 All Data Structures Namespaces Files Functions Variables Enumerations