$route['endTime'] || strtotime($route['endTime'].' UTC')>$now) continue; $steps=$route['steps'] ?? []; if (!is_array($steps) || !$steps) continue; $bad=false; foreach ($steps as $point) { if (!is_array($point) || !is_numeric($point['lat'] ?? null) || !is_numeric($point['lon'] ?? null) || !is_finite((float)$point['lat']) || !is_finite((float)$point['lon']) || abs((float)$point['lat'])>90 || abs((float)$point['lon'])>180 || ((float)$point['lat']===0.0 && (float)$point['lon']===0.0)) {$bad=true;break;} } if ($bad) continue; $route['source']='PACommonHere_GetDeviceRoutes'; $valid[]=$route; } usort($valid,static fn($a,$b)=>strcmp($b['endTime'],$a['endTime']) ?: (int)$b['routeNum']<=>(int)$a['routeNum']); $selected=array_slice($valid,0,2); foreach ($selected as $i=>&$route) { $route['displayLabel']=$i===0 ? 'Latest route' : 'Previous route'; $route['displayColor']=$i===0 ? '#e52587' : '#007f8b'; $route['originalPointCount']=count($route['steps']); // Limit rendered vertices, preserving both endpoints and the published route statistics. if (count($route['steps'])>1000) { $points=[];$last=count($route['steps'])-1; for ($n=0;$n<1000;$n++) $points[]=$route['steps'][(int)round($n*$last/999)]; $route['steps']=$points;$route['displaySimplified']=true; } else $route['displaySimplified']=false; } unset($route); return $selected; } function PA_WEB_DASH_MAP_LOAD(PDO $db,int $dashID,array $config,int $days): array { if (!in_array($days,[1,3,7],true)) throw new DomainException('Invalid history period.'); $data=PA_WEB_DASH_LOCATION_LOAD($db,$dashID,$config); // Resolve device identity from the case, never from a browser-supplied device ID. $q=$db->prepare('SELECT d.ClientID,d.ClientDevicesID,cd.DeviceID,cd.NameTag,cd.Enabled FROM PART_Dash d JOIN ClientDevices cd ON cd.ClientDevicesID=d.ClientDevicesID AND cd.ClientID=d.ClientID WHERE d.DashID=? AND d.RecordStatus=0'); $q->execute([$dashID]);$case=$q->fetch(PDO::FETCH_ASSOC); $data['routes']=[];$data['routeStatus']='No route history available.';$data['historyDays']=$days; $data['tracker']=['nameTag'=>'Not recorded','deviceID'=>'','enabled'=>null,'telemetry'=>null]; $data['profile']=['photoURL'=>'','telephone'=>'','email'=>'','vehicle'=>'']; $data['mapApiKey']=(string)($config['MAPS']['HERECOM']['APIKEY'] ?? ''); if (!$case) return $data; $data['tracker']['nameTag']=trim((string)$case['NameTag']) ?: 'Tracker'; $data['tracker']['deviceID']=(string)$case['DeviceID']; $data['tracker']['enabled']=(int)$case['Enabled']===1; $q=$db->prepare('SELECT Voltage,Phase,METHOD,InsertDateTime FROM PosLast WHERE DeviceID=?'); $q->execute([$case['DeviceID']]);$telemetry=$q->fetch(PDO::FETCH_ASSOC); if ($telemetry) $data['tracker']['telemetry']=$telemetry; $q=$db->prepare('SELECT ClientContactsID,Relationship,FirstName,LastName,Telephone,Email,VehicleDESC FROM ClientContacts WHERE ClientID=? AND ClientDevicesID=? AND ContactType=0 ORDER BY ClientContactsID LIMIT 2'); $q->execute([$case['ClientID'],$case['ClientDevicesID']]);$people=$q->fetchAll(PDO::FETCH_ASSOC); if (count($people)===1) { $person=$people[0]; $data['profile']['telephone']=(string)$person['Telephone']; $data['profile']['email']=(string)$person['Email']; $data['profile']['vehicle']=(string)$person['VehicleDESC']; try { $photo=PA_WEB_BRIEF_PHOTO($db,$case,$person); if ($photo) { require_once '/opt/SA/DEV/SERVICE/LIB/PACOMMONLIBRARY/php/AWSS3.php'; $url=AWSS3Helper::getPresignedUrl($photo['S3BucketName'],$photo['S3ObjectKey'],900); if (is_string($url) && parse_url($url,PHP_URL_SCHEME)==='https') $data['profile']['photoURL']=$url; } } catch (Throwable $e) {error_log('Tracker map photo unavailable for case '.$dashID);} } try { if (!preg_match('/^[A-Za-z0-9_-]{1,128}$/',(string)$case['DeviceID'])) throw new RuntimeException('Invalid stored device identity'); $raw=[];$count=0;$error='';$status=''; $oldZone=date_default_timezone_get();date_default_timezone_set('UTC'); try {PACommonHere_GetDeviceRoutes((string)$case['DeviceID'],['secondsAgo'=>$days*86400],$raw,$count,$error,$status);} finally {date_default_timezone_set($oldZone);} if (!in_array($error,['',0,null],true) || $status!=='OK') throw new RuntimeException('Route lookup failed'); $data['routes']=PA_WEB_DASH_MAP_ROUTES($raw,substr($data['checkedAt'],0,19)); $data['routeStatus']=$data['routes'] ? 'Recorded tracker routes' : 'No qualifying routes in this period.'; $data['routeFilterMeters']=$raw['filterMeters'] ?? 50; $data['routeGapMinutes']=$raw['timeGapMinutes'] ?? 30; } catch (Throwable $e) {$data['routeStatus']='Route history is temporarily unavailable.';error_log('Tracker map routes unavailable for case '.$dashID);} return $data; }