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) { switch ($session->get('permannu')) { case 'ROLE_USER': $showannuaire = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO') || $curentuser->hasRole('ROLE_MASTER') || $curentuser->hasRole('ROLE_USER')); break; case 'ROLE_MASTER': $showannuaire = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO') || $curentuser->hasRole('ROLE_MASTER')); break; case 'ROLE_MODO': $showannuaire = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO')); break; } switch ($session->get('permgroup')) { case 'ROLE_USER': $submitgroup = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO') || $curentuser->hasRole('ROLE_MASTER') || $curentuser->hasRole('ROLE_USER')); break; case 'ROLE_MASTER': $submitgroup = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO') || $curentuser->hasRole('ROLE_MASTER')); break; case 'ROLE_MODO': $submitgroup = ($curentuser->hasRole('ROLE_ADMIN') || $curentuser->hasRole('ROLE_MODO')); break; } } $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']; } }