Files
firefly-iii/app/Http/Controllers/Json/AutoCompleteController.php

387 lines
12 KiB
PHP
Raw Normal View History

2017-08-15 17:34:34 +02:00
<?php
/**
* AutoCompleteController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
* 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 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-08-15 17:34:34 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
2018-09-30 11:57:51 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\TransactionCollectorInterface;
2017-08-15 17:34:34 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionJournal;
2017-08-15 17:34:34 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2018-04-07 06:19:40 +02:00
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2018-04-08 16:27:52 +02:00
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
2018-04-07 06:19:40 +02:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Support\CacheProperties;
2018-06-01 22:04:52 +02:00
use Illuminate\Http\JsonResponse;
2018-09-23 06:57:00 +02:00
use Illuminate\Http\Request;
2018-09-30 11:57:51 +02:00
use Illuminate\Support\Collection;
2017-08-15 17:34:34 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class AutoCompleteController.
2018-07-20 14:34:56 +02:00
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2017-08-15 17:34:34 +02:00
*/
class AutoCompleteController extends Controller
{
2018-04-07 06:19:40 +02:00
2017-08-30 07:00:17 +02:00
/**
2018-07-21 08:06:24 +02:00
* List of all journals.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
* @param TransactionCollectorInterface $collector
2017-08-30 07:00:17 +02:00
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2017-08-30 07:00:17 +02:00
*/
2018-09-23 06:57:00 +02:00
public function allTransactionJournals(Request $request, TransactionCollectorInterface $collector): JsonResponse
2017-08-30 07:00:17 +02:00
{
2018-09-23 06:57:00 +02:00
$search = (string)$request->get('search');
$cache = new CacheProperties;
$cache->addProperty('ac-all-journals');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
2018-09-30 11:57:51 +02:00
return response()->json($cache->get()); // @codeCoverageIgnore
2018-09-23 06:57:00 +02:00
}
// find everything:
2017-08-30 07:00:17 +02:00
$collector->setLimit(250)->setPage(1);
2018-09-23 06:57:00 +02:00
$return = array_values(array_unique($collector->getTransactions()->pluck('description')->toArray()));
if ('' !== $search) {
$return = array_values(
array_unique(
array_filter(
$return, function (string $value) use ($search) {
return !(false === stripos($value, $search));
}, ARRAY_FILTER_USE_BOTH
)
)
);
}
$cache->store($return);
return response()->json($return);
}
/**
2018-09-30 11:57:51 +02:00
* @param Request $request
* @param string $subject
2018-09-23 06:57:00 +02:00
*
2018-09-30 11:57:51 +02:00
* @throws FireflyException
2018-09-23 06:57:00 +02:00
* @return JsonResponse
*/
2018-09-30 11:57:51 +02:00
public function autoComplete(Request $request, string $subject): JsonResponse
2018-09-23 06:57:00 +02:00
{
2018-09-30 11:57:51 +02:00
$search = (string)$request->get('search');
$unfiltered = null;
$filtered = null;
2018-10-12 07:39:05 +02:00
2018-09-30 11:57:51 +02:00
$cache = new CacheProperties;
$cache->addProperty($subject);
2018-09-23 06:57:00 +02:00
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
2018-09-30 11:57:51 +02:00
return response()->json($cache->get()); // @codeCoverageIgnore
2018-09-23 06:57:00 +02:00
}
2018-09-30 11:57:51 +02:00
// search for all accounts.
if ('all-accounts' === $subject) {
$unfiltered = $this->getAccounts(
[AccountType::REVENUE, AccountType::EXPENSE, AccountType::BENEFICIARY, AccountType::DEFAULT, AccountType::ASSET, AccountType::LOAN,
AccountType::DEBT, AccountType::MORTGAGE]
2018-09-23 06:57:00 +02:00
);
}
2017-08-30 07:00:17 +02:00
2018-09-30 11:57:51 +02:00
// search for expense accounts.
if ('expense-accounts' === $subject) {
$unfiltered = $this->getAccounts([AccountType::EXPENSE, AccountType::BENEFICIARY]);
2018-09-23 06:57:00 +02:00
}
2018-09-30 11:57:51 +02:00
// search for revenue accounts.
if ('revenue-accounts' === $subject) {
$unfiltered = $this->getAccounts([AccountType::REVENUE]);
2018-09-23 06:57:00 +02:00
}
2018-04-07 06:19:40 +02:00
2018-09-30 11:57:51 +02:00
// search for asset accounts.
if ('asset-accounts' === $subject) {
$unfiltered = $this->getAccounts([AccountType::ASSET, AccountType::DEFAULT]);
2018-09-23 06:57:00 +02:00
}
2018-09-30 11:57:51 +02:00
// search for categories.
if ('categories' === $subject) {
$unfiltered = $this->getCategories();
2018-09-23 06:57:00 +02:00
}
2018-04-07 06:19:40 +02:00
2018-09-30 11:57:51 +02:00
// search for budgets.
if ('budgets' === $subject) {
$unfiltered = $this->getBudgets();
2018-09-23 06:57:00 +02:00
}
2018-04-07 06:19:40 +02:00
2018-09-30 11:57:51 +02:00
// search for tags
if ('tags' === $subject) {
$unfiltered = $this->getTags();
2018-09-23 06:57:00 +02:00
}
2018-04-08 16:27:52 +02:00
2018-09-30 11:57:51 +02:00
// search for bills
if ('bills' === $subject) {
$unfiltered = $this->getBills();
2018-09-23 06:57:00 +02:00
}
2018-09-30 11:57:51 +02:00
// search for currency names.
if ('currency-names' === $subject) {
$unfiltered = $this->getCurrencyNames();
}
if ('transaction_types' === $subject) {
$unfiltered = $this->getTransactionTypes();
2018-09-23 06:57:00 +02:00
}
2018-10-12 07:39:05 +02:00
if ('transaction-types' === $subject) {
$unfiltered = $this->getTransactionTypes();
}
2017-08-15 17:34:34 +02:00
2018-09-30 11:57:51 +02:00
// filter results
$filtered = $this->filterResult($unfiltered, $search);
if (null === $filtered) {
throw new FireflyException(sprintf('Auto complete handler cannot handle "%s"', $subject)); // @codeCoverageIgnore
2018-09-23 06:57:00 +02:00
}
2018-09-30 11:57:51 +02:00
$cache->store($filtered);
2018-09-23 06:57:00 +02:00
2018-09-30 11:57:51 +02:00
return response()->json($filtered);
2017-08-15 17:34:34 +02:00
}
/**
2018-07-21 08:06:24 +02:00
* List of journals with their ID.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
* @param TransactionCollectorInterface $collector
2018-09-23 06:57:00 +02:00
* @param TransactionJournal $except
2017-09-09 06:41:45 +02:00
*
2018-07-27 03:09:35 +02:00
* @return JsonResponse
*/
2018-09-23 06:57:00 +02:00
public function journalsWithId(Request $request, TransactionCollectorInterface $collector, TransactionJournal $except): JsonResponse
{
2018-09-23 06:57:00 +02:00
$search = (string)$request->get('search');
$cache = new CacheProperties;
$cache->addProperty('ac-expense-accounts');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
2018-09-30 11:57:51 +02:00
return response()->json($cache->get()); // @codeCoverageIgnore
}
2018-09-23 06:57:00 +02:00
// find everything:
$collector->setLimit(400)->setPage(1);
$set = $collector->getTransactions()->pluck('description', 'journal_id')->toArray();
$return = [];
foreach ($set as $id => $description) {
2018-04-02 15:10:40 +02:00
$id = (int)$id;
if ($id !== $except->id) {
$return[] = [
'id' => $id,
'name' => $id . ': ' . $description,
];
}
}
2018-09-23 06:57:00 +02:00
sort($return);
if ('' !== $search) {
2018-09-30 19:11:49 +02:00
$return = array_filter(
$return, function (array $array) use ($search) {
$haystack = $array['name'];
$result = stripos($haystack, $search);
return !(false === $result);
}
2018-09-23 06:57:00 +02:00
);
2018-09-30 19:11:49 +02:00
2018-09-23 06:57:00 +02:00
}
$cache->store($return);
2018-03-10 20:30:09 +01:00
return response()->json($return);
}
2017-08-15 17:34:34 +02:00
/**
2018-09-30 11:57:51 +02:00
* List of journals by type.
2018-07-21 08:06:24 +02:00
*
2018-09-30 11:57:51 +02:00
* @param Request $request
* @param TransactionCollectorInterface $collector
* @param string $what
2017-08-15 17:34:34 +02:00
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2017-08-15 17:34:34 +02:00
*/
2018-09-30 11:57:51 +02:00
public function transactionJournals(Request $request, TransactionCollectorInterface $collector, string $what): JsonResponse
2017-08-15 17:34:34 +02:00
{
2018-09-23 06:57:00 +02:00
$search = (string)$request->get('search');
$cache = new CacheProperties;
$cache->addProperty('ac-revenue-accounts');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
2018-09-30 11:57:51 +02:00
return response()->json($cache->get()); // @codeCoverageIgnore
2018-09-23 06:57:00 +02:00
}
// find everything:
2018-09-30 11:57:51 +02:00
$type = config('firefly.transactionTypesByWhat.' . $what);
$types = [$type];
$collector->setTypes($types)->setLimit(250)->setPage(1);
$return = array_unique($collector->getTransactions()->pluck('description')->toArray());
2017-08-15 17:34:34 +02:00
sort($return);
2018-09-23 06:57:00 +02:00
if ('' !== $search) {
$return = array_values(
array_unique(
array_filter(
$return, function (string $value) use ($search) {
return !(false === stripos($value, $search));
}, ARRAY_FILTER_USE_BOTH
)
)
);
}
$cache->store($return);
2018-09-15 13:43:57 +02:00
return response()->json($return);
}
2018-04-07 06:19:40 +02:00
/**
2018-09-30 11:57:51 +02:00
* @param array $unfiltered
* @param string $query
2018-04-07 06:19:40 +02:00
*
2018-09-30 11:57:51 +02:00
* @return array|null
2018-04-07 06:19:40 +02:00
*/
2018-09-30 11:57:51 +02:00
private function filterResult(?array $unfiltered, string $query): ?array
2018-04-07 06:19:40 +02:00
{
2018-09-30 11:57:51 +02:00
if (null === $unfiltered) {
return null; // @codeCoverageIgnore
2018-09-23 06:57:00 +02:00
}
2018-09-30 11:57:51 +02:00
if ('' === $query) {
sort($unfiltered);
2018-04-07 06:19:40 +02:00
2018-09-30 11:57:51 +02:00
return $unfiltered;
}
$return = [];
if ('' !== $query) {
2018-09-23 06:57:00 +02:00
$return = array_values(
2018-09-30 11:57:51 +02:00
array_filter(
$unfiltered, function (string $value) use ($query) {
return !(false === stripos($value, $query));
}, ARRAY_FILTER_USE_BOTH
2018-09-23 06:57:00 +02:00
)
);
}
2018-09-30 11:57:51 +02:00
sort($return);
2018-09-23 06:57:00 +02:00
2018-09-30 11:57:51 +02:00
return $return;
2018-04-07 06:19:40 +02:00
}
2017-08-30 07:00:17 +02:00
/**
2018-09-30 11:57:51 +02:00
* @param string $query
* @param array $types
2017-08-30 07:00:17 +02:00
*
2018-09-30 11:57:51 +02:00
* @return array
2017-08-30 07:00:17 +02:00
*/
2018-09-30 11:57:51 +02:00
private function getAccounts(array $types): array
2017-08-30 07:00:17 +02:00
{
2018-09-30 11:57:51 +02:00
$repository = app(AccountRepositoryInterface::class);
2018-09-23 06:57:00 +02:00
// find everything:
2018-09-30 11:57:51 +02:00
/** @var Collection $collection */
$collection = $repository->getAccountsByType($types);
2018-09-30 19:11:49 +02:00
$filtered = $collection->filter(
function (Account $account) {
return $account->active === true;
}
);
$return = array_values(array_unique($filtered->pluck('name')->toArray()));
2017-08-30 07:00:17 +02:00
2018-09-30 11:57:51 +02:00
return $return;
2017-08-30 07:00:17 +02:00
2018-09-30 11:57:51 +02:00
}
2018-09-23 06:57:00 +02:00
2018-09-30 11:57:51 +02:00
/**
* @return array
*/
private function getBills(): array
{
$repository = app(BillRepositoryInterface::class);
return array_unique($repository->getActiveBills()->pluck('name')->toArray());
2017-08-30 07:00:17 +02:00
}
2018-04-07 06:19:40 +02:00
/**
2018-09-30 11:57:51 +02:00
* @return array
2018-04-07 06:19:40 +02:00
*/
2018-09-30 11:57:51 +02:00
private function getBudgets(): array
2018-04-07 06:19:40 +02:00
{
2018-09-30 11:57:51 +02:00
$repository = app(BudgetRepositoryInterface::class);
2018-04-07 06:19:40 +02:00
2018-09-30 11:57:51 +02:00
return array_unique($repository->getBudgets()->pluck('name')->toArray());
}
2018-09-23 06:57:00 +02:00
2018-09-30 11:57:51 +02:00
/**
* @return array
*/
private function getCategories(): array
{
$repository = app(CategoryRepositoryInterface::class);
return array_unique($repository->getCategories()->pluck('name')->toArray());
}
/**
* @return array
*/
private function getCurrencyNames(): array
{
/** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class);
return $repository->get()->pluck('name')->toArray();
}
2018-09-23 06:57:00 +02:00
2018-09-30 11:57:51 +02:00
/**
* @return array
*/
private function getTags(): array
{
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
return array_unique($repository->get()->pluck('tag')->toArray());
}
/**
* @return array
*/
private function getTransactionTypes(): array
{
$repository = app(JournalRepositoryInterface::class);
2018-09-23 06:57:00 +02:00
2018-09-30 11:57:51 +02:00
return array_unique($repository->getTransactionTypes()->pluck('type')->toArray());
2018-04-07 06:19:40 +02:00
}
2017-08-31 06:47:18 +02:00
}