96 lines
2.7 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\TaskBundle\Menu;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Knp\Menu\MenuItem;
use LogicException;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class MenuBuilder implements LocalMenuBuilderInterface
{
protected AuthorizationCheckerInterface $authorizationChecker;
protected TranslatorInterface $translator;
public function __construct(
AuthorizationCheckerInterface $authorizationChecker,
TranslatorInterface $translator
) {
$this->translator = $translator;
$this->authorizationChecker = $authorizationChecker;
}
public function buildMenu($menuId, MenuItem $menu, array $parameters)
{
switch ($menuId) {
case 'person':
$this->buildPersonMenu($menu, $parameters);
break;
case 'accompanyingCourse':
$this->buildAccompanyingCourseMenu($menu, $parameters);
break;
case 'section':
$menu->setExtras('icons', 'tasks');
break;
default:
throw new LogicException("this menuid {$menuId} is not implemented");
}
}
public function buildAccompanyingCourseMenu($menu, $parameters)
{
$course = $parameters['accompanyingCourse'];
if ($this->authorizationChecker->isGranted(TaskVoter::SHOW, $course)) {
$menu->addChild(
$this->translator->trans('Tasks'),
[
'route' => 'chill_task_singletask_by-course_list',
'routeParameters' => ['id' => $course->getId()],
]
)
->setExtra('order', 400);
}
}
public function buildPersonMenu($menu, $parameters)
{
//var $person \Chill\PersonBundle\Entity\Person */
$person = $parameters['person'] ?? null;
if ($this->authorizationChecker->isGranted(TaskVoter::SHOW, $person)) {
$menu->addChild(
$this->translator->trans('Tasks'),
[
'route' => 'chill_task_singletask_by-person_list',
'routeParameters' => ['id' => $person->getId()],
]
)
->setExtra('order', 400);
}
}
public static function getMenuIds(): array
{
return ['person', 'accompanyingCourse'];
}
}