<?php    
   
    $dbconf = json_decode(file_get_contents(__DIR__ . "/../credentials/sql_config.json"));

   /* The following snippet can be used to check the connection (via terminal)
    * just run the script with 'php eventHandler.php'

    $link = mysqli_connect($dbconf->host, $dbconf->user, $dbconf->pass, $dbconf->dbname);
   
    if (!$link) {
        die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
    }
    echo 'Connected... ' . mysqli_get_host_info($link) . "\n";

------------------------------------------------------------------------------------------- */

   /* Here is another testing snippet that can be used to test an INSERT INTO or UPDATE
    * query with whatever raw, hard-coded data you like.

    $_submissions = [
        "startDate" => new DateTime('01/18/2018 13:00:00'),
        "endDate" =>  new DateTime('01/18/2018 14:00:00'),
        "title" => "Testing123",
        "userID" => 11,
    ];
    
    $testArray["startDate"] = $testArray["startDate"]->format("Y-m-d H:i:s");
    $testArray["endDate"] = $testArray["endDate"]->format("Y-m-d H:i:s");
    */
    
    //Start
    
    // echo "<script>console.log(" . $_POST['startDate'] . ")</script>";
    // echo "<script>console.log(" . gettype($_POST['startDate']) . ")</script>";
<<<<<<< HEAD
    
    $_submissions = [
        "startDate" => new DateTime($_POST['startDate']),
        "endDate" =>  new DateTime($_POST['endDate']),
        "title" => $_POST['title'],
        "userID" => 11,
    ];

    echo "<script>console.log('this: '" . $_submissions['title'] . ");</script>";
    $_submissions["startDate"] = $_submissions["startDate"]->format("Y-m-d H:i:s");
    $_submissions["endDate"] = $_submissions["endDate"]->format("Y-m-d H:i:s"); 

    // Following is mostly copied from Sam ofc.
    try {
        $db = new PDO("mysql:host=$dbconf->host;charset=utf8;dbname=$dbconf->dbname",
                  $dbconf->user, $dbconf->pass);
    } catch (PDOException $ex) {
        echo "<script>console.log('failed to connect to DB');</script>";
        die("Error connecting to SQL server.");
    }

    //$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Check if it is a 'simple' event or a 'full' event request.
    // if (something) {}

    // Then prepare DB query
    $query = "INSERT INTO Events (start_date, end_date, title, user_id) VALUES(:startD, :endD, :title, :userID)";

    $statement = $db->prepare($query);

    try {
        $statement->execute(array(
            "startD" => $_submissions['startDate'],
            "endD" => $_submissions['endDate'],
            "title" => $_submissions['title'],
            "userID" => $_submissions['userID']
        ));
    } catch (PDOException $ex2) {
        echo $query . "<br>" . $ex2;
        die("Error connecting to SQL server.");
    }
    

    // Use js to go back 1 page (to get directed back from eventHandler.php)
    header("location:javascript://history.go(-1)");

    // Then execute the query, give user feedback, and updateEvents();

    //echo '<script>updateEvents();</script>';
