firefly-iii/app/Http/Requests/ReportFormRequest.php

254 lines
7.6 KiB
PHP
Raw Normal View History

<?php
2022-12-29 12:42:26 -06:00
/**
* ReportFormRequest.php
2020-01-31 00:32:04 -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);
namespace FireflyIII\Http\Requests;
use Carbon\Carbon;
use Exception;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
2017-02-15 13:07:10 -06:00
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
2020-10-24 00:55:09 -05:00
use FireflyIII\Support\Request\ChecksLogin;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Collection;
use Log;
/**
2017-11-15 05:25:49 -06:00
* Class CategoryFormRequest.
*/
2020-10-24 00:55:09 -05:00
class ReportFormRequest extends FormRequest
{
2020-10-24 00:55:09 -05:00
use ChecksLogin;
/**
2018-07-22 01:10:16 -05:00
* Validate list of accounts.
*
* @return Collection
*/
2016-12-14 11:59:12 -06:00
public function getAccountList(): Collection
{
2017-09-12 14:44:31 -05:00
// fixed
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$set = $this->get('accounts');
2022-10-30 08:24:28 -05:00
$collection = new Collection();
if (is_array($set)) {
foreach ($set as $accountId) {
2022-12-29 12:42:26 -06:00
$account = $repository->find((int)$accountId);
if (null !== $account) {
$collection->push($account);
}
}
}
return $collection;
}
/**
2018-07-22 01:10:16 -05:00
* Validate list of budgets.
*
* @return Collection
*/
2016-12-14 11:59:12 -06:00
public function getBudgetList(): Collection
{
2016-12-14 11:59:12 -06:00
/** @var BudgetRepositoryInterface $repository */
$repository = app(BudgetRepositoryInterface::class);
$set = $this->get('budget');
2022-10-30 08:24:28 -05:00
$collection = new Collection();
if (is_array($set)) {
2016-12-14 11:59:12 -06:00
foreach ($set as $budgetId) {
2022-12-29 12:42:26 -06:00
$budget = $repository->find((int)$budgetId);
if (null !== $budget) {
2016-12-14 11:59:12 -06:00
$collection->push($budget);
}
}
}
return $collection;
}
/**
2018-07-22 01:10:16 -05:00
* Validate list of categories.
*
* @return Collection
*/
2016-12-14 11:59:12 -06:00
public function getCategoryList(): Collection
{
2016-12-14 11:59:12 -06:00
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$set = $this->get('category');
2022-10-30 08:24:28 -05:00
$collection = new Collection();
if (is_array($set)) {
2016-12-14 11:59:12 -06:00
foreach ($set as $categoryId) {
2022-12-29 12:42:26 -06:00
$category = $repository->find((int)$categoryId);
if (null !== $category) {
2016-12-14 11:59:12 -06:00
$collection->push($category);
}
}
}
return $collection;
}
/**
* Validate list of accounts which exist twice in system.
*
* @return Collection
*/
public function getDoubleList(): Collection
{
/** @var AccountRepositoryInterface $repository */
$repository = app(AccountRepositoryInterface::class);
$set = $this->get('double');
2022-10-30 08:24:28 -05:00
$collection = new Collection();
if (is_array($set)) {
foreach ($set as $accountId) {
2022-12-29 12:42:26 -06:00
$account = $repository->find((int)$accountId);
if (null !== $account) {
$collection->push($account);
}
}
}
return $collection;
}
2016-11-18 13:06:08 -06:00
/**
2018-07-22 01:10:16 -05:00
* Validate end date.
*
2016-11-18 13:06:08 -06:00
* @return Carbon
2017-11-15 05:25:49 -06:00
*
2020-10-24 00:55:09 -05:00
* @throws FireflyException
2016-11-18 13:06:08 -06:00
*/
public function getEndDate(): Carbon
{
2020-09-11 00:12:33 -05:00
$date = today(config('app.timezone'));
$range = $this->get('daterange');
2022-12-29 12:42:26 -06:00
$parts = explode(' - ', (string)$range);
if (2 === count($parts)) {
2022-01-26 11:43:14 -06:00
$string = $parts[1];
// validate as date
// if regex for YYYY-MM-DD:
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][\d]|3[01])$/';
if (preg_match($pattern, $string)) {
try {
$date = new Carbon($parts[1]);
2022-12-30 13:25:04 -06:00
} catch (Exception $e) { // intentional generic exception
2022-01-26 11:43:14 -06:00
$error = sprintf('"%s" is not a valid date range: %s', $range, $e->getMessage());
Log::error($error);
throw new FireflyException($error, 0, $e);
}
return $date;
}
2022-01-26 11:43:14 -06:00
$error = sprintf('"%s" is not a valid date range: %s', $range, 'invalid format :(');
Log::error($error);
throw new FireflyException($error, 0);
}
return $date;
}
/**
2018-07-22 01:10:16 -05:00
* Validate start date.
*
* @return Carbon
2017-11-15 05:25:49 -06:00
*
2020-10-24 00:55:09 -05:00
* @throws FireflyException
*/
public function getStartDate(): Carbon
{
2020-09-11 00:12:33 -05:00
$date = today(config('app.timezone'));
$range = $this->get('daterange');
2022-12-29 12:42:26 -06:00
$parts = explode(' - ', (string)$range);
if (2 === count($parts)) {
2022-01-26 11:43:14 -06:00
$string = $parts[0];
// validate as date
// if regex for YYYY-MM-DD:
$pattern = '/^(19|20)\d\d-(0[1-9]|1[012])-(0[1-9]|[12][\d]|3[01])$/';
if (preg_match($pattern, $string)) {
try {
$date = new Carbon($parts[0]);
2022-12-30 13:25:04 -06:00
} catch (Exception $e) { // intentional generic exception
2022-01-26 11:43:14 -06:00
$error = sprintf('"%s" is not a valid date range: %s', $range, $e->getMessage());
Log::error($error);
throw new FireflyException($error, 0, $e);
}
return $date;
}
2022-01-26 11:43:14 -06:00
$error = sprintf('"%s" is not a valid date range: %s', $range, 'invalid format :(');
Log::error($error);
throw new FireflyException($error, 0);
}
return $date;
}
2017-02-15 13:07:10 -06:00
/**
2018-07-22 01:10:16 -05:00
* Validate list of tags.
*
2017-02-15 13:07:10 -06:00
* @return Collection
*/
public function getTagList(): Collection
{
/** @var TagRepositoryInterface $repository */
$repository = app(TagRepositoryInterface::class);
$set = $this->get('tag');
2022-10-30 08:24:28 -05:00
$collection = new Collection();
2023-01-20 15:08:18 -06:00
if (is_array($set)) {
Log::debug('Set is:', $set);
}
if (!is_array($set)) {
Log::error(sprintf('Set is not an array! "%s"', $set));
}
if (is_array($set)) {
2017-02-15 13:07:10 -06:00
foreach ($set as $tagTag) {
2018-09-15 06:44:36 -05:00
Log::debug(sprintf('Now searching for "%s"', $tagTag));
2017-02-15 13:07:10 -06:00
$tag = $repository->findByTag($tagTag);
2018-07-24 23:45:25 -05:00
if (null !== $tag) {
2017-02-15 13:07:10 -06:00
$collection->push($tag);
2018-08-16 22:54:29 -05:00
continue;
}
2022-12-29 12:42:26 -06:00
$tag = $repository->find((int)$tagTag);
2018-08-16 22:54:29 -05:00
if (null !== $tag) {
$collection->push($tag);
2017-02-15 13:07:10 -06:00
}
}
}
return $collection;
}
/**
2018-07-22 01:10:16 -05:00
* Rules for this request.
*
* @return array
*/
public function rules(): array
{
return [
2019-09-03 15:35:41 -05:00
'report_type' => 'in:audit,default,category,budget,tag,double',
];
}
}