2021-05-13 12:40:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2024-04-13 12:41:58 +00:00
|
|
|
use Illuminate\Http\Request;
|
2021-06-20 13:03:20 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2023-10-05 11:59:59 +00:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
2021-05-13 12:40:21 +00:00
|
|
|
|
2023-09-10 12:48:12 +00:00
|
|
|
use App\Http\Requests\DomainRequest;
|
2021-06-20 13:03:20 +00:00
|
|
|
use App\Models\{Address,Domain,Zone};
|
2021-05-13 12:40:21 +00:00
|
|
|
|
|
|
|
class DomainController extends Controller
|
|
|
|
{
|
2024-04-13 12:41:58 +00:00
|
|
|
/**
|
|
|
|
* Daily stats as shown on the about page
|
|
|
|
*
|
|
|
|
* @param Domain $o
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function api_daily_stats(Request $request): Collection
|
|
|
|
{
|
2024-04-20 12:03:47 +00:00
|
|
|
$o = Domain::where('name',$request->name)->firstOrFail();
|
|
|
|
|
|
|
|
return $o->echoarea_total_daily()
|
|
|
|
->sortBy('date')
|
|
|
|
->groupBy('date')
|
|
|
|
->transform(function($item,$key) { return [
|
|
|
|
'x'=>\Carbon\Carbon::createFromFormat('Y-m-d',$key)->timestamp,
|
|
|
|
'y'=>$item->sum('count')]; } )
|
|
|
|
->values();
|
2024-04-13 12:41:58 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 12:40:21 +00:00
|
|
|
/**
|
2021-07-26 11:21:58 +00:00
|
|
|
* Add or edit a domain
|
2021-05-13 12:40:21 +00:00
|
|
|
*/
|
2023-09-10 12:48:12 +00:00
|
|
|
public function add_edit(DomainRequest $request,Domain $o)
|
2021-05-13 12:40:21 +00:00
|
|
|
{
|
|
|
|
if ($request->post()) {
|
2024-04-26 06:18:40 +00:00
|
|
|
foreach (['name','dnsdomain','active','public','homepage','notes','flatten','accept_app'] as $key)
|
2021-05-13 12:40:21 +00:00
|
|
|
$o->{$key} = $request->post($key);
|
|
|
|
|
|
|
|
$o->save();
|
|
|
|
|
2023-10-05 12:28:18 +00:00
|
|
|
return redirect()->to('domain');
|
2021-05-13 12:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return view('domain.addedit')
|
|
|
|
->with('o',$o);
|
|
|
|
}
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
/**
|
|
|
|
* Get all the hosts for a zone of a particular region (or not)
|
|
|
|
*
|
|
|
|
* @param Zone $o
|
|
|
|
* @param int $region
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function api_hosts(Zone $o,int $region): Collection
|
|
|
|
{
|
2022-01-24 11:56:13 +00:00
|
|
|
$oo = Address::where('role',Address::NODE_NC)
|
2021-06-20 13:03:20 +00:00
|
|
|
->where('zone_id',$o->id)
|
2022-02-03 06:35:52 +00:00
|
|
|
->when($region,function($query,$region) { return $query->where('region_id',$region); })
|
2021-07-02 13:19:50 +00:00
|
|
|
->when((! $region),function($query) use ($region) { return $query->where('region_id',0); })
|
2021-06-20 13:03:20 +00:00
|
|
|
->where('point_id',0)
|
2021-07-05 13:17:00 +00:00
|
|
|
->FTNorder()
|
2021-06-20 13:03:20 +00:00
|
|
|
->with(['system'])
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return $oo->map(function($item) {
|
2021-06-26 01:48:55 +00:00
|
|
|
return ['id'=>$item->host_id,'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->name)];
|
2021-06-20 13:03:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all the hubs for a host
|
|
|
|
*
|
|
|
|
* @param Zone $o
|
|
|
|
* @param int $host
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function api_hubs(Zone $o,int $host): Collection
|
|
|
|
{
|
2022-02-03 06:35:52 +00:00
|
|
|
$oo = Address::where('role',Address::NODE_HC)
|
2021-06-20 13:03:20 +00:00
|
|
|
->where('zone_id',$o->id)
|
|
|
|
->when($host,function($query,$host) { return $query->where('host_id',$host)->where('node_id','<>',0); })
|
|
|
|
->with(['system'])
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return $oo->map(function($item) {
|
2021-06-26 01:48:55 +00:00
|
|
|
return ['id'=>$item->id,'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->name)];
|
2021-06-20 13:03:20 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all the regions for a zone
|
|
|
|
*
|
|
|
|
* @param Zone $o
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function api_regions(Zone $o): Collection
|
|
|
|
{
|
2022-01-24 11:56:13 +00:00
|
|
|
$oo = Address::where('role',Address::NODE_RC)
|
2021-06-20 13:03:20 +00:00
|
|
|
->where('zone_id',$o->id)
|
|
|
|
->where('node_id',0)
|
|
|
|
->where('point_id',0)
|
|
|
|
->orderBy('region_id')
|
2023-07-29 03:17:36 +00:00
|
|
|
->active()
|
2021-06-20 13:03:20 +00:00
|
|
|
->with(['system'])
|
|
|
|
->get();
|
|
|
|
|
|
|
|
return $oo->map(function($item) {
|
2021-06-26 01:48:55 +00:00
|
|
|
return ['id'=>$item->region_id,'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->location)];
|
2021-06-20 13:03:20 +00:00
|
|
|
});
|
|
|
|
}
|
2023-10-05 11:59:59 +00:00
|
|
|
|
|
|
|
public function view(Domain $o)
|
|
|
|
{
|
|
|
|
if (! $o->public && ! Gate::check('admin',$o))
|
|
|
|
abort(404);
|
|
|
|
|
2024-05-09 11:22:30 +00:00
|
|
|
$o->load(['zones.system','zones.domain','zones.addresses.nodes_hub']);
|
2023-10-05 11:59:59 +00:00
|
|
|
|
|
|
|
return view('domain.view')
|
|
|
|
->with('o',$o);
|
|
|
|
}
|
2023-10-05 11:42:41 +00:00
|
|
|
}
|