Add task toggle and delete

This commit is contained in:
Dane Everitt 2016-09-05 17:13:22 -04:00
parent 91ad9b3eaa
commit 9b4a0ed143
4 changed files with 174 additions and 2 deletions

View file

@ -27,6 +27,7 @@ use Alert;
use Log; use Log;
use Cron; use Cron;
use Pterodactyl\Repositories;
use Pterodactyl\Models; use Pterodactyl\Models;
use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException; use Pterodactyl\Exceptions\DisplayValidationException;
@ -84,4 +85,54 @@ class TaskController extends Controller
'task' => Models\Task::where('id', $id)->where('server', $server->id)->firstOrFail() 'task' => Models\Task::where('id', $id)->where('server', $server->id)->firstOrFail()
]); ]);
} }
public function deleteTask(Request $request, $uuid, $id)
{
$server = Models\Server::getByUUID($uuid);
$this->authorize('delete-task', $server);
$task = Models\Task::findOrFail($id);
if (!$task || $server->id !== $task->server) {
return response()->json([
'error' => 'No task by that ID was found associated with this server.'
], 404);
}
try {
$repo = new Repositories\TaskRepository;
$repo->delete($id);
return response()->json([], 204);
} catch (\Exception $ex) {
return response()->json([
'error' => 'A server error occured while attempting to delete this task.'
], 503);
}
}
public function toggleTask(Request $request, $uuid, $id)
{
$server = Models\Server::getByUUID($uuid);
$this->authorize('toggle-task', $server);
$task = Models\Task::findOrFail($id);
if (!$task || $server->id !== $task->server) {
return response()->json([
'error' => 'No task by that ID was found associated with this server.'
], 404);
}
try {
$repo = new Repositories\TaskRepository;
$resp = $repo->toggle($id);
return response()->json([
'status' => $resp
]);
} catch (\Exception $ex) {
return response()->json([
'error' => 'A server error occured while attempting to toggle this task.'
], 503);
}
}
} }

View file

@ -141,6 +141,11 @@ class ServerRoutes {
'uses' => 'Server\TaskController@deleteTask' 'uses' => 'Server\TaskController@deleteTask'
]); ]);
$router->post('tasks/toggle/{id}', [
'as' => 'server.tasks.toggle',
'uses' => 'Server\TaskController@toggleTask'
]);
// Assorted AJAX Routes // Assorted AJAX Routes
$router->group(['prefix' => 'ajax'], function ($server) use ($router) { $router->group(['prefix' => 'ajax'], function ($server) use ($router) {
// Returns Server Status // Returns Server Status

View file

@ -53,6 +53,43 @@ class TaskRepository
// //
} }
/**
* Deletes a given task.
* @param int $id
*
* @return bool
*/
public function delete($id)
{
$task = Models\Task::findOrFail($id);
try {
$task->delete();
return true;
} catch (\Exception $ex) {
throw $ex;
}
}
/**
* Toggles a task active or inactive.
* @param int $id
*
* @return int
*/
public function toggle($id)
{
$task = Models\Task::findOrFail($id);
try {
$task->active = ($task->active === 1) ? 0 : 1;
$task->queued = 0;
$task->save();
return $task->active;
} catch (\Exception $ex) {
throw $ex;
}
}
/** /**
* Create a new scheduled task for a given server. * Create a new scheduled task for a given server.
* @param int $id * @param int $id

View file

@ -51,10 +51,10 @@
@endif @endif
</td> </td>
@can('delete-task', $server) @can('delete-task', $server)
<td class="text-center text-v-center"><a href="#" data-action="delete-task" data-id="{{ $task->id }}"><i class="fa fa-fw fa-trash-o text-danger"></i></a></td> <td class="text-center text-v-center"><a href="#" data-action="delete-task" data-id="{{ $task->id }}"><i class="fa fa-fw fa-trash-o text-danger" data-toggle="tooltip" data-placement="top" title="Delete"></i></a></td>
@endcan @endcan
@can('toggle-task', $server) @can('toggle-task', $server)
<td class="text-center text-v-center"><a href="#" data-action="toggle-task" data-id="{{ $task->id }}"><i class="fa fa-fw fa-eye-slash text-primary"></i></a></td> <td class="text-center text-v-center"><a href="#" data-action="toggle-task" data-active="{{ $task->active }}" data-id="{{ $task->id }}"><i class="fa fa-fw fa-eye-slash text-primary" data-toggle="tooltip" data-placement="top" title="Toggle Status"></i></a></td>
@endcan @endcan
</tr> </tr>
@ -68,6 +68,85 @@
<script> <script>
$(document).ready(function () { $(document).ready(function () {
$('.server-tasks').addClass('active'); $('.server-tasks').addClass('active');
$('[data-toggle="tooltip"]').tooltip();
$('[data-action="delete-task"]').click(function (event) {
var self = $(this);
swal({
type: 'error',
title: 'Delete Task?',
text: 'Are you sure you want to delete this task? There is no undo.',
showCancelButton: true,
allowOutsideClick: true,
closeOnConfirm: false,
confirmButtonText: 'Delete Task',
confirmButtonColor: '#d9534f',
showLoaderOnConfirm: true
}, function () {
$.ajax({
method: 'DELETE',
url: '{{ route('server.tasks', $server->uuidShort) }}/delete/' + self.data('id'),
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
}).done(function (data) {
swal({
type: 'success',
title: '',
text: 'Task has been deleted.'
});
self.parent().parent().slideUp();
}).fail(function (jqXHR) {
console.error(jqXHR);
swal({
type: 'error',
title: 'Whoops!',
text: 'An error occured while attempting to delete this task.'
});
});
});
});
$('[data-action="toggle-task"]').click(function (event) {
var self = $(this);
swal({
type: 'info',
title: 'Toggle Task',
text: 'This will toggle the selected task.',
showCancelButton: true,
allowOutsideClick: true,
closeOnConfirm: false,
confirmButtonText: 'Continue',
showLoaderOnConfirm: true
}, function () {
$.ajax({
method: 'POST',
url: '{{ route('server.tasks', $server->uuidShort) }}/toggle/' + self.data('id'),
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
}
}).done(function (data) {
swal({
type: 'success',
title: '',
text: 'Task has been toggled.'
});
if (data.status !== 1) {
self.parent().parent().addClass('text-disabled');
} else {
self.parent().parent().removeClass('text-disabled');
}
}).fail(function (jqXHR) {
console.error(jqXHR);
swal({
type: 'error',
title: 'Whoops!',
text: 'An error occured while attempting to toggle this task.'
});
});
});
});
}); });
</script> </script>
@endsection @endsection