Skip to content

Commit

Permalink
Merge pull request #721 from owlchester/reminder-too-long
Browse files Browse the repository at this point in the history
Reminder: Too long
  • Loading branch information
ilestis authored Aug 3, 2023
2 parents 58cdbae + 856886b commit 66fe29e
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 6 deletions.
17 changes: 16 additions & 1 deletion app/Http/Controllers/CalendarController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use App\Sanitizers\CalendarSanitizer;
use App\Services\CalendarService;
use App\Traits\TreeControllerTrait;
use App\Http\Requests\ValidateReminderLength;
use App\Services\LengthValidatorService;

class CalendarController extends CrudController
{
Expand All @@ -23,6 +25,7 @@ class CalendarController extends CrudController
protected $module = 'calendars';

protected CalendarService $calendarService;
protected LengthValidatorService $lengthValidatorService;

/** @var string */
protected $model = \App\Models\Calendar::class;
Expand All @@ -36,10 +39,11 @@ class CalendarController extends CrudController
* CalendarController constructor.
* @param CalendarService $calendarService
*/
public function __construct(CalendarService $calendarService)
public function __construct(CalendarService $calendarService, LengthValidatorService $lengthValidatorService)
{
parent::__construct();
$this->calendarService = $calendarService;
$this->lengthValidatorService = $lengthValidatorService;
}

/**
Expand Down Expand Up @@ -205,4 +209,15 @@ public function today(Calendar $calendar)
return redirect()->back()
->with('success', __('calendars.edit.today'));
}

/**
* @param Calendar $calendar
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function eventLength(Calendar $calendar, ValidateReminderLength $request)
{
$this->authorize('view', $calendar);
return response()->json($this->lengthValidatorService->validateLength($calendar, $request));
}
}
33 changes: 33 additions & 0 deletions app/Http/Requests/ValidateReminderLength.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ValidateReminderLength extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'day' => 'integer|nullable',
'month' => 'required|integer',
'year' => 'required|integer',
'length' => 'required|integer',
];
}
}
40 changes: 40 additions & 0 deletions app/Services/LengthValidatorService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Services;

use App\Models\Calendar;
use App\Http\Requests\ValidateReminderLength as Request;

class LengthValidatorService
{
/**
* @return array
*/
public function validateLength(Calendar $calendar, Request $request)
{
$day = $request['day'] ?? 0;
$month = $request['month'];
$length = $request['length'];

$daysInYear = $calendar->daysInYear();
$counter = 0;
$monthLength = 0;
foreach ($calendar->monthDataProperties() as $monthData) {
$counter = $counter + 1;
if ($counter >= $month) {
$monthLength = $monthLength + $monthData['data-length'];
}
}
$totalLength = $monthLength - $day + $daysInYear;
if ($length >= $totalLength) {
return [
'overflow' => true,
'message' => __('calendars.warnings.event_length', ['documentation' => link_to('https://docs.kanka.io/en/latest/entities/calendars.html#long-lasting-reminders', '<i class="fa-solid fa-external-link" aria-hidden="true"></i> ' . __('front.menu.documentation'), ['target' => '_blank'], null, false)]),
];
}
return [
'overflow' => false,
'message' => __('calendars.warnings.event_length', ['documentation' => link_to('https://docs.kanka.io/en/latest/entities/calendars.html#long-lasting-reminders', '<i class="fa-solid fa-external-link" aria-hidden="true"></i> ' . __('front.menu.documentation'), ['target' => '_blank'], null, false)]),
];
}
}
3 changes: 3 additions & 0 deletions lang/en/calendars.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,7 @@
'moon_offset' => 'The moon first fullmoon offset can\'t be bigger than the length of the calendar\'s first month.',
'format' => 'The Calendar date format is invalid.',
],
'warnings' => [
'event_length' => 'Reminders that span multiple years are only visible on the first two years. Learn more in our :documentation .',
],
];
1 change: 1 addition & 0 deletions public/build/assets/app-bac3247a.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion public/build/assets/app-bc1d87fc.js

