clrghouz/app/Http/Controllers/DomainController.php

123 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2021-05-13 12:40:21 +00:00
<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Http\Request;
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
use App\Http\Requests\DomainRequest;
use App\Models\{Address,Domain,Zone};
2021-05-13 12:40:21 +00:00
class DomainController extends Controller
{
/**
* Daily stats as shown on the about page
*
* @param Request $request
* @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::createFromFormat('Y-m-d',$key)->timestamp*1000,
2024-04-20 12:03:47 +00:00
'y'=>$item->sum('count')]; } )
->values();
}
2021-05-13 12:40:21 +00:00
/**
* Add or edit a domain
2021-05-13 12:40:21 +00:00
*/
public function add_edit(DomainRequest $request,Domain $o)
2021-05-13 12:40:21 +00:00
{
if ($request->post()) {
foreach (['name','dnsdomain','active','public','homepage','notes','flatten','accept_app','nodestatus_id'] as $key)
2021-05-13 12:40:21 +00:00
$o->{$key} = $request->post($key);
$o->save();
return redirect()->to('domain');
2021-05-13 12:40:21 +00:00
}
return view('domain.addedit')
->with('o',$o);
}
/**
* 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
{
return $o->hosts
->filter(fn($item)=>$item->role_id === Address::NODE_NC)
->filter(fn($item)=>$region ? ($item->region_id === $region) : TRUE)
->map(function($item) {
return [
'id'=>$item->host_id,
'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->name)
];
})
->values();
}
/**
* Find all the hubs for a host
*
* @param Zone $o
* @param int $host
* @return Collection
*/
public function api_hubs(Zone $o,int $host): Collection
{
return $o->hubs
->filter(fn($item)=>$item->role_id === Address::NODE_HC)
->filter(fn($item)=>$host ? ($item->host_id === $host) : TRUE)
->map(function($item) {
return [
'id'=>$item->id,
'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->name)
];
})
->values();
}
/**
* Get all the regions for a zone
*
* @param Zone $o
* @return Collection
*/
public function api_regions(Zone $o): Collection
{
return $o->regions
->filter(fn($item)=>$item->role_id === Address::NODE_RC)
->map(function($item) {
return [
'id'=>$item->region_id,
'value'=>sprintf('%s %s',$item->ftn_3d,$item->system->location)
];
})
->values();
}
2023-10-05 11:59:59 +00:00
public function view(Domain $o)
{
if (! $o->public && ! Gate::check('admin',$o))
abort(404);
$o->load(['zones.system','zones.domain','zones.addresses.nodes_hub','zones.addresses.echomail_from']);
2023-10-05 11:59:59 +00:00
return view('domain.view')
->with('o',$o);
}
}