Files
kimai2/src/Controller/AbstractController.php

93 lines
2.5 KiB
PHP

<?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\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Translation\DataCollectorTranslator;
/**
* The abstract base controller.
*/
abstract class AbstractController extends Controller
{
public const FLASH_SUCCESS = 'success';
public const FLASH_WARNING = 'warning';
public const FLASH_ERROR = 'error';
public const DOMAIN_FLASH = 'flashmessages';
public const DOMAIN_ERROR = 'exceptions';
public const ROLE_ADMIN = 'ROLE_ADMIN';
/**
* @return DataCollectorTranslator
*/
private function getTranslator()
{
return $this->container->get('translator');
}
/**
* Adds a "successful" flash message to the stack.
*
* @param string $translationKey
* @param array $parameter
*/
protected function flashSuccess($translationKey, $parameter = [])
{
$this->addFlashTranslated(self::FLASH_SUCCESS, $translationKey, $parameter);
}
/**
* Adds a "warning" flash message to the stack.
*
* @param string $translationKey
* @param array $parameter
*/
protected function flashWarning($translationKey, $parameter = [])
{
$this->addFlashTranslated(self::FLASH_WARNING, $translationKey, $parameter);
}
/**
* Adds a "error" flash message to the stack.
*
* @param string $translationKey
* @param array $parameter
*/
protected function flashError($translationKey, $parameter = [])
{
$this->addFlashTranslated(self::FLASH_ERROR, $translationKey, $parameter);
}
/**
* Adds a fully translated (both $message and all keys in $parameter) flash message to the stack.
*
* @param string $type
* @param string $message
* @param array $parameter
*/
protected function addFlashTranslated(string $type, string $message, array $parameter = [])
{
if (!empty($parameter)) {
foreach ($parameter as $key => $value) {
$parameter[$key] = $this->getTranslator()->trans($value, [], self::DOMAIN_FLASH);
}
$message = $this->getTranslator()->trans(
$message,
$parameter,
self::DOMAIN_FLASH
);
}
$this->addFlash($type, $message);
}
}