Skip to content

Commit 9b4a0ed

Browse files
committed
Add task toggle and delete
1 parent 91ad9b3 commit 9b4a0ed

File tree

4 files changed

+174
-2
lines changed

4 files changed

+174
-2
lines changed

app/Http/Controllers/Server/TaskController.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Log;
2828
use Cron;
2929

30+
use Pterodactyl\Repositories;
3031
use Pterodactyl\Models;
3132
use Pterodactyl\Exceptions\DisplayException;
3233
use Pterodactyl\Exceptions\DisplayValidationException;
@@ -84,4 +85,54 @@ public function getView(Request $request, $uuid, $id)
8485
'task' => Models\Task::where('id', $id)->where('server', $server->id)->firstOrFail()
8586
]);
8687
}
88+
89+
public function deleteTask(Request $request, $uuid, $id)
90+
{
91+
$server = Models\Server::getByUUID($uuid);
92+
$this->authorize('delete-task', $server);
93+
94+
$task = Models\Task::findOrFail($id);
95+
96+
if (!$task || $server->id !== $task->server) {
97+
return response()->json([
98+
'error' => 'No task by that ID was found associated with this server.'
99+
], 404);
100+
}
101+
102+
try {
103+
$repo = new Repositories\TaskRepository;
104+
$repo->delete($id);
105+
return response()->json([], 204);
106+
} catch (\Exception $ex) {
107+
return response()->json([
108+
'error' => 'A server error occured while attempting to delete this task.'
109+
], 503);
110+
}
111+
}
112+
113+
public function toggleTask(Request $request, $uuid, $id)
114+
{
115+
$server = Models\Server::getByUUID($uuid);
116+
$this->authorize('toggle-task', $server);
117+
118+
$task = Models\Task::findOrFail($id);
119+
120+
if (!$task || $server->id !== $task->server) {
121+
return response()->json([
122+
'error' => 'No task by that ID was found associated with this server.'
123+
], 404);
124+
}
125+
126+
try {
127+
$repo = new Repositories\TaskRepository;
128+
$resp = $repo->toggle($id);
129+
return response()->json([
130+
'status' => $resp
131+
]);
132+
} catch (\Exception $ex) {
133+
return response()->json([
134+
'error' => 'A server error occured while attempting to toggle this task.'
135+
], 503);
136+
}
137+
}
87138
}

app/Http/Routes/ServerRoutes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ public function map(Router $router) {
141141
'uses' => 'Server\TaskController@deleteTask'
142142
]);
143143

144+
$router->post('tasks/toggle/{id}', [
145+
'as' => 'server.tasks.toggle',
146+
'uses' => 'Server\TaskController@toggleTask'
147+
]);
148+
144149
// Assorted AJAX Routes
145150
$router->group(['prefix' => 'ajax'], function ($server) use ($router) {
146151
// Returns Server Status

app/Repositories/TaskRepository.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,43 @@ public function __construct()
5353
//
5454
}
5555

56+
/**
57+
* Deletes a given task.
58+
* @param int $id
59+
*
60+
* @return bool
61+
*/
62+
public function delete($id)
63+
{
64+
$task = Models\Task::findOrFail($id);
65+
try {
66+
$task->delete();
67+
return true;
68+
} catch (\Exception $ex) {
69+
throw $ex;
70+
}
71+
}
72+
73+
/**
74+
* Toggles a task active or inactive.
75+
* @param int $id
76+
*
77+
* @return int
78+
*/
79+
public function toggle($id)
80+
{
81+
$task = Models\Task::findOrFail($id);
82+
try {
83+
$task->active = ($task->active === 1) ? 0 : 1;
84+
$task->queued = 0;
85+
$task->save();
86+
87+
return $task->active;
88+
} catch (\Exception $ex) {
89+
throw $ex;
90+
}
91+
}
92+
5693
/**
5794
* Create a new scheduled task for a given server.
5895
* @param int $id

resources/views/server/tasks/index.blade.php

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@
5151
@endif
5252
</td>
5353
@can('delete-task', $server)
54-
<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>
54+
<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>
5555
@endcan
5656
@can('toggle-task', $server)
57-
<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>
57+
<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>
5858
@endcan
5959

6060
</tr>
@@ -68,6 +68,85 @@
6868
<script>
6969
$(document).ready(function () {
7070
$('.server-tasks').addClass('active');
71+
$('[data-toggle="tooltip"]').tooltip();
72+
73+
$('[data-action="delete-task"]').click(function (event) {
74+
var self = $(this);
75+
swal({
76+
type: 'error',
77+
title: 'Delete Task?',
78+
text: 'Are you sure you want to delete this task? There is no undo.',
79+
showCancelButton: true,
80+
allowOutsideClick: true,
81+
closeOnConfirm: false,
82+
confirmButtonText: 'Delete Task',
83+
confirmButtonColor: '#d9534f',
84+
showLoaderOnConfirm: true
85+
}, function () {
86+
$.ajax({
87+
method: 'DELETE',
88+
url: '{{ route('server.tasks', $server->uuidShort) }}/delete/' + self.data('id'),
89+
headers: {
90+
'X-CSRF-TOKEN': '{{ csrf_token() }}'
91+
}
92+
}).done(function (data) {
93+
swal({
94+
type: 'success',
95+
title: '',
96+
text: 'Task has been deleted.'
97+
});
98+
self.parent().parent().slideUp();
99+
}).fail(function (jqXHR) {
100+
console.error(jqXHR);
101+
swal({
102+
type: 'error',
103+
title: 'Whoops!',
104+
text: 'An error occured while attempting to delete this task.'
105+
});
106+
});
107+
});
108+
});
109+
110+
$('[data-action="toggle-task"]').click(function (event) {
111+
var self = $(this);
112+
swal({
113+
type: 'info',
114+
title: 'Toggle Task',
115+
text: 'This will toggle the selected task.',
116+
showCancelButton: true,
117+
allowOutsideClick: true,
118+
closeOnConfirm: false,
119+
confirmButtonText: 'Continue',
120+
showLoaderOnConfirm: true
121+
}, function () {
122+
$.ajax({
123+
method: 'POST',
124+
url: '{{ route('server.tasks', $server->uuidShort) }}/toggle/' + self.data('id'),
125+
headers: {
126+
'X-CSRF-TOKEN': '{{ csrf_token() }}'
127+
}
128+
}).done(function (data) {
129+
swal({
130+
type: 'success',
131+
title: '',
132+
text: 'Task has been toggled.'
133+
});
134+
if (data.status !== 1) {
135+
self.parent().parent().addClass('text-disabled');
136+
} else {
137+
self.parent().parent().removeClass('text-disabled');
138+
}
139+
}).fail(function (jqXHR) {
140+
console.error(jqXHR);
141+
swal({
142+
type: 'error',
143+
title: 'Whoops!',
144+
text: 'An error occured while attempting to toggle this task.'
145+
});
146+
});
147+
});
148+
});
149+
71150
});
72151
</script>
73152
@endsection

0 commit comments

Comments
 (0)