firefly-iii/app/Support/Preferences.php

89 lines
1.9 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
{
/**
* @return string
*/
public function lastActivity()
{
$preference = $this->get('lastActivity', microtime())->data;
return md5($preference);
}
/**
* @return bool
*/
public function mark() {
$this->set('lastActivity',microtime());
return true;
}
2015-02-06 13:43:19 -06:00
/**
* @param $name
* @param null $default
*
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-05-25 15:56:00 -05:00
$preferences = Preference::where('user_id', Auth::user()->id)->get();
/** @var Preference $preference */
foreach ($preferences as $preference) {
if ($preference->name == $name) {
return $preference;
}
}
// 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);
}
/**
* @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)
{
2015-05-25 15:56:00 -05:00
$preferences = Preference::where('user_id', Auth::user()->id)->get();
/** @var Preference $preference */
foreach ($preferences as $preference) {
if ($preference->name == $name) {
$preference->data = $value;
$preference->save();
return $preference;
}
2015-02-06 13:43:19 -06:00
}
2015-05-25 15:56:00 -05:00
$pref = new Preference;
$pref->name = $name;
2015-02-06 13:43:19 -06:00
$pref->data = $value;
2015-05-25 15:56:00 -05:00
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
}