All checks were successful
Cadoles/nineskeletor/pipeline/head This commit looks good
170 lines
7.5 KiB
PHP
170 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace App\Service;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\DependencyInjection\ContainerInterface;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
|
|
|
class AppSession
|
|
{
|
|
private $container;
|
|
protected $em;
|
|
protected $requeststack;
|
|
protected $token;
|
|
|
|
public function __construct(ContainerInterface $container, EntityManagerInterface $em, RequestStack $requeststack, TokenStorageInterface $token)
|
|
{
|
|
$this->container = $container;
|
|
$this->requeststack = $requeststack;
|
|
$this->em = $em;
|
|
$this->token = $token;
|
|
}
|
|
|
|
public function onDomainParse(RequestEvent $event)
|
|
{
|
|
$session = $this->requeststack->getSession();
|
|
|
|
$configs = $this->em->getRepository("App\Entity\Config")->findAll();
|
|
foreach ($configs as $config) {
|
|
$session->set($config->getId(), strval($config->getValue()));
|
|
}
|
|
$session->set('headerimage', 'header/'.$session->get('headerimage'));
|
|
|
|
// Calcul couleur
|
|
$session->set('colorbgbodylight-darker', $this->adjustBrightness($session->get('colorbgbodylight'), -10));
|
|
$session->set('colorfttitlelight-darker', $this->adjustBrightness($session->get('colorfttitlelight'), -50));
|
|
|
|
$session->set('colorbgbodydark-darker', $this->adjustBrightness($session->get('colorbgbodydark'), -50));
|
|
$session->set('colorbgbodydark-lighter', $this->adjustBrightness($session->get('colorbgbodydark'), +50));
|
|
|
|
$session->set('colorbgbodydark-rgb', $this->hexToRgb($session->get('colorbgbodydark')));
|
|
$session->set('colorbgbodydark-darkrgb', $this->hexToRgb($session->get('colorbgbodydark-darker')));
|
|
$session->set('colorbgbodydark-lightrgb', $this->hexToRgb($session->get('colorbgbodydark-lighter')));
|
|
|
|
// Current user
|
|
$token = $this->token->getToken();
|
|
if (!$token) {
|
|
return;
|
|
}
|
|
$curentuser = $token->getUser();
|
|
|
|
// Préférence par défaut
|
|
$session->set('fgheader', true);
|
|
$session->set('fgaudit', false);
|
|
|
|
// Préférence
|
|
if ('anon.' != $curentuser) {
|
|
$preference = $curentuser->getPreference();
|
|
if (is_array($preference)) {
|
|
// Préférence header
|
|
if (array_key_exists('fgheader', $preference)) {
|
|
$fgheader = ('true' == $preference['fgheader'][0]);
|
|
$session->set('fgheader', $fgheader);
|
|
}
|
|
|
|
// Préférence audit
|
|
if (array_key_exists('fgaudit', $preference)) {
|
|
$fgaudit = ('true' == $preference['fgaudit'][0]);
|
|
$session->set('fgaudit', $fgaudit);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Permissions
|
|
$showannuaire = false;
|
|
$submitgroup = false;
|
|
if ('anon.' != $curentuser) {
|
|
if ($curentuser->hasRole('ROLE_ADMIN') && null != $this->container->getParameter('appAnnuscopeadmin')) {
|
|
$showannuaire = true;
|
|
$session->set('scopeannu', $this->container->getParameter('appAnnuscopeadmin'));
|
|
} elseif ($curentuser->hasRole('ROLE_MODO') && null != $this->container->getParameter('appAnnuscopemodo')) {
|
|
$showannuaire = true;
|
|
$session->set('scopeannu', $this->container->getParameter('appAnnuscopemodo'));
|
|
} elseif ($curentuser->hasRole('ROLE_MASTER') && null != $this->container->getParameter('appAnnuscopemaster')) {
|
|
$showannuaire = true;
|
|
$session->set('scopeannu', $this->container->getParameter('appAnnuscopemaster'));
|
|
} elseif ($curentuser->hasRole('ROLE_MANAGER') && null != $this->container->getParameter('appAnnuscopemanager')) {
|
|
$showannuaire = true;
|
|
$session->set('scopeannu', $this->container->getParameter('appAnnuscopemanager'));
|
|
} elseif ($curentuser->hasRole('ROLE_USER') && null != $this->container->getParameter('appAnnuscopeuser')) {
|
|
$showannuaire = true;
|
|
$session->set('scopeannu', $this->container->getParameter('appAnnuscopeuser'));
|
|
}
|
|
|
|
if (in_array('ALL', $this->container->getParameter('appGroupsubmiter'))) {
|
|
$submitgroup = true;
|
|
} elseif ($curentuser->hasRole('ROLE_ADMIN') && in_array('ROLE_ADMIN', $this->container->getParameter('appGroupsubmiter'))) {
|
|
$submitgroup = true;
|
|
} elseif ($curentuser->hasRole('ROLE_MODO') && in_array('ROLE_MODO', $this->container->getParameter('appGroupsubmiter'))) {
|
|
$submitgroup = true;
|
|
} elseif ($curentuser->hasRole('ROLE_MASTER') && in_array('ROLE_MASTER', $this->container->getParameter('appGroupsubmiter'))) {
|
|
$submitgroup = true;
|
|
} elseif ($curentuser->hasRole('ROLE_MANAGER') && in_array('ROLE_MANAGER', $this->container->getParameter('appGroupsubmiter'))) {
|
|
$submitgroup = true;
|
|
} elseif ($curentuser->hasRole('ROLE_USER') && in_array('ROLE_USER', $this->container->getParameter('appGroupsubmiter'))) {
|
|
$submitgroup = true;
|
|
}
|
|
}
|
|
$session->set('showannuaire', $showannuaire);
|
|
$session->set('submitgroup', $submitgroup);
|
|
|
|
// Visite
|
|
if ('anon.' != $curentuser) {
|
|
$now = new \DateTime();
|
|
if (!$curentuser->getVisitedate()) {
|
|
$curentuser->setVisitedate($now);
|
|
$curentuser->setVisitecpt($curentuser->getVisitecpt() + 1);
|
|
$this->em->persist($curentuser);
|
|
$this->em->flush();
|
|
} else {
|
|
$visitedate = clone $curentuser->getVisitedate();
|
|
$visitedate->add(new \DateInterval('PT1H'));
|
|
if ($visitedate < $now) {
|
|
$curentuser->setVisitedate($now);
|
|
$curentuser->setVisitecpt($curentuser->getVisitecpt() + 1);
|
|
$this->em->persist($curentuser);
|
|
$this->em->flush();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private function adjustBrightness($hex, $steps)
|
|
{
|
|
// Steps should be between -255 and 255. Negative = darker, positive = lighter
|
|
$steps = max(-255, min(255, $steps));
|
|
|
|
// Normalize into a six character long hex string
|
|
$hex = str_replace('#', '', $hex);
|
|
if (3 == strlen($hex)) {
|
|
$hex = str_repeat(substr($hex, 0, 1), 2).str_repeat(substr($hex, 1, 1), 2).str_repeat(substr($hex, 2, 1), 2);
|
|
}
|
|
|
|
// Split into three parts: R, G and B
|
|
$color_parts = str_split($hex, 2);
|
|
$return = '';
|
|
|
|
foreach ($color_parts as $color) {
|
|
$color = hexdec($color); // Convert to decimal
|
|
$color = max(0, min(255, $color + $steps)); // Adjust color
|
|
$return .= str_pad(dechex($color), 2, '0', STR_PAD_LEFT); // Make two char hex code
|
|
}
|
|
|
|
return '#'.$return;
|
|
}
|
|
|
|
public function hexToRgb($hex)
|
|
{
|
|
$hex = str_replace('#', '', $hex);
|
|
$length = strlen($hex);
|
|
$rgb['r'] = hexdec(6 == $length ? substr($hex, 0, 2) : (3 == $length ? str_repeat(substr($hex, 0, 1), 2) : 0));
|
|
$rgb['g'] = hexdec(6 == $length ? substr($hex, 2, 2) : (3 == $length ? str_repeat(substr($hex, 1, 1), 2) : 0));
|
|
$rgb['b'] = hexdec(6 == $length ? substr($hex, 4, 2) : (3 == $length ? str_repeat(substr($hex, 2, 1), 2) : 0));
|
|
|
|
return $rgb['r'].','.$rgb['g'].','.$rgb['b'];
|
|
}
|
|
}
|