Commit 51d93b09 authored by Dominik Hebeler's avatar Dominik Hebeler

Finished settings page

parent 1bf12c76
......@@ -2,6 +2,9 @@
namespace App\Http\Controllers;
use Cookie;
use LaravelLocalization;
use \App\MetaGer;
use \Illuminate\Http\Request;
class SettingsController extends Controller
......@@ -9,14 +12,189 @@ class SettingsController extends Controller
public function index(Request $request)
{
$fokus = $request->input('fokus', '');
$fokusName = "";
if (empty($fokus)) {
return redirect('/');
} else {
$fokus = trans('index.foki.' . $fokus);
$fokusName = trans('index.foki.' . $fokus);
}
$langFile = MetaGer::getLanguageFile();
$langFile = json_decode(file_get_contents($langFile));
$sumas = $this->getSumas($fokus);
# Parse the Parameter Filter
$filters = [];
$filteredSumas = false;
foreach ($langFile->filter->{"parameter-filter"} as $name => $filter) {
$values = $filter->values;
$cookie = Cookie::get($fokus . "_setting_" . $filter->{"get-parameter"});
foreach ($sumas as $suma => $sumaInfo) {
if (!$filteredSumas && $sumaInfo["filtered"]) {
$filteredSumas = true;
}
if (!$sumaInfo["filtered"] && $sumaInfo["enabled"] && !empty($filter->sumas->{$suma})) {
if (empty($filters[$name])) {
$filters[$name] = $filter;
unset($filters[$name]->values);
}
if (empty($filters[$name]->values)) {
$filters[$name]->values = (object) [];
}
foreach ($filter->sumas->{$suma}->values as $key => $value) {
$filters[$name]->values->$key = $values->$key;
}
}
}
}
$url = $request->input('url', '');
# Check if any setting is active
$cookies = Cookie::get();
$settingActive = false;
foreach ($cookies as $key => $value) {
if (\starts_with($key, [$fokus . "_engine_", $fokus . "_setting_"])) {
$settingActive = true;
}
}
return view('settings.index')
->with('title', trans('titles.settings', ['fokus' => $fokus]))
->with('fokus', $fokus);
->with('title', trans('titles.settings', ['fokus' => $fokusName]))
->with('fokus', $request->input('fokus', ''))
->with('fokusName', $fokusName)
->with('filteredSumas', $filteredSumas)
->with('sumas', $sumas)
->with('filter', $filters)
->with('settingActive', $settingActive)
->with('url', $url);
}
private function getSumas($fokus)
{
$langFile = MetaGer::getLanguageFile();
$langFile = json_decode(file_get_contents($langFile));
$sumasFoki = $langFile->foki->{$fokus}->sumas;
$sumas = [];
foreach ($sumasFoki as $suma) {
$sumas[$suma]["display-name"] = $langFile->sumas->{$suma}->{"display-name"};
$sumas[$suma]["filtered"] = false;
if (Cookie::get($fokus . "_engine_" . $suma) === "off") {
$sumas[$suma]["enabled"] = false;
} else {
$sumas[$suma]["enabled"] = true;
}
}
foreach ($langFile->filter->{"parameter-filter"} as $name => $filter) {
$values = $filter->values;
$cookie = Cookie::get($fokus . "_setting_" . $filter->{"get-parameter"});
foreach ($sumas as $suma => $sumaInfo) {
if ($cookie !== null && (empty($filter->sumas->{$suma}) || (!empty($filter->sumas->{$suma}) && empty($filter->sumas->{$suma}->values->$cookie)))) {
$sumas[$suma]["filtered"] = true;
}
}
}
return $sumas;
}
public function disableSearchEngine(Request $request)
{
$suma = $request->input('suma', '');
$fokus = $request->input('fokus', '');
$url = $request->input('url', '');
if (empty($suma) || empty($fokus)) {
abort(404);
}
# Only disable this engine if it's not the last
$sumas = $this->getSumas($fokus);
$sumaCount = 0;
foreach ($sumas as $name => $sumainfo) {
if (!$sumainfo["filtered"] && $sumainfo["enabled"]) {
$sumaCount++;
}
}
$langFile = MetaGer::getLanguageFile();
$langFile = json_decode(file_get_contents($langFile));
if ($sumaCount > 1 && in_array($suma, $langFile->foki->{$fokus}->sumas)) {
Cookie::queue($fokus . "_engine_" . $suma, "off", 525600, '/meta/', null, false, false);
}
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));
}
public function enableSearchEngine(Request $request)
{
$suma = $request->input('suma', '');
$fokus = $request->input('fokus', '');
$url = $request->input('url', '');
if (empty($suma) || empty($fokus)) {
abort(404);
}
if (Cookie::get($fokus . "_engine_" . $suma) !== null) {
Cookie::queue($fokus . "_engine_" . $suma, "", 0, '/meta/', null, false, false);
}
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));
}
public function enableFilter(Request $request)
{
$fokus = $request->input('fokus', '');
$url = $request->input('url', '');
if (empty($fokus)) {
abort(404);
}
$newFilters = $request->except(["fokus", "url"]);
$langFile = MetaGer::getLanguageFile();
$langFile = json_decode(file_get_contents($langFile));
foreach ($newFilters as $key => $value) {
if ($value === "") {
Cookie::queue($fokus . "_setting_" . $key, "", 0, '/meta/', null, false, false);
} else {
# Check if this filter and its value exists:
foreach ($langFile->filter->{"parameter-filter"} as $name => $filter) {
if ($key === $filter->{"get-parameter"} && !empty($filter->values->$value)) {
Cookie::queue($fokus . "_setting_" . $key, $value, 525600, '/meta/', null, false, false);
break;
}
}
}
}
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));
}
public function deleteSettings(Request $request)
{
$fokus = $request->input('fokus', '');
$url = $request->input('url', '');
if (empty($fokus)) {
abort(404);
}
$cookies = Cookie::get();
foreach ($cookies as $key => $value) {
if (\starts_with($key, [$fokus . "_engine_", $fokus . "_setting_"])) {
Cookie::queue($key, "", 0, '/meta/', null, false, false);
}
}
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));
}
}
......@@ -36,7 +36,6 @@ class Kernel extends HttpKernel
],
'enableCookies' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
],
......
......@@ -1490,6 +1490,16 @@ class MetaGer
return $this->language;
}
public static function getLanguageFile()
{
$locale = LaravelLocalization::getCurrentLocale();
if ($locale === "en") {
return config_path('sumasEn.json');
} else {
return config_path('sumas.json');
}
}
public function getLang()
{
return $this->lang;
......
......@@ -4,14 +4,17 @@
background-color: white;
box-shadow: 0px 1px 1.5px 0px rgba(0, 0, 0, 0.12), 1px 0px 1px 0px rgba(0, 0, 0, 0.24);
padding: 8px;
margin: 4px;
}
.card-heavy {
.card;
box-shadow: 0px 4px 5px 0px rgba(0, 0, 0, 0.12), 4px 0px 5px 0px rgba(0, 0, 0, 0.24);
margin: 8px;
}
.card-light {
.card;
box-shadow: 0px 0px 1px 0px rgba(0, 0, 0, 0.12), 0px 0px 1px 0px rgba(0, 0, 0, 0.24);
margin: 4px;
}
\ No newline at end of file
......@@ -39,7 +39,7 @@
<div class="scrollfade-right"></div>
</div>
<div id="settings">
<form action="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings')) }}" method="post">
<form action="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings')) }}" method="get">
<input type="hidden" name="fokus" value="{{ $metager->getFokus() }}">
<input type="hidden" name="url" value="{{ url()->full() }}">
<button type="submit">Sucheinstellungen</button>
......
......@@ -3,9 +3,141 @@
@section('title', $title )
@section('content')
<div class="card-heavy">
<h2>Sucheinstellungen ({{ $fokus }})</h2>
<p>Hier können Sie Sucheinstellungen für Ihre MetaGer Suche im Fokus {{ $fokus }} vornehmen. Diese bleiben solange gespeichert, wie Sie Cookies im Browser speichern.</p>
<div class="card-light">
<h2>Sucheinstellungen ({{ $fokusName }})</h2>
<p>Hier können Sie Sucheinstellungen für Ihre MetaGer Suche im Fokus {{ $fokusName }} vornehmen. Diese bleiben solange gespeichert, wie Sie Cookies im Browser speichern.</p>
</div>
<div class="card-light">
<h2>Verwendete Suchmaschinen</h2>
<p>Nachfolgend sehen Sie alle für diesen Fokus verfügbaren Suchmaschinen. Mit einem Klick auf den Namen können Sie Ein-/Ausgeschaltet werden. Eingeschaltete Suchmaschinen sind in grün dargestellt. Ausgeschaltete in rot oder grau.</p>
<p></p>
<div class="sumas enabled-engines">
@foreach($sumas as $suma => $sumaInfo)
@if(! $sumaInfo["filtered"] && $sumaInfo["enabled"])
<div class="suma">
<form action="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('disableEngine')) }}" method="post">
<input type="hidden" name="suma" value="{{ $suma }}">
<input type="hidden" name="fokus" value="{{ $fokus }}">
<input type="hidden" name="url" value="{{ $url }}">
<button type="submit">{{ $sumaInfo["display-name"] }}</button>
</form>
</div>
@endif
@endforeach
</div>
<div class="sumas disabled-engines">
@foreach($sumas as $suma => $sumaInfo)
@if( !$sumaInfo["filtered"] && !$sumaInfo["enabled"])
<div class="suma">
<form action="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('enableEngine')) }}" method="post">
<input type="hidden" name="suma" value="{{ $suma }}">
<input type="hidden" name="fokus" value="{{ $fokus }}">
<input type="hidden" name="url" value="{{ $url }}">
<button type="submit">{{ $sumaInfo["display-name"] }}</button>
</form>
</div>
@endif
@endforeach
</div>
@if($filteredSumas)
<h4>Durch Suchfilter deaktiviert:</h4>
<div class="sumas filtered-engines">
@foreach($sumas as $suma => $sumaInfo)
@if($sumaInfo["filtered"])
<div class="suma">
{{ $sumaInfo["display-name"] }}
</div>
@endif
@endforeach
</div>
@endif
</div>
<div class="card-light">
<h2>Suchfilter</h2>
<p>An dieser Stelle können Sie Suchfilter dauerhaft setzen. Mit der Auswahl eines Suchfilters stehen nur noch Suchmaschinen zur Verfügung, welche diesen Filter unterstützen. Umgekehrt werden auch nur Suchfilter angezeigt, welche von der aktuellen Suchmaschinenauswahl unterstützt werden.</p>
<form id="filter-form" action="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('enableFilter')) }}" method="post" class="form">
<input type="hidden" name="fokus" value="{{ $fokus }}">
<input type="hidden" name="url" value="{{ $url }}">
<div id="filter-options">
@foreach($filter as $name => $filterInfo)
<div class="form-group">
<label for="{{ $filterInfo->{"get-parameter"} }}">@lang($filterInfo->name)</label>
<select name="{{ $filterInfo->{"get-parameter"} }}" id="{{ $filterInfo->{"get-parameter"} }}" class="form-control">
<option value="" @if(Cookie::get($fokus . "_setting_" . $filterInfo->{"get-parameter"}) === null)disabled selected @endif>Beliebig</option>
@foreach($filterInfo->values as $key => $value)
@if(!empty($key))
<option value="{{ $key }}" {{ Cookie::get($fokus . "_setting_" . $filterInfo->{"get-parameter"}) === $key ? "disabled selected" : "" }}>@lang($value)</option>
@endif
@endforeach
</select>
</div>
@endforeach
</div>
<button type="submit" class="btn btn-default">Speichern</button>
</form>
</div>
<div class="card-light" id="actions">
@if($settingActive)
<div id="reset">
<form action="{{ LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('deleteSettings', ["fokus" => $fokus, "url" => $url])) }}" method="post">
<input type="hidden" name="url" value="{{ $url }}">
<input type="hidden" name="fokus" value="{{ $fokus }}">
<button type="submit" class="btn btn-sm btn-danger">Alle Einstellungen löschen</button>
</form>
</div>
@endif
<div id="back">
<a href="{{ $url }}" class="btn btn-sm btn-default">Zurück zur letzten Seite</a>
</div>
</div>
<style>
.sumas {
display: flex;
flex-wrap: wrap;
margin-left: -8px;
}
.sumas .suma button, .sumas.filtered-engines .suma {
padding: 8px;
margin-left: 8px;
margin-top: 8px;
border-radius: 4px;
}
.sumas.enabled-engines .suma button {
background-color: #9ad79a;
}
.sumas.disabled-engines .suma button {
background-color: #fea0a0;
}
.sumas.filtered-engines .suma{
background-color: #bababa;
}
#filter-options {
display: flex;
flex-wrap: wrap;
margin-left: -8px;
}
#filter-options > div {
margin-left: 8px;
}
#actions {
display: flex;
align-items: center;
justify-content: center;
}
#actions #back {
margin-left: 8px;
}
</style>
<script>
$(document).ready(function(){
$("#filter-form").find("button[type=submit]").css("display", "none");
$("#filter-form").find("select").on("change", function(){
$("#filter-form").submit();
});
console.log("Test");
});
</script>
@endsection
......@@ -10,6 +10,15 @@ Route::group(
Route::post('meta/key', 'KeyController@setKey');
Route::post('meta/key/remove', 'KeyController@removeKey');
Route::post('meta/settings', 'SettingsController@index')->name('settings');
Route::group([
'prefix' => 'meta/settings',
],
function () {
Route::get('/', 'SettingsController@index')->name('settings');
Route::post('de', 'SettingsController@disableSearchEngine')->name('disableEngine');
Route::post('ee', 'SettingsController@enableSearchEngine')->name('enableEngine');
Route::post('ef', 'SettingsController@enableFilter')->name('enableFilter');
Route::post('ds', 'SettingsController@deleteSettings')->name('deleteSettings');
});
}
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment