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

186 lines
7.5 KiB
PHP
Raw Normal View History

2018-07-20 07:34:56 -05:00
<?php
/**
* RecurrenceController.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-20 07:34:56 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-20 07:34:56 -05: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 07:34:56 -05:00
*
* This program is distributed in the hope that it will be useful,
2018-07-20 07:34:56 -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.
2018-07-20 07:34:56 -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/>.
2018-07-20 07:34:56 -05: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-24 23:17:17 -06:00
use Log;
2018-07-20 07:34:56 -05:00
/**
* Class RecurrenceController
*/
class RecurrenceController extends Controller
{
2018-07-21 01:06:24 -05:00
/** @var RecurringRepositoryInterface The recurring repository. */
2018-07-20 07:34:56 -05:00
private $recurring;
/**
2018-07-21 01:06:24 -05:00
* RecurrenceController constructor.
2020-01-24 23:17:17 -06:00
*
2019-06-29 12:47:31 -05:00
* @codeCoverageIgnore
2018-07-20 07:34:56 -05:00
*/
public function __construct()
{
parent::__construct();
// translations:
$this->middleware(
function ($request, $next) {
$this->recurring = app(RecurringRepositoryInterface::class);
return $next($request);
}
);
}
/**
2018-07-21 01:06:24 -05:00
* Shows all events for a repetition. Used in calendar.
*
2018-07-20 07:34:56 -05:00
* @param Request $request
*
* @throws FireflyException
2018-07-20 07:34:56 -05:00
* @return JsonResponse
2019-08-17 03:47:10 -05:00
*
2018-07-20 07:34:56 -05:00
*/
public function events(Request $request): JsonResponse
{
$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');
2018-07-20 07:34:56 -05:00
$repetitionType = explode(',', $request->get('type'))[0];
$repetitions = (int) $request->get('reps');
2018-07-20 07:34:56 -05:00
$repetitionMoment = '';
$start->startOfDay();
// if $firstDate is beyond $end, simply return an empty array.
if ($firstDate->gt($end)) {
2019-02-13 10:38:41 -06:00
return response()->json();
2018-07-20 07:34:56 -05:00
}
// if $firstDate is beyond start, use that one:
$actualStart = clone $firstDate;
2018-07-22 01:10:16 -05:00
if ('weekly' === $repetitionType || 'monthly' === $repetitionType) {
2018-07-20 07:34:56 -05:00
$repetitionMoment = explode(',', $request->get('type'))[1] ?? '1';
}
2018-07-22 01:10:16 -05:00
if ('ndom' === $repetitionType) {
2018-07-20 07:34:56 -05:00
$repetitionMoment = str_ireplace('ndom,', '', $request->get('type'));
}
2018-07-22 01:10:16 -05:00
if ('yearly' === $repetitionType) {
2018-07-20 07:34:56 -05:00
$repetitionMoment = explode(',', $request->get('type'))[1] ?? '2018-01-01';
}
$repetition = new RecurrenceRepetition;
$repetition->repetition_type = $repetitionType;
$repetition->repetition_moment = $repetitionMoment;
$repetition->repetition_skip = (int) $request->get('skip');
$repetition->weekend = (int) $request->get('weekend');
2018-07-20 07:34:56 -05:00
$actualEnd = clone $end;
2018-08-31 14:12:53 -05:00
$occurrences = [];
2018-07-20 07:34:56 -05:00
switch ($endsAt) {
case 'forever':
// simply generate up until $end. No change from default behavior.
$occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd);
break;
case 'until_date':
$actualEnd = $endDate ?? clone $end;
$occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd);
break;
case 'times':
$occurrences = $this->recurring->getXOccurrences($repetition, $actualStart, $repetitions);
break;
}
/** @var Carbon $current */
foreach ($occurrences as $current) {
if ($current->gte($start)) {
$event = [
'id' => $repetitionType . $firstDate->format('Ymd'),
'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 01:06:24 -05:00
* Suggests repetition moments.
*
2018-07-20 07:34:56 -05:00
* @param Request $request
*
* @return JsonResponse
*/
public function suggest(Request $request): JsonResponse
{
2020-01-24 23:17:17 -06:00
$string = $request->get('date') ?? date('Y-m-d');
2020-01-27 12:37:22 -06:00
$today = Carbon::now()->startOfDay();
$date = Carbon::createFromFormat('Y-m-d', $string)->startOfDay();
$preSelected = (string) $request->get('pre_select');
$locale = app('steam')->getLocale();
2020-01-24 23:17:17 -06:00
Log::debug(sprintf('date = %s, today = %s. date > today? %s', $date->toAtomString(), $today->toAtomString(), var_export($date > $today, true)));
Log::debug(sprintf('past = true? %s', var_export('true' === (string) $request->get('past'), true)));
2020-01-24 23:17:17 -06:00
$result = [];
if ($date > $today || 'true' === (string) $request->get('past')) {
2020-01-24 23:17:17 -06:00
Log::debug('Will fill dropdown.');
2018-07-20 07:34:56 -05:00
$weekly = sprintf('weekly,%s', $date->dayOfWeekIso);
$monthly = sprintf('monthly,%s', $date->day);
$dayOfWeek = (string) trans(sprintf('config.dow_%s', $date->dayOfWeekIso));
2018-07-20 07:34:56 -05:00
$ndom = sprintf('ndom,%s,%s', $date->weekOfMonth, $date->dayOfWeekIso);
$yearly = sprintf('yearly,%s', $date->format('Y-m-d'));
$yearlyDate = $date->formatLocalized((string) trans('config.month_and_day_no_year', [], $locale));
2018-07-20 07:34:56 -05:00
$result = [
'daily' => ['label' => (string) trans('firefly.recurring_daily'), 'selected' => 0 === strpos($preSelected, 'daily')],
$weekly => ['label' => (string) trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]),
2018-07-20 07:34:56 -05:00
'selected' => 0 === strpos($preSelected, 'weekly')],
$monthly => ['label' => (string) trans('firefly.recurring_monthly', ['dayOfMonth' => $date->day]),
2018-07-20 07:34:56 -05:00
'selected' => 0 === strpos($preSelected, 'monthly')],
$ndom => ['label' => (string) trans('firefly.recurring_ndom', ['weekday' => $dayOfWeek, 'dayOfMonth' => $date->weekOfMonth]),
2018-07-20 07:34:56 -05:00
'selected' => 0 === strpos($preSelected, 'ndom')],
$yearly => ['label' => (string) trans('firefly.recurring_yearly', ['date' => $yearlyDate]),
'selected' => 0 === strpos($preSelected, 'yearly')],
2018-07-20 07:34:56 -05:00
];
}
2020-01-24 23:17:17 -06:00
Log::debug('Dropdown is', $result);
2018-07-20 07:34:56 -05:00
return response()->json($result);
}
}