Skip to content

Commit

Permalink
Finish character scheduling rule interface
Browse files Browse the repository at this point in the history
  • Loading branch information
recursivetree committed Aug 19, 2024
1 parent 57dbb4f commit 569f429
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 9 deletions.
50 changes: 48 additions & 2 deletions src/Http/Controllers/Configuration/ScheduleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
namespace Seat\Web\Http\Controllers\Configuration;

use Artisan;
use Illuminate\Http\Request;
use Seat\Services\Models\Schedule;
use Seat\Web\Http\Controllers\Controller;
use Seat\Web\Http\Validation\NewSchedule;
use Seat\Web\Models\CharacterSchedulingRule;

/**
* Class ScheduleController.
Expand All @@ -52,11 +54,12 @@ public function listSchedule()
'every five minutes' => '*/5 * * * *',
'every ten minutes' => '*/10 * * * *',
'every thirty minutes' => '*/30 * * * *',

];

$scheduling_rules = CharacterSchedulingRule::all();

return view('web::configuration.schedule.view',
compact('schedule', 'commands', 'expressions'));
compact('schedule', 'commands', 'expressions', 'scheduling_rules'));
}

/**
Expand Down Expand Up @@ -85,4 +88,47 @@ public function deleteSchedule(int $schedule_id)
return redirect()->back()
->with('success', 'Schedule entry deleted!');
}

public function createSchedulingRule(Request $request)
{
$request->validate([
'filters' => 'required|json',
'name' => 'required|string',
'time' => 'required|numeric',
'timeunit' => 'required|in:hour,day,week'
]);

// $time_modifier: conversion factor from timeunit to seconds
if($request->timeunit === 'hour') {
$time_modifier = 60 * 60;
} elseif ($request->timeunit === 'day') {
$time_modifier = 60 * 60 * 24;
} elseif ($request->timeunit === 'week') {
$time_modifier = 60 * 60 * 24 * 7;
}
$time = $request->time * $time_modifier;

$rule = CharacterSchedulingRule::where('name', $request->name)->first();
if($rule === null) {
$rule = new CharacterSchedulingRule();
$rule->name = $request->name;
}
$rule->interval = $time;
$rule->filter = $request->filters;
$rule->save();

return redirect()->back()
->with('success', 'Character Scheduling Rule added!');
}

public function deleteSchedulingRule(Request $request)
{
$request->validate([
'rule_id' => 'required|numeric'
]);

CharacterSchedulingRule::destroy($request->rule_id);

return redirect()->back()->with('success','Successfully removed character scheduling rule!');
}
}
4 changes: 4 additions & 0 deletions src/Http/Routes/Configuration/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@
Route::post('/rules/create')
->name('seatcore::configuration.schedule.rule.create')
->uses('ScheduleController@createSchedulingRule');

Route::post('/rules/delete')
->name('seatcore::configuration.schedule.rule.delete')
->uses('ScheduleController@deleteSchedulingRule');
42 changes: 42 additions & 0 deletions src/Models/CharacterSchedulingRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of SeAT
*
* Copyright (C) 2015 to present Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

namespace Seat\Web\Models;

use Carbon\Carbon;
use Seat\Eveapi\Models\RefreshToken;
use Seat\Eveapi\Models\RefreshTokenSchedule;
use Seat\Services\Models\ExtensibleModel;

/**
* @property int $id
* @property string name
* @property string filter
* @property int interval
*/
class CharacterSchedulingRule extends ExtensibleModel
{
/**
* @var bool
*/
public $timestamps = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public function up()
{
Schema::create('character_scheduling_rules', function (Blueprint $table) {
$table->bigIncrements('id')->primary();
$table->bigIncrements('id');
$table->string("name");
$table->integer("interval")->unsigned();
$table->json("filter");
Expand Down
4 changes: 4 additions & 0 deletions src/resources/lang/en/seat.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
'hour' => 'hour',
'week' => 'week',
'day' => 'day',
'save' => 'Save',

// Requirements
'requirements' => 'Requirements',
Expand Down Expand Up @@ -379,10 +380,13 @@
'scheduled_commands' => 'Scheduled Commands',
'choose_prepop' => 'Choose a pre-populated cron expression, or write your own.',
'add_scheduled' => 'Add Scheduled Command',
'character_scheduling_rule'=>'character scheduling rule',
'character_scheduling_rules' => 'Character Scheduling Rules',
'new_character_scheduling_rule' => 'New Character Scheduling Rule',
'update_interval' => 'Update Interval',
'name_input_placeholder' => 'Enter a name...',
'character_scheduling_rules_empty' => 'There are no character scheduling rules defined, using a default of one hour for everyone.',
'character_scheduling_rules_default' => 'When no rules apply to a character, an update interval of one hour is used.',


// Security
Expand Down
52 changes: 46 additions & 6 deletions src/resources/views/configuration/schedule/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class="btn btn-danger btn-sm confirmlink">
<h3 class="card-title">{{ trans('web::seat.new_character_scheduling_rule') }}</h3>
</div>
<div class="card-body">
<form action="{{ route('seatcore::configuration.schedule.rule.create') }}" method="POST">
<form action="{{ route('seatcore::configuration.schedule.rule.create') }}" method="POST" id="rule-form">
@csrf

<div class="form-group">
Expand All @@ -150,7 +150,7 @@ class="btn btn-danger btn-sm confirmlink">
<label for="time">{{ trans('web::seat.update_interval') }}</label>
<div class="row mx-0">
<input class="form-control col-md-9" type="number" name="time" value="1" min="1" step="0.01" id="time">
<select name="timeunit" class="form-control col-md-3">
<select name="timeunit" class="form-control col-md-3" id="timeunit">
<option selected value="hour">{{trans('web::seat.hour')}}</option>
<option value="day">{{trans('web::seat.day')}}</option>
<option value="week">{{trans('web::seat.week')}}</option>
Expand All @@ -166,7 +166,7 @@ class="btn btn-danger btn-sm confirmlink">

<button type="submit" class="btn btn-success float-right">
<i class="fas fa-plus-square"></i>
{{trans('web::seat.add')}}
{{trans('web::seat.save')}}
</button>
</form>
</div>
Expand All @@ -184,17 +184,43 @@ class="btn btn-danger btn-sm confirmlink">
<tr>
<th>{{trans_choice('web::seat.rule', 1)}}</th>
<th>{{trans('web::seat.update_interval')}}</th>
<th>{{trans('web::seat.action')}}</th>
<th class="text-right">{{trans('web::seat.action')}}</th>
</tr>
</thead>
<tbody>

@foreach($scheduling_rules as $scheduling_rule)
<tr>
<td>{{ $scheduling_rule->name }}</td>
<td>{{ \Carbon\CarbonInterval::seconds($scheduling_rule->interval)->cascade() }}</td>
<td class="d-flex flex-row align-items-center justify-content-end">
<button type="button" class="btn btn-success btn-sm btn-edit-rule mx-2" data-filter="{{$scheduling_rule->filter}}" data-name="{{$scheduling_rule->name}}" data-interval="{{$scheduling_rule->interval}}">{{ trans('web::seat.edit') }}</button>

<form action="{{ route('seatcore::configuration.schedule.rule.delete') }}" method="POST">
@csrf
<input type="hidden" name="rule_id" value="{{$scheduling_rule->id}}">
<button type="submit" class="btn btn-danger btn-sm confirmdelete" data-seat-entity="{{trans('web::seat.character_scheduling_rule')}}">{{ trans('web::seat.delete') }}</button>
</form>
</td>
</tr>
@endforeach

<tr>
<td colspan="3" class="text-center">
@if($scheduling_rules->isEmpty())
{{trans('web::seat.character_scheduling_rules_empty')}}
@endif
</td>
</tr>
</tbody>
</table>
@if(!$scheduling_rules->isEmpty())
<p class="text-muted">
{{trans('web::seat.character_scheduling_rules_default')}}
</p>
@endif
</div>
</div>
</div>

</div>


Expand All @@ -220,4 +246,18 @@ class="btn btn-danger btn-sm confirmlink">
});
</script>

<script>
$('#rule-form').on('submit', function () {
$('input[name="filters"]').val(document.getElementById('filters-btn').dataset.filters);
});
$('.btn-edit-rule').on('click', function (){
$('#filters-btn').attr('data-filters',$(this).attr('data-filter'))
$('#rule-name').val($(this).data('name'))
$('#time').val($(this).data('interval')/3600)
$('#timeunit').val("hour")
$(this).blur()
})
</script>

@endpush

0 comments on commit 569f429

Please sign in to comment.