<?php
ini_set('display_errors', 0);
//error_reporting(E_ALL);	
//$connection=mysql_connect('localhost', 'root', '1234') or die ("Verbindungsversuch fehlgeschlagen");
//mysql_select_db('intelectra_shop__', $connection) or die("Konnte die Datenbank nicht waehlen.1");

$connection=mysql_connect('localhost', 'int-db-sql-shop', 'Msc7t24?381L') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('webshop-sql', $connection) or die("Konnte die Datenbank nicht waehlen.1");

include_once './libs/phImaginator/phImaginator.class.php';

$handle=opendir ("./upload/");
echo "Verzeichnisinhalt:<br>";
 $z = 0;
while ($datei = readdir ($handle)) {
    if ($datei != '.' && $datei != '..') {
        $z++;
        if ($z <= 1000) {
                echo "$datei ";
                $number = substr($datei,0,6);
                // get item id
                $file_exists = 0;
                $sql = "SELECT * FROM item_files WHERE file_name='".$datei."'";
                $query2 = mysql_query($sql) or die("Anfrage nicht erfolgreich");
                while($obj2 = mysql_fetch_object($query2)) {
                        $file_exists = 1;
                }
            
                    if ($file_exists == 0) {
                        echo "- neu ";
                         // get item id for a new file
                        $item_id = 0;
                        $sql = "SELECT * FROM items WHERE number='".$number."'";
                        $query2 = mysql_query($sql) or die("Anfrage nicht erfolgreich");
                        while($obj2 = mysql_fetch_object($query2)) {
                                $item_id = $obj2->id;
                        }
                        if ($item_id) {
                            // get item images
                            $sql = "SELECT * FROM item_files WHERE type=0 AND item_id='".$item_id."'";
                            $query2 = mysql_query($sql) or die("Anfrage nicht erfolgreich");
                            $images = array();
                            while($obj2 = mysql_fetch_object($query2)) {
                                    $images[] = $obj2;
                            }

                            // set first default image
                            if (!$images) {
                                // set default image
                                $sql = "UPDATE items
                                        SET default_image_file_name='".$datei."'
                                        WHERE id=".$item_id;
                                $query = mysql_query($sql);
                                if (!$query) {
                                    echo $sql;
                                    echo "<br />";
                                }
                            }
                                
                            echo "bild in db anlegen ";
                            // insert image
                            $sql = "INSERT INTO item_files
                                SET
                                    rang='".count($images)."',
                                    type=0,
                                    file_name='".$datei."',
                                    item_id=".$item_id;
                            $query = mysql_query($sql);
                            if (!$query) {
                                echo $sql;
                                echo "<br />";
                            }
                            else {
                                $file_exists = 1;
                            }
                        }
                    }
                    else {
                        echo "- ueberschreiben ";
                    }
            
                    if ($file_exists == 1) {
                         // resize image
                         $default_generatable_images = array(
                                 'overview_' => 189,
                                 'thumb_' => 90,
                                 'details_' => 380,
                                 'detaildefault_' => 245
                         );
                         $targetFile = "./web/intelectra_shop/images/article/".$datei;
                         copy ("./upload/".$datei,$targetFile);
                         unlink("./upload/".$datei);
                         foreach ($default_generatable_images as $key => $size) {
                                 $image_name = $key;
                                 $imaginator = new phImaginator();
                                 $imaginator->add($targetFile);
                                 $imaginator->Resize_fitBoth($size, $size);
                                 $imaginator->nameAdd($image_name);
                                 $imaginator->save();
                         }

                 }
                 else {
                     unlink("./upload/".$datei);
                     echo "- passender Artikel nicht gefunden ";
                 }
                 echo "<br>";
        }
    }
}
closedir($handle);


?>