$a['ageSeconds']<=>$b['ageSeconds'] ?: (($a['source']==='PosLast' ? 0 : 1)<=>($b['source']==='PosLast' ? 0 : 1))); return $positions[0] ?? null; } function PA_WEB_DASH_LOCATION_GEOCODE(array $point,string $apiKey,?callable $lookup=null,?string $cacheDir=null): array { if ($apiKey==='' && $lookup===null) return []; $lookup ??= static fn()=>PACommonHereAlertLocation($point['latitude'],$point['longitude'],$apiKey,false); // Cache failures must not trigger the development site's warning-to-HTTP-500 handler. $cacheIO=static function(callable $operation) { set_error_handler(static fn()=>true); try {return $operation();} finally {restore_error_handler();} }; $cacheDir ??= sys_get_temp_dir().'/pa-dash-location-cache-'.(function_exists('posix_geteuid') ? posix_geteuid() : 'web'); if (!is_dir($cacheDir)) $cacheIO(static fn()=>mkdir($cacheDir,0700,true)); if (!is_dir($cacheDir) || !is_writable($cacheDir)) { try {$address=$lookup();} catch (Throwable $e) {$address=[];} if (!is_array($address)) return []; unset($address['map_png']); return $address; } $path=$cacheDir.'/'.hash('sha256',sprintf('%.7f,%.7f',$point['latitude'],$point['longitude']).hash('sha256',$apiKey)).'.json'; $cached=is_file($path) ? json_decode((string)$cacheIO(static fn()=>file_get_contents($path)),true) : null; if (is_array($cached) && (int)($cached['expires'] ?? 0)>time() && is_array($cached['address'] ?? null)) return $cached['address']; // A per-coordinate lock keeps multiple command-center screens from repeating the same lookup. $lock=$cacheIO(static fn()=>fopen($path.'.lock','c')); if ($lock) $cacheIO(static fn()=>chmod($path.'.lock',0600)); if ($lock && !flock($lock,LOCK_EX|LOCK_NB)) {fclose($lock);return [];} try { $cached=is_file($path) ? json_decode((string)$cacheIO(static fn()=>file_get_contents($path)),true) : null; if (is_array($cached) && (int)($cached['expires'] ?? 0)>time() && is_array($cached['address'] ?? null)) return $cached['address']; try {$address=$lookup();} catch (Throwable $e) {$address=[];} if (!is_array($address)) $address=[]; unset($address['map_png']); $ttl=trim((string)($address['city'] ?? ''))!=='' || trim((string)($address['nearby_address'] ?? ''))!=='' ? 3600 : 30; if ($lock) { $cacheIO(static fn()=>file_put_contents($path,json_encode(['expires'=>time()+$ttl,'address'=>$address],JSON_INVALID_UTF8_SUBSTITUTE),LOCK_EX)); $cacheIO(static fn()=>chmod($path,0600)); } return $address; } finally {if ($lock) {flock($lock,LOCK_UN);fclose($lock);}} } function PA_WEB_DASH_LOCATION_LOAD(PDO $db,int $dashID,array $config,bool $includeAddress=true): array { $q=$db->prepare('SELECT d.DashID,d.CaseName,d.Status,d.ClientID,d.ClientDevicesID,d.ClientDevicesEventID,cd.DeviceID FROM PART_Dash d LEFT 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); if (!$case) throw new DomainException('Case not found.'); $q=$db->prepare('SELECT FirstName,LastName FROM ClientContacts WHERE ClientID=? AND ClientDevicesID=? AND ContactType=0 LIMIT 2'); $q->execute([$case['ClientID'],$case['ClientDevicesID']]);$people=$q->fetchAll(PDO::FETCH_ASSOC); $person=count($people)===1 ? trim($people[0]['FirstName'].' '.$people[0]['LastName']) : ''; $rows=[]; if ($case['DeviceID']) { $q=$db->prepare("SELECT DeviceID,LAT,LON,ACC,TS,InsertDateTime,'PosLast' AS Source FROM PosLast WHERE DeviceID=?"); $q->execute([$case['DeviceID']]);$rows=$q->fetchAll(PDO::FETCH_ASSOC); $q=$db->prepare("SELECT e.ClientDevicesEventID,e.LAT,e.LON,e.ACC,e.InsertDateTime,'ClientDeviceEvent' AS Source FROM ClientDeviceEvent e WHERE e.ClientID=? AND e.DeviceID=? AND e.RecordStatus=0 AND (e.ClientDevicesEventID=? OR EXISTS (SELECT 1 FROM PART_DashDeviceEvents l WHERE l.DashID=? AND l.ClientDevicesEventID=e.ClientDevicesEventID)) ORDER BY e.InsertDateTime DESC,e.ClientDevicesEventID DESC LIMIT 25"); $q->execute([$case['ClientID'],$case['DeviceID'],$case['ClientDevicesEventID'],$dashID]); $rows=array_merge($rows,$q->fetchAll(PDO::FETCH_ASSOC)); } $snapshot=gmdate('Y-m-d H:i:s'); $point=PA_WEB_DASH_LOCATION_SELECT($rows,$snapshot); $address=$point && $includeAddress ? PA_WEB_DASH_LOCATION_GEOCODE($point,(string)($config['MAPS']['HERECOM']['APIKEY'] ?? '')) : []; return ['dashID'=>$dashID,'caseName'=>$case['CaseName'],'caseActive'=>$case['Status']==='Active', 'trackerUser'=>$person ?: 'Tracker user','checkedAt'=>$snapshot.' UTC','checkedAtMs'=>strtotime($snapshot.' UTC')*1000, 'location'=>$point,'address'=>$address]; } function PA_WEB_DASH_LOCATION_PANEL(int $dashID): void { ?>
Latest reported tracker location

Loading location...

Checking
Tracker device position; not a confirmed sighting of the person.
Map and recent routes