Skip to content

Commit e7e2a1c

Browse files
committed
Added new import/export routes; Updated index.blade to show import/export options; Updated TasksController to export Tasks to json and allow importing json
1 parent 96f13ab commit e7e2a1c

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

resources/views/tasks/index.blade.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,21 @@
7171
@stop
7272
@section('main-panel-footer')
7373
<a class="uk-button uk-button-primary uk-button-small" href="{{route('totem.task.create')}}">New Task</a>
74+
<a class="uk-button uk-button-primary uk-button-small uk-float-right" href="{{route('totem.task.export')}}">Export</a>
7475
{{$tasks->links('totem::partials.pagination')}}
76+
@stop
77+
@section('main-panel-after')
78+
<div class="uk-card uk-card-default">
79+
<div class="uk-card-footer">
80+
@if($errors->any())
81+
<div class="uk-text-danger">
82+
{{$errors->first()}}
83+
</div>
84+
@endif
85+
{!! Form::open(['route' => 'totem.task.import', 'enctype' => 'multipart/form-data']) !!}
86+
{!! Form::file('tasks') !!}
87+
{!! Form::submit('Upload', ['class' => 'uk-button uk-button-primary uk-button-small uk-float-right']) !!}
88+
{!! Form::close() !!}
89+
</div>
90+
</div>
7591
@stop

routes/web.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
Route::get('create', 'TasksController@create')->name('totem.task.create');
1616
Route::post('create', 'TasksController@store');
1717

18+
Route::get('export', 'TasksController@export')->name('totem.task.export');
19+
20+
Route::get('import', 'TasksController@import')->name('totem.task.import');
21+
1822
Route::get('{task}', 'TasksController@view')->name('totem.task.view');
1923

2024
Route::get('{task}/edit', 'TasksController@edit')->name('totem.task.edit');

src/Http/Controllers/TasksController.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Studio\Totem\Http\Controllers;
44

5+
use Studio\Totem\Frequency;
56
use Studio\Totem\Task;
67
use Studio\Totem\Totem;
78
use Studio\Totem\Contracts\TaskInterface;
@@ -127,4 +128,72 @@ public function destroy($task)
127128
->route('totem.tasks.all')
128129
->with('success', trans('totem::messages.success.delete'));
129130
}
131+
132+
/**
133+
* JSON representation of tasks and their frequencies
134+
*
135+
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
136+
*/
137+
public function export()
138+
{
139+
return response($this->tasks->findAll()->toJson(), 200, [
140+
'Content-Type' => 'application/json',
141+
'Content-Disposition' => 'attachment; filename="totem_tasks.json"',
142+
]);
143+
}
144+
145+
/**
146+
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
147+
*/
148+
public function import()
149+
{
150+
$errors = [];
151+
$records_imported = 0;
152+
if (request()->hasFile('tasks')) {
153+
$file = request()->file('tasks');
154+
if(ends_with($file->getClientOriginalName(), '.json')) {
155+
try {
156+
$data = json_decode(file_get_contents($file->getPathname()));
157+
foreach ($data as $record) {
158+
$task = Task::updateOrCreate([
159+
'id' => $record->id,
160+
], [
161+
'description' => $record->description,
162+
'command' => $record->command,
163+
'parameters' => $record->parameters,
164+
'expression' => $record->expression,
165+
'timezone' => $record->timezone,
166+
'is_active' => $record->is_active,
167+
'dont_overlap' => $record->dont_overlap,
168+
'run_in_maintenance' => $record->run_in_maintenance,
169+
'notification_email_address' => $record->notification_email_address,
170+
'notification_phone_number' => $record->notification_phone_number,
171+
'notification_slack_webhook' => $record->notification_slack_webhook,
172+
]);
173+
174+
if (!empty($record->frequencies)) {
175+
foreach ($record->frequencies as $freq) {
176+
Frequency::updateOrCreate([
177+
'id' => $freq->id,
178+
], [
179+
'task_id' => $task->id,
180+
'label' => $freq->label,
181+
'interval' => $freq->interval,
182+
]);
183+
}
184+
}
185+
$records_imported++;
186+
}
187+
} catch (\Exception $ex) {
188+
$errors[] = 'An error occurred while importing data.';
189+
}
190+
}
191+
}
192+
193+
if($records_imported == 0) {
194+
$errors[] = 'Invalid data or no record selected.';
195+
}
196+
197+
return redirect(route('totem.tasks.all'))->withErrors($errors);
198+
}
130199
}

0 commit comments

Comments
 (0)