diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index f05d6b24..aaddf4dd 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -570,60 +570,6 @@ public function delete(int $id, Request $request) return back(); } - /** - * Return the users events in iCal format. - * - * @return Response|RedirectResponse|string - */ - public function ical(int $id, Request $request) - { - // check if there is a logged in user - if (!$this->user) { - flash()->error('Error', 'No user is logged in.'); - - return back(); - } - - if (!$user = User::find($id)) { - flash()->error('Error', 'No such user'); - - return back(); - } - define('ICAL_FORMAT', 'Ymd\THis\Z'); - - // create a calendar object - $vCalendar = new Calendar($this->user->getFullNameAttribute().' Calendar'); - - // add the following response - // get the next x events they are attending - $events = $user->getAttendingFuture()->take(self::DEFAULT_SHOW_COUNT); - - // loop over events - foreach ($events as $event) { - $venue = $event->venue ? $event->venue->name : ''; - - $vEvent = new Event(); - $vEvent - ->setDtStart($event->start_at) - ->setDtEnd($event->end_at) - ->setDtStamp($event->created_at) - ->setSummary($event->name) - ->setDescription($event->description) - ->setUniqueId($event->id) - ->setLocation($venue) - ->setModified($event->updated_at) - ->setStatus('CONFIRMED') - ->setUrl($event->primary_link); - - $vCalendar->addComponent($vEvent); - } - - // Set the headers - header('Content-type: text/calendar; charset=utf-8'); - header('Content-Disposition: attachment; filename="cal.ics"'); - - return $vCalendar->render(); - } /** * @return RedirectResponse|Response diff --git a/app/Http/Controllers/EventsController.php b/app/Http/Controllers/EventsController.php index 6210bba5..b276d712 100644 --- a/app/Http/Controllers/EventsController.php +++ b/app/Http/Controllers/EventsController.php @@ -52,18 +52,7 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Http\UploadedFile; use Symfony\Component\HttpFoundation\BinaryFileResponse; -use Eluceo\iCal\Domain\Entity\Calendar; -use Eluceo\iCal\Domain\Entity\Event as iCalEvent; -use Eluceo\iCal\Domain\ValueObject\Organizer; -use Eluceo\iCal\Domain\ValueObject\Uri; -use Eluceo\iCal\Domain\ValueObject\EmailAddress; -use Eluceo\iCal\Domain\ValueObject\Location; -use Eluceo\iCal\Domain\ValueObject\UniqueIdentifier; -use Eluceo\iCal\Presentation\Factory\CalendarFactory; -use Eluceo\iCal\Domain\ValueObject\TimeSpan; -use Eluceo\iCal\Domain\ValueObject\DateTime; -use Eluceo\iCal\Domain\ValueObject\Attachment; - +use App\Services\Calendar\CalBuilder; class EventsController extends Controller @@ -208,6 +197,7 @@ public function indexIcal( Request $request, ListParameterSessionStore $listParamSessionStore, ListEntityResultBuilder $listEntityResultBuilder, + CalBuilder $iCalBuilder ) { @@ -247,93 +237,9 @@ public function indexIcal( ->paginate($listResultSet->getLimit()); // create a calendar object - $vCalendar = new Calendar([]); - - // loop over events - foreach ($events as $event) { - // use the route for the event as the unique id - $uniqueId = route('events.show', ['event' => $event]); - - // set up unique ID - $uniqueIdentifier = new UniqueIdentifier($uniqueId); - - $vEvent = new iCalEvent($uniqueIdentifier); - - // set up occurrence - $start = new DateTime($event->start_at, false); - $end = $event->end_at ? new DateTime($event->end_at, false) : null; - $occurrence = new TimeSpan($start, $end ? $end : $start); - - $vEvent->setOccurrence($occurrence) - ->setSummary($event->name) - ->setDescription($event->description); - - // convert $event->updated_at to timestamp - $updated = new DateTime($event->updated_at, false); - $vEvent->touch($updated); - - // set the url - $url = $event->primary_link ? $event->primary_link : $uniqueId; - $url = new Uri($url); - $vEvent->setUrl($url); - - // set up the venue location - // get the name for the venue or set to empty - $venue = $event->venue ? $event->venue->name : ''; - - // set the location - if ($venue) { - $vEvent->setLocation(new Location($venue)); - } - - // get the promoter to set organizer - if ($event->promoter) { - // check for contacts on the promoter - if ($event->promoter->contacts->count() > 0) { - - // cycle through all contacts to find one with an email address - foreach ($event->promoter->contacts as $contact) { - if ($contact->email) { - - $organizer = new Organizer( - new EmailAddress('test@example.org'), - $event->promoter->name, - new Uri('ldap://example.com:6666/o=ABC%20Industries,c=US???(cn=Jim%20Dolittle)'), - new EmailAddress('sender@example.com') - ); - - $vEvent->setOrganizer($organizer); - - break; - } - } - } - } - - // add the primary image as a url attachment - $photo = $event->getPrimaryPhoto(); - if ($photo) { - $imageUrl = Storage::disk('external')->url($photo->getStoragePath()); - - $urlAttachment = new Attachment( - new Uri($imageUrl), - 'image/jpeg' - ); - - $vEvent->addAttachment($urlAttachment); - } - - $vCalendar->addEvent($vEvent); - } - - $componentFactory = new CalendarFactory(); - $calendarComponent = $componentFactory->createCalendar($vCalendar); - - // Set the headers - header('Content-type: text/calendar; charset=utf-8'); - header('Content-Disposition: attachment; filename="arcane-city-ical.ics"'); + $calendar = $iCalBuilder->buildCalendar('arcane-city-ical.ics', $events); - return $calendarComponent; + return $calendar; } diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index ec163232..ea0d0511 100644 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -12,7 +12,6 @@ use App\Mail\WeeklyUpdate; use App\Models\Activity; use App\Models\Group; -use App\Models\Photo; use App\Models\Profile; use App\Models\User; use App\Models\UserStatus; @@ -20,8 +19,6 @@ use App\Services\ImageHandler; use App\Services\SessionStore\ListParameterSessionStore; use Carbon\Carbon; -use Eluceo\iCal\Component\Calendar; -use Eluceo\iCal\Component\Event; use Exception; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; @@ -30,7 +27,7 @@ use Illuminate\Support\Facades\Mail; use Illuminate\Validation\ValidationException; use Illuminate\View\View; -use Symfony\Component\HttpFoundation\File\UploadedFile; +use App\Services\Calendar\CalBuilder; use Throwable; class UsersController extends Controller @@ -605,7 +602,10 @@ public function delete(int $id, Request $request) * * @return Response|RedirectResponse|string */ - public function ical(int $id, Request $request) + public function ical( + int $id, + Request $request, + CalBuilder $iCalBuilder) { // check if there is a logged in user if (!$this->user) { @@ -619,40 +619,14 @@ public function ical(int $id, Request $request) return back(); } - define('ICAL_FORMAT', 'Ymd\THis\Z'); - - // create a calendar object - $vCalendar = new Calendar($this->user->getFullNameAttribute().' Calendar'); - // add the following response // get the next x events they are attending $events = $user->getAttendingFuture()->take(self::DEFAULT_SHOW_COUNT); - // loop over events - foreach ($events as $event) { - $venue = $event->venue ? $event->venue->name : ''; - - $vEvent = new Event(); - $vEvent - ->setDtStart($event->start_at) - ->setDtEnd($event->end_at) - ->setDtStamp($event->created_at) - ->setSummary($event->name) - ->setDescription($event->description) - ->setUniqueId($event->id) - ->setLocation($venue) - ->setModified($event->updated_at) - ->setStatus('CONFIRMED') - ->setUrl($event->primary_link); - - $vCalendar->addComponent($vEvent); - } - - // Set the headers - header('Content-type: text/calendar; charset=utf-8'); - header('Content-Disposition: attachment; filename="cal.ics"'); + // create a calendar object + $calendar = $iCalBuilder->buildCalendar($this->user->getFullNameAttribute().' Calendar', $events); - return $vCalendar->render(); + return $calendar; } /** diff --git a/app/Services/Calendar/CalBuilder.php b/app/Services/Calendar/CalBuilder.php new file mode 100644 index 00000000..da8ba15a --- /dev/null +++ b/app/Services/Calendar/CalBuilder.php @@ -0,0 +1,123 @@ + $event]); + + // set up unique ID + $uniqueIdentifier = new UniqueIdentifier($uniqueId); + + $vEvent = new iCalEvent($uniqueIdentifier); + + // set up occurrence + $start = new DateTime($event->start_at, false); + $end = $event->end_at ? new DateTime($event->end_at, false) : null; + $occurrence = new TimeSpan($start, $end ? $end : $start); + + $vEvent->setOccurrence($occurrence) + ->setSummary($event->name) + ->setDescription($event->description); + + // convert $event->updated_at to timestamp + $updated = new DateTime($event->updated_at, false); + $vEvent->touch($updated); + + // set the url + $url = $event->primary_link ? $event->primary_link : $uniqueId; + $url = new Uri($url); + $vEvent->setUrl($url); + + // set up the venue location + // get the name for the venue or set to empty + $venue = $event->venue ? $event->venue->name : ''; + + // set the location + if ($venue) { + $vEvent->setLocation(new Location($venue)); + } + + // get the promoter to set organizer + if ($event->promoter) { + // check for contacts on the promoter + if ($event->promoter->contacts->count() > 0) { + + // cycle through all contacts to find one with an email address + foreach ($event->promoter->contacts as $contact) { + if ($contact->email) { + + $organizer = new Organizer( + new EmailAddress($contact->email), + $event->promoter->name, + new Uri($uniqueId), + new EmailAddress($contact->email) + ); + + $vEvent->setOrganizer($organizer); + + break; + } + } + } + } + + // add the primary image as a url attachment + $photo = $event->getPrimaryPhoto(); + if ($photo) { + $imageUrl = Storage::disk('external')->url($photo->getStoragePath()); + + $urlAttachment = new Attachment( + new Uri($imageUrl), + 'image/jpeg' + ); + + $vEvent->addAttachment($urlAttachment); + } + + $vCalendar->addEvent($vEvent); + } + + $componentFactory = new CalendarFactory(); + $calendarComponent = $componentFactory->createCalendar($vCalendar); + + // Set the headers + header('Content-type: text/calendar; charset=utf-8'); + header('Content-Disposition: attachment; filename="'.$calendarName.'"'); + + return $calendarComponent; + } +}