diff --git a/app/Models/Address.php b/app/Models/Address.php index 4e244e2..83fd3e6 100644 --- a/app/Models/Address.php +++ b/app/Models/Address.php @@ -254,6 +254,9 @@ class Address extends Model $ftn = self::parseFTN($address); $do = $ftn['d'] ? Domain::where('name',$ftn['d'])->single() : NULL; + if ($ftn['z'] === 0) + Log::alert(sprintf('%s:! newFTN was parsed an FTN [%s] with a zero zone',self::LOGKEY,$address)); + $zo = Zone::where('zone_id',$ftn['z']) ->when($do,fn($query)=>$query->where('domain_id',$do->id)) ->single(); @@ -337,34 +340,39 @@ class Address extends Model $region_id = 0; $zone_id = NULL; - // We can only work out region if we have a domain + // We can only work out region/zone if we have a domain - this is for 2D parsing if ($matches[5] ?? NULL) { $o = new self; $o->host_id = $matches[2]; $o->node_id = $matches[3]; $o->point_id = empty($matches[4]) ? 0 : (int)$matches[4]; - $zo = Zone::select('zones.*')->where('zone_id',$matches[1])->join('domains',['domains.id'=>'zones.domain_id'])->where('domains.name',$matches[5])->single(); + if ($matches[1] !== "0") { + $zo = Zone::select('zones.*') + ->where('zone_id',$matches[1]) + ->join('domains',['domains.id'=>'zones.domain_id']) + ->where('domains.name',$matches[5]) + ->single(); + + // Try and find out the zone from the host_id + } else { + $zo = Zone::select('zones.*') + ->where(fn($query)=>$query->where('host_id',$matches[2])->orWhere('region_id',$matches[2])) + ->join('domains',['domains.id'=>'zones.domain_id']) + ->join('addresses',['addresses.zone_id'=>'zones.id']) + ->where('domains.name',$matches[5]) + ->first(); + } + $o->zone_id = $zo?->id; $parent = $o->parent(); - $zone_id = $parent?->zone->zone_id; - - // For flattened domains - if ($zo?->domain->flatten && is_null($zone_id)) - foreach ($zo->domain->zones as $zoo) { - $o->zone_id = $zoo->id; - $parent = $o->parent(); - - if ($parent) - break; - } $region_id = $parent?->region_id; $zone_id = $parent?->zone->zone_id; } return [ - 'z' => (int)$zone_id ?: $matches[1], + 'z' => (int)($zone_id ?: $matches[1]), 'r' => (int)$region_id, 'n' => (int)$matches[2], 'f' => (int)$matches[3],