src/Controller/ThemeController.php line 66

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\Yaml\Yaml;
  8. use Symfony\Component\HttpKernel\KernelInterface;
  9. use Symfony\Component\Finder\Finder;
  10. class ThemeController extends AbstractController
  11. {
  12.     private $appKernel;
  13.     public function __construct(KernelInterface $appKernel)
  14.     {
  15.         $this->appKernel $appKernel;
  16.     }
  17.     public function list(Request $request)
  18.     {
  19.         $session=$request->getSession();
  20.         $finder = new Finder();
  21.         $dir $this->appKernel->getProjectDir()."/public/themes";
  22.         $finder->in($dir)->directories()->depth('== 0');
  23.         $themes=[];
  24.         $themes[""]["dir"]="";
  25.         $themes[""]["name"]="Thème par défaut";
  26.         foreach ($finder as $file) {
  27.             $key=$file->getRelativePathname();
  28.             $themes[$key]["dir"]=$key;
  29.             $yml=Yaml::parseFile($dir.'/'.$key.'/info.yml');
  30.             $themes[$key]["name"]=$yml["name"];
  31.         }
  32.         
  33.         $current=$session->get("theme");
  34.         $currentheme=$themes[$current];
  35.         unset($themes[$current]);
  36.         return $this->render('Theme\list.html.twig',[
  37.             'useheader'     => true,
  38.             'usemenu'       => false,
  39.             'usesidebar'    => true,
  40.             'currentheme'   => $currentheme,
  41.             'themes'        => $themes
  42.         ]);
  43.     }   
  44.     public function select($nameRequest $requestManagerRegistry $em)
  45.     {
  46.         $config=$em->getRepository("App\Entity\Config")->findoneBy(["id"=>"theme"]);
  47.         $config->setValue($name);
  48.         $em->getManager()->flush();        
  49.         return $this->redirectToRoute("app_core_config_theme");
  50.     }
  51.     public function setconfig(Request $request)
  52.     {
  53.         $session=$request->getSession();
  54.         $themename=$session->get("theme");
  55.         $logo=$request->query->get('logo');
  56.         if(!empty($logo)) $session->set('logo',"themes/$themename/".$logo);
  57.        
  58.         $header=$request->query->get('header');
  59.         if(!empty($header)) $session->set('header',"themes/$themename/".$header);
  60.         $heightheader=$request->query->get('heightheader');
  61.         if(!empty($heightheader)) $session->set('heightheader',$heightheader);
  62.         
  63.         $colormain=$request->query->get('colormain');
  64.         if(!empty($colormain)) $session->set('colormain',$colormain);        
  65.         $fontcolorhover=$request->query->get('fontcolorhover');
  66.         if(!empty($fontcolorhover)) $session->set('fontcolorhover',$fontcolorhover);   
  67.         $colorbody=$request->query->get('colorbody');
  68.         if(!empty($colorbody)) $session->set('colorbody',$colorbody);    
  69.         $fontfacetitle=$request->query->get('fontfacetitle');
  70.         if(!empty($fontfacetitle)) $session->set('fontfacetitle',$fontfacetitle);        
  71.         $fontfacebody=$request->query->get('fontfacebody');
  72.         if(!empty($fontfacebody)) $session->set('fontfacebody',$fontfacebody);        
  73.         $this->setColor($session);
  74.         return new Response();
  75.     }
  76.     public function setColor($session) {
  77.         $colormain      =$session->get('colormain');
  78.         $colorlight     =$this->adjustBrightness($colormain,+50);
  79.         $colordark      =$this->adjustBrightness($colormain,-50);
  80.         $colormainrgb   =$this->hexToRgb($colormain);
  81.         $colorlightrgb  =$this->hexToRgb($colorlight);
  82.         $colordarkrgb   =$this->hexToRgb($colordark);        
  83.         $fontcolorhover =$session->get('fontcolorhover');
  84.         $colorbody      =$session->get('colorbody');
  85.         $tbcolor=array(
  86.             "main"                  =>$colormain,
  87.             "light"                 =>$this->adjustBrightness($colormain,+50),
  88.             "dark"                  =>$this->adjustBrightness($colormain,-50),
  89.             "mainrgb"               =>$this->hexToRgb($colormain),
  90.             "lightrgb"              =>$this->hexToRgb($colorlight),
  91.             "darkrgb"               =>$this->hexToRgb($colordark),
  92.             "fontcolorhover"        =>$fontcolorhover,
  93.             "fontcolorhoverlight"   =>$this->adjustBrightness($fontcolorhover,+50),
  94.             "fontcolorhoverdark"    =>$this->adjustBrightness($fontcolorhover,-50),
  95.             "colorbody"             =>$colorbody,
  96.         );
  97.         
  98.         $session->set('color'$tbcolor);
  99.     }
  100.     public function adjustBrightness($hex$steps) {
  101.         // Steps should be between -255 and 255. Negative = darker, positive = lighter
  102.         $steps max(-255min(255$steps));
  103.         // Normalize into a six character long hex string
  104.         $hex str_replace('#'''$hex);
  105.         if (strlen($hex) == 3) {
  106.             $hex str_repeat(substr($hex,0,1), 2).str_repeat(substr($hex,1,1), 2).str_repeat(substr($hex,2,1), 2);
  107.         }
  108.         // Split into three parts: R, G and B
  109.         $color_parts str_split($hex2);
  110.         $return '';
  111.         foreach ($color_parts as $color) {
  112.             $color   hexdec($color); // Convert to decimal
  113.             $color   max(0,min(255,$color $steps)); // Adjust color
  114.             $return .= str_pad(dechex($color), 2'0'STR_PAD_LEFT); // Make two char hex code
  115.         }
  116.         return $return;
  117.     }
  118.     public function hexToRgb($hex) {
  119.        $hex      str_replace('#'''$hex);
  120.        $length   strlen($hex);
  121.        $rgb['r'] = hexdec($length == substr($hex02) : ($length == str_repeat(substr($hex01), 2) : 0));
  122.        $rgb['g'] = hexdec($length == substr($hex22) : ($length == str_repeat(substr($hex11), 2) : 0));
  123.        $rgb['b'] = hexdec($length == substr($hex42) : ($length == str_repeat(substr($hex21), 2) : 0));
  124.        return $rgb;
  125.    }      
  126. }