AddToCalendarIcsController.php in Open Social 10.3.x
File
modules/social_features/social_event/modules/social_event_addtocal/src/Controller/AddToCalendarIcsController.php
View source
<?php
namespace Drupal\social_event_addtocal\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\File\FileSystemInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
class AddToCalendarIcsController extends ControllerBase {
protected $request;
protected $fileSystem;
public function __construct(Request $request, FileSystemInterface $file_system) {
$this->request = $request;
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack')
->getCurrentRequest(), $container
->get('file_system'));
}
public function downloadIcs() {
$dates = $this->request
->get('dates');
$name = md5(serialize($this->request->query
->all()));
$filename = $name . '.ics';
$file = 'temporary://' . $filename;
if (!file_exists($file)) {
$file_data = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
'METHOD:PUBLISH',
'BEGIN:VEVENT',
'UID:' . $name,
'SUMMARY:' . $this->request
->get('title'),
];
if ($dates['all_day']) {
$file_data[] = 'DTSTART:' . $dates['start'];
$file_data[] = 'DTEND:' . $dates['end'];
}
else {
$file_data[] = 'DTSTART;TZID=' . $dates['start'];
$file_data[] = 'DTEND;TZID=' . $dates['end'];
}
if ($this->request
->get('description')) {
$file_data[] = 'DESCRIPTION:' . $this->request
->get('description');
}
if ($this->request
->get('location')) {
$file_data[] = 'LOCATION:' . $this->request
->get('location');
}
$file_data[] = 'END:VEVENT';
$file_data[] = 'END:VCALENDAR';
$data = implode("\r\n", $file_data);
$this->fileSystem
->saveData($data, $file, FileSystemInterface::EXISTS_REPLACE);
}
$response = new BinaryFileResponse($file);
$response->headers
->set('Content-Type', 'application/calendar; charset=utf-8');
$response
->setContentDisposition('attachment', $filename);
return $response;
}
}