<?php    
    include_once dirname(__FILE__) . '/../global_after_session.php';
    include_once dirname(__FILE__) . '/../database.php';
    include_once dirname(__FILE__) . '/../global_functions.php';

    function addStickyNote($db, $userRow, $inAssociatedCategoryID, $inStickyNoteCategory, $inStickyNoteTitle, $inStickyNoteContent, $inStickyNoteImageElement){
        $result = "";
        
        // Set up what the user posted at first, to be changed with logic.
        $associatedCategoryID = $inAssociatedCategoryID;
        $stickyNoteCategory=$inStickyNoteCategory;
        $stickyNoteContent=$inStickyNoteContent;
        $stickyNoteTitle=$inStickyNoteTitle;
        $stickyNoteImageElement=$inStickyNoteImageElement;
        
        try {
            // Ensure that we have something to work on, a title and content to post
            if ($associatedCategoryID>0 && 
                $stickyNoteContent!=null && $stickyNoteContent!=''  && 
                $stickyNoteTitle!=null && $stickyNoteTitle!=''){
                // create a query to determine if we have a task to add the sticky note to as default
                $sql = 'SELECT * FROM tasks ' .
                       'WHERE TaskID = ' . $associatedCategoryID . ';';
                
                // Determine what the category is and change the query to match
                switch ($stickyNoteCategory) {
                    case 'COMPANY':
                        // create a query to determine if we have a company to add the sticky note to
                        $sql = 'SELECT * FROM companies ' .
                               'WHERE CompanyID = ' . $associatedCategoryID . ';';
                        
                        // Breakout of the loop
                        break;

                    case 'COMPANY':
                        // create a query to determine if we have a user to add the sticky note to
                        $sql = 'SELECT * FROM users ' .
                               'WHERE UserID = ' . $associatedCategoryID . ';';
                        
                        // Breakout of the loop
                        break;

                    case 'MATERIAL':
                        // create a query to determine if we have a material to add the sticky note to
                        $sql = 'SELECT * FROM material ' .
                               'WHERE MaterialID = ' . $associatedCategoryID . ';';
                        
                        // Breakout of the loop
                        break;

                    case 'TOOL':
                        // create a query to determine if we have a tool to add the sticky note to
                        $sql = 'SELECT * FROM tools ' .
                               'WHERE ToolID = ' . $associatedCategoryID . ';';
                        
                        // Breakout of the loop
                        break;

                    default:
                        break;
                }

                // Attempt to run the query
                $db->queryDB($sql, $userRow, "add_sticky_note Form occurance 1");
                
                // Attempt to run the query and evaluate the response
                if (mysqli_num_rows($updatedTaskQuery)!=1){
                    // Get the posted items that I am worried about
                    $stickyNoteFileName = null; 
                    $stickyNoteFileSize = null; 
                    $stickyNoteFileType = null; 
                    $StickyNoteFileContent  = null;

                    // determine if the image has been uploaded
                    if(is_uploaded_file($stickyNoteImageElement['tmp_name'])) {
                        // Get the properties of the file to upload
                        $stickyNoteFileName    = $db->getDBConnection()->real_escape_string($stickyNoteImageElement['name']); 
                        $tmpName  = $db->getDBConnection()->real_escape_string($stickyNoteImageElement['tmp_name']);
                        $stickyNoteFileSize = intval($stickyNoteImageElement['size']);
                        $stickyNoteFileType = $db->getDBConnection()->real_escape_string($stickyNoteImageElement['type']);

                        // Determine if the file size is such that the database han handle storing it. The database can handle 4GB (4,294,967,296 Bytes)
                        if ($stickyNoteFileSize < 4294967296){
                            //process the file
                            $StickyNoteFileContent = $db->getDBConnection()->real_escape_string(file_get_contents($tmpName));

                            // Determine if we got any data
                            if ($StickyNoteFileContent!=null) {
                                // Determine if the magic quotes option is on
                                if(!get_magic_quotes_gpc()){
                                    // Add slashes to the name
                                    $stickyNoteFileName = addslashes($stickyNoteFileName);
                                }
                            }
                        }
                        else {
                            // reset all values to nothing
                            $stickyNoteFileName = null; 
                            $stickyNoteFileSize = null; 
                            $stickyNoteFileType = null; 
                            $StickyNoteFileContent  = null;

                            // Send and error message back
                            return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::The file ("' . $stickyNoteFileName . '") ' . 
                                   'will not be uploaded, because it is too large. File must be under 4GB. Aborted.';
                        }
                    }   

                    // Let's create the insert query off of what we have
                    $sql='INSERT INTO notes (IsSticky, AssociatedCategoryID, Category ';
                    if ($stickyNoteTitle!=null) {$sql = $sql . ', Title';}
                    if ($stickyNoteContent!=null) {$sql = $sql . ',Note';}
                    if ($stickyNoteFileName!=null) {$sql = $sql . ',FileName';}
                    if ($stickyNoteFileSize!=null) {$sql = $sql . ',ByteSize';}
                    if ($stickyNoteFileType!=null) {$sql = $sql . ',FileType';}
                    if ($StickyNoteFileContent!=null) {$sql = $sql . ',Datum';}
                    if ($userRow["UserID"]!=null) {$sql = $sql . ', EnteredByID';}
                    $sql=$sql .') VALUES (1, ' . $associatedCategoryID . ', "' . $stickyNoteCategory . '" ';
                    if ($stickyNoteTitle!=null) {$sql = $sql . ', "' . $stickyNoteTitle . '"';}
                    if ($stickyNoteContent!=null) {$sql = $sql . ', "' . $stickyNoteContent . '"';}
                    if ($stickyNoteFileName!=null) {$sql = $sql . ', "' . $stickyNoteFileName . '"';}
                    if ($stickyNoteFileSize!=null) {$sql = $sql . ', ' . $stickyNoteFileSize;}
                    if ($stickyNoteFileType!=null) {$sql = $sql . ', "' . $stickyNoteFileType . '"';}
                    if ($StickyNoteFileContent!=null) {$sql = $sql . ', "' . $StickyNoteFileContent . '"';}
                    if ($userRow["UserID"]!=null) {$sql = $sql . ', ' . $userRow["UserID"];}
                    $sql=$sql .');';

                    // Attempt to run the query and get the response
                    if ($db->queryDB($sql, $userRow, "note_presentation Form occurance 3.")){
                    }
                }
                else {
                    // Send and error message back
                    return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::There is no task ' . 
                              'to move into the queue. Aborted.';
                }
            }
            else {
                // Determine which was the problem
                if ($associatedCategoryID<1){
                    // Send and error message back
                    return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::There is no task ' . 
                              'to add a sticky note to. Aborted.';
                }
                else if ($stickyNoteContent==null||$stickyNoteContent==''){
                    // Send and error message back
                    return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::There is no content ' . 
                              'to add for the sticky note. Aborted.';
                }
                else if ($stickyNoteTitle==null||$stickyNoteTitle==''){
                    // Send and error message back
                    return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::There is no title ' . 
                              'to add for the sticky note. Aborted.';
                }
            }
        } catch (Exception $ex) {
            // Set up an error message for the exception
            return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::There was an error ' . 
                      'trying to move the task ID ' . $associatedCategoryID . ' on to the next phase. ' . 
                      '\n\n The error is: \n' . $ex->getMessage();
        }

        // Return whatever we got
        return $result;
    }
    
    function addStickyNoteImage($db, $userRow, $inAssociatedCategoryID, $inStickyNoteImageElement){
        $result = "";
        
        // Set up what the user posted at first, to be changed with logic.
        $associatedCategoryID = (int)$inAssociatedCategoryID;
        $stickyNoteImageElement = $inStickyNoteImageElement;
        
        try {
            // Ensure that we have something to work on, a title and content to post
            if ($associatedCategoryID>0){
                // create a query to determine if we have a task to add the sticky note to as default
                $sql = 'SELECT * FROM tasks ' .
                       'WHERE TaskID = ' . $associatedCategoryID . ';';
                
                // Attempt to run the query
                $db->queryDB($sql, null, "add_sticky_note Form occurance 1");
                
                // Attempt to run the query and evaluate the response
                if (mysqli_num_rows($updatedTaskQuery)!=1){
                    // Get the posted items that I am worried about
                    $stickyNoteFileName = null; 
                    $stickyNoteFileSize = null; 
                    $stickyNoteFileType = null; 
                    $StickyNoteFileContent  = null;

                    // determine if the image has been uploaded
                    if(is_uploaded_file($stickyNoteImageElement['tmp_name'])) {
                        // Get the properties of the file to upload
                        $stickyNoteFileName    = $db->getDBConnection()->real_escape_string($stickyNoteImageElement['name']); 
                        $tmpName  = $db->getDBConnection()->real_escape_string($stickyNoteImageElement['tmp_name']);
                        $stickyNoteFileSize = intval($stickyNoteImageElement['size']);
                        $stickyNoteFileType = $db->getDBConnection()->real_escape_string($stickyNoteImageElement['type']);

                        // Determine if the file size is such that the database han handle storing it. The database can handle 4GB (4,294,967,296 Bytes)
                        if ($stickyNoteFileSize < 4294967296){
                            //process the file
                            $StickyNoteFileContent = $db->getDBConnection()->real_escape_string(file_get_contents($tmpName));

                            // Determine if we got any data
                            if ($StickyNoteFileContent!=null) {
                                // Determine if the magic quotes option is on
                                if(!get_magic_quotes_gpc()){
                                    // Add slashes to the name
                                    $stickyNoteFileName = addslashes($stickyNoteFileName);
                                }
                            }
                        }
                        else {
                            // reset all values to nothing
                            $stickyNoteFileName = null; 
                            $stickyNoteFileSize = null; 
                            $stickyNoteFileType = null; 
                            $StickyNoteFileContent  = null;

                            // Send and error message back
                            return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::The file ("' . $stickyNoteFileName . '") ' . 
                                   'will not be uploaded, because it is too large. File must be under 4GB. Aborted.';
                        }
                    }   

                    // Let's create the insert query off of what we have
                    $sql='UPDATE notes set FileName="' . $stickyNoteFileName . '", ByteSize=' . 
                         $stickyNoteFileSize . ', FileType="' . $stickyNoteFileType . '", ' . 
                         'Datum="' . $StickyNoteFileContent . '" WHERE NoteID=' . $inAssociatedCategoryID . ';';
                    
                    // Attempt to run the query and get the response
                    if ($db->queryDB($sql, null, "note_presentation Form occurance 2.")){
                    }
                }
                else {
                    // Send and error message back
                    return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::There is no note ' . 
                              'to add image to. Aborted.';
                }
            }
            else {
                // Determine which was the problem
                if ($associatedCategoryID<1){
                    // Send and error message back
                    return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::There is no note ID ' . 
                              'to add a sticky note image to. Aborted.';
                }
            }
        } catch (Exception $ex) {
            // Set up an error message for the exception
            return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::There was an error ' . 
                      'trying to move the task ID ' . $associatedCategoryID . 
                      ' on to the next phase. \n\n The error is: \n' . 
                      $ex->getMessage();
        }

        // Return whatever we got
        return $result;
    }
    
