<?php
/*
###################################################################################
  Bigware Shop 2.3
  Release Datum: 23.08.2015
  
  Bigware Shop
  http://www.bigware.de

  Copyright (c) 2015 Bigware LTD
  (c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
  (c) 2015	Bigware LTD

  Copyrightvermerke duerfen nicht entfernt werden.
  ------------------------------------------------------------------------
  Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
  der GNU General Public License, wie von der Free Software Foundation
  veroeffentlicht, weitergeben und/oder modifizieren, entweder gemaess Version 2 
  der Lizenz oder (nach Ihrer Option) jeder spaeteren Version.
  Die Veroeffentlichung dieses Programms erfolgt in der Hoffnung, dass es Ihnen
  von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE, sogar ohne die
  implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FUER EINEN
  BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
  
  Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
  Programm erhalten haben. Falls nicht, schreiben Sie an die Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.

  Infos:
  ------------------------------------------------------------------------
  Der Bigware Shop wurde vor vielen Jahren bereits aus dem bekannten Shopsystem osCommerce
  weiter- und neuentwickelt.
  Der Bigware Shop legt im hohen Masse Wert auf Bedienerfreundlichkeit, beinhaltet eine leichte
  Installation, viele neue professionelle Werkzeuge und zeichnet sich aus durch eine grosse 
  Community, die bei Problemen weiterhelfen kann.
  
  Der Bigware Shop ist auf jedem System lauffaehig, welches eine PHP Umgebung
  (ab PHP 4.1.3) und mySQL zur Verfuegung stellt und auf Linux basiert.
 
  Hilfe erhalten Sie im Forum auf www.bigware.de 
  
  -----------------------------------------------------------------------
  
 ##################################################################################




*/
?>
    <table border="0" style="width:100%" cellspacing="0" cellpadding="0">
      <tr>
        <td><table border="0" style="width:100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageGeneralsign"><h1><?php echo ABOVE_TITLE_2; ?></h1></td>
            <td class="pageGeneralsign" align="right"></td>
          </tr>
        </table></td>
      </tr>
      <tr>
        <td><?php echo go_fetch_dividing_up('tranparentes.gif', '100%', '10'); ?></td>
      </tr>
      <tr>
        <td>
	<?php 
	$date_availability = 'AND (to_days(items_date_available_end) IS NULL OR (to_days(items_date_available_end) IS NOT NULL AND to_days(now()) <= to_days(items_date_available_end)))';

	$define_list = array('ITEM_LIST_MODEL' => ITEM_LIST_MODEL,
			'ITEM_LIST_NAME' => ITEM_LIST_NAME,
			'ITEM_LIST_PRODUCER' => ITEM_LIST_PRODUCER,
			'ITEM_LIST_PRICE' => ITEM_LIST_PRICE,
			'ITEM_LIST_QUANTITY' => ITEM_LIST_QUANTITY,
			'ITEM_LIST_WEIGHT' => ITEM_LIST_WEIGHT,
			'ITEM_LIST_PICTURE' => ITEM_LIST_PICTURE,
			'ITEM_LIST_BUY_NOW' => ITEM_LIST_BUY_NOW);
