firefly-iii/app/Support/Preferences.php

59 lines
1.2 KiB
PHP
Raw Normal View History

2015-02-06 13:43:19 -06:00
<?php
namespace FireflyIII\Support;
use Auth;
2015-02-11 00:35:10 -06:00
use FireflyIII\Models\Preference;
2015-02-06 13:43:19 -06:00
/**
* Class Preferences
*
* @package FireflyIII\Support
*/
class Preferences
{
/**
* @param $name
* @param null $default
*
2015-03-29 11:28:49 -05:00
* @return null|Preference
2015-02-06 13:43:19 -06:00
*/
public function get($name, $default = null)
{
$pref = Preference::where('user_id', Auth::user()->id)->where('name', $name)->first();
if (is_null($pref) && is_null($default)) {
// return NULL
return null;
}
if (!is_null($pref)) {
return $pref;
}
return $this->set($name, $default);
}
/**
* @param $name
* @param $value
*
2015-03-29 11:28:49 -05:00
* @return Preference
2015-02-06 13:43:19 -06:00
*/
public function set($name, $value)
{
$pref = Preference::where('user_id', Auth::user()->id)->where('name', $name)->first();
if (is_null($pref)) {
$pref = new Preference;
$pref->name = $name;
}
$pref->data = $value;
2015-03-13 02:44:07 -05:00
if (!is_null(Auth::user()->id)) {
$pref->user()->associate(Auth::user());
$pref->save();
}
2015-02-06 13:43:19 -06:00
return $pref;
}
2015-03-29 01:14:32 -05:00
}