From 1f4f4169f09b8d189aa67430694b5787e5fc818a Mon Sep 17 00:00:00 2001
From: Davide Aprea <davide@suma-ev.de>
Date: Thu, 22 Oct 2020 15:17:40 +0200
Subject: [PATCH] merged darkmode with enable setting function

---
 app/Http/Controllers/SettingsController.php | 63 +++++++--------------
 resources/lang/de/settings.php              |  4 ++
 resources/lang/en/settings.php              |  4 ++
 resources/views/settings/index.blade.php    | 15 +++--
 routes/cookie.php                           |  1 -
 5 files changed, 39 insertions(+), 48 deletions(-)

diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php
index 45979e3f6..03b6629eb 100644
--- a/app/Http/Controllers/SettingsController.php
+++ b/app/Http/Controllers/SettingsController.php
@@ -209,23 +209,32 @@ class SettingsController extends Controller
         $fokus = $request->input('fokus', '');
         $url = $request->input('url', '');
         // Currently only the setting for quotes is supported
+
         $quotes = $request->input('zitate', '');
-        if (empty($fokus) || empty($quotes)) {
-            abort(404);
+        if(!empty($quotes)){
+            if($quotes === "off"){
+                $path = \Request::path();
+                $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
+                Cookie::queue($fokus . "_setting_zitate", "off", 0, $cookiePath, null, false, false);
+            }elseif($quotes === "on") {
+                $path = \Request::path();
+                $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
+                Cookie::queue($fokus . "_setting_zitate", "", 0, $cookiePath, null, false, false);
+            }
         }
 
-        if($quotes === "off"){
-            $path = \Request::path();
-            $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
-            Cookie::queue($fokus . "_setting_zitate", "off", 0, $cookiePath, null, false, false);
-        }elseif($quotes === "on") {
-            $path = \Request::path();
-            $cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
-            Cookie::queue($fokus . "_setting_zitate", "", 0, $cookiePath, null, false, false);
-        }else{
-            abort(404);
+        $darkmode = $request->input('dm');
+        if(!empty($darkmode)){
+            if($darkmode === "off"){
+                Cookie::queue('dark_mode', '1', 0, '/', null, false, false);
+            }elseif($darkmode === "on") {
+                Cookie::queue('dark_mode', '2', 0, '/', null, false, false);
+            }elseif($darkmode === "system"){
+                Cookie::queue('dark_mode', '', 0, '/', null, false, false);
+            }
         }
 
+
         return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));
     }
 
@@ -408,34 +417,4 @@ class SettingsController extends Controller
 
         return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), url('/')));
     }
-
-    public function darkmode(Request $request)
-    {
-        $fokus = $request->input('fokus', '');
-        $url = $request->input('url', '');
-
-        $path = \Request::path();
-        $cookiePath = "/";
-
-        $cookies = Cookie::get();
-        $setCookie = true;
-        
-        $darkmode = "0";
-
-        if(!empty($cookies)){
-            foreach($cookies as $key => $value){
-                if($key === 'dark_mode'){
-                    if($value === "0" || $value == "1"){
-                        $darkmode = "2";
-                    }elseif($value === "2"){
-                        $darkmode = "1";
-                    }
-                    Cookie::queue('dark_mode', $darkmode, 0, $cookiePath, null, false, false);
-                    $setCookie = false;
-                }
-            }
-        }else{
-            Cookie::queue('dark_mode', "2", 0, $cookiePath, null, false, false);
-        }
-        return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));    }
 }
diff --git a/resources/lang/de/settings.php b/resources/lang/de/settings.php
index ae113833a..fd04c613f 100644
--- a/resources/lang/de/settings.php
+++ b/resources/lang/de/settings.php
@@ -25,7 +25,11 @@ return [
     'add' => 'Hinzufügen',
     'clear' => 'Blacklist leeren',
     'copy' => 'Kopieren',
+
     'darkmode' => 'Dunklen Modus umschalten',
+    'system' => 'Systemstandard',
+    'dark' => 'Dunkel',
+    'light' => 'Hell',
 
     // Translations from the settings overview
     'noSettings' => 'Aktuell sind keine Einstellungen gesetzt!',
diff --git a/resources/lang/en/settings.php b/resources/lang/en/settings.php
index bbb9dd8fd..fe734139b 100644
--- a/resources/lang/en/settings.php
+++ b/resources/lang/en/settings.php
@@ -25,7 +25,11 @@ return [
     'add' => 'Add',
     'clear' => 'Clear black list',
     'copy' => 'Copy',
+
     'darkmode' => 'Toggle dark mode',
+    'system' => 'System Default',
+    'dark' => 'Dark',
+    'light' => 'Light',
 
     // Translations from the settings overview
     'noSettings' => "Currently no settings are set!",
diff --git a/resources/views/settings/index.blade.php b/resources/views/settings/index.blade.php
index 59ec7ad34..a33037dfe 100644
--- a/resources/views/settings/index.blade.php
+++ b/resources/views/settings/index.blade.php
@@ -125,10 +125,18 @@
     
         <div class="card-light">
             <h2>Weitere Einstellungen</h2>
-            @if(LaravelLocalization::getCurrentLocale() === "de")
             <form id="setting-form" action="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('enableSetting')) }}" method="post" class="form">
                 <input type="hidden" name="fokus" value="{{ $fokus }}">
                 <input type="hidden" name="url" value="{{ $url }}">
+                <div class="form-group">
+                    <label for="dm">@lang('settings.darkmode')</label>
+                    <select name="dm" id="dm" class="form-control">
+                        <option value="system" {{ !Cookie::has('dark_mode') ? "disabled selected" : "" }}>@lang('settings.system')</option>
+                        <option value="off" {{ Cookie::get('dark_mode') === "1" ? "disabled selected" : "" }}>@lang('settings.light')</option>
+                        <option value="on" {{ Cookie::get('dark_mode') === "2" ? "disabled selected" : "" }}>@lang('settings.dark')</option>
+                    </select>
+                </div>
+                @if(LaravelLocalization::getCurrentLocale() === "de")
                 <div class="form-group">
                     <label for="zitate">Zitate</label>
                     <select name="zitate" id="zitate" class="form-control">
@@ -136,12 +144,9 @@
                         <option value="off" {{ Cookie::get($fokus . "_setting_zitate") === "off" ? "disabled selected" : "" }}>Nicht Anzeigen</option>
                     </select>
                 </div>
+                @endif
                 <button type="submit" class="btn btn-default">@lang('settings.save')</button>
             </form>
-            @endif
-            <form id="darkmode" action="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('darkmode', ["fokus" => $fokus, "url" => $url])) }}" method="post" class="form">
-            <button type="submit" class="btn btn-default">@lang('settings.darkmode')</button>
-            </form>
         </div>
     <div class="card-light" id="actions">
         @if($settingActive)
diff --git a/routes/cookie.php b/routes/cookie.php
index babef196f..515dd0ca8 100644
--- a/routes/cookie.php
+++ b/routes/cookie.php
@@ -29,7 +29,6 @@ Route::group(
                 Route::post('all-settings/removeOne', 'SettingsController@removeOneSetting')->name('removeOneSetting');
                 Route::post('all-settings/removeAll', 'SettingsController@removeAllSettings')->name('removeAllSettings');
                 Route::get('load-settings', 'SettingsController@loadSettings')->name('loadSettings');
-                Route::post('darkmode', 'SettingsController@darkmode')->name('darkmode');
             });
     }
 );
-- 
GitLab