if($_FILES['file']['name'] != ''){
    $test = explode('.', $_FILES['file']['name']);
    $extension = end($test);    
    $name = rand(100,999).'.'.$extension;

    $location = 'uploads/'.$name;
    move_uploaded_file($_FILES['file']['tmp_name'], $location);

    echo '<img src="'.$location.'" height="100" width="100" />';
    
    
            return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::There was an error ' . 
                      'trying to move the task ID ';
}    
            return 'ABORT STICKY NOTE IMAGE (add_sticky_note)::Failure';
    
    
    $db = new Database();
    $db->setupTmsDB();

    // Setup a variable to hold the form data
    $fd=null;
    
    // Attempt to get the form data
    if (isset($_GET["fd"])){
return 'Here1';
        // Get it
        $fd=$_GET["fd"];
        
        // Get the category ID
        $categoryID = $fd[1];

        // Get the image
        $stickyNoteImage = $fd[0];

        // Go and get the machines and all of their tasks
        echo addStickyNoteImage($db, $userRow, $categoryID, $stickyNoteImage);
    }
return 'Here2';
    
//    // Set defaults for the passed in variables
//    $sessionID=-1;
//
//    // check the passed in session ID
//    if (isset($_GET['sessionID']) ){
//        // get the passed in session ID
//        $sessionID = $_GET['sessionID'];
//    }
//
//    // Attempt to log in with this user
//    $currentUser = $db->getLoggedInSessionUser($sessionID);
//    if (mysqli_num_rows($currentUser)!=1){
//        echo 'ABORT STICKY NOTE IMAGE (add_sticky_note)::Your session has expired.  ' . 
//             'Refresh the page before adding a sticky note.';
//    }
//    else {
//        // Set defaults for the passed in variables
//        $categoryID=-1;
//        $stickyNoteCategory='TASK';
//        $stickyNoteTo=-1;
//        $stickyNoteTitle='';
//        $stickyNoteContent='';
//        $stickyNoteImage=null;
//
//        // Check for the category ID
//        if (isset($_GET['categoryID']) ){
//            // Hold on to the category ID
//            $categoryID = $_GET['categoryID'];
//        }
//
//        // Check for the category type
//        if (isset($_GET['stickyNoteCategory']) ){
//            // Hold on to the category type
//            $stickyNoteCategory = $_GET['stickyNoteCategory'];
//        }
//
//        // Check for the user to assign this sticky note to
//        if (isset($_GET['stickyNoteTo']) ){
//            // Hold on to the user to assign this sticky note to
//            $stickyNoteTo = $_GET['stickyNoteTo'];
//        }
//
//        // Check for the sticky note title
//        if (isset($_GET['stickyNoteTitle']) ){
//            // Hold on to the sticky note title
//            $stickyNoteTitle = htmlspecialchars($_GET['stickyNoteTitle']);
//        }
//
//        // Check for the sticky note content
//        if (isset($_GET['stickyNoteContent']) ){
//            // Hold on to the sticky note content
//            $stickyNoteContent = htmlspecialchars($_GET['stickyNoteContent']);
//        }
//
//        // Check for the sticky note image
//        if (isset($_GET['stickyNoteImage']) ){
//            // Hold on to the sticky note image
//            $stickyNoteImage = htmlspecialchars($_GET['stickyNoteImage']);
//        }
//
//        // Attempt to get the user row
//        $userRow = $currentUser->fetch_assoc();
//
//        // Go and get the machines and all of their tasks
//        echo addStickyNote($db, $userRow, $categoryID, $stickyNoteCategory, 
//                $stickyNoteTo, $stickyNoteTitle, $stickyNoteContent, $stickyNoteImage);
//
//        // Go and get the machines and all of their tasks
//        echo addStickyNoteImage($db, $userRow, $categoryID, $stickyNoteImage);
//    }
?>   

