<?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 
  
  -----------------------------------------------------------------------
  
 ##################################################################################




*/
# strip functon
# if anyone can help with international translations please correct the file and upload
  function strip($convert_me) {
	$strip_array = array("&#39",chr(33),chr(34),chr(35),chr(36),chr(37),chr(38),chr(39),chr(40),chr(41),chr(42),chr(43),chr(44),chr(45),chr(46),chr(47),chr(58),chr(59),chr(60),chr(61),chr(62),chr(63),chr(91),chr(92),chr(93),chr(94),chr(95),chr(96),chr(123),chr(124),chr(125),chr(126) );  
	$convert_me = str_replace($strip_array, '', $convert_me);
	$convert_me = str_replace(array(' ', '  ', '__', '--'), '-', $convert_me);
	$convert_me = strtolower($convert_me);
	return $convert_me;
  }
	# Set the cache name
	$cache_file = $cache_dir . 'seo_urls_';	
	
	# if the cache is not stored in the database
	$cache->is_cached($cache_file . 'items', $is_cached, $is_expired);  	
	if ( !$is_cached || $is_expired ) { 
	# Query for the items
	$item_query = go_db_query("select p.items_id, pd.items_name from ((items p) left join items_description pd on p.items_id=pd.items_id and pd.language_id='".(int)$languages_id."') where p.items_status='1'");
	# Initialize the item array
	$prod_array = array();
	# Loop the returned rows
	while ($item = go_db_fetch_array($item_query)) {
		$prod_array[$item['items_id']] = array('name' => strip($item['items_name']),
													 'id' => $item['items_id']);
	}
	# Free the memory - could be large, clean as we go!
	go_db_free_result($item_query);
	# Initialize the container used to check for duplicate names
	$prod_container = array();
	# Loop the item array
	$prod_cache = '';
	foreach($prod_array as $record){
		$id = $record['id'];
		# If the item name hasn't been set		
		if ( !isset($prod_container[ $record['name'] ]) ){
			$name = $record['name'];
		} else { # This is a duplicate - get the counter and append
			# Increase the counter
			$prod_container[ $record['name'] ]['counter']++;
			$prod_counter = $prod_container[ $record['name'] ]['counter'];
			# Append the counter to the item name
			$name = $record['name'] . '-' . $prod_counter;
		}
		# Add the defines to the output string		
		$prod_cache .= 'define(\'ITEM_NAME_' . $id . '\', \'' . $name . '\'); ' . "\n";
		$prod_cache .= 'define(\'' . $name  . '\', \'items_id=' . $id . '\'); ' . "\n";
		# Add the item name to the container array
		$prod_container[$name] = array('name' => $name, 'counter' => 1);
	}
	# Save the cached data to database
	# Params: [ cache name, cache data, compressed, global ]
	$cache->save_cache($cache_file . 'items', $prod_cache, 'EVAL', 1 , 1);
	# Unset the variables used - could be large, clean as we go!
	unset($prod_array, $prod_container, $prod_cache);
	} # end if items is not cached
 	
	# if the cache is not stored in the database
	$cache->is_cached($cache_file . 'producers', $is_cached, $is_expired);  	
	if ( !$is_cached || $is_expired ) { 
	# Query for the producers
	$producers_query = go_db_query("select m.producers_id, m.producers_name from ((producers m) left join producers_info md on m.producers_id=md.producers_id and md.languages_id='".(int)$languages_id."')");
	# Initialize the item array
	$man_array = array();
	# Loop the returned rows
	while ($producer = go_db_fetch_array($producers_query)) {
		$man_array[$producer['producers_id']] = array('name' => strip($producer['producers_name']),
													 'id' => $producer['producers_id']);
	}
	# Free the memory - could be large, clean as we go!
	go_db_free_result($producers_query);
	# Initialize the container used to check for duplicate names
	$man_container = array();
	# Loop the item array
	$man_cache = '';
	foreach($man_array as $record){
		$id = $record['id'];
		# If the item name hasn't been set		
		if ( !isset($man_container[ $record['name'] ]) ){
			$name = $record['name'];
		} else { # This is a duplicate - get the counter and append
			# Increase the counter
			$man_container[ $record['name'] ]['counter']++;
			$man_counter = $man_container[ $record['name'] ]['counter'];
			# Append the counter to the item name
			$name = $record['name'] . '-' . $man_counter;
		}
		# Add the defines to the output string		
		$man_cache .= 'define(\'PRODUCER_NAME_' . $id . '\', \'' . $name . '\'); ' . "\n";
		$man_cache .= 'define(\'' . $name  . '\', \'producers_id=' . $id . '\'); ' . "\n";
		# Add the producer name to the container array
		$man_container[$name] = array('name' => $name, 'counter' => 1);
	}
	# Save the cached data to database
	# Params: [ cache name, cache data, compressed, global ]
	$cache->save_cache($cache_file . 'producers', $man_cache, 'EVAL', 1 , 1);
	# Unset the variables used - could be large, clean as we go!
	unset($man_array, $man_container, $man_cache);
	} # end if producers is not cached
 	
	
	# if the cache is not stored in the database
	$cache->is_cached($cache_file . 'categories', $is_cached, $is_expired);  	
	if ( !$is_cached || $is_expired ) { 
	# Query for the categories
	$category_query = go_db_query("select c.categories_id, c.parent_id, c.sort_order, cd.categories_name from ((categories c) left join categories_description cd on c.categories_id=cd.categories_id and cd.language_id='".(int)$languages_id."') order by c.parent_id, c.sort_order ASC");
	# Initialize the cat array
	$cat_array = array();
	# Loop the returned rows
	while ($category = go_db_fetch_array($category_query)) {	
		$cat_array[$category['categories_id']] = array('name' => strip($category['categories_name']),
												  'id' => $category['categories_id'],
												  'parent' => $category['parent_id']);
	}
	# Free the memory - could be large, clean as we go!
	go_db_free_result($category_query);
	# Initialize the container used to check for duplicate names
	$cat_container = array();
	# Loop the cat array
	$cat_cache = '';
	foreach ($cat_array as $record){
		$name = $record['name'];
		$id = $record['id'];
		# If the category name hasn't been set
		if ( !isset($cat_container[ $cat_array[$record['parent']]['name'] .'-'.$name ]) ){
			$parent_name = ($record['parent']=='0' ? '' : $cat_array[$record['parent']]['name'].'-');
		} else { # This is a duplicate category - get the counter and append
			# Increase the counter
			$cat_container[ $cat_array[$record['parent']]['name'] .'-'.$name ]['counter']++;
			$parent_counter = $cat_container[ $cat_array[$record['parent']]['name'] .'-'.$name ]['counter'];
			# Append the counter to the parent name
			$parent_name = ($record['parent']=='0' ? '' : $cat_array[$record['parent']]['name'].'-'.$parent_counter.'-');
		}
		# Initialize the array to hold the category path
		$c = array();
		# Get the category path
		go_get_parent_categories($c, $record['id']);
		# For some reason it seems to return in reverse order so reverse the array
		$c = array_reverse($c);
		# Implode the array to get the full category path
		$id = (implode('_', $c) ? implode('_', $c) . '_' . $record['id'] : $record['id']);
		# Add the defines to the output string		
		$cat_cache .= 'define(\'CATEGORY_NAME_' . $id . '\', \'' . $parent_name . $name . '\'); ' . "\n";
		$cat_cache .= 'define(\'' . $parent_name . $name . '\', \'bigPfad=' . $id . '\'); ' . "\n";		
		# Add the category name to the container array 
		$cat_container[$parent_name . $name] = array('id' => $id, 'counter' => 1);
	}
	# Save the cached data to the database
	# Params: [ cache name, cache data, compressed, global ]
	$cache->save_cache($cache_file . 'categories', $cat_cache, 'EVAL', 1 , 1);
	# Unset the arrays used - could be large, clean as we go!
	unset($cat_array, $cat_container, $cat_cache);
	}# end if categories is not cached
?>