2015-02-06 13:43:19 -06:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* Preferences.php
|
2017-10-21 01:40:00 -05:00
|
|
|
* Copyright (c) 2017 thegrumpydictator@gmail.com
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* This file is part of Firefly III.
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2017-10-21 01:40:00 -05:00
|
|
|
* Firefly III is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Firefly III is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2017-12-17 07:44:05 -06:00
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2015-02-06 13:43:19 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
2015-06-03 15:11:50 -05:00
|
|
|
use Cache;
|
2018-03-08 22:45:22 -06:00
|
|
|
use Exception;
|
2015-02-11 00:35:10 -06:00
|
|
|
use FireflyIII\Models\Preference;
|
2016-03-29 04:55:02 -05:00
|
|
|
use FireflyIII\User;
|
2017-09-26 01:52:16 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2018-04-01 12:22:30 -05:00
|
|
|
use Log;
|
2016-11-25 09:55:04 -06:00
|
|
|
use Session;
|
2015-06-03 14:25:11 -05:00
|
|
|
|
2015-02-06 13:43:19 -06:00
|
|
|
/**
|
2017-11-15 05:25:49 -06:00
|
|
|
* Class Preferences.
|
2015-02-06 13:43:19 -06:00
|
|
|
*/
|
|
|
|
class Preferences
|
|
|
|
{
|
2017-09-26 01:52:16 -05:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param string $search
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function beginsWith(User $user, string $search): Collection
|
|
|
|
{
|
|
|
|
$set = Preference::where('user_id', $user->id)->where('name', 'LIKE', $search . '%')->get();
|
|
|
|
|
|
|
|
return $set;
|
|
|
|
}
|
|
|
|
|
2016-03-07 13:28:26 -06:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-09-26 01:52:16 -05:00
|
|
|
public function delete(string $name): bool
|
2016-03-07 13:28:26 -06:00
|
|
|
{
|
2016-11-03 15:54:07 -05:00
|
|
|
$fullName = sprintf('preference%s%s', auth()->user()->id, $name);
|
2016-03-07 13:28:26 -06:00
|
|
|
if (Cache::has($fullName)) {
|
|
|
|
Cache::forget($fullName);
|
|
|
|
}
|
2018-03-08 22:45:22 -06:00
|
|
|
try {
|
|
|
|
Preference::where('user_id', auth()->user()->id)->where('name', $name)->delete();
|
|
|
|
} catch (Exception $e) {
|
2018-07-26 22:03:37 -05:00
|
|
|
Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
|
2018-03-08 22:45:22 -06:00
|
|
|
// don't care.
|
|
|
|
}
|
2016-03-07 13:28:26 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:52:16 -05:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function findByName(string $name): Collection
|
|
|
|
{
|
2018-04-02 07:50:17 -05:00
|
|
|
return Preference::where('name', $name)->get();
|
2017-09-26 01:52:16 -05:00
|
|
|
}
|
|
|
|
|
2016-04-03 00:07:17 -05:00
|
|
|
/**
|
2018-07-14 16:04:05 -05:00
|
|
|
* @param string $name
|
2018-07-26 22:03:37 -05:00
|
|
|
* @param mixed $default
|
2016-04-03 00:07:17 -05:00
|
|
|
*
|
2016-04-05 15:00:03 -05:00
|
|
|
* @return \FireflyIII\Models\Preference|null
|
2016-04-03 00:07:17 -05:00
|
|
|
*/
|
2018-07-14 16:04:05 -05:00
|
|
|
public function get(string $name, $default = null): ?Preference
|
2016-04-03 00:07:17 -05:00
|
|
|
{
|
2017-11-19 02:20:29 -06:00
|
|
|
/** @var User $user */
|
2016-09-16 05:15:58 -05:00
|
|
|
$user = auth()->user();
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $user) {
|
2016-04-03 00:07:17 -05:00
|
|
|
return $default;
|
|
|
|
}
|
|
|
|
|
2017-11-19 02:20:29 -06:00
|
|
|
return $this->getForUser($user, $name, $default);
|
2016-04-03 00:07:17 -05:00
|
|
|
}
|
|
|
|
|
2016-11-20 08:30:16 -06:00
|
|
|
/**
|
2016-12-14 11:59:12 -06:00
|
|
|
* @param \FireflyIII\User $user
|
|
|
|
* @param array $list
|
2016-11-20 08:30:16 -06:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getArrayForUser(User $user, array $list): array
|
|
|
|
{
|
|
|
|
$result = [];
|
|
|
|
$preferences = Preference::where('user_id', $user->id)->whereIn('name', $list)->get(['id', 'name', 'data']);
|
|
|
|
/** @var Preference $preference */
|
|
|
|
foreach ($preferences as $preference) {
|
|
|
|
$result[$preference->name] = $preference->data;
|
|
|
|
}
|
|
|
|
foreach ($list as $name) {
|
|
|
|
if (!isset($result[$name])) {
|
|
|
|
$result[$name] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:43:19 -06:00
|
|
|
/**
|
2016-09-13 12:19:58 -05:00
|
|
|
* @param \FireflyIII\User $user
|
2017-11-15 05:25:49 -06:00
|
|
|
* @param string $name
|
2017-01-05 03:06:46 -06:00
|
|
|
* @param null|string $default
|
2015-02-06 13:43:19 -06:00
|
|
|
*
|
2016-09-13 12:19:58 -05:00
|
|
|
* @return \FireflyIII\Models\Preference|null
|
2015-02-06 13:43:19 -06:00
|
|
|
*/
|
2018-07-14 16:04:05 -05:00
|
|
|
public function getForUser(User $user, string $name, $default = null): ?Preference
|
2015-02-06 13:43:19 -06:00
|
|
|
{
|
2016-11-03 15:54:07 -05:00
|
|
|
$fullName = sprintf('preference%s%s', $user->id, $name);
|
2015-06-03 15:11:50 -05:00
|
|
|
if (Cache::has($fullName)) {
|
|
|
|
return Cache::get($fullName);
|
|
|
|
}
|
|
|
|
|
2018-06-29 05:11:44 -05:00
|
|
|
$preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
|
2018-04-01 12:22:30 -05:00
|
|
|
if (null !== $preference && null === $preference->data) {
|
|
|
|
try {
|
|
|
|
$preference->delete();
|
|
|
|
} catch (Exception $e) {
|
2018-07-26 22:03:37 -05:00
|
|
|
Log::debug(sprintf('Could not delete preference #%d: %s', $preference->id, $e->getMessage()));
|
2018-04-01 12:22:30 -05:00
|
|
|
}
|
2018-04-21 13:29:44 -05:00
|
|
|
$preference = null;
|
2018-04-01 12:22:30 -05:00
|
|
|
}
|
2015-05-25 15:56:00 -05:00
|
|
|
|
2018-04-21 13:29:44 -05:00
|
|
|
if (null !== $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:
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $default) {
|
2015-02-06 13:43:19 -06:00
|
|
|
// return NULL
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-04-03 00:07:17 -05:00
|
|
|
return $this->setForUser($user, $name, $default);
|
2015-02-06 13:43:19 -06:00
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function lastActivity(): string
|
2016-01-20 08:23:36 -06:00
|
|
|
{
|
2017-03-02 12:57:46 -06:00
|
|
|
$lastActivity = microtime();
|
|
|
|
$preference = $this->get('lastActivity', microtime());
|
2018-06-24 06:46:34 -05:00
|
|
|
|
2018-04-01 12:22:30 -05:00
|
|
|
if (null !== $preference && null !== $preference->data) {
|
2017-03-02 12:57:46 -06:00
|
|
|
$lastActivity = $preference->data;
|
|
|
|
}
|
2018-04-02 07:50:17 -05:00
|
|
|
if (\is_array($lastActivity)) {
|
2018-03-05 12:35:58 -06:00
|
|
|
$lastActivity = implode(',', $lastActivity);
|
|
|
|
}
|
2018-06-24 06:46:34 -05:00
|
|
|
$hash = md5($lastActivity);
|
2018-07-29 14:02:03 -05:00
|
|
|
//Log::debug(sprintf('Value of last activity is %s, hash is %s', $lastActivity, $hash));
|
2016-01-20 08:23:36 -06:00
|
|
|
|
2018-06-24 06:46:34 -05:00
|
|
|
return $hash;
|
2016-01-20 08:23:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-14 16:04:05 -05:00
|
|
|
*
|
2016-01-20 08:23:36 -06:00
|
|
|
*/
|
2018-07-14 16:04:05 -05:00
|
|
|
public function mark(): void
|
2016-01-20 08:23:36 -06:00
|
|
|
{
|
|
|
|
$this->set('lastActivity', microtime());
|
2016-11-25 09:55:04 -06:00
|
|
|
Session::forget('first');
|
2016-01-20 08:23:36 -06:00
|
|
|
}
|
|
|
|
|
2015-02-06 13:43:19 -06:00
|
|
|
/**
|
2018-07-14 16:04:05 -05:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
2015-02-06 13:43:19 -06:00
|
|
|
*
|
2018-07-14 16:04:05 -05:00
|
|
|
* @return \FireflyIII\Models\Preference
|
2015-02-06 13:43:19 -06:00
|
|
|
*/
|
2018-07-14 16:04:05 -05:00
|
|
|
public function set(string $name, $value): Preference
|
2015-02-06 13:43:19 -06:00
|
|
|
{
|
2016-09-16 05:15:58 -05:00
|
|
|
$user = auth()->user();
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $user) {
|
2016-04-06 02:27:45 -05:00
|
|
|
// make new preference, return it:
|
2016-04-25 11:43:09 -05:00
|
|
|
$pref = new Preference;
|
2016-04-06 02:27:45 -05:00
|
|
|
$pref->name = $name;
|
|
|
|
$pref->data = $value;
|
2016-04-25 11:43:09 -05:00
|
|
|
|
2016-04-06 02:27:45 -05:00
|
|
|
return $pref;
|
2016-03-29 04:55:02 -05:00
|
|
|
}
|
|
|
|
|
2016-09-16 05:15:58 -05:00
|
|
|
return $this->setForUser(auth()->user(), $name, $value);
|
2016-03-29 04:55:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \FireflyIII\User $user
|
2018-07-14 16:04:05 -05:00
|
|
|
* @param string $name
|
2018-06-06 14:23:00 -05:00
|
|
|
* @param mixed $value
|
2016-03-29 04:55:02 -05:00
|
|
|
*
|
|
|
|
* @return Preference
|
|
|
|
*/
|
2018-07-14 16:04:05 -05:00
|
|
|
public function setForUser(User $user, string $name, $value): Preference
|
2016-03-29 04:55:02 -05:00
|
|
|
{
|
2016-11-03 15:54:07 -05:00
|
|
|
$fullName = sprintf('preference%s%s', $user->id, $name);
|
2015-06-03 15:11:50 -05:00
|
|
|
Cache::forget($fullName);
|
2018-06-29 05:11:44 -05:00
|
|
|
$pref = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
|
2016-03-29 04:55:02 -05:00
|
|
|
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null !== $pref) {
|
2015-06-03 14:58:06 -05:00
|
|
|
$pref->data = $value;
|
2016-05-20 10:53:03 -05:00
|
|
|
$pref->save();
|
|
|
|
|
|
|
|
Cache::forever($fullName, $pref);
|
2015-06-03 14:58:06 -05:00
|
|
|
|
2016-05-20 10:53:03 -05:00
|
|
|
return $pref;
|
2015-03-13 02:44:07 -05:00
|
|
|
}
|
2016-05-20 10:53:03 -05:00
|
|
|
|
|
|
|
$pref = new Preference;
|
|
|
|
$pref->name = $name;
|
|
|
|
$pref->data = $value;
|
|
|
|
$pref->user()->associate($user);
|
|
|
|
|
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-03-29 01:14:32 -05:00
|
|
|
}
|