<?php
//This script will change the image urls in the DispatchTrack XML files to point to the location on our server
//http://10.1.1.41/DT_Fetch/changeLinks.php
$fileArr = preg_grep('/^([^.])/',scandir('DTHistoryNew'));

foreach ($fileArr as $file) {
    $dt = substr($file,0,10); // output like 2020-01-03
    $x = date('m-d-Y', strtotime($dt)); //rearrange the date format
    if(strtotime($dt) > strtotime('October 27 2019')){ //pickup where we left off if the script times out
       //change images links
        $xml = simplexml_load_file("DTHistoryNew/".$file); //load file into simplexml object so we can parse it
        foreach ($xml->service_order as $soInfo) {
            if($soInfo->images['count'] > 0){
                $order_number = $soInfo['order_number'];
                foreach ($soInfo->images->image as $image) {
                    //$imageURL = $image['src'];
                    $imageName = $image['name'];
                    $imageURL = "http://10.1.1.41/DT_Fetch/DTImages/".$order_number."_".$imageName.".jpg";

                    //manipulate the xml
                    $image['src'] = $imageURL;
                    $image['thumbnail'] = '';
                }
            }
        }
        $output = $xml->asXML("DTHistory/".$file); //save the new xml 
    }
}

?>
