2015-02-06 13:43:19 -06:00
|
|
|
<?php
|
2022-12-29 12:42:26 -06:00
|
|
|
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* Preferences.php
|
2020-02-16 06:56:52 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 05:41:23 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-04 23:52:15 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 01:40:00 -05:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-01 23:37:26 -05:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 01:40:00 -05:00
|
|
|
*
|
2019-10-01 23:37:26 -05:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://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;
|
2021-04-06 10:00:16 -05:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
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;
|
2021-07-17 10:26:12 -05:00
|
|
|
use PDOException;
|
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.
|
2019-09-04 10:39:39 -05:00
|
|
|
*
|
2023-02-12 00:15:06 -06:00
|
|
|
|
2015-02-06 13:43:19 -06:00
|
|
|
*/
|
|
|
|
class Preferences
|
|
|
|
{
|
2021-09-18 03:26:12 -05:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function all(): Collection
|
|
|
|
{
|
|
|
|
$user = auth()->user();
|
|
|
|
if (null === $user) {
|
2022-10-30 08:24:37 -05:00
|
|
|
return new Collection();
|
2021-09-18 03:26:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return Preference::where('user_id', $user->id)->get();
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:43:19 -06:00
|
|
|
/**
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param User $user
|
2023-05-29 06:56:55 -05:00
|
|
|
* @param string $search
|
2015-02-06 13:43:19 -06:00
|
|
|
*
|
2023-05-29 06:56:55 -05:00
|
|
|
* @return Collection
|
2015-02-06 13:43:19 -06:00
|
|
|
*/
|
2023-05-29 06:56:55 -05:00
|
|
|
public function beginsWith(User $user, string $search): Collection
|
2015-02-06 13:43:19 -06:00
|
|
|
{
|
2023-05-29 06:56:55 -05:00
|
|
|
return Preference::where('user_id', $user->id)->where('name', 'LIKE', $search.'%')->get();
|
2020-10-24 00:18:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function delete(string $name): bool
|
|
|
|
{
|
|
|
|
$fullName = sprintf('preference%s%s', auth()->user()->id, $name);
|
|
|
|
if (Cache::has($fullName)) {
|
|
|
|
Cache::forget($fullName);
|
|
|
|
}
|
2022-12-30 13:38:54 -06:00
|
|
|
Preference::where('user_id', auth()->user()->id)->where('name', $name)->delete();
|
2022-12-29 12:42:26 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-29 06:56:55 -05:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function findByName(string $name): Collection
|
|
|
|
{
|
|
|
|
return Preference::where('name', $name)->get();
|
|
|
|
}
|
|
|
|
|
2022-12-29 12:42:26 -06:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function forget(User $user, string $name): void
|
|
|
|
{
|
|
|
|
$key = sprintf('preference%s%s', $user->id, $name);
|
|
|
|
Cache::forget($key);
|
|
|
|
Cache::put($key, '', 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $name
|
2023-05-29 06:56:55 -05:00
|
|
|
* @param mixed $default
|
2020-10-24 00:18:37 -05:00
|
|
|
*
|
2023-05-29 06:56:55 -05:00
|
|
|
* @return Preference|null
|
2021-05-24 01:57:02 -05:00
|
|
|
* @throws FireflyException
|
2020-10-24 00:18:37 -05:00
|
|
|
*/
|
2023-05-29 06:56:55 -05:00
|
|
|
public function get(string $name, $default = null): ?Preference
|
2020-10-24 00:18:37 -05:00
|
|
|
{
|
2023-05-29 06:56:55 -05:00
|
|
|
/** @var User|null $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
if (null === $user) {
|
|
|
|
$preference = new Preference();
|
|
|
|
$preference->data = $default;
|
2021-09-18 03:26:12 -05:00
|
|
|
|
2023-05-29 06:56:55 -05:00
|
|
|
return $preference;
|
2021-09-18 03:26:12 -05:00
|
|
|
}
|
2015-02-06 13:43:19 -06:00
|
|
|
|
2023-05-29 06:56:55 -05:00
|
|
|
return $this->getForUser($user, $name, $default);
|
2022-12-29 12:42:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param array $list
|
|
|
|
*
|
|
|
|
* @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;
|
2017-03-02 12:57:46 -06:00
|
|
|
}
|
2022-12-29 12:42:26 -06:00
|
|
|
foreach ($list as $name) {
|
|
|
|
if (!array_key_exists($name, $result)) {
|
|
|
|
$result[$name] = null;
|
|
|
|
}
|
2018-03-05 12:35:58 -06:00
|
|
|
}
|
2018-08-06 12:14:30 -05:00
|
|
|
|
2022-12-29 12:42:26 -06:00
|
|
|
return $result;
|
2016-01-20 08:23:36 -06:00
|
|
|
}
|
|
|
|
|
2023-05-29 06:56:55 -05:00
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param string $name
|
|
|
|
* @param null|string|int $default
|
|
|
|
*
|
|
|
|
* @return Preference|null
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function getForUser(User $user, string $name, $default = null): ?Preference
|
|
|
|
{
|
|
|
|
$preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'user_id', 'name', 'data', 'updated_at', 'created_at']);
|
|
|
|
if (null !== $preference && null === $preference->data) {
|
|
|
|
$preference->delete();
|
|
|
|
$preference = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $preference) {
|
|
|
|
return $preference;
|
|
|
|
}
|
|
|
|
// no preference found and default is null:
|
|
|
|
if (null === $default) {
|
|
|
|
// return NULL
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->setForUser($user, $name, $default);
|
|
|
|
}
|
|
|
|
|
2015-02-06 13:43:19 -06:00
|
|
|
/**
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $default
|
2015-02-06 13:43:19 -06:00
|
|
|
*
|
2021-09-18 03:26:12 -05:00
|
|
|
* @return Preference|null
|
2021-05-24 01:57:02 -05:00
|
|
|
* @throws FireflyException
|
2015-02-06 13:43:19 -06:00
|
|
|
*/
|
2022-12-29 12:42:26 -06:00
|
|
|
public function getFresh(string $name, $default = null): ?Preference
|
2015-02-06 13:43:19 -06:00
|
|
|
{
|
2021-09-18 03:26:12 -05:00
|
|
|
/** @var User|null $user */
|
2016-09-16 05:15:58 -05:00
|
|
|
$user = auth()->user();
|
2017-11-15 05:25:49 -06:00
|
|
|
if (null === $user) {
|
2022-10-30 08:24:37 -05:00
|
|
|
$preference = new Preference();
|
2021-09-18 03:26:12 -05:00
|
|
|
$preference->data = $default;
|
2016-04-25 11:43:09 -05:00
|
|
|
|
2021-09-18 03:26:12 -05:00
|
|
|
return $preference;
|
2016-03-29 04:55:02 -05:00
|
|
|
}
|
|
|
|
|
2022-12-29 12:42:26 -06:00
|
|
|
return $this->getFreshForUser($user, $name, $default);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param string $name
|
|
|
|
* @param null $default
|
|
|
|
*
|
|
|
|
* @return Preference|null
|
|
|
|
* TODO remove me.
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function getFreshForUser(User $user, string $name, $default = null): ?Preference
|
|
|
|
{
|
2021-09-18 03:26:12 -05:00
|
|
|
return $this->getForUser($user, $name, $default);
|
2016-03-29 04:55:02 -05:00
|
|
|
}
|
|
|
|
|
2022-12-29 12:42:26 -06:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function lastActivity(): string
|
|
|
|
{
|
|
|
|
$lastActivity = microtime();
|
|
|
|
$preference = $this->get('lastActivity', microtime());
|
|
|
|
|
|
|
|
if (null !== $preference && null !== $preference->data) {
|
|
|
|
$lastActivity = $preference->data;
|
|
|
|
}
|
|
|
|
if (is_array($lastActivity)) {
|
|
|
|
$lastActivity = implode(',', $lastActivity);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash('sha256', $lastActivity);
|
|
|
|
}
|
|
|
|
|
2020-10-24 00:18:37 -05:00
|
|
|
/**
|
2021-09-18 03:26:12 -05:00
|
|
|
*
|
2020-10-24 00:18:37 -05:00
|
|
|
*/
|
2021-09-18 03:26:12 -05:00
|
|
|
public function mark(): void
|
2020-10-24 00:18:37 -05:00
|
|
|
{
|
2021-09-18 03:26:12 -05:00
|
|
|
$this->set('lastActivity', microtime());
|
|
|
|
Session::forget('first');
|
2020-10-24 00:18:37 -05:00
|
|
|
}
|
|
|
|
|
2016-03-29 04:55:02 -05:00
|
|
|
/**
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
2016-03-29 04:55:02 -05:00
|
|
|
*
|
|
|
|
* @return Preference
|
2021-04-06 10:00:16 -05:00
|
|
|
* @throws FireflyException
|
2016-03-29 04:55:02 -05:00
|
|
|
*/
|
2021-09-18 03:26:12 -05:00
|
|
|
public function set(string $name, $value): Preference
|
2016-03-29 04:55:02 -05:00
|
|
|
{
|
2021-09-18 03:26:12 -05:00
|
|
|
$user = auth()->user();
|
|
|
|
if (null === $user) {
|
|
|
|
// make new preference, return it:
|
2022-10-30 08:24:37 -05:00
|
|
|
$pref = new Preference();
|
2021-04-06 11:48:02 -05:00
|
|
|
$pref->name = $name;
|
2021-09-18 03:26:12 -05:00
|
|
|
$pref->data = $value;
|
|
|
|
|
|
|
|
return $pref;
|
2021-04-06 11:36:37 -05:00
|
|
|
}
|
2015-06-03 15:11:50 -05:00
|
|
|
|
2021-09-18 03:26:12 -05:00
|
|
|
return $this->setForUser(auth()->user(), $name, $value);
|
2015-02-06 13:43:19 -06:00
|
|
|
}
|
2023-05-29 06:56:55 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
* @param string $name
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return Preference
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function setForUser(User $user, string $name, $value): Preference
|
|
|
|
{
|
|
|
|
$fullName = sprintf('preference%s%s', $user->id, $name);
|
|
|
|
Cache::forget($fullName);
|
|
|
|
/** @var Preference|null $pref */
|
|
|
|
$pref = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
|
|
|
|
|
|
|
|
if (null !== $pref && null === $value) {
|
|
|
|
$pref->delete();
|
|
|
|
|
|
|
|
return new Preference();
|
|
|
|
}
|
|
|
|
if (null === $value) {
|
|
|
|
return new Preference();
|
|
|
|
}
|
|
|
|
if (null === $pref) {
|
|
|
|
$pref = new Preference();
|
|
|
|
$pref->user_id = $user->id;
|
|
|
|
$pref->name = $name;
|
|
|
|
}
|
|
|
|
$pref->data = $value;
|
|
|
|
try {
|
|
|
|
$pref->save();
|
|
|
|
} catch (PDOException $e) {
|
|
|
|
throw new FireflyException(sprintf('Could not save preference: %s', $e->getMessage()), 0, $e);
|
|
|
|
}
|
|
|
|
Cache::forever($fullName, $pref);
|
|
|
|
|
|
|
|
return $pref;
|
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|