<?php
########################################################
##
## whh 2020.03.02
##
########################################################
# make sure you are logged in to run this
########################################################
// $logged_in = false;
session_start();
$login_in = $_SESSION['logged_in'];

// echo "session_start: " . $login_in . "<br>";

if (!$login_in) {
    die ('You do not have permission to access this page. Please login');
}
########################################################

$version = "<strong style='font-size:10px; color:grey;'> DELETE.PHP Build: 2020.03.02</strong>";
//echo $version;
########################################################

date_default_timezone_set("America/Toronto");
$dateTime = date("l, d-M-y H:i:s T");

########################################################
# GET data from index.php delete request button in the message
########################################################

  $localdateTime = $_POST['localdateTime'];
  // $messageDelete = $_POST['messageDelete'];

           $debug = true;

  //$deleteLocaldateTime = "Saturday, 22-Feb-20 13:43:16 EST";
  $messageDelete = "Delete";

//echo " localdateTime: " . $localdateTime . "<BR>";
//echo " messageDelete:  " . $messageDelete  . "<BR>";

    $deleteLocaldateTime = $localdateTime;

##################################################################################
## Traverse array and get the message for localdateTime of message to delete

    $receivedMessages = "data/SMSmessages.json"; //Get existing JSON file
    $sentMessages = "data/SMSmessagesSent.json"; //Get existing JSON file

    $jsondata = file_get_contents($receivedMessages); //Get data from existing JSON file
    $jsondata2 = file_get_contents($sentMessages); //Get data from existing JSON file


    $arr_data = json_decode($jsondata, true); // converts existing JSON data into an Array
    $arr_data2 = json_decode($jsondata2, true); // converts existing JSON data into an Array

    $size = sizeof($arr_data); //get total number of clients in the exiting client array and display
    $size2 = sizeof($arr_data2); //get total number of clients in the exiting client array and display


    //echo "<BR> Existing Contact Array Size: " . $size . "<BR>";
    //echo "<BR> Existing Contact Array Size: " . $size2 . "<BR>";

###################################################
## find received messages to delete
    foreach ($arr_data as $key1 => $value1) {
                //echo $arr_data[$key1]["localdateTime"] . "<br>";
      if($arr_data[$key1]["localdateTime"] == $deleteLocaldateTime){
          deleteMessage ($deleteLocaldateTime, $arr_data, $messageDelete, $receivedMessages); // call delete function pass it dateTime - array - filename

        }
    };
//echo "<br><br>";

###################################################
## find send messages to delete
    foreach ($arr_data2 as $key1 => $value1) {
              //echo $arr_data2[$key1]["localdateTime"] . "<br>";
      if($arr_data2[$key1]["localdateTime"] == $deleteLocaldateTime){
        deleteMessage ($deleteLocaldateTime, $arr_data2, $messageDelete, $sentMessages); // call delete function pass it dateTime - array - filename

        }
    };


// *********************************************************************
// This will delete the message with localdateTime
// *********************************************************************

function deleteMessage($dateTime, $arr_delete, $messageDelete, $filetosave) {

        // make sure the delete flag was set
        if ($messageDelete == "Delete") {

            //echo "In the Delete section: " . $messageDelete . "<br>";

          foreach($arr_delete as $key => $value) {

              //echo "In the Delete section: " . $dateTime . "<br>";

             if($value['localdateTime'] == $dateTime) {
              unset($arr_delete[$key]);
             }
           }
         }

    $reindex = array_values($arr_delete); //normalize index
    $arr_delete = $reindex; //update variable

   //convert array back into json format
   $jsondataNew = json_encode($arr_delete, JSON_PRETTY_PRINT);
   //write the messafe file with messages deleted
   file_put_contents($filetosave, $jsondataNew);

}; // end function delete message

echo "Deleted: <br>";

/*
//============================================================================================
// Deleted messages log
//============================================================================================

//Builds the content of the client log
    // data/phonenumber.txt - Creates file for client log if it does not exist
      // Builds the log file contents and adds to exiting data or creates it for the first time
        $ip1 = "<br>" . "firstName: " . $firstName  . "<br>" . "lastName: " . $lastName . "<br>" . "Initials: " . $initials . " Phone Number: " . $phonenum . "<br>". $type . "<br>";
        $ip2 = $ip1 . "Created: " . date("l, d-M-y H:i:s T");
        $ip3 = $ip2 . "<br>" ."<hr>";

echo "<br>" . "-LOGGED AS-: " . $ip3;

        $datei2 = fopen("data/". $phonenum . ".txt","a") or die("Cannot open file:" . "data/" . $phonenum . ".txt"); //implicitly creates file if it doesn't exist
        fwrite($datei2, $ip3);
        fclose($datei2);

*/

// *********************************************************************
  bigFail:
// *********************************************************************
//echo "-DONE- " ;
//SAVED


?>