This file was deleted.

2 changes: 1 addition & 1 deletion public/build/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"src": "resources/js/api.js"
},
"resources/js/app.js": {
"file": "assets/app-bc1d87fc.js",
"file": "assets/app-bac3247a.js",
"imports": [
"_delete-confirm-fec6aa52.js",
"_mention-5d95822d.js",
Expand Down
26 changes: 24 additions & 2 deletions resources/js/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function registerModalLoad() {
$('#campaign-delete-form').focus();
});
}

//this one
function registerEntityNameCheck() {
if (entityName.data('live-disabled')) {
return;
Expand Down Expand Up @@ -243,7 +243,7 @@ function registerEntityCalendarModal() {
return;
}
// Load month list
loadCalendarDates(entityCalendarField.val());
loadCalendarDates(entityCalendarField.val());
});

//var defaultCalendarId = entityCalendarAdd.data('default-calendar');
Expand All @@ -252,6 +252,28 @@ function registerEntityCalendarModal() {
entityCalendarSubForm.fadeIn();
loadCalendarDates(entityCalendarField.val());
}

$('.entity-calendar-subform input[name="length"]').focusout(function () {
if (!$(this).val()) {
return;
}
let url = $(this).data('url').replace('/0/', '/' + entityCalendarField.val() + '/')

let params = {
day: entityCalendarDayField.val(),
month: entityCalendarMonthField.val(),
year: entityCalendarYearField.val(),
length: $(this).val(),
}

$.ajax(url, {data: params}).done(function (data) {
if (data.overflow == true) {
$('.length-warning').show();
} else {
$('.length-warning').hide();
}
});
});
}


Expand Down
5 changes: 4 additions & 1 deletion resources/views/calendars/events/_subform.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
{{ __('calendars.fields.length') }}
<i class="fa-solid fa-question-circle hidden-xs hidden-sm" data-toggle="tooltip" title="{{ __('calendars.hints.event_length') }}"></i>
</label>
{!! Form::number('length', (empty($entityEvent) ? 1 : null), ['placeholder' => __('calendars.placeholders.length'), 'class' => 'form-control', 'maxlength' => 1]) !!}
{!! Form::number('length', (empty($entityEvent) ? 1 : null), ['placeholder' => __('calendars.placeholders.length'), 'class' => 'form-control', 'maxlength' => 1, 'data-url' => route('calendars.event-length', ['calendar' => 0])]) !!}

<p class="help-block hidden-md hidden-lg">{{ __('calendars.hints.event_length') }}</p>
<p class="length-warning hidden text-red">
{!! __('calendars.warnings.event_length', ['documentation' => link_to('https://docs.kanka.io/en/latest/entities/calendars.html#long-lasting-reminders', '<i class="fa-solid fa-external-link" aria-hidden="true"></i> ' . __('front.menu.documentation'), ['target' => '_blank'], null, false)])!!}
</p>

</div>

Expand Down
1 change: 1 addition & 0 deletions routes/campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
Route::get('/calendars/{calendar}/month-list', 'CalendarController@monthList')->name('calendars.month-list');
Route::get('/calendars/{calendar}/events', 'CalendarController@events')->name('calendars.events');
Route::get('/calendars/{calendar}/today', 'CalendarController@today')->name('calendars.today');
Route::get('/calendars/{calendar}/validate-length', [\App\Http\Controllers\CalendarController::class, 'eventLength'])->name('calendars.event-length');

// Route::get('/calendars/{calendar}/weather', 'Calendar\CalendarWeatherController@form')->name('calendars.weather.create');
// Route::post('/calendars/{calendar}/weather', 'Calendar\CalendarWeatherController@store')->name('calendars.weather.store');
Expand Down

0 comments on commit 66fe29e

Please sign in to comment.