1 | <?php |
||
2 | /** |
||
3 | * RecurrenceController.php |
||
4 | * Copyright (c) 2018 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | |||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Http\Controllers\Json; |
||
25 | |||
26 | |||
27 | use Carbon\Carbon; |
||
28 | use FireflyIII\Exceptions\FireflyException; |
||
29 | use FireflyIII\Http\Controllers\Controller; |
||
30 | use FireflyIII\Models\RecurrenceRepetition; |
||
31 | use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface; |
||
32 | use Illuminate\Http\JsonResponse; |
||
33 | use Illuminate\Http\Request; |
||
34 | |||
35 | /** |
||
36 | * Class RecurrenceController |
||
37 | */ |
||
38 | class RecurrenceController extends Controller |
||
39 | { |
||
40 | /** @var RecurringRepositoryInterface The recurring repository. */ |
||
41 | private $recurring; |
||
42 | |||
43 | /** |
||
44 | * RecurrenceController constructor. |
||
45 | */ |
||
46 | public function __construct() |
||
47 | { |
||
48 | parent::__construct(); |
||
49 | |||
50 | // translations: |
||
51 | $this->middleware( |
||
52 | function ($request, $next) { |
||
53 | $this->recurring = app(RecurringRepositoryInterface::class); |
||
54 | |||
55 | return $next($request); |
||
56 | } |
||
57 | ); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Shows all events for a repetition. Used in calendar. |
||
62 | * |
||
63 | * @param Request $request |
||
64 | * |
||
65 | * @throws FireflyException |
||
66 | * @return JsonResponse |
||
67 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
68 | * @SuppressWarnings(PHPMD.NPathComplexity) |
||
69 | * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
||
70 | */ |
||
71 | public function events(Request $request): JsonResponse |
||
72 | { |
||
73 | $return = []; |
||
74 | $start = Carbon::createFromFormat('Y-m-d', $request->get('start')); |
||
75 | $end = Carbon::createFromFormat('Y-m-d', $request->get('end')); |
||
76 | $firstDate = Carbon::createFromFormat('Y-m-d', $request->get('first_date')); |
||
77 | $endDate = '' !== (string)$request->get('end_date') ? Carbon::createFromFormat('Y-m-d', $request->get('end_date')) : null; |
||
78 | $endsAt = (string)$request->get('ends'); |
||
79 | $repetitionType = explode(',', $request->get('type'))[0]; |
||
80 | $repetitions = (int)$request->get('reps'); |
||
81 | $repetitionMoment = ''; |
||
82 | $start->startOfDay(); |
||
83 | |||
84 | // if $firstDate is beyond $end, simply return an empty array. |
||
85 | if ($firstDate->gt($end)) { |
||
86 | return response()->json(); |
||
87 | } |
||
88 | // if $firstDate is beyond start, use that one: |
||
89 | $actualStart = clone $firstDate; |
||
90 | |||
91 | if ('weekly' === $repetitionType || 'monthly' === $repetitionType) { |
||
92 | $repetitionMoment = explode(',', $request->get('type'))[1] ?? '1'; |
||
93 | } |
||
94 | if ('ndom' === $repetitionType) { |
||
95 | $repetitionMoment = str_ireplace('ndom,', '', $request->get('type')); |
||
96 | } |
||
97 | if ('yearly' === $repetitionType) { |
||
98 | $repetitionMoment = explode(',', $request->get('type'))[1] ?? '2018-01-01'; |
||
99 | } |
||
100 | $repetition = new RecurrenceRepetition; |
||
101 | $repetition->repetition_type = $repetitionType; |
||
102 | $repetition->repetition_moment = $repetitionMoment; |
||
103 | $repetition->repetition_skip = (int)$request->get('skip'); |
||
104 | $repetition->weekend = (int)$request->get('weekend'); |
||
105 | $actualEnd = clone $end; |
||
106 | $occurrences = []; |
||
107 | switch ($endsAt) { |
||
108 | case 'forever': |
||
109 | // simply generate up until $end. No change from default behavior. |
||
110 | $occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd); |
||
111 | break; |
||
112 | case 'until_date': |
||
113 | $actualEnd = $endDate ?? clone $end; |
||
114 | $occurrences = $this->recurring->getOccurrencesInRange($repetition, $actualStart, $actualEnd); |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
115 | break; |
||
116 | case 'times': |
||
117 | $occurrences = $this->recurring->getXOccurrences($repetition, $actualStart, $repetitions); |
||
118 | break; |
||
119 | } |
||
120 | |||
121 | |||
122 | /** @var Carbon $current */ |
||
123 | foreach ($occurrences as $current) { |
||
124 | if ($current->gte($start)) { |
||
125 | $event = [ |
||
126 | 'id' => $repetitionType . $firstDate->format('Ymd'), |
||
127 | 'title' => 'X', |
||
128 | 'allDay' => true, |
||
129 | 'start' => $current->format('Y-m-d'), |
||
130 | 'end' => $current->format('Y-m-d'), |
||
131 | 'editable' => false, |
||
132 | 'rendering' => 'background', |
||
133 | ]; |
||
134 | $return[] = $event; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | return response()->json($return); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Suggests repetition moments. |
||
143 | * |
||
144 | * @param Request $request |
||
145 | * |
||
146 | * @return JsonResponse |
||
147 | */ |
||
148 | public function suggest(Request $request): JsonResponse |
||
149 | { |
||
150 | $string = $request->get('date') ?? date('Y-m-d'); |
||
151 | $today = new Carbon; |
||
152 | $date = Carbon::createFromFormat('Y-m-d', $string); |
||
153 | $preSelected = (string)$request->get('pre_select'); |
||
154 | $result = []; |
||
155 | if ($date > $today || 'true' === (string)$request->get('past')) { |
||
156 | $weekly = sprintf('weekly,%s', $date->dayOfWeekIso); |
||
157 | $monthly = sprintf('monthly,%s', $date->day); |
||
158 | $dayOfWeek = (string)trans(sprintf('config.dow_%s', $date->dayOfWeekIso)); |
||
159 | $ndom = sprintf('ndom,%s,%s', $date->weekOfMonth, $date->dayOfWeekIso); |
||
160 | $yearly = sprintf('yearly,%s', $date->format('Y-m-d')); |
||
161 | $yearlyDate = $date->formatLocalized((string)trans('config.month_and_day_no_year')); |
||
162 | $result = [ |
||
163 | 'daily' => ['label' => (string)trans('firefly.recurring_daily'), 'selected' => 0 === strpos($preSelected, 'daily')], |
||
164 | $weekly => ['label' => (string)trans('firefly.recurring_weekly', ['weekday' => $dayOfWeek]), |
||
165 | 'selected' => 0 === strpos($preSelected, 'weekly')], |
||
166 | $monthly => ['label' => (string)trans('firefly.recurring_monthly', ['dayOfMonth' => $date->day]), |
||
167 | 'selected' => 0 === strpos($preSelected, 'monthly')], |
||
168 | $ndom => ['label' => (string)trans('firefly.recurring_ndom', ['weekday' => $dayOfWeek, 'dayOfMonth' => $date->weekOfMonth]), |
||
169 | 'selected' => 0 === strpos($preSelected, 'ndom')], |
||
170 | $yearly => ['label' => (string)trans('firefly.recurring_yearly', ['date' => $yearlyDate]), 'selected' => 0 === strpos($preSelected, 'yearly')], |
||
171 | ]; |
||
172 | } |
||
173 | |||
174 | |||
175 | return response()->json($result); |
||
176 | } |
||
177 | |||
178 | } |
||
179 |