asort($define_list);
$column_list = array();
reset($define_list);
while (list($key, $value) = each($define_list)) {
	if ($value > 0) $column_list[] = $key;
}
$select_column_list = '';
for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
	switch ($column_list[$i]) {
		case 'ITEM_LIST_MODEL':
			$select_column_list .= 'p.items_model, ';
			break;
		case 'ITEM_LIST_PRODUCER':
			$select_column_list .= 'm.producers_name, ';
			break;
		case 'ITEM_LIST_QUANTITY':
			$select_column_list .= 'p.items_quantity, ';
			break;
		case 'ITEM_LIST_PICTURE':
			$select_column_list .= 'p.items_picture, ';
			break;
		case 'ITEM_LIST_WEIGHT':
			$select_column_list .= 'p.items_weight, ';
			break;
	}
}
$select_str = "select distinct " . $select_column_list . " m.producers_id, p.items_id, pd.items_name, pd.items_name2, p.items_price, p.items_tax_class_id, IF(s.status, s.specials_new_items_price, NULL) as specials_new_items_price, IF(s.status, s.specials_new_items_price, p.items_price) as final_price ";
if ( (SHOW_PRICE_WITH_TAX == 'true') && (go_not_null($pfrom) || go_not_null($pto)) ) {
	$select_str .= ", tr.tax_rate as tax_rate ";
}
if ( (SHOW_PRICE_WITH_TAX == 'true') && (go_not_null($pfrom) || go_not_null($pto)) ) {
	$from_str = "from ((";
} 
else{$from_str = "from ";}
$from_str .= "(((" . DB_TBL_ITEMS . " p left join " . DB_TBL_PRODUCERS . " m using(producers_id), " . DB_TBL_ITEMS_DESCRIPTION . " pd) left join " . DB_TBL_SPECIALS . " s on p.items_id = s.items_id, " . DB_TBL_CATEGORIES . " c, " . DB_TBL_ITEMS_TO_CATEGORIES . " p2c)";
if ( (SHOW_PRICE_WITH_TAX == 'true') && (go_not_null($pfrom) || go_not_null($pto)) ) {
	if (!go_session_is_registered('attendee_land_id')) {
		$attendee_land_id = SHOP_LAND;
		$attendee_zone_id = SHOP_ZONE;
	}
	$from_str .= " left join " . DB_TBL_TAX_RATES . " tr on p.items_tax_class_id = tr.tax_class_id) left join " . DB_TBL_ZONES_TO_GEO_ZONES . " gz on tr.tax_zone_id = gz.geo_zone_id and (gz.zone_land_id is null or gz.zone_land_id = '0' or gz.zone_land_id = '" . (int)$attendee_land_id . "') and (gz.zone_id is null or gz.zone_id = '0' or gz.zone_id = '" . (int)$attendee_zone_id . "'))";
} 
$where_str = ") where p.items_status = '1' and c.categories_status = '1' and p.items_id = pd.items_id and pd.language_id = '" . (int)$languages_id . "' and p.items_id = p2c.items_id and p2c.categories_id = c.categories_id $date_availability ";
if (isset($_GET['categories_id']) && go_not_null($_GET['categories_id'])) {
	if (isset($_GET['inc_subcat']) && ($_GET['inc_subcat'] == '1')) {
		$subcategories_array = array();
		go_get_subcategories($subcategories_array, $_GET['categories_id']);
		$where_str .= " and p2c.items_id = p.items_id and p2c.items_id = pd.items_id and (p2c.categories_id = '" . (int)$_GET['categories_id'] . "'";
		for ($i=0, $n=sizeof($subcategories_array); $i<$n; $i++ ) {
			$where_str .= " or p2c.categories_id = '" . (int)$subcategories_array[$i] . "'";
		}
		$where_str .= ")";
	} else {
		$where_str .= " and p2c.items_id = p.items_id and p2c.items_id = pd.items_id and pd.language_id = '" . (int)$languages_id . "' and p2c.categories_id = '" . (int)$_GET['categories_id'] . "'";
	}
}
if (isset($_GET['producers_id']) && go_not_null($_GET['producers_id'])) {
	$where_str .= " and m.producers_id = '" . (int)$_GET['producers_id'] . "'";
}
if ( (isset($search_keywords) && (sizeof($search_keywords) > 0))&&(empty($productcode)) ) {
	$where_str .= " and (";
	for ($i=0, $n=sizeof($search_keywords); $i<$n; $i++ ) {
		switch ($search_keywords[$i]) {
			case '(':
			case ')':
			case 'and':
			case 'or':
				$where_str .= " " . $search_keywords[$i] . " ";
				break;
			default:
				$keyword = go_db_producing_input($search_keywords[$i]);
				$where_str .= "(pd.items_name like '%" . go_db_input($keyword) . "%' or p.items_model like '%" . go_db_input($keyword) . "%' or m.producers_name like '%" . go_db_input($keyword) . "%'";
				if (isset($_GET['search_in_description']) && ($_GET['search_in_description'] == '1')) $where_str .= " or pd.items_description like '%" . go_db_input($keyword) . "%'";
				$where_str .= ')';
				break;
		}
	}
	$where_str .= " )";
}

