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

95 lines
3.3 KiB
PHP
Raw Normal View History

2017-08-15 17:34:34 +02:00
<?php
/**
* AutoCompleteController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2017-08-15 17:34:34 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02:00
*
* 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/>.
2017-08-15 17:34:34 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
use Amount;
2017-08-15 17:34:34 +02:00
use FireflyIII\Http\Controllers\Controller;
2020-06-07 11:31:01 +02:00
use FireflyIII\Models\ObjectGroup;
2019-11-10 07:26:49 +01:00
use FireflyIII\Models\PiggyBank;
2019-05-04 20:58:43 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2020-06-07 11:31:01 +02:00
use FireflyIII\Repositories\ObjectGroup\ObjectGroupRepositoryInterface;
2019-05-04 20:58:43 +02:00
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
2019-08-27 07:26:32 +02:00
use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface;
use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
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
/**
2017-11-15 12:25:49 +01:00
* Class AutoCompleteController.
2018-07-20 14:34:56 +02:00
*
2019-06-29 19:47:31 +02:00
* TODO autocomplete for transaction types.
*
2017-08-15 17:34:34 +02:00
*/
class AutoCompleteController extends Controller
{
2018-04-07 06:19:40 +02:00
2019-07-20 06:47:34 +02:00
/**
* Searches in the titles of all transaction journals.
* The result is limited to the top 15 unique results.
*
* If the query is numeric, it will append the journal with that particular ID.
*
* @param Request $request
2019-08-27 07:26:32 +02:00
*
2019-07-20 06:47:34 +02:00
* @return JsonResponse
*/
public function allJournalsWithID(Request $request): JsonResponse
{
$search = (string) $request->get('search');
2019-07-20 06:47:34 +02:00
/** @var JournalRepositoryInterface $repository */
$repository = app(JournalRepositoryInterface::class);
2019-08-27 07:26:32 +02:00
/** @var TransactionGroupRepositoryInterface $groupRepos */
$groupRepos = app(TransactionGroupRepositoryInterface::class);
$result = $repository->searchJournalDescriptions($search);
$array = [];
2019-07-20 06:47:34 +02:00
if (is_numeric($search)) {
2019-08-27 07:26:32 +02:00
// search for group, not journal.
$firstResult = $groupRepos->find((int) $search);
2019-07-20 06:47:34 +02:00
if (null !== $firstResult) {
2019-08-27 07:26:32 +02:00
// group may contain multiple journals, each a result:
foreach ($firstResult->transactionJournals as $journal) {
$array[] = $journal->toArray();
}
2019-07-20 06:47:34 +02:00
}
}
// if not numeric, search ahead!
// limit and unique
2019-08-27 07:26:32 +02:00
$limited = $result->slice(0, 15);
$array = array_merge($array, $limited->toArray());
2019-07-20 06:47:34 +02:00
foreach ($array as $index => $item) {
// give another key for consistency
2019-08-13 18:38:15 +02:00
$array[$index]['name'] = sprintf('#%d: %s', $item['transaction_group_id'], $item['description']);
2019-07-20 06:47:34 +02:00
}
2019-07-20 06:47:34 +02:00
return response()->json($array);
}
2019-05-04 20:58:43 +02:00
2017-08-31 06:47:18 +02:00
}