dashboard widgets are configurable via config (#269)

This commit is contained in:
Kevin Papst
2018-08-17 23:17:02 +02:00
committed by GitHub
parent 7804ef83ce
commit dbbb434723
49 changed files with 2011 additions and 938 deletions

View File

@@ -9,23 +9,19 @@
namespace App\EventSubscriber;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Event\DashboardEvent;
use App\Model\TimesheetGlobalStatistic;
use App\Model\TimesheetStatistic;
use App\Model\UserStatistic;
use App\Model\WidgetRow;
use App\Repository\Query\TimesheetQuery;
use Doctrine\Common\Persistence\ManagerRegistry;
use App\Model\DashboardSection;
use App\Model\Widget;
use App\Repository\ActivityRepository;
use App\Repository\CustomerRepository;
use App\Repository\ProjectRepository;
use App\Repository\UserRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
/**
* Used to add Dashboard widgets for a user.
* Used to add Dashboard widgets for users with ROLE_ADMIN.
*/
class DashboardSubscriber implements EventSubscriberInterface
{
@@ -34,18 +30,41 @@ class DashboardSubscriber implements EventSubscriberInterface
*/
protected $security;
/**
* @var ManagerRegistry
* @var UserRepository
*/
protected $registry;
protected $user;
/**
* @var ActivityRepository
*/
protected $activity;
/**
* @var ProjectRepository
*/
protected $project;
/**
* @var CustomerRepository
*/
protected $customer;
/**
* MenuSubscriber constructor.
* @param AuthorizationCheckerInterface $security
* @param UserRepository $user
* @param ActivityRepository $activity
* @param ProjectRepository $project
* @param CustomerRepository $customer
*/
public function __construct(AuthorizationCheckerInterface $security, ManagerRegistry $registry)
{
public function __construct(
AuthorizationCheckerInterface $security,
UserRepository $user,
ActivityRepository $activity,
ProjectRepository $project,
CustomerRepository $customer
) {
$this->security = $security;
$this->registry = $registry;
$this->user = $user;
$this->activity = $activity;
$this->project = $project;
$this->customer = $customer;
}
/**
@@ -64,106 +83,57 @@ class DashboardSubscriber implements EventSubscriberInterface
*/
public function onDashboardEvent(DashboardEvent $event)
{
$timesheetRepo = $this->registry->getRepository(Timesheet::class);
$timesheetGlobal = $timesheetRepo->getGlobalStatistics();
$timesheetUser = $timesheetRepo->getUserStatistics($event->getUser());
$userStats = $this->registry->getRepository(User::class)->getGlobalStatistics();
$this->addUserWidgets($event, $timesheetUser);
if (!$this->security->isGranted('ROLE_TEAMLEAD')) {
if (!$this->security->isGranted(User::ROLE_ADMIN)) {
return;
}
$this->addTeamleadWidgets($event, $timesheetGlobal, $userStats);
if (!$this->security->isGranted('ROLE_ADMIN')) {
return;
}
$this->addAdminWidgets($event, $timesheetGlobal, $userStats);
$this->addAdminWidgets($event);
}
/**
* @param DashboardEvent $event
* @param TimesheetStatistic $timesheet
*/
protected function addUserWidgets(DashboardEvent $event, TimesheetStatistic $timesheet)
{
/*
$row = new WidgetRow('dashboard.you');
$widgets = [
[
'widgets' => [
"{{ widgets.info_box_progress('Bewilligte Stunden', 'Stunden zur Abrechnung bewilligt', 120, 10, 'star') }}",
"{{ widgets.info_box_progress('Umsatz / Monat', '70% Increase in 30 Days', 6830, 30, 'credit-card', 'black') }}",
"{{ widgets.info_box_progress('Stunden persönlich', 'Das ist noch nicht genug', 135, 60, 'hourglass') }}",
"{{ widgets.info_box_progress('Anzahl Benutzer', 'Mehr ist besser!', 5, 90, 'user') }}",
],
],
$event->addWidgetRow($row);
*/
$row = new WidgetRow('profile.stats', 'dashboard.you');
$row
->add("{{ widgets.info_box_counter('stats.durationThisMonth', " . $timesheet->getDurationThisMonth() . "|duration(true), 'far fa-hourglass', 'green') }}")
//->add("{{ widgets.info_box_counter('stats.amountThisMonth', ".$timesheet->getAmountThisMonth()."|money, 'money', 'blue') }}")
->add("{{ widgets.info_box_counter('stats.durationTotal', " . $timesheet->getDurationTotal() . "|duration(true), 'far fa-hourglass', 'red') }}")
//->add("{{ widgets.info_box_counter('stats.amountTotal', ".$timesheet->getAmountTotal()."|money, 'money', 'yellow') }}")
;
$event->addWidgetRow($row);
}
/**
* @param DashboardEvent $event
* @param TimesheetGlobalStatistic $timesheet
* @param UserStatistic $userStats
*/
protected function addTeamleadWidgets(DashboardEvent $event, TimesheetGlobalStatistic $timesheet, UserStatistic $userStats)
{
$row = new WidgetRow('alluser.stats', 'dashboard.all');
$row
->add("{{ widgets.info_box_counter('stats.durationThisMonth', " . $timesheet->getDurationThisMonth() . "|duration(true), 'far fa-hourglass', 'blue') }}")
->add("{{ widgets.info_box_counter('stats.durationTotal', " . $timesheet->getDurationTotal() . "|duration(true), 'far fa-hourglass', 'yellow') }}")
->add("{{ widgets.info_box_counter('stats.activeRecordings', " . $timesheet->getActiveCurrently() . ", 'far fa-hourglass', 'red', path('admin_timesheet', {'state': " . TimesheetQuery::STATE_RUNNING . '})) }}')
;
$event->addWidgetRow($row);
$row = new WidgetRow('user.stats');
$row
->add("{{ widgets.info_box_counter('stats.userTotal', " . $userStats->getTotalAmount() . ", 'user', 'red') }}")
->add("{{ widgets.info_box_counter('stats.userActiveThisMoth', " . $timesheet->getActiveThisMonth() . ", 'user', 'yellow') }}")
->add("{{ widgets.info_box_counter('stats.userActiveEver', " . $timesheet->getActiveTotal() . ", 'user', 'blue') }}")
;
$event->addWidgetRow($row);
}
/**
* @param DashboardEvent $event
* @param TimesheetGlobalStatistic $timesheet
* @param UserStatistic $user
* @throws \Doctrine\ORM\NonUniqueResultException
*/
protected function addAdminWidgets(DashboardEvent $event, TimesheetGlobalStatistic $timesheet, UserStatistic $user)
protected function addAdminWidgets(DashboardEvent $event)
{
$row = new WidgetRow('alluser.money_stats');
$row
->add("{{ widgets.info_box_counter('stats.amountThisMonth', " . $timesheet->getAmountThisMonth() . "|money, 'far fa-money-bill-alt', 'green') }}")
->add("{{ widgets.info_box_counter('stats.amountTotal', " . $timesheet->getAmountTotal() . "|money, 'far fa-money-bill-alt', 'red') }}")
;
$event->addWidgetRow($row);
$section = new DashboardSection('dashboard.admin');
$section->setOrder(100);
$activity = $this->registry->getRepository(Activity::class)->getGlobalStatistics();
$project = $this->registry->getRepository(Project::class)->getGlobalStatistics();
$customer = $this->registry->getRepository(Customer::class)->getGlobalStatistics();
$row = new WidgetRow('admin.stats', 'dashboard.admin');
$row
->add("{{ widgets.info_box_more('stats.userTotal', " . $user->getTotalAmount() . ", ' ', path('admin_user'), 'user') }}")
->add("{{ widgets.info_box_more('stats.customerTotal', " . $customer->getCount() . ", '', path('admin_customer'), 'customer', 'blue') }}")
->add("{{ widgets.info_box_more('stats.projectsTotal', " . $project->getCount() . ", '', path('admin_project'), 'project', 'yellow') }}")
->add("{{ widgets.info_box_more('stats.activitiesTotal', " . $activity->getCount() . ", '', path('admin_activity'), 'activity', 'purple') }}")
$widget = new Widget('stats.userTotal', $this->user->countUser());
$widget
->setRoute('admin_user')
->setIcon('user')
->setType(Widget::TYPE_MORE)
;
$event->addWidgetRow($row);
$section->addWidget($widget);
$widget = new Widget('stats.customerTotal', $this->customer->countCustomer());
$widget
->setRoute('admin_customer')
->setIcon('customer')
->setColor('blue')
->setType(Widget::TYPE_MORE)
;
$section->addWidget($widget);
$widget = new Widget('stats.projectTotal', $this->project->countProject());
$widget
->setRoute('admin_project')
->setIcon('project')
->setColor('yellow')
->setType(Widget::TYPE_MORE)
;
$section->addWidget($widget);
$widget = new Widget('stats.activityTotal', $this->activity->countActivity());
$widget
->setRoute('admin_activity')
->setIcon('activity')
->setColor('purple')
->setType(Widget::TYPE_MORE)
;
$section->addWidget($widget);
$event->addSection($section);
}
}