Skip to content

Commit

Permalink
feat(calendar): Calendar export ical file event
Browse files Browse the repository at this point in the history
  • Loading branch information
gnovaro committed Mar 11, 2024
1 parent 3a7f021 commit 4c098ad
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 288 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use App\Models\Calendar;
use Illuminate\Http\Request;

class DeleteCalendarEventController extends MainController
class CalendarDeleteEventController extends MainController
{
/**
* @return \Illuminate\Http\JsonResponse
Expand Down
58 changes: 58 additions & 0 deletions app/Http/Controllers/Calendar/CalendarExportController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Calendar;

use App\Http\Controllers\Controller;
use App\Models\Calendar as CalendarModel;
use DateTime;
use Spatie\IcalendarGenerator\Components\Calendar;
use Spatie\IcalendarGenerator\Components\Event;

class CalendarExportController extends Controller
{
public function exportICal(int $id): \Symfony\Component\HttpFoundation\BinaryFileResponse
{
// Buscar el evento de calendario por su ID
$calendarEvent = CalendarModel::find($id);

// Verificar si el evento existe
if (! $calendarEvent) {
abort(404, 'Evento de calendario no encontrado.');
}

// Crear el objeto Calendar
$calendar = Calendar::create($calendarEvent->title);

// Crear el objeto Event con los detalles del evento de calendario
$event = Event::create($calendarEvent->title)
->startsAt(new DateTime($calendarEvent->start_date))
->endsAt(new DateTime($calendarEvent->end_date))
->description((string) $calendarEvent->description); // Puedes agregar más propiedades según necesites

// Agregar el organizador si existe el user_id
if ($calendarEvent->user_id) {
// Obtener información del organizador según el user_id
$organizerEmail = $calendarEvent->organizer->email;
$organizerName = $calendarEvent->organizer->getFullName();

$event->organizer($organizerEmail, $organizerName);
}

// Agregar el evento al calendario
$calendar->event($event);

// Generar el archivo iCalendar y obtener su contenido
$iCalContent = $calendar->get();

// Guardar el contenido en un archivo temporal
$tempFilePath = tempnam(sys_get_temp_dir(), 'calendar');
file_put_contents($tempFilePath, $iCalContent);

// Descargar el archivo iCalendar
return response()
->download($tempFilePath, $calendarEvent->title.'.ics')
->deleteFileAfterSend(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Carbon\Carbon;
use Illuminate\Http\Request;

class CalendarController extends MainController
class CalendarIndexController extends MainController
{
/**
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class SaveCalendarEventController extends MainController
class CalendarSaveEventController extends MainController
{
private $calendarRepository;
private CalendarRepository $calendarRepository;

public function __construct(CalendarRepository $calendarRepository)
public function __construct(CalendarRepository $calendarRepository, Request $request)
{
parent::__construct($request);
$this->calendarRepository = $calendarRepository;
}

Expand Down
20 changes: 20 additions & 0 deletions app/Http/Controllers/Calendar/CalendarUpdateEventController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Calendar;

use App\Http\Controllers\MainController;
use App\Models\Calendar;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CalendarUpdateEventController extends MainController
{
public function update(Request $request, int $id): JsonResponse
{
$calendar = Calendar::find($id);

return response()->json(['calendar' => $calendar]);
}
}
20 changes: 0 additions & 20 deletions app/Http/Controllers/Calendar/UpdateCalendarEventController.php

This file was deleted.

6 changes: 6 additions & 0 deletions app/Models/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Auth;
use OpenApi\Attributes as OAT;

Expand Down Expand Up @@ -56,6 +57,11 @@ class Calendar extends Model
#[OAT\Property(type: 'int', example: 1)]
protected ?int $user_id;

public function organizer()
{
return $this->hasOne(User::class, 'id', 'user_id');
}

protected function startDate(): Attribute
{
return Attribute::make(
Expand Down
Loading

0 comments on commit 4c098ad

Please sign in to comment.