<?php
//This was my first attempt at fetching the images seperately using the XML URLs.  It's not necessary to run this script.

require_once("dt_sql_config.php"); // database connection config.
$urlToPost = "https://fam.dispatchtrack.com/orders/api/export.xml?code=fam&api_key=c4225f7fcc1b6e76c";
$apiLimit=0;
$inc=1;
$filenames = array();

$testDate = "2021-12-14";
$testOrder= "222529695-1"; //known to have images

//load prev processed filenames into array
$sql = "SELECT filename FROM dispatch.filesTracker";
$result = mysqli_query($db1, $sql);
if($result !== null){
    while ($row = mysqli_fetch_assoc($result)){
        $filenames[] = $row['filename'];
    }
}

//load filename list from /DTHistory folder into array
$fileArr = scandir('DTHistory');

//testing code
/* $file = $testDate.".xml";
$date = substr($file, 0, 10);
$localxml = simplexml_load_file('DTHistory/'.$testDate.'.xml');
foreach ($localxml->service_order as $soInfo) {
    if($soInfo->images['count'] > 0 && $inc <= 40){
        $order_number = $soInfo['order_number'];
        //local file has image tag, so fetch the remote xml so we get active image links
        $url = $urlToPost."&date=".$date."&service_order_id=".$order_number;
        $response = simplexml_load_file($url);
        $inc++; //we're calling the DT API so increment the count tracker
        foreach ($response->service_order->images->image as $image) {
            $imageURL = $image['src'];
            $imageName = $image['name'];
            $img = file_get_contents($imageURL);
            file_put_contents("DTImages/".$order_number."_".$imageName.".jpg", $img);
        }
        $apiLimit = $inc;
    }
} */

foreach ($fileArr as $file){
    if(!in_array($file, $filenames)){ // lets not process the same date again
        if($apiLimit <= 40){
            //in each file, search for the xml image element
            $localxml = simplexml_load_file('DTHistory/'.$file);
            foreach ($localxml->service_order as $soInfo) {
                if($soInfo->images['count'] > 0 && $inc <= 40){
                    $inc++;
                    $order_number = $soInfo['order_number'];
                    $date = substr($file, 0, 10);
                    //local file has image tag, so fetch the remote xml so we get active image links
                    $url = $urlToPost."&date=".$date."&service_order_id=".$order_number;
                    $response = simplexml_load_file($url);
                    foreach ($response->service_order->images->image as $image) {
                        $imageURL = $image['src'];
                        $imageName = $image['name'];
                        $img = file_get_contents($imageURL);
                        file_put_contents("DTImages/".$order_number."_".$imageName.".jpg", $img);
                    }
                    $apiLimit = $inc;
                }
            }
            $sql = "INSERT INTO dispatch.filesTracker (`filename`) VALUES ('".$file."')";
            $run = mysqli_query($db1, $sql);
        }
    }
}
  
?>
