Start on user dashboard

This commit is contained in:
Deon George
2021-10-26 23:19:55 +11:00
parent 8c127ba5da
commit a0db589dc5
8 changed files with 309 additions and 7 deletions

View File

@@ -13,6 +13,11 @@ use App\Models\{Address,Domain,Echomail,Setup};
class HomeController extends Controller
{
public function home()
{
return redirect(Auth::check() ? 'dashboard' : 'about');
}
public function network(Domain $o)
{
if (! $o->public && ! Gate::check('admin',$o))

View File

@@ -9,11 +9,6 @@ use App\Models\User;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('can:admin');
}
/**
* Add or edit a node
*/
@@ -53,6 +48,11 @@ class UserController extends Controller
->with('o',$o);
}
public function dashboard()
{
return view('dashboard');
}
public function home()
{
return view('user.home');

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use App\Traits\ScopeActive;
@@ -49,4 +50,32 @@ class Domain extends Model
{
$this->attributes['homepage'] = base64_encode(gzcompress($value,9));
}
/* METHODS */
public function stats(System $o=NULL): Collection
{
if (! $this->echoareas->count())
return collect();
$where = collect(['echoarea_id'=>$this->echoareas->pluck('id')->toArray()]);
if ($o)
$where->put('fftn_id',$o->addresses()->pluck('id'));
$echostats = Echomail::countGroupBy('echoarea_id',$where->toArray());
return $this->echoareas->map(function($item) use ($echostats) {
$stats = $echostats->filter(function($x) use ($item) {
return $x->id->echoarea_id == $item->id;
});
$item->count = 0;
foreach ($stats as $o)
$item->count += $o->count;
return $item;
});
}
}

View File

@@ -60,6 +60,13 @@ class User extends Authenticatable implements MustVerifyEmail
protected $dates = ['last_on'];
/* RELATIONS */
public function systems()
{
return $this->belongsToMany(System::class);
}
/* GENERAL METHODS */
/**

View File

@@ -5,6 +5,8 @@
*/
namespace App\Traits;
use Illuminate\Database\Eloquent\Collection;
trait UseMongo
{
/**
@@ -40,4 +42,41 @@ trait UseMongo
{
$this->attributes['subject'] = utf8_encode($value);
}
/* METHODS */
public static function countGroupBy(string $field,array $where=[]): Collection
{
$query = collect();
if (count($where)) {
$where_condition = [];
foreach ($where as $key => $values) {
if (! is_array($values))
throw new \Exception('Where values must be an array.');
$where_condition[$key] = ['$in' => $values];
}
$query->push([
'$match' => $where_condition
]);
}
$query->push([
'$group' => [
'_id' => [$field=>'$'.$field],
'count' => ['$sum' => 1]
]
]);
return (new self)
->groupBy($field)
->raw(function($collection) use ($query) {
return $collection->aggregate(
$query->toArray()
);
});
}
}