<?php
 
 ###########################################################################
 #   OsCommerce WAP 1.1                                                    #
 #   This file is part of the wap tweak for OsCommerce (c) copyright 2004  #
 #   by Tom Nitzschner (tom@toms-home.com)                                 #
 #   http://www.toms-home.com                                              #
 #                                                                         #
 #   As always, make a backup before messing with anything. All code       #
 #   release by me is considered sample code only. It may be fully         #
 #   functual, but you use it at your own risk, if you break it,           #
 #   you get to fix it too. No waranty is given or implied.                #
 #                                                                         #
 #   All my work is released under the GNU GENERAL PUBLIC LICENSE.         #
 #   Please see the README for more information.                           #
 ###########################################################################

# User changable item below
# You can set the products listed per page with the setting below.
# I would not reccomend setting it too high, as that can cause
# timeouts while a WAP device reads the data and some devices have
# limits on the amount they can display

$increment = 5;

#change this to 1 if you wish to show the quantity of stock remaining

$show_quantity = "0";

# End user changable settings. 
# Do NOT change anything below unless you really know what you are doing.

header( 'content-Type: text/vnd.wap.wml');

// Print the file text header
echo  '<?xml version="1.0" ?><!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
?>

<wml>
<template>
<do type="prev" label="Back"><prev/></do>
<do type="accept" label="Main"><go href="wapindex.php"/></do>
</template>

<card id="cate" title="Categories">

<?php
require("includes/wap_top.php");
#
# Configure the items per page as per setting in the user section above.
#
$prod_increment = $increment;
$next_page = $increment * $page;
global $sub_id;
$i = 1;
$p=1;
$tree = array();
#
# Ok, lets look if we are starting with a parent or child category and output the results.
#
if ($sub_id == "") {
	$parent = 1;
	#
	# Ok, we are in a parent category, lets get the data and fill the array.
	#
	$categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '0' and c.categories_id = cd.categories_id order by sort_order, cd.categories_name");
	$cat_total = tep_db_num_rows($categories_query);
	while ($categories = tep_db_fetch_array($categories_query))  {
		$tree[$i]['categories_name'] = $categories['categories_name'];
		$tree[$i]['parent_id'] = "0";
		$tree[$i]['categories_id'] =  $categories['categories_id'];
		$i++;
	}
} else {	
	$parent = 0;
	#
	# We are in a child/sub category, get the data and fill the array.
	#
	$categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.parent_id = '" . (int)$sub_id . "' and c.categories_id = cd.categories_id order by sort_order, cd.categories_name");
	if (tep_db_num_rows($categories_query)) {
		$cat_total = tep_db_num_rows($categories_query);
		while ($categories = tep_db_fetch_array($categories_query)) {
			$tree[$i]['categories_name'] = $categories['categories_name'];
			$tree[$i]['parent_id'] = $categories['parent_id'];
			$tree[$i]['categories_id'] =  $categories['categories_id'];
			$i++;
		}
	}
}
#
# Work out which category page we will display.
#
$k=($increment * $page)+1;
if ($increment > ($cat_total - ($increment * $page))) $increment = ($cat_total - ($increment * $page));
$j=$increment + $next_page;
if ($k <= $j) echo ("<p>Categories:</p>\n");
#
# Return category listing while looping, limit the categories per page as set above.
#
while ($k <= $j) {
   	$cat_id = $tree[$k]['categories_id'];
   	$cat_name = $tree[$k]['categories_name'];
   	$cat_parent = $tree[$k]['parent_id'];

	echo ("<p><a href=\"wapcat.php?sub_id=".$cat_id."\">" . tep_clean_wap_text($cat_name) . "</a></p>\n");
 	$k++;
 }
echo ("<p> </p>\n");
#
# Show the previous/next page links for categories as required.
#
if (($k > ($increment+1))  && ($cat_total > 0)) {
	echo ("<p><a href=\"wapcat.php?page=".($page-1)."\">Previous Categories</a></p>\n");
}
if ($k < ($cat_total)) {
	echo ("<p><a href=\"wapcat.php?page=".($page+1)."\">More Categories</a></p>\n");
}
#
# Now we need to check is the category also had some products in it. 
#
$item_list_query = tep_db_query("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where categories_id = '" . (int)$sub_id . "'");
if (tep_db_num_rows($item_list_query)) {
	#
	# Yep, found some product listings too, lets get the data and fill another array.
	#
	while ($products_items = tep_db_fetch_array($item_list_query)) {
		$products_query_available = tep_db_query("SELECT pd.products_id as products_id, pd.products_name as products_name, p.products_price as products_price, p.products_quantity as products_quantity from " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_PRODUCTS . " p where products_status = '1' and (p.products_id = pd.products_id) and pd.products_id = '".$products_items['products_id']."'");
		$prod_total = tep_db_num_rows($products_query_available);
		while ($products_list = tep_db_fetch_array($products_query_available)) {
			$prod[$p]['products_name'] = $products_list['products_name'];
			$prod[$p]['products_id'] = $products_list['products_id'];
			$prod[$p]['products_quantity'] = $products_list['products_quantity'];
			$prod[$p]['products_price'] =  tep_round($products_list['products_price'], 2);
			$p++;
		}
	}
}
#
# Work out which product page we will display 
#
$prod_total = $p-1;
$q=($prod_increment * $page)+1;
if ($prod_increment > ($prod_total - ($prod_increment * $page))) $prod_increment = ($prod_total - ($prod_increment * $page));
$r=$prod_increment + $next_page;
#
# Return product listing while looping, limit the items per page as set above.
#
if ($q <= $r) echo ("<p>Products:</p\n>");
while ($q <= $r) {
	$prod_id = $prod[$q]['products_id'];
	$prod_name = $prod[$q]['products_name'];
	$prod_price = $prod[$q]['products_price'];
	$prod_quantity = $prod[$q]['products_quantity'];
	if ($show_quantity == "1") {
		echo ("<p><a href=\"wapitem.php?item_id=".$prod_id."\">" . $prod_name . "</a> (Price: $$" . $prod_price . ")(Stock: ".$prod_quantity.")</p>\n");
	} else {
		echo ("<p><a href=\"wapitem.php?item_id=".$prod_id."\">" . $prod_name . "</a> (Price: $$" . $prod_price . ")</p>\n");
	}

	$q++;
}
#
# Show No items only if we didn't have a hit in a category section.
#
if (empty($tree)) if ($prod_total == "0")  echo ("<p>No Items Found</p>\n");
#
# Show the previous/next page links for products as required
#
echo ("<p> </p>\n");
	if (($r > ($prod_increment+1)) && ($prod_increment > 0)) {
		echo ("<p><a href=\"wapcat.php?page=".($page-1)."&sub_id=$sub_id\">Previous Products</a></p>\n");
	}
	if ($r < ($prod_total)) {
		echo ("<p><a href=\"wapcat.php?page=".($page+1)."&sub_id=$sub_id\">More Products</a></p>\n");
	}
?>
</card>
</wml>
