|
2 | 2 |
|
3 | 3 | namespace Studio\Totem\Http\Controllers;
|
4 | 4 |
|
| 5 | +use Studio\Totem\Frequency; |
5 | 6 | use Studio\Totem\Task;
|
6 | 7 | use Studio\Totem\Totem;
|
7 | 8 | use Studio\Totem\Contracts\TaskInterface;
|
@@ -127,4 +128,72 @@ public function destroy($task)
|
127 | 128 | ->route('totem.tasks.all')
|
128 | 129 | ->with('success', trans('totem::messages.success.delete'));
|
129 | 130 | }
|
| 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 | + } |
130 | 199 | }
|
0 commit comments