listProducts(); if (count($Products) < 1) { $Cats = $Category->listCategories(); foreach ($Cats as $NewCategory) { $Products = findProducts($NewCategory); if (count($Products) > 0) break; } } return $Products; } function findProducts($Category) { $Product = $Category->getProductWithPicture(); if ($Product == NULL) { $Cats = $Category->listCategories(); foreach ($Cats as $NewCategory) { $Product = findProducts($NewCategory); if ($Product != NULL) break; } } return $Product; } // Initialize system $Config = new CConfig; $DbConnection = $Config->getDbConnection(); $Smarty = new CSmarty($DbConnection); include 'lang.php'; mysql_query('SET NAMES utf8'); $Smarty->assign("lang", $lang); $_SESSION['unit_system'] = $unit_system; $Smarty->assign("unit_system", $unit_system); //Remove Language and Unit Systems preferences, they are strored in server variables $query_string = $_SERVER['QUERY_STRING']; $query_string = preg_replace('/&?(lang|unit_system)=[a-z]+/', '', $query_string); $Smarty->assign('query_string', $query_string); //Get a list of all preferences $prefs = $Config->listPreferences(); $Smarty->assign("prefs", $prefs); $ThisCategory = new CCategory($DbConnection, $cat_id); // Unit system if (isset($_GET['unit_system']) and $_GET['unit_system']) { $unit_system = $_GET['unit_system']; } elseif (isset($_SESSION['unit_system']) and $_SESSION['unit_system']) { $unit_system = $_SESSION['unit_system']; } else { $unit_system = $prefs['default_unit_system']; } // Step 1: Initialize // Check if Login is required if ($prefs['login_required'] == 'yes') { $UserLoggedIn = checkAccess($DbConnection, $administratorRequired=FALSE); } $logged_in = false; $greetingName = ''; if ($prefs['allow_reg_login'] == "yes" && $user != "") { $List = new CUserList($DbConnection); $User = $List->getUser($user); if (isset($User)) { if ($User->authenticate($user, $passwd, $error_msg)) { $logged_in = true; $Smarty->assign("user_email", $User->getAttribute("email")); $Smarty->assign("user_id", $User->getAttribute("user_id")); $firstName = $User->getAttribute('first_name'); $userName = $User->getAttribute('user_name'); $greetingName = ($firstName != '') ? $firstName : $userName; $Smarty->assign('greetingName', $greetingName); } else { $logged_in = false; } } } $Smarty->assign("greetingName", $greetingName); $Smarty->assign("logged_in", $logged_in); $cur_location = $PHP_SELF ."?". $QUERY_STRING; $loginOption = ($QUERY_STRING == '') ? 'login' : '&login'; $cur_location_login = 'main.php' . "?" . $QUERY_STRING . $loginOption; $Smarty->assign("cur_location_login", $cur_location_login); if (isset($_GET['login']) and !$logged_in) { $returnUrl = $cur_location; $message = ''; DisplayLogin($message, $returnUrl, $exit=TRUE); } else { $cur_location = preg_replace('/[&?]login/', '', $cur_location); } $Smarty->assign("cur_location", $cur_location); // Step 2: List category path /*$category_path = array(); $ParentCategory = $ThisCategory; while($ParentCategory->getID() != 0) { $ParentCategory = $ParentCategory->getParent(); $category_path[] = array($ParentCategory->getName(), $PHP_SELF."?cat_id=".$ParentCategory->getID()); } $Smarty->assign("category_path", array_reverse($category_path));*/ $counts = array(); // Step 3: List subcategories //These functions build up a list of all categories and how many products they have to be used for the tree menu function buildCatList(&$list, $Category, $currentLevel, $parents, $cur_id, $expandToLevel=1) { global $cat_id, $counts, $struct, $prefs; $tmp = $struct; $Category->setStruct($tmp); $SubCatList = $Category->listCategoriesMenu($cur_id); $id = $cur_id; $padding = $currentLevel * 0; $style = "style='margin-left: $padding"."px;"; if ($id == 0 or $id == $cat_id or in_array($id,$parents) or $currentLevel < $expandToLevel) { $style .= "'"; } else { $style .= " display: none;'"; } $list .= "
\n"; foreach ($SubCatList as $sub_cat_id=>$SubCat) { if ($SubCat['hidden']) continue; if ($SubCat['product_count'] == "") $SubCat['product_count'] = 0; $num_items = array($SubCat['product_count'], $SubCat['cat_count']); //$MoreSubCats = $SubCat->listCategories(); $id = $sub_cat_id; if ($id == $cat_id || in_array($id,$parents)) $img = "open.gif"; else $img = "closed.gif"; if ($num_items[1] > 0) { if ($prefs['expand_category_images'] == 'yes') { $expand = "Expand"; } else { $expand = ''; } $style = ""; } else { $expand = ""; if ($prefs['expand_category_images'] == 'yes') { $style = "style='margin-left: 12px;'"; } else { $style = ""; } } $cat_title = $SubCat['cat_title']; $title_color = $SubCat['title_color']; $display_cat_title = ($title_color) ? '' . $cat_title : $cat_title; if ($id == $cat_id) { $list .= "
" . $expand. "$display_cat_title"; if ($prefs['menu_totals_enabled'] == "yes") $list .= " ($num_items[0])"; $list .= "
\n"; } else { if ($display_cat_title) { $list .= "
".$expand. "".$display_cat_title. " "; if ($prefs['menu_totals_enabled'] == "yes") $list .= "($num_items[0])"; $list .= "
\n"; } } if ($num_items[1] > 0) { buildCatList($list, $Category, $currentLevel+1, $parents, $id, $expandToLevel); } } $list .= "
\n"; return; } // // Find the Toplevel Category // // TODO - Put the Top Level Category and Bread Crumb stuff in // in a function. It is common with product.php // if (isset($prefs['top_level_category_is_server_name']) and $prefs['top_level_category_is_server_name'] == 'yes') { $Category = new CCategory($DbConnection); $serverName = preg_replace('/www\./i', '', $_SERVER['SERVER_NAME']); $topLevelCatIds = $Category->findCategoriesByTitle($serverName, 0); if (count($topLevelCatIds) == 1) { $topLevelCatId = $topLevelCatIds[0]; } else { trigger_error("Unable to find the parent category for server '$serverName'.", ERROR); $topLevelCatId = 0; } $cat_id = ($cat_id) ? $cat_id : $topLevelCatId; } else if (isset($prefs['top_level_home_categories']) and $prefs['top_level_home_categories'] == 'yes') { $Category = new CCategory($DbConnection); if (isset($_GET['product_area']) and $_GET['product_area'] != '') { $topLevelCategory = $_GET['product_area']; } else if (isset($_SESSION['product_area']) and $_SESSION['product_area'] != '') { $topLevelCategory = $_SESSION['product_area']; } else { $topLevelCategory = ''; } $_SESSION['product_area'] = $topLevelCategory; if ($topLevelCategory == '') { $SubCategories = $Category->listCategories($ordered=TRUE); $topLevelCatId = $SubCategories[0]->getID(); } else { $topLevelCatIds = $Category->findCategoriesByTitle($topLevelCategory, 0); if (count($topLevelCatIds) == 1) { $topLevelCatId = $topLevelCatIds[0]; } else { trigger_error("Unable to find the parent category '$topLevelCategory'.", ERROR); $SubCategories = $Category->listCategories($ordered=TRUE); $topLevelCatId = $SubCategories[0]->getID(); } } $cat_id = ($cat_id) ? $cat_id : $topLevelCatId; } else { $cat_id = isset($cat_id) ? $cat_id : 0; $topLevelCatId = 0; } $Smarty->assign("product_area", $topLevelCategory ); $Smarty->assign("cat_id", $cat_id); $ThisCategory = new CCategory($DbConnection, $cat_id); if ($cat_id == 0 or ($cat_id == $topLevelCatId)) { if ($prefs['top_level_category_name']) { $currentCategory = $prefs['top_level_category_name']; } else { $currentCategory = 'Set preference "Top Level Category Name" in Site Preference->Appearance'; } } else { $currentCategory = $ThisCategory->getName(); } $Smarty->assign("current_cat", $currentCategory); $Smarty->assign("current_cat_color", $ThisCategory->getCategoryTitleColor()); $Smarty->assign("category_image", $ThisCategory->getCatImage()); // // Bread Crumb // $parents = array(); $ThisCategory->getParents($parents); $parentList = array(); for ($i = count($parents)-1; $i >= 0; $i--) { $parentId = $parents[$i]; if ($parentId == 0) { if (!$topLevelCatId) { $parentList = array(); $parentList[0] = ''; } } else if ($parentId == $topLevelCatId) { $parentList = array(); $Parent = new CCategory($DbConnection, $parentId); $parentList[$parentId] = ''; } else { $Parent = new CCategory($DbConnection, $parentId); $parentList[$parentId] = $Parent->getName(); } } if ($cat_id != 0) { $parentList[$cat_id] = ($cat_id != $topLevelCatId) ? $ThisCategory->getName() : ''; } $Smarty->assign("parentList", $parentList); //Build up the side menu $subcategories = array(); $RootCategory = new CCategory($DbConnection, $topLevelCatId); $list = ""; $parents = array(); $ThisCategory->getParents($parents); $struct = $ThisCategory->prepareStructure(0, NULL, $lang); $expandToLevel = ($prefs['expand_category_menu_to_level']) ? $prefs['expand_category_menu_to_level'] : 1; buildCatList($list, $RootCategory, 0, $parents, $topLevelCatId, $expandToLevel); $Smarty->assign("menu",$list); // Attributes $Smarty->assign("description", $ThisCategory->getDescription()); $Smarty->assign("header_description", $ThisCategory->getHeaderDescription()); $Smarty->assign("header_keywords", $ThisCategory->getHeaderKeywords()); // Step 4: List products $products = array(); $optional_fields = array(); $pictures = array(); $searchResults = FALSE; $searchAttributesOnly = ($prefs['search_attributes_only'] == 'yes' ) ? TRUE : FALSE; if ($search_terms != "") { $ProductList = $ThisCategory->search(true, $search_terms, $searchAttributesOnly); $searchResults = TRUE; } else if ($advanced_search != "") { $ProductList = AdvancedSearch($DbConnection); $searchResults = TRUE; } else { $ProductList = $ThisCategory->listProducts(); } $Smarty->assign("search_results", $searchResults); $Smarty->assign("count_products", count($ProductList)); // // Check if there is a custom page, if so display it, instead of the products // $customPage = $ThisCategory->getCustomPage(); if ($customPage != "") { $filePath = $ThisCategory->getCustomPage($fullPath=TRUE); $body = ''; $title = ''; $metaData = ''; $styles = ''; $errorMessage = ParseHtmlFile($filePath, $body, $title, $metaData, $styles); if ($errorMessage) trigger_error($errorMessage, ERROR); $Smarty->assign("custom_styles", $styles); $Smarty->assign("custom_page", $body); } else { //If there aren't any products on this page, get the first picture from each sub-category //and use it as a link to that category (if the entry page preference is enabled) if (count($ProductList) < 1 && $prefs['entry_page_enabled']=="yes") { $Cats = $ThisCategory->listCategories(); if (count($Cats) > 0) { foreach ($Cats as $Category) { if ($Category->getCatImage() != "") { $products[] = array($Category->getName($lang), "", $Category->getID()); $pictures[] = $Category->getCatImage(); $picture_borders[] = FALSE; } else { $ThisProduct = findProducts($Category); if ($ThisProduct != 0) { //Get the first product with a picture //$i = 0; //while ($Temp[$i]->getPrimaryPicture() == 0 && $i < count($Temp)-1) { // $i++; //} //$ThisProduct = $Temp[$i]; $products[] = array($Category->getName($lang), $ThisProduct->getDescription(), $Category->getID()); // Get a list of pictures for the product $ThisPicture = $ThisProduct->getPrimaryPicture(); if ($ThisPicture) { $prefs['watermark_images'] == "yes" ? $wm = true : $wm = false; $pictures[] = $ThisPicture->getPicture(0,$prefs['thumb_height_size'],$prefs['thumb_width_size'], $wm, $prefs['watermark_transparency']); } else { $pictures[] = "pictures/noimage.gif"; } $picture_borders[] = $ThisPicture->getAttribute("add_border"); } } } $Smarty->assign("entry_page", "true"); } } foreach ($ProductList as $ThisProduct) { $products[] = array($ThisProduct->getTitle($lang), $ThisProduct->getDescription($lang), $ThisProduct->getID()); // Get all the optional fields for the product $Units = new CUnits($DbConnection); $this_fields = array(); $field_list = $ThisProduct->ListAttributes(false, true, false, $lang); foreach ($field_list as $field) { $attr = $ThisProduct->getAttribute($field[0]); if ($attr['enabled']) { if ($field[2] == "units" and $attr['value']) { list($value, $unitName, $displayAs) = $Units->convertValue($attr['value'], $unit_system, $attr['unit_id']); } else { $value =$attr['value']; $displayAs = ''; } array_push($field, $value); array_push($field, $displayAs); $this_fields[] = $field; } } $optional_fields[] = $this_fields; //Get the prices for each product if necessary if ($prefs['allow_prices'] == "yes") { $Price = new CPrice($DbConnection, $ThisProduct->getID()); if ($prefs['require_login'] == "yes" && !$logged_in) { // Do not show prices } else { $defaultPriceCategory = isset($prefs['default_price_cat']) ? $prefs['default_price_cat'] : NULL; $userPriceCategory = ($logged_in and $User->getAttribute("price_cat_id")) ? $User->getAttribute("price_cat_id") : $defaultPriceCategory; $userDiscount = ($logged_in) ? $User->getAttribute("discount") : 0.0; $displayLowestPrice = ($prefs['display_lowest_price'] == "yes") ? TRUE : FALSE; $prices[] = $Price->getPriceRange($defaultPriceCategory, $userPriceCategory, $userDiscount, $displayLowestPrice, $onSale); } $onSaleItems[] = $onSale; } // Get a list of pictures for the product $ThisPicture = $ThisProduct->getPrimaryPicture(); if ($ThisPicture) { $prefs['watermark_images'] == "yes" ? $wm = true : $wm = false; $pictures[] = $ThisPicture->getPicture(0,$prefs['thumb_height_size'],$prefs['thumb_width_size'], $wm, $prefs['watermark_transparency']); $picture_borders[] = $ThisPicture->getAttribute("add_border"); } else { $pictures[] = "pictures/noimage.gif"; $picture_borders[] = TRUE; } $gift_certificates[] =$ThisProduct->isGiftCertificate(); } $Smarty->assign("gift_certificates", $gift_certificates); $Smarty->assign("products", $products); $Smarty->assign("optional_fields", $optional_fields); $Smarty->assign("field_list", $field_list); $Smarty->assign("pictures", $pictures); $Smarty->assign("picture_borders", $picture_borders); $Smarty->assign("prices", $prices); $Smarty->assign("on_sale_items", $onSaleItems); //Let the template know how many products go on each row $Smarty->assign("itemsPerRow", $prefs['num_columns']); } //Get the page title from the preferences if possible, otherwise get it from the config file if ($prefs['builder_title'] != "") { $Smarty->assign("builder_title", $prefs['builder_title']); } else { $title = $Config->getValue("builder_title"); $Smarty->assign("builder_title", $title); } $Smarty->assign("error_msg", urldecode($error_msg)); // Render page $Smarty->display($ThisCategory->getTemplate($prefs)); // // Advanced Search ---------------------------------------------------------------- // function AdvancedSearch($DbConnection) { $matchValues = array(); // // The following defaults will create links to the dimensions fields. They are always needed // for the order by clause. $matchValues['width_inches[as_is]'] = " ( 1 = 1 ) "; $matchValues['width_feet[as_is]'] = " ( 1 = 1 ) "; $matchValues['length_inches[as_is]'] = " ( 1 = 1 ) "; $matchValues['length_feet[as_is]'] = " ( 1 = 1 ) "; foreach ($_GET as $field => $value) { if ($field == 'product_area') continue; if ($value == '') continue; switch ($field) { case 'search_submit': case 'search_submit_x': case 'search_submit_y': case 'advanced_search': break; case 'bg_color': case 'border_color': case 'third_color': $colours = array("Earth Tones" => array("Beige", "Brown", "Camel", "Crème", "Gold", "White", "Yellow"), "Reds" => array("Burgundy", "Mauve", "Orange", "Peach", "Pink", "Purple", "Red", "Rust", "Salmon"), "Greens" => array("Green", "Hunter Green", "Taupe", "Pistachio"), "Blues" => array("Blue", "Navy", "Turquoise"), "Blacks" => array("Black", "Grey")); $matchValues[$field . '[in]'] = $colours[$value]; break; case 'dimensions': // Logic 'as is' from the original search.php, it is only set for rectangular shapes $dim = explode(",",$value); if ($dim[0] == 3 && $dim[1] == 5) { // Small sizes $matchValues['width_feet[as_is]'] = " ((pfv_width_feet.value + (pfv_width_inches.value / 12)) <= 4 ) "; $matchValues['length_feet[as_is]'] = " ((pfv_length_feet.value + (pfv_length_inches.value / 12)) <= 6) "; } else if ($dim[0] == 0 && $dim[1] == 0) { // Large sizes $matchValues['width_feet[as_is]'] = " (pfv_width_feet.value > 11) "; $matchValues['length_feet[as_is]'] = " (pfv_length_feet.value > 14) "; } else { // Selected sizes $matchValues['width_feet[as_is]'] = " (pfv_width_feet.value + (pfv_width_inches.value / 12)) >= " . ($dim[0] - 1) ; $matchValues['width_inches[as_is]'] = " (pfv_width_feet.value + (pfv_width_inches.value / 12)) <= " . ($dim[0] + 1) ; $matchValues['length_feet[as_is]'] = " (pfv_length_feet.value + (pfv_length_inches.value / 12)) >= " . ($dim[1] - 1) ; $matchValues['length_inches[as_is]'] = " (pfv_length_feet.value + (pfv_length_inches.value / 12)) <= " . ($dim[1] + 1) ; } break; default: if ($value != '') { $matchValues[$field] = $value; } } } $Category = new CCategory($DbConnection); $orderBy = '((pfv_width_feet.value + pfv_width_inches.value/12) * (pfv_length_feet.value + pfv_length_inches.value/12))'; $Products = $Category->fieldSearch($matchValues, $orderBy); return $Products; } ?>