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

197 lines
7.8 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;
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;
2020-01-25 06:17:17 +01:00
use Log;
2022-12-29 19:41:57 +01:00
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
2018-07-20 14:34:56 +02:00
/**
* 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.
2020-01-25 06:17:17 +01:00
*
2019-06-29 19:47:31 +02:00
* @codeCoverageIgnore
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.
*
2022-12-29 19:41:57 +01:00
* @param Request $request
2018-07-20 14:34:56 +02:00
*
* @return JsonResponse
2019-08-17 10:47:10 +02: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
{
2022-12-30 09:28:03 +01:00
$occurrences = [];
2018-07-20 14:34:56 +02:00
$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'));
2022-12-29 19:41:57 +01:00
$endDate = '' !== (string)$request->get('end_date') ? Carbon::createFromFormat('Y-m-d', $request->get('end_date')) : null;
$endsAt = (string)$request->get('ends');
2018-07-20 14:34:56 +02:00
$repetitionType = explode(',', $request->get('type'))[0];
2022-12-29 19:41:57 +01:00
$repetitions = (int)$request->get('reps');
2018-07-20 14:34:56 +02:00
$repetitionMoment = '';
$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-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;
2022-12-29 19:41:57 +01:00
$repetition->repetition_skip = (int)$request->get('skip');
$repetition->weekend = (int)$request->get('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 = [
2022-12-29 19:41:57 +01:00
'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.
*
2022-12-29 19:41:57 +01:00
* @param Request $request
2018-07-20 14:34:56 +02:00
*
* @return JsonResponse
2022-03-29 15:10:05 +02:00
* @throws FireflyException
2022-12-29 19:41:57 +01:00
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
2018-07-20 14:34:56 +02:00
*/
public function suggest(Request $request): JsonResponse
{
2023-01-17 05:40:56 +01:00
$request->validate(['date' => ['required', 'date'],]);
2020-01-25 06:17:17 +01:00
$string = $request->get('date') ?? date('Y-m-d');
2023-02-11 07:36:45 +01:00
$today = today(config('app.timezone'))->startOfDay();
$date = Carbon::createFromFormat('Y-m-d', $string)->startOfDay();
2022-12-29 19:41:57 +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
Log::debug(sprintf('date = %s, today = %s. date > today? %s', $date->toAtomString(), $today->toAtomString(), var_export($date > $today, true)));
2022-12-29 19:41:57 +01:00
Log::debug(sprintf('past = true? %s', var_export('true' === (string)$request->get('past'), true)));
2020-01-25 06:17:17 +01:00
$result = [];
2022-12-29 19:41:57 +01:00
if ($date > $today || 'true' === (string)$request->get('past')) {
2020-01-25 06:17:17 +01:00
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);
2022-12-29 19:41:57 +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'));
2022-12-29 19:41:57 +01:00
$yearlyDate = $date->isoFormat((string)trans('config.month_and_day_no_year_js', [], $locale));
2018-07-20 14:34:56 +02:00
$result = [
2022-12-29 19:41:57 +01:00
'daily' => ['label' => (string)trans('firefly.recurring_daily'), 'selected' => str_starts_with($preSelected, 'daily')],
$weekly => [
'label' => (string)trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]),
'selected' => str_starts_with($preSelected, 'weekly'),
],
$monthly => [
'label' => (string)trans('firefly.recurring_monthly', ['dayOfMonth' => $date->day]),
'selected' => str_starts_with($preSelected, 'monthly'),
],
$ndom => [
'label' => (string)trans('firefly.recurring_ndom', ['weekday' => $dayOfWeek, 'dayOfMonth' => $date->weekOfMonth]),
'selected' => str_starts_with($preSelected, 'ndom'),
],
$yearly => [
'label' => (string)trans('firefly.recurring_yearly', ['date' => $yearlyDate]),
'selected' => str_starts_with($preSelected, 'yearly'),
],
2018-07-20 14:34:56 +02:00
];
}
2020-01-25 06:17:17 +01:00
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);
}
}