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

606 lines
21 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;
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;
2017-08-15 17:34:34 +02:00
/**
2018-09-23 06:57:00 +02:00
* TODO refactor so each auto-complete thing is a function call because lots of code duplication.
2017-11-15 12:25:49 +01:00
* Class AutoCompleteController.
2018-07-20 14:34:56 +02:00
*
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @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-15 17:34:34 +02:00
/**
* Returns a JSON list of all accounts.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
2017-08-15 17:34:34 +02:00
* @param AccountRepositoryInterface $repository
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2017-08-15 17:34:34 +02:00
*/
2018-09-23 06:57:00 +02:00
public function allAccounts(Request $request, AccountRepositoryInterface $repository): 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-all-accounts');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
return response()->json($cache->get());
}
// find everything:
$return = array_values(
array_unique(
$repository->getAccountsByType(
[AccountType::REVENUE, AccountType::EXPENSE, AccountType::BENEFICIARY, AccountType::DEFAULT, AccountType::ASSET]
)->pluck('name')->toArray()
)
2017-08-15 17:34:34 +02:00
);
2018-09-23 06:57:00 +02:00
if ('' !== $search) {
$return = array_values(
array_filter(
$return, function (string $value) use ($search) {
return !(false === stripos($value, $search));
}, ARRAY_FILTER_USE_BOTH
)
);
}
$cache->store($return);
2017-08-15 17:34:34 +02:00
2018-03-10 20:30:09 +01:00
return response()->json($return);
2017-08-15 17:34:34 +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()) {
return response()->json($cache->get());
}
// 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);
}
/**
* List of revenue accounts.
*
* @param Request $request
* @param AccountRepositoryInterface $repository
*
* @return JsonResponse
*/
public function assetAccounts(Request $request, AccountRepositoryInterface $repository): JsonResponse
{
$search = (string)$request->get('search');
$cache = new CacheProperties;
$cache->addProperty('ac-asset-accounts');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
return response()->json($cache->get());
}
// find everything:
$set = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
$filtered = $set->filter(
function (Account $account) {
if (true === $account->active) {
return $account;
}
return false; // @codeCoverageIgnore
}
);
$return = array_values(array_unique($filtered->pluck('name')->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);
2017-08-30 07:00:17 +02:00
2018-03-10 20:30:09 +01:00
return response()->json($return);
2017-08-30 07:00:17 +02:00
}
2018-04-07 06:19:40 +02:00
/**
* Returns a JSON list of all bills.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
2018-04-07 06:19:40 +02:00
* @param BillRepositoryInterface $repository
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2018-04-07 06:19:40 +02:00
*/
2018-09-23 06:57:00 +02:00
public function bills(Request $request, BillRepositoryInterface $repository): JsonResponse
2018-04-07 06:19:40 +02:00
{
2018-09-23 06:57:00 +02:00
$search = (string)$request->get('search');
$cache = new CacheProperties;
$cache->addProperty('ac-bills');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
return response()->json($cache->get());
}
// find everything:
$return = array_unique($repository->getActiveBills()->pluck('name')->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);
2018-04-07 06:19:40 +02:00
return response()->json($return);
}
/**
2018-07-21 08:06:24 +02:00
* List of budgets.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
2018-04-07 06:19:40 +02:00
* @param BudgetRepositoryInterface $repository
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2018-04-07 06:19:40 +02:00
*/
2018-09-23 06:57:00 +02:00
public function budgets(Request $request, BudgetRepositoryInterface $repository): JsonResponse
2018-04-07 06:19:40 +02:00
{
2018-09-23 06:57:00 +02:00
$search = (string)$request->get('search');
$cache = new CacheProperties;
$cache->addProperty('ac-budgets');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
return response()->json($cache->get());
}
// find everything:
2018-04-07 06:19:40 +02:00
$return = array_unique($repository->getBudgets()->pluck('name')->toArray());
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-04-07 06:19:40 +02:00
return response()->json($return);
}
/**
* Returns a list of categories.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
2018-04-07 06:19:40 +02:00
* @param CategoryRepositoryInterface $repository
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2018-04-07 06:19:40 +02:00
*/
2018-09-23 06:57:00 +02:00
public function categories(Request $request, CategoryRepositoryInterface $repository): JsonResponse
2018-04-07 06:19:40 +02:00
{
2018-09-23 06:57:00 +02:00
$search = (string)$request->get('search');
$cache = new CacheProperties;
$cache->addProperty('ac-categories');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
return response()->json($cache->get());
}
// find everything:
2018-04-07 06:19:40 +02:00
$return = array_unique($repository->getCategories()->pluck('name')->toArray());
2018-09-23 06:57:00 +02:00
if ('' !== $search) {
$return = array_values(
array_filter(
$return, function (string $value) use ($search) {
return !(false === stripos($value, $search));
}, ARRAY_FILTER_USE_BOTH
)
);
}
$cache->store($return);
2018-04-07 06:19:40 +02:00
return response()->json($return);
}
2018-04-08 16:27:52 +02:00
/**
2018-07-21 08:06:24 +02:00
* List of currency names.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
2018-04-08 16:27:52 +02:00
* @param CurrencyRepositoryInterface $repository
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2018-04-08 16:27:52 +02:00
*/
2018-09-23 06:57:00 +02:00
public function currencyNames(Request $request, CurrencyRepositoryInterface $repository): JsonResponse
2018-04-08 16:27:52 +02:00
{
2018-09-23 06:57:00 +02:00
$search = (string)$request->get('search');
$cache = new CacheProperties;
$cache->addProperty('ac-currency-names');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
return response()->json($cache->get());
}
// find everything:
2018-04-08 16:27:52 +02:00
$return = $repository->get()->pluck('name')->toArray();
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-04-08 16:27:52 +02:00
return response()->json($return);
}
2017-08-15 17:34:34 +02:00
/**
* Returns a JSON list of all beneficiaries.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
2017-08-15 17:34:34 +02:00
* @param AccountRepositoryInterface $repository
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2017-08-15 17:34:34 +02:00
*/
2018-09-23 06:57:00 +02:00
public function expenseAccounts(Request $request, AccountRepositoryInterface $repository): 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-expense-accounts');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
return response()->json($cache->get());
}
// find everything:
2017-08-15 17:34:34 +02:00
$set = $repository->getAccountsByType([AccountType::EXPENSE, AccountType::BENEFICIARY]);
$filtered = $set->filter(
function (Account $account) {
2018-07-08 07:59:58 +02:00
if (true === $account->active) {
2017-08-15 17:34:34 +02:00
return $account;
}
return false;
}
);
$return = array_unique($filtered->pluck('name')->toArray());
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-03-10 20:30:09 +01:00
return response()->json($return);
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-23 06:57:00 +02:00
return response()->json($cache->get());
}
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) {
$return = array_values(
array_unique(
array_filter(
$return, function (array $array) use ($search) {
$value = $array['name'];
2018-09-23 06:57:00 +02:00
return !(false === stripos($value, $search));
}, ARRAY_FILTER_USE_BOTH
)
)
);
}
$cache->store($return);
2018-03-10 20:30:09 +01:00
return response()->json($return);
}
2017-08-15 17:34:34 +02:00
/**
2018-07-21 08:06:24 +02:00
* List of revenue accounts.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
2017-08-15 17:34:34 +02:00
* @param AccountRepositoryInterface $repository
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2017-08-15 17:34:34 +02:00
*/
2018-09-23 06:57:00 +02:00
public function revenueAccounts(Request $request, AccountRepositoryInterface $repository): 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()) {
return response()->json($cache->get());
}
// find everything:
2017-08-15 17:34:34 +02:00
$set = $repository->getAccountsByType([AccountType::REVENUE]);
$filtered = $set->filter(
function (Account $account) {
2018-07-08 07:59:58 +02:00
if (true === $account->active) {
2017-08-15 17:34:34 +02:00
return $account;
}
return false;
}
);
$return = array_unique($filtered->pluck('name')->toArray());
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
/**
* Returns a JSON list of all beneficiaries.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
2018-04-07 06:19:40 +02:00
* @param TagRepositoryInterface $tagRepository
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2018-04-07 06:19:40 +02:00
*/
2018-09-23 06:57:00 +02:00
public function tags(Request $request, TagRepositoryInterface $tagRepository): JsonResponse
2018-04-07 06:19:40 +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()) {
return response()->json($cache->get());
}
// find everything:
2018-04-07 06:19:40 +02:00
$return = array_unique($tagRepository->get()->pluck('tag')->toArray());
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-04-07 06:19:40 +02:00
return response()->json($return);
}
2017-08-30 07:00:17 +02:00
/**
2018-07-21 08:06:24 +02:00
* List of journals by type.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
* @param TransactionCollectorInterface $collector
2018-09-23 06:57:00 +02:00
* @param string $what
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 transactionJournals(Request $request, TransactionCollectorInterface $collector, string $what): 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-revenue-accounts');
// very unlikely a user will actually search for this string.
$key = '' === $search ? 'skjf0893j89fj2398hd89dh289h2398hr7isd8900828u209ujnxs88929282u' : $search;
$cache->addProperty($key);
if ($cache->has()) {
return response()->json($cache->get());
}
// find everything:
2017-08-30 07:00:17 +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-30 07:00:17 +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-03-10 20:30:09 +01:00
return response()->json($return);
2017-08-30 07:00:17 +02:00
}
2018-04-07 06:19:40 +02:00
/**
2018-07-21 08:06:24 +02:00
* List if transaction types.
*
2018-09-23 06:57:00 +02:00
* @param Request $request
2018-04-07 06:19:40 +02:00
* @param JournalRepositoryInterface $repository
*
2018-06-01 22:04:52 +02:00
* @return JsonResponse
2018-04-07 06:19:40 +02:00
*/
2018-09-23 06:57:00 +02:00
public function transactionTypes(Request $request, JournalRepositoryInterface $repository): JsonResponse
2018-04-07 06:19:40 +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()) {
return response()->json($cache->get());
}
// find everything:
2018-04-07 06:19:40 +02:00
$return = array_unique($repository->getTransactionTypes()->pluck('type')->toArray());
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-04-07 06:19:40 +02:00
return response()->json($return);
2018-09-23 06:57:00 +02:00
2018-04-07 06:19:40 +02:00
}
2017-08-31 06:47:18 +02:00
}