<?php
		
mysql_connect("localhost","astral_chameleon","ChamElEoN#XY!143");
mysql_select_db("astral_chameleonrocks");
		
if ($handle = opendir('../Migration/ProductImages')) {
    echo "Directory handle: $handle\n";
    /* This is the correct way to loop over the directory. */
    while (false !== ($filename = readdir($handle))) {
		$product_id = 0;
	  	$string_pos1= strpos($filename,'-');
		$string_pos2= strpos($filename,'-',$string_pos1+1);

	  	$model = substr($filename,0,$string_pos1 ) ;
		$product_id = getSingleDbField('product','product_id',"model='".$model."'");
	  
	  	if($product_id == 0){
		  echo "Unable to Find the Product for this Model :" . $model;
		  continue;
		} 
	   	
		$mystring = strtoupper($filename);
		$pos = strrpos($mystring, "V1");
		if ($pos === false) {		 			// Not Main Images	
			
			$View = substr($filename,$string_pos1+2,$string_pos2-$string_pos1-2 ) ;
			
    		if (file_exists('../image/data/test/'. $model . '/V'. $View .'/'. $filename )) {
				unlink('../image/data/test/'. $model . '/V'. $View .'/'. $filename );
			}
			
			echo '.<br>./Migration/ProductImages/' . $filename  . "  To  " .  '../image/data/test/'. $model . '/V'. $View .'/'. $filename . '<br>';
			moveFile('../Migration/ProductImages/' . $filename , '../image/data/test/'. $model . '/V'. $View .'/' );
			
			$imageCount = getSingleDbField('product_image','count(image)+1 as imageCount',"product_id='$product_id'");
			echo $View  . '=' . $imageCount .'<br>' ;
			if( intval($View) > $imageCount ){
				$sql="insert into product_image(product_id,image,sort_order) values ($product_id,'no_image.jpg',". $imageCount .")";
				$result = mysql_query($sql);
			}
			
		}else{
			echo '<br>../Migration/ProductImages/' . $filename  . "  To  " .  '../image/data/test/'. $model . '/V1/'. $filename . '<br>';
			
    		if (file_exists('../image/data/test/'. $model . '/V1/'. $filename )) {
				unlink('../image/data/test/'. $model . '/V1/'. $filename );
			}
			moveFile('../Migration/ProductImages/' . $filename , '../image/data/test/'. $model . '/V1/' );
			
			echo '<br>File Name : ' . $filename . '<br>';
			$string_pos3= strpos($filename,'-',$string_pos2+1);
			$string_pos4= strpos($filename,'.',$string_pos3+1);
    	    $Item = substr($filename,$string_pos2+1,$string_pos3-$string_pos2-1 ) ;
	
			$image = 'data/test/'. $model . '/V1/'. $filename ;
			
			if($Item[0]=='M'){
				$id = substr($Item,1,strlen($Item)-1);
				$metal=substr($filename,$string_pos3+1,$string_pos4-$string_pos3-1);
				$metal_id = getSingleDbField('metal','metal_id',"metal_code='$metal'");
				$metal_exist = getSingleDbField('product_metalavail','metal_id',"metal_id='$metal_id' and product_id='$product_id' and metal='$id' ");
				if($metal_exist <> $metal_id ){
					echo '<br>--Inserting Metal : ' . $id . ' ; Metal Name : ' . substr($filename,$string_pos3+1,$string_pos4-$string_pos3-1) . ' ; Product ID : ' . $product_id  . '<br>';  
					$sql="insert into product_metalavail(product_id,metal_id,metal,image,defaultnew,statusnew,currency) values ($product_id,$metal_id,$id,'$image',0,1,'GBP')";
					$result = mysql_query($sql);
				}
			}else{
				$id = substr($Item,1,strlen($Item)-1);
				$gemstone = substr($filename,$string_pos3+1,$string_pos4-$string_pos3-1);
				$gemstone_id = getSingleDbField('gemstone','gemstone_id'," upper(gemname)= upper('".$gemstone."')");
				$gemstone_exist = getSingleDbField('product_gemavail','gemstone_id',"gemstone_id='$gemstone_id' and product_id='$product_id' and gem='$id' ");
				if($gemstone_exist <> $gemstone_id ){
					echo '<br>--Inserting Gemstone : ' . $id  . ' ; Gemstone Name : ' . substr($filename,$string_pos3+1,$string_pos4-$string_pos3-1). ' ; Product ID : ' . $product_id. '<br>';
					$sql="insert into product_gemavail(product_id,gemstone_id,gem,image,defaultnew,statusnew) values ($product_id,$gemstone_id,$id,'$image',0,1)";
					$result = mysql_query($sql);
				}
			}
 		}
    }

    closedir($handle);
}

function moveFile($file, $newFolder) {
    if (!is_dir($newFolder)) {
        mkdir($newFolder, 0755 , true);
		echo "$newFolder";
    }
			
    $newFolder = (in_array(substr($newFolder, -1), array('/', '\\')) ? $newFolder : $newFolder.'/');
    if (is_file($file)) {
        $filename = basename($file);
        rename($file, $newFolder.$filename);
        return 'File '.$filename.' moved to '.$newFolder;
    }
    return 'File '.$file.' does not exist';
}
function getSingleDbField($table,$field,$where) {
	//echo "SELECT $field FROM $table WHERE $where ";
	$result = mysql_query("SELECT $field FROM $table WHERE $where ");
	$row = mysql_fetch_array($result);
	return $row[0];
}


?>