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

202 lines
8.3 KiB
PHP
Raw Normal View History

2018-07-20 14:34:56 +02:00
<?php
/**
* RecurrenceController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-20 14:34:56 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-20 14:34:56 +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.
2018-07-20 14:34:56 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-07-20 14:34:56 +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.
2018-07-20 14:34:56 +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/>.
2018-07-20 14:34:56 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
use Carbon\Carbon;
2023-06-25 06:24:08 +02:00
use Carbon\Exceptions\InvalidFormatException;
2018-07-20 14:34:56 +02:00
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\RecurrenceRepetition;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
/**
* Class RecurrenceController
*/
class RecurrenceController extends Controller
{
2021-02-03 06:25:58 +01:00
private RecurringRepositoryInterface $recurring;
2018-07-20 14:34:56 +02:00
/**
2018-07-21 08:06:24 +02:00
* RecurrenceController constructor.
2018-07-20 14:34:56 +02:00
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
$this->recurring = app(RecurringRepositoryInterface::class);
return $next($request);
}
);
}
/**
2018-07-21 08:06:24 +02:00
* Shows all events for a repetition. Used in calendar.
*
2023-12-22 20:12:38 +01:00
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2023-12-22 20:12:38 +01:00
*
2021-03-21 09:15:40 +01:00
* @throws FireflyException
2018-07-20 14:34:56 +02:00
*/
public function events(Request $request): JsonResponse
{
$occurrences = [];
$return = [];
$start = Carbon::createFromFormat('Y-m-d', $request->get('start'));
$end = Carbon::createFromFormat('Y-m-d', $request->get('end'));
$firstDate = Carbon::createFromFormat('Y-m-d', $request->get('first_date'));
$endDate = '' !== (string) $request->get('end_date') ? Carbon::createFromFormat('Y-m-d', $request->get('end_date')) : null;
$endsAt = (string) $request->get('ends');
$repetitionType = explode(',', $request->get('type'))[0];
$repetitions = (int) $request->get('reps');
$weekend = (int) $request->get('weekend');
$repetitionMoment = '';
$skip = (int) $request->get('skip');
$skip = $skip < 1 || $skip > 31 ? 1 : $skip;
$weekend = $weekend < 1 || $weekend > 4 ? 1 : $weekend;
2023-11-28 04:45:07 +01:00
2023-12-10 06:51:59 +01:00
if (false === $start || false === $end || false === $firstDate || false === $endDate) {
2023-11-28 04:45:07 +01:00
return response()->json();
}
2018-07-20 14:34:56 +02:00
$start->startOfDay();
// if $firstDate is beyond $end, simply return an empty array.
if ($firstDate->gt($end)) {
2019-02-13 17:38:41 +01:00
return response()->json();
2018-07-20 14:34:56 +02:00
}
// if $firstDate is beyond start, use that one:
$actualStart = clone $firstDate;
2018-07-20 14:34:56 +02:00
2018-07-22 08:10:16 +02:00
if ('weekly' === $repetitionType || 'monthly' === $repetitionType) {
2018-07-20 14:34:56 +02:00
$repetitionMoment = explode(',', $request->get('type'))[1] ?? '1';
}
2018-07-22 08:10:16 +02:00
if ('ndom' === $repetitionType) {
2018-07-20 14:34:56 +02:00
$repetitionMoment = str_ireplace('ndom,', '', $request->get('type'));
}
2018-07-22 08:10:16 +02:00
if ('yearly' === $repetitionType) {
2018-07-20 14:34:56 +02:00
$repetitionMoment = explode(',', $request->get('type'))[1] ?? '2018-01-01';
}
2021-02-03 06:25:58 +01:00
$actualStart->startOfDay();
2022-10-30 14:24:28 +01:00
$repetition = new RecurrenceRepetition();
2018-07-20 14:34:56 +02:00
$repetition->repetition_type = $repetitionType;
$repetition->repetition_moment = $repetitionMoment;
2024-01-01 14:13:06 +01:00
$repetition->repetition_skip = $skip;
2024-01-01 14:13:49 +01:00
$repetition->weekend = $weekend;
2018-07-20 14:34:56 +02:00
$actualEnd = clone $end;
2021-02-03 06:25:58 +01:00
2022-12-30 20:38:54 +01:00
if ('until_date' === $endsAt) {
2022-12-30 09:28:03 +01:00
$actualEnd = $endDate ?? clone $end;
$occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd);
2018-07-20 14:34:56 +02:00
}
2022-12-30 20:38:54 +01:00
if ('times' === $endsAt) {
2022-12-30 09:28:03 +01:00
$occurrences = $this->recurring->getXOccurrences($repetition, $actualStart, $repetitions);
}
2022-12-30 20:38:54 +01:00
if ('times' !== $endsAt && 'until_date' !== $endsAt) {
2022-12-30 09:28:03 +01:00
// 'forever'
$occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd);
}
2018-07-20 14:34:56 +02:00
/** @var Carbon $current */
foreach ($occurrences as $current) {
if ($current->gte($start)) {
$event = [
'id' => $repetitionType.$firstDate->format('Ymd'),
2018-07-20 14:34:56 +02:00
'title' => 'X',
'allDay' => true,
'start' => $current->format('Y-m-d'),
'end' => $current->format('Y-m-d'),
'editable' => false,
'rendering' => 'background',
];
$return[] = $event;
}
}
return response()->json($return);
}
/**
2018-07-21 08:06:24 +02:00
* Suggests repetition moments.
2018-07-20 14:34:56 +02:00
*/
public function suggest(Request $request): JsonResponse
{
$string = '' === (string) $request->get('date') ? date('Y-m-d') : (string) $request->get('date');
$today = today(config('app.timezone'))->startOfDay();
2023-12-20 19:35:52 +01:00
2023-06-25 06:24:08 +02:00
try {
2023-11-28 04:45:07 +01:00
$date = Carbon::createFromFormat('Y-m-d', $string, config('app.timezone'));
2023-07-15 16:02:42 +02:00
} catch (InvalidFormatException $e) {
2023-06-25 06:24:08 +02:00
$date = Carbon::today(config('app.timezone'));
}
2023-12-10 06:51:59 +01:00
if (false === $date) {
2023-11-28 04:45:07 +01:00
return response()->json();
}
$date->startOfDay();
2024-01-01 14:13:49 +01:00
$preSelected = (string) $request->get('pre_select');
2021-03-21 09:15:40 +01:00
$locale = app('steam')->getLocale();
2020-01-25 06:17:17 +01:00
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('date = %s, today = %s. date > today? %s', $date->toAtomString(), $today->toAtomString(), var_export($date > $today, true)));
2024-01-01 14:13:49 +01:00
app('log')->debug(sprintf('past = true? %s', var_export('true' === (string) $request->get('past'), true)));
2020-01-25 06:17:17 +01:00
$result = [];
2024-01-01 14:13:49 +01:00
if ($date > $today || 'true' === (string) $request->get('past')) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Will fill dropdown.');
2018-07-20 14:34:56 +02:00
$weekly = sprintf('weekly,%s', $date->dayOfWeekIso);
$monthly = sprintf('monthly,%s', $date->day);
2024-01-01 14:13:49 +01:00
$dayOfWeek = (string) trans(sprintf('config.dow_%s', $date->dayOfWeekIso));
2018-07-20 14:34:56 +02:00
$ndom = sprintf('ndom,%s,%s', $date->weekOfMonth, $date->dayOfWeekIso);
$yearly = sprintf('yearly,%s', $date->format('Y-m-d'));
2024-01-01 14:13:49 +01:00
$yearlyDate = $date->isoFormat((string) trans('config.month_and_day_no_year_js', [], $locale));
2018-07-20 14:34:56 +02:00
$result = [
2024-01-01 14:13:49 +01:00
'daily' => ['label' => (string) trans('firefly.recurring_daily'), 'selected' => str_starts_with($preSelected, 'daily')],
2022-12-29 19:41:57 +01:00
$weekly => [
2024-01-01 14:13:49 +01:00
'label' => (string) trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]),
2022-12-29 19:41:57 +01:00
'selected' => str_starts_with($preSelected, 'weekly'),
],
$monthly => [
2024-01-01 14:13:49 +01:00
'label' => (string) trans('firefly.recurring_monthly', ['dayOfMonth' => $date->day]),
2022-12-29 19:41:57 +01:00
'selected' => str_starts_with($preSelected, 'monthly'),
],
$ndom => [
2024-01-01 14:13:49 +01:00
'label' => (string) trans('firefly.recurring_ndom', ['weekday' => $dayOfWeek, 'dayOfMonth' => $date->weekOfMonth]),
2022-12-29 19:41:57 +01:00
'selected' => str_starts_with($preSelected, 'ndom'),
],
$yearly => [
2024-01-01 14:13:49 +01:00
'label' => (string) trans('firefly.recurring_yearly', ['date' => $yearlyDate]),
2022-12-29 19:41:57 +01:00
'selected' => str_starts_with($preSelected, 'yearly'),
],
2018-07-20 14:34:56 +02:00
];
}
2023-10-29 06:33:43 +01:00
app('log')->debug('Dropdown is', $result);
2021-03-28 11:46:23 +02:00
2018-07-20 14:34:56 +02:00
return response()->json($result);
}
}