?>
=======

    include '../needs_login.php';
    include '../db_conn.php';

    if (isset($_POST['existingEvent'])) {   
    
        $eventID = [
            'id' => $_POST['existingEvent']
        ];
        
        // Delete old event
        $query = "DELETE FROM Events WHERE event_id=:id";
    
        $statement = $db->prepare($query);
    
        try {
            $statement->execute(array(
                "id" => $eventID['id']
            ));
        } catch (PDOException $ex2) {
            echo $query . "<br>" . $ex2;
            die("Error connecting to SQL server.");
        }

        // Add edited event

        if (isset($_POST['allDay'])) {
            $startDate = new DateTime($_POST['startDate'] . ' 00:00:00');
            $endDate = new DateTime($_POST['endDate'] . ' 00:00:00');
        } else {
            $startDate = new DateTime($_POST['startDate'] . ' ' . $_POST['startTime']);
            $endDate = new DateTime($_POST['endDate'] . ' ' . $_POST['endTime']);
        }
    
        $_submissions = [
            "startDate" => $startDate,
            "endDate" =>  $endDate,
            "title" => htmlspecialchars(strip_tags($_POST['title'])),
            "description" => htmlspecialchars(strip_tags($_POST['description'])),
            "userID" => $_SESSION['user_id'],
            "allDay" => isset($_POST['allDay'])
        ];

        $_submissions["startDate"] = $_submissions["startDate"]->format("Y-m-d H:i:s");
        $_submissions["endDate"] = $_submissions["endDate"]->format("Y-m-d H:i:s");

        $query = "INSERT INTO Events (all_day, start_date, end_date, title, event_text, user_id) VALUES(:allDay, :startD, :endD, :title, :descr, :userID)";
    
        $statement = $db->prepare($query);
        try {
            $statement->execute(array(
                "startD" => $_submissions['startDate'],
                "endD" => $_submissions['endDate'],
                "title" => $_submissions['title'],
                "descr" => $_submissions['description'],
                "userID" => $_submissions['userID'],
                "allDay" => $_submissions['allDay']
            ));
        } catch (PDOException $ex2) {
            echo $query . "<br>" . $ex2;
            die("Error connecting to SQL server.");
        }
        
        $event_id = $db->lastInsertId();
        $labelIDs = [];
    
        if (!isset($_POST['labelID'])) {
            $labelIDs[0] = 0;
        } else {
            foreach ($_POST['labelID'] as $id) {
                array_push($labelIDs, $id);
            }
        }
    
        $query = "INSERT INTO Event_labels (event_id, label_id) VALUES(:eventID, :labelID)";
    
        foreach ($labelIDs as $id) {
            
            $statement = $db->prepare($query);
            
            try {
                $statement->execute(array(
                    "eventID" => $event_id,
                    "labelID" => $id
                ));
            } catch (PDOException $ex2) {
                echo $query . "<br>" . $ex2;
                die("Error connecting to SQL server.");
            }
    
        }
    } else {

        if (isset($_POST['allDay'])) {
            $startDate = new DateTime($_POST['startDate'] . ' 00:00:00');
            $endDate = new DateTime($_POST['endDate'] . ' 00:00:00');
        } else {
            $startDate = new DateTime($_POST['startDate'] . ' ' . $_POST['startTime']);
            $endDate = new DateTime($_POST['endDate'] . ' ' . $_POST['endTime']);
        }
    
        $_submissions = [
            "startDate" => $startDate,
            "endDate" =>  $endDate,
            "title" => htmlspecialchars(strip_tags($_POST['title'])),
            "description" => htmlspecialchars(strip_tags($_POST['description'])),
            "userID" => $_SESSION['user_id'],
            "allDay" => isset($_POST['allDay'])
        ];
    
        $_submissions["startDate"] = $_submissions["startDate"]->format("Y-m-d H:i:s");
        $_submissions["endDate"] = $_submissions["endDate"]->format("Y-m-d H:i:s");
    
        include '../db_conn.php';
    
        //$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
        // Check if it is a 'simple' event or a 'full' event request.
        // if (something) {}
    
        // Then prepare DB query
        $query = "INSERT INTO Events (all_day, start_date, end_date, title, event_text, user_id) VALUES(:allDay, :startD, :endD, :title, :descr, :userID)";
    
        $statement = $db->prepare($query);
        try {
            $statement->execute(array(
                "startD" => $_submissions['startDate'],
                "endD" => $_submissions['endDate'],
                "title" => $_submissions['title'],
                "descr" => $_submissions['description'],
                "userID" => $_submissions['userID'],
                "allDay" => $_submissions['allDay']
            ));
        } catch (PDOException $ex2) {
            echo $query . "<br>" . $ex2;
            die("Error connecting to SQL server.");
        }
        
        $event_id = $db->lastInsertId();
        $labelIDs = [];
    
        if (!isset($_POST['labelID'])) {
            $labelIDs[0] = 0;
        } else {
            foreach ($_POST['labelID'] as $id) {
                array_push($labelIDs, $id);
            }
        }
    
        $query = "INSERT INTO Event_labels (event_id, label_id) VALUES(:eventID, :labelID)";
    
        foreach ($labelIDs as $id) {
            
            $statement = $db->prepare($query);
            
            try {
                $statement->execute(array(
                    "eventID" => $event_id,
                    "labelID" => $id
                ));
            } catch (PDOException $ex2) {
                echo $query . "<br>" . $ex2;
                die("Error connecting to SQL server.");
            }
    
        }
    }
?>
>>>>>>> eb0d56a89ca95780a0266b5898e23107925fc206
