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

284 lines
8.0 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;
2015-02-11 07:35:10 +01:00
use FireflyIII\Models\Preference;
use FireflyIII\User;
2019-09-04 17:39:39 +02:00
use Illuminate\Contracts\Auth\Authenticatable;
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
{
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-09 05:45:22 +01:00
try {
Preference::where('user_id', auth()->user()->id)->where('name', $name)->delete();
} catch (Exception $e) {
2018-08-28 21:48:10 +02:00
Log::debug(sprintf('Could not delete preference: %s', $e->getMessage()));
2018-03-09 05:45:22 +01: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 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
*
2016-04-05 22:00:03 +02:00
* @return \FireflyIII\Models\Preference|null
2016-04-03 07:07:17 +02:00
*/
public function get(string $name, $default = null): ?Preference
2016-04-03 07:07:17 +02:00
{
if ('testing' === config('app.env')) {
2019-08-29 17:53:25 +02:00
Log::warning(sprintf('%s("%s") should NOT be called in the TEST environment!', __METHOD__, $name));
}
2017-11-19 09:20:29 +01:00
/** @var User $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
}
2016-11-20 15:30:16 +01:00
/**
2016-12-14 18:59:12 +01:00
* @param \FireflyIII\User $user
2019-09-04 17:39:39 +02:00
* @param array $list
2016-11-20 15:30:16 +01: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 15:30:16 +01: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 20:43:19 +01:00
/**
2019-09-04 17:39:39 +02:00
* @param \FireflyIII\User|Authenticatable $user
* @param string $name
* @param null|string $default
2015-02-06 20:43:19 +01:00
*
* @return \FireflyIII\Models\Preference|null
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
{
if ('testing' === config('app.env')) {
2019-08-29 17:53:25 +02: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 22:11:50 +02:00
if (Cache::has($fullName)) {
return Cache::get($fullName);
}
2018-06-29 12:11:44 +02: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-27 05:03:37 +02:00
Log::debug(sprintf('Could not delete preference #%d: %s', $preference->id, $e->getMessage()));
}
2018-04-21 20:29:44 +02:00
$preference = null;
}
2015-05-25 22:56:00 +02:00
2018-04-21 20:29:44 +02:00
if (null !== $preference) {
2015-06-03 22:11:50 +02:00
Cache::forever($fullName, $preference);
2015-06-03 21:58:06 +02:00
return $preference;
2015-05-25 22:56:00 +02:00
}
// no preference found and default is null:
2017-11-15 12:25:49 +01:00
if (null === $default) {
2015-02-06 20:43:19 +01:00
// return NULL
return null;
}
2016-04-03 07:07:17 +02:00
return $this->setForUser($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
{
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should NOT be called in the TEST environment!', __METHOD__));
}
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
2018-08-04 17:30:06 +02:00
return md5($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
*
* @return \FireflyIII\Models\Preference
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);
}
/**
* @param \FireflyIII\User $user
2019-09-04 17:39:39 +02: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 22:11:50 +02:00
Cache::forget($fullName);
2019-02-03 18:25:58 +01:00
/** @var Preference $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) {
Log::error(sprintf('Could not delete preference: %s', $e->getMessage()));
}
return new Preference;
}
if (null === $value) {
return new Preference;
}
2017-11-15 12:25:49 +01:00
if (null !== $pref) {
2015-06-03 21:58:06 +02:00
$pref->data = $value;
$pref->save();
Cache::forever($fullName, $pref);
2015-06-03 21:58:06 +02:00
return $pref;
2015-03-13 08:44:07 +01:00
}
$pref = new Preference;
$pref->name = $name;
$pref->data = $value;
$pref->user()->associate($user);
2015-06-03 21:58:06 +02:00
$pref->save();
2015-02-06 20:43:19 +01:00
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
}