86 lines
1.9 KiB
PHP
86 lines
1.9 KiB
PHP
<?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;
|
|
}
|
|
} |