-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV-1283. Turned ical code into service that will generate a calendar…
… from a name and a list of events.
- Loading branch information
1 parent
c4b6e13
commit 7b8b544
Showing
4 changed files
with
135 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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('[email protected]'), | ||
$event->promoter->name, | ||
new Uri('ldap://example.com:6666/o=ABC%20Industries,c=US???(cn=Jim%20Dolittle)'), | ||
new EmailAddress('[email protected]') | ||
); | ||
|
||
$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; | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace App\Services\Calendar; | ||
|
||
use App\Models\Event; | ||
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 Storage; | ||
|
||
|
||
/** | ||
* Builds and exports ical calendar | ||
*/ | ||
class CalBuilder | ||
{ | ||
|
||
/** | ||
* Build iCal calendar from events | ||
* @param string $calendarName Name of the calendar file | ||
* @param $events Array of Event models | ||
* @return string The iCal formatted calendar | ||
*/ | ||
public function buildCalendar(string $calendarName = "event-tracker.ics", $events): string | ||
{ | ||
// 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($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; | ||
} | ||
} |