<?php
$file = $real_fotonet_path.strtolower(preg_replace("/[^A-Za-z0-9\-\_\.]/",'',$wahl)).'.inc';
$fileIDs = $real_fotonet_path.'neue-ids/'.strtolower(preg_replace("/[^A-Za-z0-9\-\_\.]/",'',$wahl)).'_ids_neu.inc';
$fileTMP = $real_fotonet_path.'_nach-alleunsortiert-kopieren.inc';
$fileNEU = $real_fotonet_path.'_nach-kategorie-kopieren.inc';

// ------------------ Variable zum Bearbeiten -------------------
$sortieren = 0;	// zum Bearbeiten auf !=0 ändern
$inc_create = 0;
// --------------------------------------------------------------


// --------------------------- Check for BESTSELLER Kategorie --------------------------- 
if (@is_file($fileIDs) && $inc_create) 	
{
		include ($real_fotonet_path.'alleunsortiert.inc');
		$yesInfo = 0;
		$lines = array();
		$handle = @fopen($fileIDs, "r");
		if ($handle) {
   			while (!feof($handle)) {
       			$lines[] = fgets($handle, 4096);
   			}
		}
   		fclose($handle);

		$handle = @fopen ($fileNEU, "w");
		$handle_TMP = @fopen ($fileTMP, "w");

		for ($i=0; $i<count($lines);$i++)
		{
		   if ($lines[$i]) {
			$key = array_search($lines[$i],$value_id);
			if ($key !== false) {
				$yesInfo += 1;
				$DateiInfo = '$value_id[]='.$value_id[$key].';	$value_price_xl[]='.$value_price_xl[$key].';	$value_original_width[]='.$value_original_width[$key].';	$value_original_height[]='.$value_original_height[$key].';	$value_thumbnail_height[]='.$value_thumbnail_height[$key].';	$value_thumbnail_width[]='.$value_thumbnail_width[$key].';	$value_title[]="'.$value_title[$key].'";	$value_thumbnail_url[]="'.$value_thumbnail_url[$key].'";';
				@fputs($handle,"$DateiInfo".chr(13).chr(10));
			}
			else {
				echo $lines[$i].'<br>';
				$results = $api->getSearchResults(
    					array(
						'words' => $lines[$i],
						'language_id' => $language_id,
						'thumbnail_size' => 160,
    					));
				if ($results['nb_results'] >= 1) 
				{
					foreach( $results as $key => $value) 
					{
						if ( is_numeric($key) ) 
						{
							$price_xl=0;
							$V=0; $lizenz=0; 
							foreach ($value[licenses] as $k => $price_detail ) 
							{
								if ($price_detail[name] == "L")	{ $lizenz="L"; $price_xl = $price_detail[price]*1.5; }
								if ($price_detail[name] == "XL")	{ $lizenz="XL"; $price_xl = $price_detail[price]*1.5; }
								if ($price_detail[name] == "XXL")	{ $lizenz="XXL"; $price_xl = $price_detail[price]*1.5; }
								if ($price_detail[name] == "V")	$V=1;
							}
							if ($price_xl)
							{
								if ($price_xl < $minFotoliaPreis) $price_xl = $minFotoliaPreis;
								$fileW = 0;
								$fileH = 0;
								if (!$V)
								{
									$fileW = $value[width];
									$fileH = $value[height];
								}
								$DateiInfo = '$value_id[]='.$value[id].';	$value_price_xl[]='.$price_xl.';	$value_original_width[]='.$fileW.';	$value_original_height[]='.$fileH.';	$value_thumbnail_height[]='.$value[thumbnail_height].';	$value_thumbnail_width[]='.$value[thumbnail_width].';	$value_title[]="'.preg_replace("/[^A-Za-z0-9\-\_\.]/",' ',$value[title]).'";	$value_thumbnail_url[]="'.$value[thumbnail_url].'";';
								@fputs($handle,"$DateiInfo".chr(13).chr(10));
								@fputs($handle_TMP,"$DateiInfo".chr(13).chr(10));
							}
						}
					}
				}
			}
		   }
		}
		@fclose($handle);
		@fclose($handle_TMP);
		echo '<div class="col-md-12 bottom14"><hr>File DONE mit : ' .count($lines). ' Motive, davon NEU : ' .(count($lines)-$yesInfo-1). ' <hr></div>';
		@unlink($fileIDs);
}
else include ("galerie_listview.inc");
if ($words || $cat1 || $cat2 || $serie) { include ("galerie_getSearchResults.inc"); }

// --------------------------- Sort Listen nach "folder" --------------------------- 

if ($sortieren) {

	$DateiInc = strtolower(preg_replace("/[^A-Za-z0-9\-\_\.]/",'',$wahl)).'.inc';	// ändern Original-Dateiname zum Sortieren

	$fileUnsort = $real_fotonet_path.$DateiInc;
	$fileSort = $real_fotonet_path.'_sortiert.inc';						// new sorted copy of original file
	$fileDB = $real_fotonet_path.'_DB.inc';							// DB-Export
	$fotoliaNr_liste = array(); $fotoliaNr_menge = array();
	$value_menge = array();	
	include ($fileDB);
	include ($fileUnsort);
	for ($i=0; $i<count($value_id);$i++)
	{
		$key = array_search($value_id[$i],$fotoliaNr_liste);
		if ($key !== false) $value_menge[$i] = $fotoliaNr_menge[$key];
		else $value_menge[$i] = 1;
	}
	arsort($value_menge);
	if (@is_file($fileSort)) @unlink($fileSort);
	$handle = @fopen ($fileSort, "w");
	@fputs($handle,"<?php".chr(13).chr(10));
	$DateiInfo = '// -------------- '.$DateiInc.' ---------------------';
	@fputs($handle,"$DateiInfo".chr(13).chr(10));
	$DateiInfo = '$value_id = array();		$value_price_xl = array();	$value_original_width = array();	$value_original_height = array();	$value_thumbnail_height = array();	$value_thumbnail_width = array();	$value_title = array();	$value_thumbnail_url = array();';
	@fputs($handle,"$DateiInfo".chr(13).chr(10));
	foreach ($value_menge as $key => $val) {
		$DateiInfo = '$value_id[]='.$value_id[$key].';	$value_price_xl[]='.$value_price_xl[$key].';	$value_original_width[]='.$value_original_width[$key].';	$value_original_height[]='.$value_original_height[$key].';	$value_thumbnail_height[]='.$value_thumbnail_height[$key].';	$value_thumbnail_width[]='.$value_thumbnail_width[$key].';	$value_title[]="'.$value_title[$key].'";	$value_thumbnail_url[]="'.$value_thumbnail_url[$key].'";';
		@fputs($handle,"$DateiInfo".chr(13).chr(10));
	}
	@fputs($handle,"?>".chr(13).chr(10));
	@fclose($handle);
}
?>
