FEATURE: move bussiness logic out of models
This commit is contained in:
+2
-78
@@ -7,85 +7,9 @@ use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
||||
|
||||
class Settings extends Model
|
||||
{
|
||||
use ModelTraits\Settings;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user