if (!empty($productcode)) {
	$where_str .= " and p.items_model='".go_db_input($productcode)."'";
}  
if (go_not_null($dfrom)) {
	$where_str .= " and p.items_date_added >= '" . go_date_raw($dfrom) . "'";
}
if (go_not_null($dto)) {
	$where_str .= " and p.items_date_added <= '" . go_date_raw($dto) . "'";
}
if (go_not_null($pfrom)) {
	if ($currencies->is_set($currency)) {
		$rate = $currencies->get_value($currency);
		$pfrom = $pfrom / $rate;
	}
}
if (go_not_null($pto)) {
	if (isset($rate)) {
		$pto = $pto / $rate;
	}
}
if (SHOW_PRICE_WITH_TAX == 'true') {
	if ($pfrom > 0) $where_str .= " and (IF(s.status, s.specials_new_items_price, p.items_price) * if(gz.geo_zone_id is null, 1, 1 + (tr.tax_rate / 100) ) >= " . (double)$pfrom . ")";
	if ($pto > 0) $where_str .= " and (IF(s.status, s.specials_new_items_price, p.items_price) * if(gz.geo_zone_id is null, 1, 1 + (tr.tax_rate / 100) ) <= " . (double)$pto . ")";
} else {
	if ($pfrom > 0) $where_str .= " and (IF(s.status, s.specials_new_items_price, p.items_price) >= " . (double)$pfrom . ")";
	if ($pto > 0) $where_str .= " and (IF(s.status, s.specials_new_items_price, p.items_price) <= " . (double)$pto . ")";
}
if ( (SHOW_PRICE_WITH_TAX == 'true') && (go_not_null($pfrom) || go_not_null($pto)) ) {
	$where_str .= " group by p.items_id, tr.tax_priority";
}
if ( (!isset($_GET['sort'])) || (!preg_match('/[1-8][ad]/', $_GET['sort'])) || (substr($_GET['sort'], 0, 1) > sizeof($column_list)) ) {
	for ($i=0, $n=sizeof($column_list); $i<$n; $i++) {
		if ($column_list[$i] == 'ITEM_LIST_NAME') {
			$_GET['sort'] = $i+1 . 'a';
			$order_str = ' order by pd.items_name';
			break;
		}
	}
} else {
	$sort_col = substr($_GET['sort'], 0 , 1);
	$sort_order = substr($_GET['sort'], 1);
	$order_str = ' order by ';
	switch ($column_list[$sort_col-1]) {
		case 'ITEM_LIST_MODEL':
			$order_str .= "p.items_model " . ($sort_order == 'd' ? "desc" : "") . ", pd.items_name";
			break;
		case 'ITEM_LIST_NAME':
			$order_str .= "pd.items_name " . ($sort_order == 'd' ? "desc" : "");
			break;
		case 'ITEM_LIST_PRODUCER':
			$order_str .= "m.producers_name " . ($sort_order == 'd' ? "desc" : "") . ", pd.items_name";
			break;
		case 'ITEM_LIST_QUANTITY':
			$order_str .= "p.items_quantity " . ($sort_order == 'd' ? "desc" : "") . ", pd.items_name";
			break;
		case 'ITEM_LIST_PICTURE':
			$order_str .= "pd.items_name";
			break;
		case 'ITEM_LIST_WEIGHT':
			$order_str .= "p.items_weight " . ($sort_order == 'd' ? "desc" : "") . ", pd.items_name";
			break;
		case 'ITEM_LIST_PRICE':
			$order_str .= "final_price " . ($sort_order == 'd' ? "desc" : "") . ", pd.items_name";
			break;
	}
}
$listing_sql = $select_str . $from_str . $where_str . $order_str;
require(FOLDER_RELATIV_MODULES . $GLOBALS[CONFIG_NAME_FILE][item_listing]);
?>
        </td>
      </tr>
      <tr>
        <td><?php echo go_fetch_dividing_up('tranparentes.gif', '100%', '10'); ?></td>
      </tr>
      <tr>
        <td class="main"><?php echo '<a href="' . go_href_link($GLOBALS[CONFIG_NAME_FILE][main_bigware_2], go_get_all_get_parameter(array('sort', 'page')), 'NONSSL', true, false) . '">' . go_picture_button('button_back.gif', PICTURE_BUTTON_BACK) . '</a>'; ?></td>
      </tr>
    </table>
