You are here

public function AddToCalendarIcsController::downloadIcs in Open Social 8.9

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_event/modules/social_event_addtocal/src/Controller/AddToCalendarIcsController.php \Drupal\social_event_addtocal\Controller\AddToCalendarIcsController::downloadIcs()
  2. 10.0.x modules/social_features/social_event/modules/social_event_addtocal/src/Controller/AddToCalendarIcsController.php \Drupal\social_event_addtocal\Controller\AddToCalendarIcsController::downloadIcs()
  3. 10.1.x modules/social_features/social_event/modules/social_event_addtocal/src/Controller/AddToCalendarIcsController.php \Drupal\social_event_addtocal\Controller\AddToCalendarIcsController::downloadIcs()
  4. 10.2.x modules/social_features/social_event/modules/social_event_addtocal/src/Controller/AddToCalendarIcsController.php \Drupal\social_event_addtocal\Controller\AddToCalendarIcsController::downloadIcs()

Download generated ICS file.

Return value

\Symfony\Component\HttpFoundation\BinaryFileResponse Empty array.

1 string reference to 'AddToCalendarIcsController::downloadIcs'
social_event_addtocal.routing.yml in modules/social_features/social_event/modules/social_event_addtocal/social_event_addtocal.routing.yml
modules/social_features/social_event/modules/social_event_addtocal/social_event_addtocal.routing.yml

File

modules/social_features/social_event/modules/social_event_addtocal/src/Controller/AddToCalendarIcsController.php, line 59

Class

AddToCalendarIcsController
Class AddToCalendarIcsController.

Namespace

Drupal\social_event_addtocal\Controller

Code

public function downloadIcs() {

  // Event dates.
  $dates = $this->request
    ->get('dates');

  // Create ICS filename.
  $name = md5(serialize($this->request->query
    ->all()));
  $filename = $name . '.ics';

  // ICS file destination.
  $file = 'temporary://' . $filename;

  // Generate data for ICS file if it not exists.
  if (!file_exists($file)) {

    // Set initial data.
    $file_data = [
      'BEGIN:VCALENDAR',
      'VERSION:2.0',
      'METHOD:PUBLISH',
      'BEGIN:VEVENT',
      'UID:' . $name,
      'SUMMARY:' . $this->request
        ->get('title'),
    ];

    // Set start and end datetime for event.
    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'];
    }

    // Set location.
    if ($this->request
      ->get('description')) {
      $file_data[] = 'DESCRIPTION:' . $this->request
        ->get('description');
    }

    // Set description.
    if ($this->request
      ->get('location')) {
      $file_data[] = 'LOCATION:' . $this->request
        ->get('location');
    }

    // Set end of file.
    $file_data[] = 'END:VEVENT';
    $file_data[] = 'END:VCALENDAR';

    // Convert array to correct ICS format.
    $data = implode("\r\n", $file_data);

    // Save datta to file.
    $this->fileSystem
      ->saveData($data, $file, FileSystemInterface::EXISTS_REPLACE);
  }

  // Set response for file download.
  $response = new BinaryFileResponse($file);
  $response->headers
    ->set('Content-Type', 'application/calendar; charset=utf-8');
  $response
    ->setContentDisposition('attachment', $filename);
  return $response;
}