Merge branch 'features/export' into 'dev'
add in stash export See merge request sae-but2/2025-26/gestion-benevoles-association/backend!26
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class ExportController extends Controller
|
||||
{
|
||||
public function exportIcs($userId)
|
||||
{
|
||||
$user = User::with('tasks.event')->findOrFail($userId);
|
||||
|
||||
$lines = [
|
||||
'BEGIN:VCALENDAR',
|
||||
'VERSION:2.0',
|
||||
'PRODID:-//VotreApp//FR',
|
||||
];
|
||||
|
||||
$now = Carbon::now()->utc()->format('Ymd\THis\Z');
|
||||
|
||||
foreach ($user->tasks as $task) {
|
||||
|
||||
// On ignore les tâches sans dates
|
||||
if (!$task->start || !$task->end) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$uid = $task->id . '-' . $user->id . '@votreapp.local';
|
||||
|
||||
$dtstart = Carbon::parse($task->start)->utc()->format('Ymd\THis\Z');
|
||||
$dtend = Carbon::parse($task->end)->utc()->format('Ymd\THis\Z');
|
||||
|
||||
$summary = $this->escapeText($task->name);
|
||||
|
||||
$description = $this->escapeText(
|
||||
trim(
|
||||
($task->description ?? '') .
|
||||
($task->event ? "\nÉvénement: {$task->event->name}" : '')
|
||||
)
|
||||
);
|
||||
|
||||
$location = $this->escapeText($task->location ?? '');
|
||||
|
||||
$lines[] = 'BEGIN:VEVENT';
|
||||
$lines[] = 'UID:' . $uid;
|
||||
$lines[] = 'DTSTAMP:' . $now;
|
||||
$lines[] = 'DTSTART:' . $dtstart;
|
||||
$lines[] = 'DTEND:' . $dtend;
|
||||
$lines[] = 'SUMMARY:' . $summary;
|
||||
$lines[] = 'DESCRIPTION:' . $description;
|
||||
$lines[] = 'LOCATION:' . $location;
|
||||
$lines[] = 'END:VEVENT';
|
||||
}
|
||||
|
||||
$lines[] = 'END:VCALENDAR';
|
||||
|
||||
$content = implode("\r\n", $lines) . "\r\n";
|
||||
|
||||
return response($content, 200, [
|
||||
'Content-Type' => 'text/calendar; charset=utf-8',
|
||||
'Content-Disposition' => 'attachment; filename="agenda_user_' . $user->id . '.ics"',
|
||||
]);
|
||||
}
|
||||
|
||||
protected function escapeText($text)
|
||||
{
|
||||
$text = (string) $text;
|
||||
|
||||
return str_replace(
|
||||
["\\", "\r\n", "\n", "\r", ";", ","],
|
||||
["\\\\", "\\n", "\\n", "\\n", "\\;", "\\,"],
|
||||
$text
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\ExportController;
|
||||
|
||||
Route::get('/export/ics/{userId}', [ExportController::class, 'exportIcs']);
|
||||
Route::get("/hello", function (Request $request) {
|
||||
return "Hello";
|
||||
});
|
||||
|
||||
@@ -5,7 +5,6 @@ use App\Http\Controllers\UserController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
|
||||
|
||||
Route::middleware(['web', 'auth:sanctum'])->get('/users/me', [UserController::class, "me"]);
|
||||
|
||||
Route::middleware(['web', 'auth:sanctum'])->delete('/users', [UserController::class, "delete"]);
|
||||
|
||||
Reference in New Issue
Block a user