2015-02-06 13:43:19 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
|
|
|
use Auth;
|
2015-06-03 15:11:50 -05:00
|
|
|
use Cache;
|
2015-02-11 00:35:10 -06:00
|
|
|
use FireflyIII\Models\Preference;
|
2015-06-03 14:25:11 -05:00
|
|
|
|
2015-02-06 13:43:19 -06:00
|
|
|
/**
|
|
|
|
* Class Preferences
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Support
|
|
|
|
*/
|
|
|
|
class Preferences
|
|
|
|
{
|
2015-06-02 10:44:50 -05:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function lastActivity()
|
|
|
|
{
|
|
|
|
$preference = $this->get('lastActivity', microtime())->data;
|
|
|
|
|
|
|
|
return md5($preference);
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:43:19 -06:00
|
|
|
/**
|
2015-06-05 03:40:26 -05:00
|
|
|
* @param string $name
|
2015-06-06 16:09:12 -05:00
|
|
|
* @param string $default
|
2015-02-06 13:43:19 -06:00
|
|
|
*
|
2015-05-05 03:23:01 -05:00
|
|
|
* @return null|\FireflyIII\Models\Preference
|
2015-02-06 13:43:19 -06:00
|
|
|
*/
|
|
|
|
public function get($name, $default = null)
|
|
|
|
{
|
2015-06-03 15:11:50 -05:00
|
|
|
$fullName = 'preference' . Auth::user()->id . $name;
|
|
|
|
if (Cache::has($fullName)) {
|
|
|
|
return Cache::get($fullName);
|
|
|
|
}
|
|
|
|
|
|
|
|
$preference = Preference::where('user_id', Auth::user()->id)->where('name', $name)->first(['id', 'name', 'data_encrypted']);
|
2015-05-25 15:56:00 -05:00
|
|
|
|
2015-06-03 14:58:06 -05:00
|
|
|
if ($preference) {
|
2015-06-03 15:11:50 -05:00
|
|
|
Cache::forever($fullName, $preference);
|
|
|
|
|
2015-06-03 14:58:06 -05:00
|
|
|
return $preference;
|
2015-05-25 15:56:00 -05:00
|
|
|
}
|
|
|
|
// no preference found and default is null:
|
|
|
|
if (is_null($default)) {
|
2015-02-06 13:43:19 -06:00
|
|
|
// return NULL
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->set($name, $default);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-06-06 16:09:12 -05:00
|
|
|
* @param $name
|
2015-06-05 03:40:26 -05:00
|
|
|
* @param string $value
|
2015-02-06 13:43:19 -06:00
|
|
|
*
|
2015-03-29 11:28:49 -05:00
|
|
|
* @return Preference
|
2015-02-06 13:43:19 -06:00
|
|
|
*/
|
|
|
|
public function set($name, $value)
|
|
|
|
{
|
2015-06-03 15:11:50 -05:00
|
|
|
$fullName = 'preference' . Auth::user()->id . $name;
|
|
|
|
Cache::forget($fullName);
|
|
|
|
$pref = Preference::where('user_id', Auth::user()->id)->where('name', $name)->first(['id', 'name', 'data_encrypted']);
|
2015-06-03 14:58:06 -05:00
|
|
|
if ($pref) {
|
|
|
|
$pref->data = $value;
|
|
|
|
} else {
|
|
|
|
$pref = new Preference;
|
|
|
|
$pref->name = $name;
|
|
|
|
$pref->data = $value;
|
2015-03-13 02:44:07 -05:00
|
|
|
$pref->user()->associate(Auth::user());
|
2015-06-03 14:58:06 -05:00
|
|
|
|
2015-03-13 02:44:07 -05:00
|
|
|
}
|
2015-06-03 14:58:06 -05:00
|
|
|
$pref->save();
|
2015-02-06 13:43:19 -06:00
|
|
|
|
2015-06-03 15:11:50 -05:00
|
|
|
Cache::forever($fullName, $pref);
|
|
|
|
|
2015-02-06 13:43:19 -06:00
|
|
|
return $pref;
|
|
|
|
|
|
|
|
}
|
2015-06-03 14:25:11 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function mark()
|
|
|
|
{
|
|
|
|
$this->set('lastActivity', microtime());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|