FEATURE: refactorize settings

This commit is contained in:
Michal Kváček
2017-08-02 10:27:58 +02:00
parent e5d9ce682d
commit e8065c99c7
4 changed files with 37 additions and 37 deletions
-86
View File
@@ -1,86 +0,0 @@
<?php
namespace App\Models\ModelTraits;
use App\Models\Settings as SettingsModel;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
trait Settings
{
/**
* Prepares common query for selecting settings
*
* @return mixed
*/
protected function getSettingsQuery()
{
return SettingsModel::select(['key', 'value', 'language'])
->where('language', LaravelLocalization::getCurrentLocale())
->orWhere('language', null)
->orderBy('language', 'ASC');
}
/**
* Load all settings
*
* @return \stdClass
*/
public function getAll()
{
$settings = $this->getSettingsQuery()
->get();
return $this->returnPairs($settings);
}
/**
* Get all settings with language info
*
* @return \stdClass
*/
public function getAllWithLanguage()
{
$settings = $this->getSettingsQuery()
->get();
$data = new \stdClass();
foreach ($settings as $conf) {
$data->{$conf->key} = [
'val' => $conf->value,
'lang' => $conf->language
];
}
return $data;
}
/**
* Load only config for views
*
* @return \stdClass
*/
public function getForView()
{
$settings = $this->getSettingsQuery()
->where('load_into_views', 1)
->get();
return $this->returnPairs($settings);
}
/**
* From pair <key, value> create object key => value
*
* @param $settings
*
* @return \stdClass
*/
private function returnPairs($settings)
{
$data = new \stdClass();
foreach ($settings as $conf) {
$data->{$conf->key} = $conf->value;
}
return $data;
}
}
-2
View File
@@ -7,8 +7,6 @@ use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
class Settings extends Model
{
use ModelTraits\Settings;
protected $table = 'settings';
public $timestamps = false;
protected $fillable = ['key', 'value', 'language'];