FEATURE: refactorize settings
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
namespace App\CMS\Helpers;
|
namespace App\CMS\Helpers;
|
||||||
|
|
||||||
|
use App\CMS\Repositories\Settings\Settings as SettingsRepository;
|
||||||
use App\Models\Settings as SettingsModel;
|
use App\Models\Settings as SettingsModel;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
@@ -78,8 +79,7 @@ class Settings
|
|||||||
{
|
{
|
||||||
if (!self::$initialized) {
|
if (!self::$initialized) {
|
||||||
self::$settings = Cache::remember('settings', config('cache.settingCacheLifetime'), function () {
|
self::$settings = Cache::remember('settings', config('cache.settingCacheLifetime'), function () {
|
||||||
$model = new SettingsModel();
|
app(SettingsRepository::class)->getAll();
|
||||||
return $model->getAll();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace App\Models\ModelTraits;
|
|
||||||
|
namespace App\CMS\Repositories\Settings;
|
||||||
|
|
||||||
use App\Models\Settings as SettingsModel;
|
use App\Models\Settings as SettingsModel;
|
||||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||||
|
|
||||||
trait Settings
|
class Settings
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Prepares common query for selecting settings
|
* Prepares common query for selecting settings
|
||||||
*
|
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
protected function getSettingsQuery()
|
protected function getSettingsQuery()
|
||||||
@@ -21,7 +21,6 @@ trait Settings
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Load all settings
|
* Load all settings
|
||||||
*
|
|
||||||
* @return \stdClass
|
* @return \stdClass
|
||||||
*/
|
*/
|
||||||
public function getAll()
|
public function getAll()
|
||||||
@@ -34,7 +33,6 @@ trait Settings
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all settings with language info
|
* Get all settings with language info
|
||||||
*
|
|
||||||
* @return \stdClass
|
* @return \stdClass
|
||||||
*/
|
*/
|
||||||
public function getAllWithLanguage()
|
public function getAllWithLanguage()
|
||||||
@@ -55,7 +53,6 @@ trait Settings
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Load only config for views
|
* Load only config for views
|
||||||
*
|
|
||||||
* @return \stdClass
|
* @return \stdClass
|
||||||
*/
|
*/
|
||||||
public function getForView()
|
public function getForView()
|
||||||
@@ -83,4 +80,29 @@ trait Settings
|
|||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save settings
|
||||||
|
*
|
||||||
|
* @param array $data
|
||||||
|
*/
|
||||||
|
public function save(array $data)
|
||||||
|
{
|
||||||
|
$commonSettings = isset($data['commonSettings']) ? $data['commonSettings'] : [];
|
||||||
|
unset($data['commonSettings']);
|
||||||
|
|
||||||
|
foreach ($data as $key => $value) {
|
||||||
|
// select language for this setting key
|
||||||
|
$language = isset($commonSettings[$key]) ? null : LaravelLocalization::getCurrentLocale();
|
||||||
|
$setting = SettingsModel::firstOrNew(['language' => $language, 'key' => $key]);
|
||||||
|
|
||||||
|
if ($setting->value == $value) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set value and save or delete
|
||||||
|
$setting->value = $value;
|
||||||
|
empty($value) ? $setting->delete() : $setting->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2,15 +2,15 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Admin;
|
namespace App\Http\Controllers\Admin;
|
||||||
|
|
||||||
|
use App\CMS\Repositories\Settings\Settings as SettingsRepository;
|
||||||
use App\Http\Requests;
|
use App\Http\Requests;
|
||||||
use App\Models\Settings as SettingsModel;
|
use App\Models\Settings as SettingsModel;
|
||||||
use Illuminate\Support\Facades\App;
|
use Illuminate\Support\Facades\App;
|
||||||
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
|
||||||
|
|
||||||
class Settings extends Controller
|
class Settings extends Controller
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var SettingsModel
|
* @var SettingsRepository
|
||||||
*/
|
*/
|
||||||
protected $settings;
|
protected $settings;
|
||||||
|
|
||||||
@@ -19,14 +19,13 @@ class Settings extends Controller
|
|||||||
*
|
*
|
||||||
* @param SettingsModel $settings
|
* @param SettingsModel $settings
|
||||||
*/
|
*/
|
||||||
public function __construct(SettingsModel $settings)
|
public function __construct(SettingsRepository $settings)
|
||||||
{
|
{
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get overview (with form) for editing settings
|
* Get overview (with form) for editing settings
|
||||||
*
|
|
||||||
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
||||||
*/
|
*/
|
||||||
public function getOverview()
|
public function getOverview()
|
||||||
@@ -45,7 +44,6 @@ class Settings extends Controller
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Save all settings (ignore empty ones)
|
* Save all settings (ignore empty ones)
|
||||||
*
|
|
||||||
* When settings used to be non-empty string and user deletes it, key is also deleted from database
|
* When settings used to be non-empty string and user deletes it, key is also deleted from database
|
||||||
*
|
*
|
||||||
* @param Requests\SaveSettingsRequest $request
|
* @param Requests\SaveSettingsRequest $request
|
||||||
@@ -54,25 +52,7 @@ class Settings extends Controller
|
|||||||
*/
|
*/
|
||||||
public function saveSettings(Requests\SaveSettingsRequest $request)
|
public function saveSettings(Requests\SaveSettingsRequest $request)
|
||||||
{
|
{
|
||||||
$data = $request->all();
|
$this->settings->save($request->except(['_token']));
|
||||||
$commonSettings = isset($data['commonSettings']) ? $data['commonSettings'] : [];
|
|
||||||
|
|
||||||
// remove CSRF token field
|
|
||||||
unset($data['_token'], $data['commonSettings']);
|
|
||||||
|
|
||||||
foreach ($data as $key => $value) {
|
|
||||||
// select language for this setting key
|
|
||||||
$language = isset($commonSettings[$key]) ? null : LaravelLocalization::getCurrentLocale();
|
|
||||||
$setting = SettingsModel::firstOrNew(['language' => $language, 'key' => $key]);
|
|
||||||
|
|
||||||
if ($setting->value == $value) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// set value and save or delete
|
|
||||||
$setting->value = $value;
|
|
||||||
empty($value) ? $setting->delete() : $setting->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
return redirect()
|
return redirect()
|
||||||
->route('settings.overview')
|
->route('settings.overview')
|
||||||
|
|||||||
@@ -7,8 +7,6 @@ use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
|||||||
|
|
||||||
class Settings extends Model
|
class Settings extends Model
|
||||||
{
|
{
|
||||||
use ModelTraits\Settings;
|
|
||||||
|
|
||||||
protected $table = 'settings';
|
protected $table = 'settings';
|
||||||
public $timestamps = false;
|
public $timestamps = false;
|
||||||
protected $fillable = ['key', 'value', 'language'];
|
protected $fillable = ['key', 'value', 'language'];
|
||||||
|
|||||||
Reference in New Issue
Block a user