Update the dashboard with an organisations widget

This commit is contained in:
2022-03-07 16:27:52 +00:00
parent 2005a17eef
commit 012b633eaf
10 changed files with 554 additions and 0 deletions

View File

@@ -0,0 +1,109 @@
<?php
/*
* AVSDev UF Organisations (https://avsdev.uk)
*
* @link https://git.avsdev.uk/avsdev/sprinkle-organisations
* @license https://git.avsdev.uk/avsdev/sprinkle-organisations/blob/master/LICENSE.md (LGPL-3.0 License)
*/
namespace UserFrosting\Sprinkle\Organisations\Controller;
use Carbon\Carbon;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use UserFrosting\Sprinkle\Core\Controller\SimpleController;
use UserFrosting\Sprinkle\Core\Util\EnvironmentInfo;
use UserFrosting\Support\Exception\ForbiddenException;
/**
* Controller class for organisation-related requests, including listing organisations, CRUD for organisations, etc.
*
* @author Craig Williams (https://avsdev.uk)
*/
class DashboardController extends SimpleController
{
/**
* Renders the admin panel dashboard.
*
* @param Request $request
* @param Response $response
* @param array $args
*/
public function pageDashboard(Request $request, Response $response, $args)
{
//** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
$authorizer = $this->ci->authorizer;
/** @var \Illuminate\Cache\Repository $cache */
$cache = $this->ci->cache;
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
$classMapper = $this->ci->classMapper;
/** @var \UserFrosting\Support\Repository\Repository $config */
$config = $this->ci->config;
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
$currentUser = $this->ci->currentUser;
// Access-controlled page
if (!$authorizer->checkAccess($currentUser, 'uri_dashboard')) {
throw new ForbiddenException();
}
// Probably a better way to do this
$users = $classMapper->getClassMapping('user')::orderBy('created_at', 'desc')
->take(8)
->get();
// Transform the `create_at` date in "x days ago" type of string
$users->transform(function ($item, $key) {
$item->registered = Carbon::parse($item->created_at)->diffForHumans();
return $item;
});
// Probably a better way to do this
$organisations = $classMapper->getClassMapping('organisation')::orderBy('created_at', 'desc')
->take(4)
->get();
// Transform the `create_at` date in "x days ago" type of string
$organisations->transform(function ($item, $key) {
$item->registered = Carbon::parse($item->created_at)->diffForHumans();
return $item;
});
// Get each sprinkle db version
$sprinkles = $this->ci->sprinkleManager->getSprinkleNames();
return $this->ci->view->render($response, 'pages/dashboard.html.twig', [
'counter' => [
'users' => $classMapper->getClassMapping('user')::count(),
'roles' => $classMapper->getClassMapping('role')::count(),
'groups' => $classMapper->getClassMapping('group')::count(),
'organisations' => $classMapper->getClassMapping('organisation')::count(),
],
'info' => [
'version' => [
'UF' => \UserFrosting\VERSION,
'php' => phpversion(),
'database' => EnvironmentInfo::database(),
],
'database' => [
'name' => $config['db.default.database'],
],
'environment' => $this->ci->environment,
'path' => [
'project' => \UserFrosting\ROOT_DIR,
],
],
'sprinkles' => $sprinkles,
'users' => $users,
'organisations' => $organisations,
]);
}
}

View File

@@ -135,6 +135,19 @@ class Organisation extends Model implements OrganisationInterface, TokenOwnerInt
->where('flag_admin', true);
}
/**
* Get a list of pending members within this organisation.
*/
public function pendingMembers()
{
/** @var \UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */
$classMapper = static::$ci->classMapper;
return $this
->belongsToMany($classMapper->getClassMapping('user'), 'organisation_members', 'organisation_id', 'user_id')
->where('flag_approved', false);
}
/**
* Delete this organisation from the database, along with any linked objects.
*

View File

@@ -33,6 +33,7 @@ class OrganisationSprunje extends Sprunje
'member_count',
'admin_count',
'status',
'created_at',
];
protected $filterable = [
@@ -42,6 +43,7 @@ class OrganisationSprunje extends Sprunje
'admin_count',
'status',
'info',
'created_at',
];
protected $excludeForAll = [
@@ -136,4 +138,20 @@ class OrganisationSprunje extends Sprunje
return $this;
}
/**
* Sort based on created date.
*
* @param Builder $query
* @param string $direction
*
* @return self
*/
protected function sortCreatedAt($query, $direction)
{
$query->orderBy('organisations.created_at', $direction)
->orderby('organisations.id', $direction);
return $this;
}
}