Files
firefly-iii/app/Support/Preferences.php

293 lines
7.8 KiB
PHP
Raw Normal View History

2015-02-06 20:43:19 +01:00
<?php
/**
* Preferences.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* 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 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02: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/>.
*/
declare(strict_types=1);
2015-02-06 20:43:19 +01:00
namespace FireflyIII\Support;
2015-06-03 22:11:50 +02:00
use Cache;
2018-03-09 05:45:22 +01:00
use Exception;
2021-04-06 17:00:16 +02:00
use FireflyIII\Exceptions\FireflyException;
2015-02-11 07:35:10 +01:00
use FireflyIII\Models\Preference;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
2016-11-25 16:55:04 +01:00
use Session;
2015-06-03 21:25:11 +02:00
2015-02-06 20:43:19 +01:00
/**
2017-11-15 12:25:49 +01:00
* Class Preferences.
2019-09-04 17:39:39 +02:00
*
2019-07-31 16:53:09 +02:00
* @codeCoverageIgnore
2015-02-06 20:43:19 +01:00
*/
class Preferences
{
/**
2019-09-04 17:39:39 +02:00
* @param User $user
* @param string $search
*
* @return Collection
*/
public function beginsWith(User $user, string $search): Collection
{
2020-10-23 19:11:25 +02:00
return Preference::where('user_id', $user->id)->where('name', 'LIKE', $search . '%')->get();
}
/**
2021-04-06 17:00:16 +02:00
* @param string $name
*
* @return bool
2021-04-06 17:00:16 +02:00
* @throws FireflyException
*/
public function delete(string $name): bool
{
$fullName = sprintf('preference%s%s', auth()->user()->id, $name);
if (Cache::has($fullName)) {
Cache::forget($fullName);
}
2018-03-09 05:45:22 +01:00
try {
Preference::where('user_id', auth()->user()->id)->where('name', $name)->delete();
} catch (Exception $e) {
2021-04-06 17:00:16 +02:00
throw new FireflyException(sprintf('Could not delete preference: %s', $e->getMessage()), 0, $e);
2018-03-09 05:45:22 +01:00
}
return true;
}
/**
* @param string $name
*
* @return Collection
*/
public function findByName(string $name): Collection
{
2018-04-02 14:50:17 +02:00
return Preference::where('name', $name)->get();
}
2016-04-03 07:07:17 +02:00
/**
* @param string $name
2019-09-04 17:39:39 +02:00
* @param mixed $default
2016-04-03 07:07:17 +02:00
*
2021-05-24 08:57:02 +02:00
* @return Preference|null
* @throws FireflyException
2016-04-03 07:07:17 +02:00
*/
public function get(string $name, $default = null): ?Preference
2016-04-03 07:07:17 +02:00
{
2021-04-06 17:00:16 +02:00
/** @var User|null $user */
2016-09-16 12:15:58 +02:00
$user = auth()->user();
2017-11-15 12:25:49 +01:00
if (null === $user) {
2019-08-09 18:06:43 +02:00
$preference = new Preference;
$preference->data = $default;
return $preference;
2016-04-03 07:07:17 +02:00
}
2017-11-19 09:20:29 +01:00
return $this->getForUser($user, $name, $default);
2016-04-03 07:07:17 +02:00
}
2020-10-24 07:18:37 +02:00
/**
* @param string $name
* @param mixed $default
*
2021-05-24 08:57:02 +02:00
* @return Preference|null
2020-10-24 07:18:37 +02:00
*/
public function getFresh(string $name, $default = null): ?Preference
{
2021-04-06 17:00:16 +02:00
/** @var User|null $user */
2020-10-24 07:18:37 +02:00
$user = auth()->user();
if (null === $user) {
$preference = new Preference;
$preference->data = $default;
return $preference;
}
return $this->getFreshForUser($user, $name, $default);
}
2016-11-20 15:30:16 +01:00
/**
2021-05-24 08:57:02 +02:00
* @param User $user
* @param array $list
2016-11-20 15:30:16 +01: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) {
2021-04-06 17:00:16 +02:00
if (!array_key_exists($name, $result)) {
2016-11-20 15:30:16 +01:00
$result[$name] = null;
}
}
return $result;
}
2015-02-06 20:43:19 +01:00
/**
2021-04-05 21:52:55 +02:00
* @param User $user
* @param string $name
* @param null|string|int $default
2015-02-06 20:43:19 +01:00
*
2021-05-24 08:57:02 +02:00
* @return Preference|null
2021-04-06 17:00:16 +02:00
* @throws FireflyException
2015-02-06 20:43:19 +01:00
*/
public function getForUser(User $user, string $name, $default = null): ?Preference
2015-02-06 20:43:19 +01:00
{
2021-04-22 18:34:08 +02:00
$preference = Preference::where('user_id', $user->id)->where('name', $name)->first(['id','user_id', 'name', 'data', 'updated_at', 'created_at']);
2020-10-24 07:18:37 +02:00
if (null !== $preference && null === $preference->data) {
try {
$preference->delete();
} catch (Exception $e) {
2021-04-06 17:00:16 +02:00
throw new FireflyException(sprintf('Could not delete preference #%d: %s', $preference->id, $e->getMessage()), 0, $e);
2020-10-24 07:18:37 +02:00
}
$preference = null;
}
if (null !== $preference) {
return $preference;
}
// no preference found and default is null:
if (null === $default) {
// return NULL
return null;
}
2015-06-03 22:11:50 +02:00
2020-10-24 07:18:37 +02:00
return $this->setForUser($user, $name, $default);
}
/**
2021-05-24 08:57:02 +02:00
* @param User $user
* @param string $name
* @param null $default
2020-10-24 07:18:37 +02:00
*
2021-05-24 08:57:02 +02:00
* @return Preference|null
2021-06-11 20:39:01 +02:00
* See reference nr. 44
2021-05-24 08:57:02 +02:00
* @throws FireflyException
2020-10-24 07:18:37 +02:00
*/
public function getFreshForUser(User $user, string $name, $default = null): ?Preference
{
2021-03-08 09:56:40 +01:00
return $this->getForUser($user, $name, $default);
2015-02-06 20:43:19 +01:00
}
2016-01-20 15:23:36 +01:00
/**
* @return string
*/
2016-04-05 22:00:03 +02:00
public function lastActivity(): string
2016-01-20 15:23:36 +01:00
{
2017-03-02 19:57:46 +01:00
$lastActivity = microtime();
$preference = $this->get('lastActivity', microtime());
2018-06-24 13:46:34 +02:00
if (null !== $preference && null !== $preference->data) {
2017-03-02 19:57:46 +01:00
$lastActivity = $preference->data;
}
if (is_array($lastActivity)) {
2018-03-05 19:35:58 +01:00
$lastActivity = implode(',', $lastActivity);
}
2018-08-06 19:14:30 +02:00
2020-04-11 06:42:21 +02:00
return hash('sha256', $lastActivity);
2016-01-20 15:23:36 +01:00
}
/**
*
2016-01-20 15:23:36 +01:00
*/
public function mark(): void
2016-01-20 15:23:36 +01:00
{
$this->set('lastActivity', microtime());
2016-11-25 16:55:04 +01:00
Session::forget('first');
2016-01-20 15:23:36 +01:00
}
2015-02-06 20:43:19 +01:00
/**
* @param string $name
2019-09-04 17:39:39 +02:00
* @param mixed $value
2015-02-06 20:43:19 +01:00
*
2021-05-24 08:57:02 +02:00
* @return Preference
* @throws FireflyException
2015-02-06 20:43:19 +01:00
*/
public function set(string $name, $value): Preference
2015-02-06 20:43:19 +01:00
{
2016-09-16 12:15:58 +02:00
$user = auth()->user();
2017-11-15 12:25:49 +01:00
if (null === $user) {
2016-04-06 09:27:45 +02:00
// make new preference, return it:
2016-04-25 18:43:09 +02:00
$pref = new Preference;
2016-04-06 09:27:45 +02:00
$pref->name = $name;
$pref->data = $value;
2016-04-25 18:43:09 +02:00
2016-04-06 09:27:45 +02:00
return $pref;
}
2016-09-16 12:15:58 +02:00
return $this->setForUser(auth()->user(), $name, $value);
}
2020-10-24 07:18:37 +02: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);
}
/**
2021-05-24 08:57:02 +02:00
* @param User $user
* @param string $name
* @param mixed $value
*
* @return Preference
2021-04-06 17:00:16 +02:00
* @throws FireflyException
*/
public function setForUser(User $user, string $name, $value): Preference
{
$fullName = sprintf('preference%s%s', $user->id, $name);
2015-06-03 22:11:50 +02:00
Cache::forget($fullName);
2021-04-06 18:48:02 +02:00
/** @var Preference|null $pref */
2018-06-29 12:11:44 +02:00
$pref = Preference::where('user_id', $user->id)->where('name', $name)->first(['id', 'name', 'data', 'updated_at', 'created_at']);
2019-02-03 18:25:58 +01:00
if (null !== $pref && null === $value) {
try {
$pref->delete();
} catch (Exception $e) {
2021-04-06 17:00:16 +02:00
throw new FireflyException(sprintf('Could not delete preference: %s', $e->getMessage()), 0, $e);
2019-02-03 18:25:58 +01:00
}
return new Preference;
}
if (null === $value) {
return new Preference;
}
2021-04-06 18:36:37 +02:00
if(null === $pref) {
$pref = new Preference;
2021-04-06 18:48:02 +02:00
$pref->user_id = $user->id;
$pref->name = $name;
2021-04-06 18:36:37 +02:00
}
$pref->data = $value;
2015-06-03 21:58:06 +02:00
$pref->save();
2015-06-03 22:11:50 +02:00
Cache::forever($fullName, $pref);
2015-02-06 20:43:19 +01:00
return $pref;
}
2015-03-29 08:14:32 +02:00
}