firefly-iii/app/Support/Preferences.php

283 lines
7.9 KiB
PHP
Raw Normal View History

2015-02-06 13:43:19 -06:00
<?php
/**
* Preferences.php
2020-02-16 06:56:52 -06: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 01:40:00 -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
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -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/>.
*/
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;
use FireflyIII\User;
use Illuminate\Support\Collection;
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.
2019-09-04 10:39:39 -05:00
*
2019-07-31 09:53:09 -05:00
* @codeCoverageIgnore
2015-02-06 13:43:19 -06:00
*/
class Preferences
{
/**
2019-09-04 10:39:39 -05:00
* @param User $user
* @param string $search
*
* @return Collection
*/
public function beginsWith(User $user, string $search): Collection
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$set = Preference::where('user_id', $user->id)->where('name', 'LIKE', $search . '%')->get();
return $set;
}
/**
* @param $name
*
* @return bool
*/
public function delete(string $name): bool
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
$fullName = sprintf('preference%s%s', auth()->user()->id, $name);
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-08-28 14:48:10 -05:00
Log::debug(sprintf('Could not delete preference: %s', $e->getMessage()));
2018-03-08 22:45:22 -06:00
// don't care.
}
return true;
}
/**
* @param string $name
*
* @return Collection
*/
public function findByName(string $name): Collection
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
2018-04-02 07:50:17 -05:00
return Preference::where('name', $name)->get();
}
2016-04-03 00:07:17 -05:00
/**
* @param string $name
2019-09-04 10:39:39 -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
*/
public function get(string $name, $default = null): ?Preference
2016-04-03 00:07:17 -05:00
{
if ('testing' === config('app.env')) {
2019-08-29 10:53:25 -05:00
Log::warning(sprintf('%s("%s") should NOT be called in the TEST environment!', __METHOD__, $name));
}
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) {
2019-08-09 11:06:43 -05:00
$preference = new Preference;
$preference->data = $default;
return $preference;
2016-04-03 00:07:17 -05:00
}
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
2019-09-04 10:39:39 -05:00
* @param array $list
2016-11-20 08:30:16 -06:00
*
* @return array
*/
public function getArrayForUser(User $user, array $list): array
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
2016-11-20 08:30:16 -06:00
$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
/**
* @param User $user
2019-09-04 10:39:39 -05:00
* @param string $name
* @param null|string $default
2015-02-06 13:43:19 -06:00
*
* @return \FireflyIII\Models\Preference|null
2015-02-06 13:43:19 -06:00
*/
public function getForUser(User $user, string $name, $default = null): ?Preference
2015-02-06 13:43:19 -06:00
{
if ('testing' === config('app.env')) {
2019-08-29 10:53:25 -05:00
Log::warning(sprintf('%s("%s") should NOT be called in the TEST environment!', __METHOD__, $name));
}
$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']);
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-21 13:29:44 -05:00
$preference = null;
}
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
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
2017-03-02 12:57:46 -06:00
$lastActivity = microtime();
$preference = $this->get('lastActivity', microtime());
2018-06-24 06:46:34 -05:00
if (null !== $preference && null !== $preference->data) {
2017-03-02 12:57:46 -06:00
$lastActivity = $preference->data;
}
if (is_array($lastActivity)) {
2018-03-05 12:35:58 -06:00
$lastActivity = implode(',', $lastActivity);
}
2018-08-06 12:14:30 -05:00
2020-04-10 23:42:21 -05:00
return hash('sha256', $lastActivity);
2016-01-20 08:23:36 -06:00
}
/**
*
2016-01-20 08:23:36 -06: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
/**
* @param string $name
2019-09-04 10:39:39 -05:00
* @param mixed $value
2015-02-06 13:43:19 -06:00
*
* @return \FireflyIII\Models\Preference
2015-02-06 13:43:19 -06: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-09-16 05:15:58 -05:00
return $this->setForUser(auth()->user(), $name, $value);
}
/**
* @param \FireflyIII\User $user
2019-09-04 10:39:39 -05:00
* @param string $name
* @param mixed $value
*
* @return Preference
*/
public function setForUser(User $user, string $name, $value): Preference
{
$fullName = sprintf('preference%s%s', $user->id, $name);
2015-06-03 15:11:50 -05:00
Cache::forget($fullName);
2019-02-03 11:25:58 -06:00
/** @var Preference $pref */
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']);
2019-02-03 11:25:58 -06:00
if (null !== $pref && null === $value) {
try {
$pref->delete();
} catch (Exception $e) {
Log::error(sprintf('Could not delete preference: %s', $e->getMessage()));
}
return new Preference;
}
if (null === $value) {
return new Preference;
}
2017-11-15 05:25:49 -06:00
if (null !== $pref) {
2015-06-03 14:58:06 -05:00
$pref->data = $value;
$pref->save();
Cache::forever($fullName, $pref);
2015-06-03 14:58:06 -05:00
return $pref;
2015-03-13 02:44:07 -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
}