From 3f9f52c3dd01eab3085a3858f1c413651944bc6a Mon Sep 17 00:00:00 2001
From: Davide <davide@suma-ev.de>
Date: Tue, 20 Oct 2020 09:20:01 +0200
Subject: [PATCH] fixed dark mode cookie and added cookie awareness

---
 app/Http/Controllers/SettingsController.php   | 16 ++--------------
 app/Http/Controllers/StartpageController.php  | 11 +----------
 resources/views/layouts/resultPage.blade.php  |  7 ++++++-
 resources/views/layouts/staticPages.blade.php |  9 +++++++--
 4 files changed, 16 insertions(+), 27 deletions(-)

diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php
index 4e7a7b020..c6bb7a55d 100644
--- a/app/Http/Controllers/SettingsController.php
+++ b/app/Http/Controllers/SettingsController.php
@@ -72,18 +72,6 @@ class SettingsController extends Controller
         # Generating link with set cookies
         $cookieLink = LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('loadSettings', $cookies));
 
-        # Checking if dark mode active
-        $darkmode = 0;
-        foreach($cookies as $key => $value){
-            if($key === 'dark_mode'){
-                if($value === 1)
-                    $darkmode = 1;
-                elseif($value === 2){
-                    $darkmode = 2;
-                }
-            }
-        }
-
         return view('settings.index')
             ->with('title', trans('titles.settings', ['fokus' => $fokusName]))
             ->with('fokus', $request->input('fokus', ''))
@@ -95,7 +83,7 @@ class SettingsController extends Controller
             ->with('url', $url)
             ->with('blacklist', $blacklist)
             ->with('cookieLink', $cookieLink)
-            ->with('darkmode', $darkmode);
+            ->with('darkmode', Cookie::get('dark_mode'));
     }
 
     private function getSumas($fokus)
@@ -414,7 +402,7 @@ class SettingsController extends Controller
         $url = $request->input('url', '');
 
         $path = \Request::path();
-        $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
+        $cookiePath = "/";
 
         $cookies = Cookie::get();
         $setCookie = true;
diff --git a/app/Http/Controllers/StartpageController.php b/app/Http/Controllers/StartpageController.php
index d270ccab6..1251e1ff4 100644
--- a/app/Http/Controllers/StartpageController.php
+++ b/app/Http/Controllers/StartpageController.php
@@ -43,15 +43,6 @@ class StartpageController extends Controller
             $lang = 'all';
         }
 
-        $darkmode = 0;
-
-        $cookies = Cookie::get();
-        foreach($cookies as $key => $value){
-            if($key === 'dark_mode' && $value === 1){
-                $darkmode = 1;
-            }
-        }
-
         return view('index')
             ->with('title', trans('titles.index'))
             ->with('homeIcon')
@@ -63,7 +54,7 @@ class StartpageController extends Controller
             ->with('option_values', $option_values)
             ->with('autocomplete', $autocomplete)
             ->with('pluginmodal', $request->input('plugin-modal', 'off'))
-            ->with('darkmode', $darkmode);
+            ->with('darkmode', Cookie::get('dark_mode'));
     }
 
     public function loadPage($subpage)
diff --git a/resources/views/layouts/resultPage.blade.php b/resources/views/layouts/resultPage.blade.php
index 6144841f2..0fe54cf0e 100644
--- a/resources/views/layouts/resultPage.blade.php
+++ b/resources/views/layouts/resultPage.blade.php
@@ -22,8 +22,13 @@
 		<link href="/fonts/liberationsans/stylesheet.css" rel="stylesheet">
 		<link type="text/css" rel="stylesheet" href="{{ mix('css/fontawesome.css') }}" />
 		<link type="text/css" rel="stylesheet" href="{{ mix('css/fontawesome-solid.css') }}" />
-		<link type="text/css" rel="stylesheet alternate" href="{{ mix('css/themes/metager-dark.css') }}" title="MetaGer Dark"/>
+
+		@if($darkmode === "2")
+		<link type="text/css" rel="stylesheet" href="{{ mix('css/themes/metager-dark.css') }}" title="MetaGer Dark"/>
+		@else
 		<link type="text/css" rel="stylesheet" href="{{ mix('css/themes/metager.css') }}" title="MetaGer"/>
+		@endif
+
 @endif
 		<title>{{ $eingabe }} - MetaGer</title>
 		<meta content="width=device-width, initial-scale=1.0, user-scalable=no" name="viewport" />
diff --git a/resources/views/layouts/staticPages.blade.php b/resources/views/layouts/staticPages.blade.php
index 16e63d595..9e157c25a 100644
--- a/resources/views/layouts/staticPages.blade.php
+++ b/resources/views/layouts/staticPages.blade.php
@@ -26,8 +26,13 @@
 		@else
 		<link rel="search" type="application/opensearchdescription+xml" title="{{ trans('staticPages.opensearch') }}" href="{{  LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), action('StartpageController@loadPlugin', ['key' => Cookie::get('key')])) }}">
 		@endif
-		<link type="text/css" rel="stylesheet alternate" href="{{ mix('css/themes/metager-dark.css') }}" title="MetaGer Dark"/>
-		<link type="text/css" rel="stylesheet" href="{{ mix('css/themes/metager.css') }}" title="MetaGer"/>
+
+		@if($darkmode === "2")
+			<link type="text/css" rel="stylesheet" href="{{ mix('css/themes/metager-dark.css') }}" title="MetaGer Dark"/>
+		@else
+			<link type="text/css" rel="stylesheet" href="{{ mix('css/themes/metager.css') }}" title="MetaGer"/>
+		@endif
+
 		<link type="text/css" rel="stylesheet" href="{{ mix('css/utility.css') }}" />
 		<link href="/fonts/liberationsans/stylesheet.css" rel="stylesheet">
 		<link type="text/css" rel="stylesheet" href="{{ mix('css/fontawesome.css') }}" />
-- 
GitLab