View source
<?php
namespace Drupal\addtocal\Plugin\AddToCal\Type;
use Drupal\addtocal\AddToCalTypeBase;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Exception;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
class Generic extends AddToCalTypeBase {
protected $fileSystem;
private $_fd;
function __construct(array $configuration, $plugin_id, $plugin_definition, $dateFormatter, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $dateFormatter);
$this->fileSystem = $file_system;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('date.formatter'), $container
->get('file_system'));
}
public function open($uri) {
$this->_fd = fopen($uri, 'w');
}
public function close() {
if ($this->_fd) {
fclose($this->_fd);
}
else {
throw new Exception('Cannot close stream that is not open.');
}
}
private function write($data) {
$result = fputs($this->_fd, $data);
if ($result === FALSE) {
throw new Exception('Unable to write data: ' . substr($data, 0, 20));
}
}
public function generateStructure(array $info) {
$uri = $this->fileSystem
->tempnam('temporary://', 'ics_');
$title = Html::escape($info['title']);
$ics = <<<ICS
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:{<span class="php-variable">$info</span>[<span class="php-string">'entity_type'</span>]}-{<span class="php-variable">$info</span>[<span class="php-string">'entity_id'</span>]}@{<span class="php-variable">$_SERVER</span>[<span class="php-string">'HTTP_HOST'</span>]}
DTSTAMP:{<span class="php-variable">$info</span>[<span class="php-string">'rfc3339'</span>][<span class="php-string">'start'</span>]}
DTSTART:{<span class="php-variable">$info</span>[<span class="php-string">'rfc3339'</span>][<span class="php-string">'start'</span>]}
DTEND:{<span class="php-variable">$info</span>[<span class="php-string">'rfc3339'</span>][<span class="php-string">'end'</span>]}
SUMMARY:{<span class="php-variable">$title</span>}
DESCRIPTION:{<span class="php-variable">$info</span>[<span class="php-string">'description'</span>]}
LOCATION:{<span class="php-variable">$info</span>[<span class="php-string">'location'</span>]}
END:VEVENT
END:VCALENDAR
ICS;
$this
->open($uri);
$this
->write($ics);
$this
->close();
return $uri;
}
public function downloadSubmit(EntityInterface $entity, array $settings, $delta, FormStateInterface $form_state) {
$eventDetails = $this
->extractEventDetails($entity, $settings, $delta);
$filename = preg_replace('/[\\x00-\\x1F]/u', '_', strip_tags($eventDetails['title'])) . '.ics';
$ics = $this
->generateStructure($eventDetails);
$response = new BinaryFileResponse($ics);
$response->headers
->set('Content-Type', 'application/calendar; charset=utf-8');
$response
->setContentDisposition('attachment', $filename);
$form_state
->setResponse($response);
}
}