92 lines
2.0 KiB
PHP
92 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
|
|
|
class Settings extends Model
|
|
{
|
|
protected $table = 'settings';
|
|
public $timestamps = false;
|
|
protected $fillable = ['key', 'value', 'language'];
|
|
|
|
/**
|
|
* Prepares common query for selecting settings
|
|
*
|
|
* @return mixed
|
|
*/
|
|
protected function getSettingsQuery()
|
|
{
|
|
return Settings::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;
|
|
}
|
|
}
|