Commit 92d71712 authored by Dominik Hebeler's avatar Dominik Hebeler
Browse files

Merge branch '872-integrate-some-search-settings' into 'development'

Resolve "Integrate some search settings"

Closes #872

See merge request !1417
parents b14c55a3 14325023
<?php
namespace App\Http\Controllers;
use Cookie;
use LaravelLocalization;
use \App\MetaGer;
use \Illuminate\Http\Request;
class SettingsController extends Controller
{
public function index(Request $request)
{
$fokus = $request->input('fokus', '');
$fokusName = "";
if (empty($fokus)) {
return redirect('/');
} else {
$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' => $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)) {
$path = \Request::path();
$cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
Cookie::queue($fokus . "_engine_" . $suma, "off", 525600, $cookiePath, 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) {
$path = \Request::path();
$cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
Cookie::queue($fokus . "_engine_" . $suma, "", 0, $cookiePath, 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 === "") {
$path = \Request::path();
$cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
Cookie::queue($fokus . "_setting_" . $key, "", 0, $cookiePath, 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)) {
$path = \Request::path();
$cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
Cookie::queue($fokus . "_setting_" . $key, $value, 525600, $cookiePath, 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_"])) {
$path = \Request::path();
$cookiePath = "/" . substr($path, 0, strpos($path, "meta/") + 5);
Cookie::queue($key, "", 0, $cookiePath, null, false, false);
}
}
return redirect(LaravelLocalization::getLocalizedURL(LaravelLocalization::getCurrentLocale(), route('settings', ["fokus" => $fokus, "url" => $url])));
}
}
......@@ -26,7 +26,6 @@ class Kernel extends HttpKernel
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
......@@ -36,7 +35,6 @@ class Kernel extends HttpKernel
],
'enableCookies' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
],
......
......@@ -481,10 +481,10 @@ class MetaGer
# Check if this engine is disabled and can't be used
$disabled = empty($this->sumaFile->sumas->{$suma}->disabled) ? false : $this->sumaFile->sumas->{$suma}->disabled;
$autoDisabled = empty($this->sumaFile->sumas->{$suma}->{"auto-disabled"}) ? false : $this->sumaFile->sumas->{$suma}->{"auto-disabled"};
if ($disabled || $autoDisabled) {
continue;
if ($disabled || $autoDisabled
|| \Cookie::get($this->getFokus() . "_engine_" . $suma) === "off") { # Check if the user has disabled this engine
continue;
}
# Check if this engine can use eventually defined query-filter
$valid = true;
foreach ($this->queryFilter as $queryFilter => $filter) {
......@@ -497,8 +497,7 @@ class MetaGer
if ($valid) {
foreach ($this->parameterFilter as $filterName => $filter) {
# We need to check if the searchengine supports the parameter value, too
$value = $request->input($filter->{"get-parameter"}, "");
if (empty($filter->sumas->$suma) || empty($filter->sumas->{$suma}->values->{$value})) {
if (empty($filter->sumas->$suma) || empty($filter->sumas->{$suma}->values->{$filter->value})) {
$valid = false;
break;
}
......@@ -525,12 +524,17 @@ class MetaGer
}
}
# Implements Yahoo Ads if Yahoo is not enabled as a searchengine
if (!$this->apiAuthorized && empty($this->enabledSearchengines["yahoo"]) && $this->fokus != "bilder" && !empty($this->sumaFile->sumas->{"yahoo-ads"})) {
$this->enabledSearchengines["yahoo-ads"] = $this->sumaFile->sumas->{"yahoo-ads"};
}
# Special case if search engines are disabled
# Since bing is normally only active if a filter is set but it should be active, too if yahoo is disabled
if ($this->getFokus() === "web" && empty($this->enabledSearchengines["yahoo"]) && \Cookie::get("web_engine_bing") !== "off") {
$this->enabledSearchengines["bing"] = $this->sumaFile->sumas->{"bing"};
}
if (sizeof($this->enabledSearchengines) === 0) {
$filter = "";
foreach ($this->queryFilter as $queryFilter => $filterPhrase) {
......@@ -629,27 +633,55 @@ class MetaGer
$availableFilter = [];
foreach ($parameterFilter as $filterName => $filter) {
$values = $filter->values;
# Check if any of the enabled search engines provide this filter
foreach ($this->enabledSearchengines as $engineName => $engine) {
if (!empty($filter->sumas->$engineName)) {
$availableFilter[$filterName] = $filter;
if (empty($availableFilter[$filterName])) {
$availableFilter[$filterName] = $filter;
unset($availableFilter[$filterName]->values);
}
if (empty($availableFilter[$filterName]->values)) {
$availableFilter[$filterName]->values = (object) ["" => $values->{""}];
}
foreach ($filter->sumas->{$engineName}->values as $key => $value) {
$availableFilter[$filterName]->values->$key = $values->$key;
}
}
}
# We will also add the filter from the opt-in search engines (the searchengines that are only used when a filter of it is too)
foreach ($this->sumaFile->foki->{$this->fokus}->sumas as $suma) {
if ($this->sumaFile->sumas->{$suma}->{"filter-opt-in"}) {
if ($this->sumaFile->sumas->{$suma}->{"filter-opt-in"} && \Cookie::get($this->getFokus() . "_engine_" . $suma) !== "off") {
if (!empty($filter->sumas->{$suma})) {
# If the searchengine is disabled this filter shouldn't be available
if ((!empty($this->sumaFile->sumas->{$suma}->disabled) && $this->sumaFile->sumas->{$suma}->disabled === true)
|| (!empty($this->sumaFile->sumas->{$suma}->{"auto-disabled"}) && $this->sumaFile->sumas->{$suma}->{"auto-disabled"} === true)) {
continue;
}
$availableFilter[$filterName] = $filter;
if (empty($availableFilter[$filterName])) {
$availableFilter[$filterName] = $filter;
unset($availableFilter[$filterName]->values);
}
if (empty($availableFilter[$filterName]->values)) {
$availableFilter[$filterName]->values = (object) ["" => $values->{""}];
}
foreach ($filter->sumas->{$suma}->values as $key => $value) {
$availableFilter[$filterName]->values->$key = $values->$key;
}
}
}
}
}
# Set the current values for the filters
foreach ($availableFilter as $filterName => $filter) {
if (\Request::filled($filter->{"get-parameter"})) {
$filter->value = \Request::input($filter->{"get-parameter"});
} else if (\Cookie::get($this->getFokus() . "_setting_" . $filter->{"get-parameter"}) !== null) {
$filter->value = \Cookie::get($this->getFokus() . "_setting_" . $filter->{"get-parameter"});
}
}
return $availableFilter;
}
......@@ -1018,10 +1050,17 @@ class MetaGer
} else {
$usedParameters[$filter->{"get-parameter"}] = true;
}
if ($request->filled($filter->{"get-parameter"})) {
$this->parameterFilter[$filterName] = $filter;
if (($request->filled($filter->{"get-parameter"}) && $request->input($filter->{"get-parameter"}) !== "off") ||
\Cookie::get($this->getFokus() . "_setting_" . $filter->{"get-parameter"}) !== null) { # If the filter is set via Cookie
$this->parameterFilter[$filterName] = $filter;
$this->parameterFilter[$filterName]->value = $request->input($filter->{"get-parameter"}, '');
if (empty($this->parameterFilter[$filterName]->value)) {
$this->parameterFilter[$filterName]->value = \Cookie::get($this->getFokus() . "_setting_" . $filter->{"get-parameter"});
}
}
}
$this->searchCheckHostBlacklist($request);
$this->searchCheckDomainBlacklist($request);
$this->searchCheckUrlBlacklist();
......@@ -1418,6 +1457,31 @@ class MetaGer
return $this->searchUid;
}
public function getManualParameterFilterSet()
{
$filters = $this->sumaFile->filter->{"parameter-filter"};
foreach ($filters as $filterName => $filter) {
if (\Request::filled($filter->{"get-parameter"})
&& \Cookie::get($this->getFokus() . "_setting_" . $filter->{"get-parameter"}) !== \Request::input($filter->{"get-parameter"})) {
return true;
}
}
return false;
}
public function getSavedSettingCount()
{
$cookies = \Cookie::get();
$count = 0;
foreach ($cookies as $key => $value) {
if (starts_with($key, [$this->getFokus() . "_setting_", $this->getFokus() . "_engine_"])) {
$count++;
}
}
return $count;
}
# Einfache Getter
public function getVerificationId()
......@@ -1490,6 +1554,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;
......
......@@ -7,7 +7,6 @@ use App\MetaGer;
use Cache;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Redis;
use Request;
abstract class Searchengine
{
......@@ -76,7 +75,8 @@ abstract class Searchengine
# Parse enabled Parameter-Filter
foreach ($metager->getParameterFilter() as $filterName => $filter) {
$inputParameter = Request::input($filter->{"get-parameter"}, "");
$inputParameter = $filter->value;
if (empty($inputParameter) || empty($filter->sumas->{$name}->values->{$inputParameter})) {
continue;
}
......
$(function() {
setLabelText();
setKeyListeners();
setDropdownListeners();
});
/**
* Sets all action listeners for this page
*/
function setKeyListeners() {
$(document).keydown(function(event) {
if ($("input#show-create-focus").is(":checked")) {
if (event.keyCode == 27) {
$("input#show-create-focus").prop("checked", false);
} else if (event.keyCode == 13) {
$("#customSearchForm").submit();
}
}
});
}
function setDropdownListeners() {
// Listener for 'Open/Collapse all' label
$("input:checkbox#toggle-dropdowns").change(function() {
if ($(this).is(":checked")) {
// Open all dropdowns
$(".focus-dropdown-toggle").prop("checked", true);
} else {
// Close all dropdowns
$(".focus-dropdown-toggle").prop("checked", false);
}
setLabelText();
});
// Change 'Open/Collapse' all when single dropdown is changed
$(".focus-dropdown-toggle").change(function() {
var expanded = false;
$(".focus-dropdown-toggle").each(function() {
if ($(this).is(":checked")) {
expanded = true;
}
});
if (expanded === true) {
$("input:checkbox#toggle-dropdowns").prop("checked", true);
} else {
$("input:checkbox#toggle-dropdowns").prop("checked", false);
}
setLabelText();
});
}
//
// Adjusts the 'Open/Colapse all' label
function setLabelText() {
if ($("input:checkbox#toggle-dropdowns").is(":checked")) {
$("#toggle-dropdowns-label").html(
t("close-dropdowns") +
' <i class="fa fa-minus-square" aria-hidden="true"></i>'
);
} else {
$("#toggle-dropdowns-label").html(
t("open-dropdowns") +
' <i class="fa fa-plus-square" aria-hidden="true"></i>'
);
}
}
$(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");
});
\ No newline at end of file
$(function() {
loadLocalStorage();
setActionListenersSearchbar();
updateLangLabelCode();
});
function setActionListenersSearchbar() {
$("#input-key").change(saveKey);
$("#input-lang").change(saveLang);
}
function saveKey() {
var key = $("#input-key").val();
localStorage.setItem("key", key);
}
function loadKey() {
var key = localStorage.getItem("key");
if (key != null) {
$("#input-key").val(key);
}
}
function saveLang() {
var lang = $("#input-lang").val();
if (lang != "all") localStorage.setItem("lang", lang);
else localStorage.removeItem("lang");
updateLangLabelCode(lang);
}
function loadLang() {
var lang = localStorage.getItem("lang");
if (lang != null) {
$("#input-lang").val(lang);
}
}
/**
* Loads the user theme and stored settings from local storage
*/
function loadLocalStorage() {
if (localStorage) {
setSettings();
loadKey();
loadLang();
}
}
function setSettings() {
var acceptedParams = ["autocomplete", "key", "lang", "newtab", "sprueche"];
for (var key in localStorage) {
var value = localStorage.getItem(key);
var accepted = false;
for (var i in acceptedParams) {
if (key === "param_" + acceptedParams[i]) {
accepted = true;
}
}
if (accepted) {
key = key.substring(6);
// Check for existing hidden fields for this key
var existing = $('.search-hidden input[name="' + key + '"]');
if (existing.length === 0) {
// if none exist, create a new one
$(".search-hidden").append(
'<input type="hidden" name="' + key + '" value="' + value + '">'
);
}
}
}
// Change the request method to the given parameter
var requestMethod = localStorage.getItem("request");
if (
requestMethod !== null &&
(requestMethod === "GET" || requestMethod === "POST")
) {
$("#searchForm").attr("method", requestMethod);
}
}
function updateLangLabelCode(langcode = null) {
if (!langcode) {
var langcode = localStorage.getItem("lang");
}
if (langcode == "all") langcode = "";
$("#lang-label-code").html(langcode);
}
$(document).ready(function () {
// Wenn LocalStorage verfügbar ist, geben wir die Möglichkeit die Einstellungen dort zu speichern
tickOptions();
if (localStorage) {
$('#save').removeClass('hidden');
$('#save').click(function () {
localStorage.setItem('pers', true);
$('input[type=checkbox]:checked, input[type=hidden]').each(function () {
localStorage.setItem($(this).attr('name'), $(this).val());
});
$('select').each(function () {
localStorage.setItem($(this).attr('name'), $(this).val());
});
$('input[type=text]').each(function () {
localStorage.setItem($(this).attr('name'),