add user preference: default report (#2430)

This commit is contained in:
Kevin Papst
2021-03-14 13:32:47 +01:00
committed by GitHub
parent 0a6ea6be42
commit c0378b4f51
16 changed files with 264 additions and 57 deletions

View File

@@ -0,0 +1,58 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form\Type;
use App\Entity\User;
use App\Reporting\ReportingService;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Custom form field type to select a report.
*/
class ReportType extends AbstractType
{
private $reportingService;
public function __construct(ReportingService $reportingService)
{
$this->reportingService = $reportingService;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('required', true);
$resolver->setDefault('translation_domain', 'reporting');
$resolver->setDefault('choices', function (Options $options) {
/** @var User $user */
$user = $options['user'];
$choices = [];
foreach ($this->reportingService->getAvailableReports($user) as $report) {
$choices[$report->getLabel()] = $report->getId();
}
return $choices;
});
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return ChoiceType::class;
}
}