<?php
require_once ("../config.inc.php");
$function = $_POST['function'];

if ($function == 'save_with_files')
{

    if (isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST")
    {
        ## Dieses Script wird für jede Datei einzeln aufgerufen durch vpb_uploader.js. Bei 3 Dateien, 3x
        $titel = $_POST['titel'];
        $verlag = $_POST['verlag'];
        $anz_lizenzen = $_POST['anz_lizenzen'];
        $streamlizenz = $_POST['streamlizenz'];

        if (isset($streamlizenz))
        {
            if ($streamlizenz == '1')
            {
                $streamlizenz = '1';
            }
            else
            {
                $streamlizenz = '0';
            }
        }
        else
        {
            $streamlizenz = '0';
        }
        $datum_file = date("Ymd_His_");
        #$vpb_file_name = strip_tags($_FILES['upload_file']['name']); //File Name
        $vpb_file_name = str_replace(array(
            "ä",
            "ö",
            "ü",
            "ß",
            "Ä",
            "Ö",
            "Ü",
            " "
        ) , array(
            "ae",
            "oe",
            "ue",
            "ss",
            "Ae",
            "Oe",
            "Ue",
            "_"
        ) , $_FILES['upload_file']['name']);
        $originalname = $_FILES['upload_file']['name'];
        $vpb_file_name = $datum_file . $vpb_file_name;
        $vpb_file_id = strip_tags($_POST['upload_file_ids']); // File id is gotten from the file name
        $vpb_file_size = $_FILES['upload_file']['size']; // File Size
        $vpb_uploaded_files_location = '../media/file_upload/noten/'; //This is the directory where uploaded files are saved on your server
        $vpb_final_location = $vpb_uploaded_files_location . $vpb_file_name; //Directory to save file plus the file to be saved
        //Without Validation and does not save filenames in the database
        

        if (move_uploaded_file(strip_tags($_FILES['upload_file']['tmp_name']) , $vpb_final_location))
        {
            $datum = date("Y-m-d H:i:s");
            $result = $db->query("SELECT jndid 
                                        FROM jumi_noten_daten
                                       WHERE titel = '$titel'
                                         AND verlag = '$verlag'
                                         AND anz_lizenzen = '$anz_lizenzen'
                                         AND streamlizenz = '$streamlizenz'
                                    ");
            $row = $result->fetch_array();
            if ($row['jndid'] == '')
            {
                $sql1 = $db->query("INSERT INTO jumi_noten_daten ( titel
                                                              , verlag
                                                              , anz_lizenzen
                                                              , streamlizenz
                                                              , uid
                                                              , datum
                                                              )
                                            VALUES
                                                              ( '$titel'
                                                              , '$verlag'
                                                              , '$anz_lizenzen'
                                                              , '$streamlizenz'
                                                              , $uid
                                                              , '$datum'
                                                              )
                                          ");
                $jndid = $db->insert_id;
            }
            else
            {
                $jndid = $row['jndid'];
            }

            $sql2 = $db->query("INSERT INTO jumi_noten_uploads ( jndid
                                                              , filename
                                                              , originalname
                                                              , uid
                                                              , datum
                                                              )
                                            VALUES
                                                              ( $jndid
                                                              , '$vpb_final_location'
                                                              , '$originalname'
                                                              , $uid
                                                              , '$datum'
                                                              )
                                          ");
            //Display the file id
            if ($sql2)
            {
                echo '<div class="alert alert-success"><i class="fa fa-fw fa-thumbs-up"></i> Noten wurden angelegt!</div>|***|success|***|' . $vpb_file_id;
                exit;
            }
            else
            {
                echo '<div class="alert alert-danger"><i class="fa fa-fw fa-thumbs-down"></i> Noten wurden nicht angelegt: Insert Fehler Datenbank.</div>|***|error|***|' . $vpb_file_id;
                exit;
            }

        }
        else
        {
            //Display general system error
            echo 'general_system_error';
        }

    }
}

if ($function == 'save_without_files')
{
    if (isset($_POST['titel']))
    {
        $titel = $_POST['titel'];
    }
    if (isset($_POST['verlag']))
    {
        $verlag = $_POST['verlag'];
    }
    if (isset($_POST['anz_lizenzen']))
    {
        $anz_lizenzen = $_POST['anz_lizenzen'];
    }
    if (isset($_POST['streamlizenz']))
    {
        $streamlizenz = $_POST['streamlizenz'];
    }

    if (isset($streamlizenz))
    {
        if ($streamlizenz == '1')
        {
            $streamlizenz = '1';
        }
        else
        {
            $streamlizenz = '0';
        }
    }
    else
    {
        $streamlizenz = '0';
    }
    $db = dbconnect();
    $datum = date("Y-m-d H:i:s");
    $sql1 = $db->query("INSERT INTO jumi_noten_daten ( titel
                                              , verlag
                                              , anz_lizenzen
                                              , streamlizenz
                                              , uid
                                              , datum
                                              )
                            VALUES
                                              ( '$titel'
                                              , '$verlag'
                                              , '$anz_lizenzen'
                                              , '$streamlizenz'
                                              , $uid
                                              , '$datum'
                                              )
                          ");
    if ($sql1)
    {
        echo '<div class="alert alert-success"><i class="fa fa-fw fa-thumbs-up"></i> Noten wurden angelegt!</div>|***|success|***|';
        exit;
    }
    else
    {
        echo '<div class="alert alert-danger"><i class="fa fa-fw fa-thumbs-down"></i> Noten wurden nicht angelegt: Insert Fehler Datenbank.</div>|***|error';
        exit;
    }
}

?>