class Generic in Add to Cal 8
Provides generic calendar type (ics).
Plugin annotation
@AddToCalType(
id = "generic",
label = @Translation("Generic Calendar"),
description = @Translation("Generic calendar type (ics).")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\addtocal\AddToCalTypeBase implements AddToCalTypeInterface
- class \Drupal\addtocal\Plugin\AddToCal\Type\Generic
- class \Drupal\addtocal\AddToCalTypeBase implements AddToCalTypeInterface
Expanded class hierarchy of Generic
File
- src/
Plugin/ AddToCal/ Type/ Generic.php, line 23
Namespace
Drupal\addtocal\Plugin\AddToCal\TypeView source
class Generic extends AddToCalTypeBase {
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* File handle of the current ICS stream.
*
* @var resource
*/
private $_fd;
/**
* Add in the filesystem service for writing the ICS
*
* {@inheritdoc}
*/
function __construct(array $configuration, $plugin_id, $plugin_definition, $dateFormatter, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $dateFormatter);
$this->fileSystem = $file_system;
}
/**
* @inheritdoc
*/
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'));
}
/**
* Open the stream.
*/
public function open($uri) {
// Open in write mode. Will overwrite the stream if it already exists.
$this->_fd = fopen($uri, 'w');
}
/**
* Implements Drupal\Component\Gettext\PoStreamInterface::close().
*
* @throws Exception
* If the stream is not open.
*/
public function close() {
if ($this->_fd) {
fclose($this->_fd);
}
else {
throw new Exception('Cannot close stream that is not open.');
}
}
/**
* Write data to the stream.
*
* @param string $data
* Piece of string to write to the stream. If the value is not directly a
* string, casting will happen in writing.
*
* @throws Exception
* If writing the data is not possible.
*/
private function write($data) {
$result = fputs($this->_fd, $data);
if ($result === FALSE) {
throw new Exception('Unable to write data: ' . substr($data, 0, 20));
}
}
/**
* Generates an ICS file for download
*/
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;
}
/**
* @param \Drupal\Core\Entity\EntityInterface $entity
* @param array $settings
* @param int $delta
* @param \Drupal\Core\Form\FormStateInterface $form_state
*/
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);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AddToCalTypeBase:: |
protected | property | The date formatter service. | |
AddToCalTypeBase:: |
constant | |||
AddToCalTypeBase:: |
protected | function | ||
AddToCalTypeBase:: |
protected | function | ||
AddToCalTypeBase:: |
public | function |
Return the name of the type plugin. Overrides AddToCalTypeInterface:: |
|
AddToCalTypeBase:: |
constant | |||
AddToCalTypeBase:: |
protected | function | Returns an array containing RFC 3339 formatted start and end dates. | |
Generic:: |
protected | property | The file system service. | |
Generic:: |
private | property | File handle of the current ICS stream. | |
Generic:: |
public | function | Implements Drupal\Component\Gettext\PoStreamInterface::close(). | |
Generic:: |
public static | function |
@inheritdoc Overrides AddToCalTypeBase:: |
|
Generic:: |
public | function |
Overrides AddToCalTypeInterface:: |
|
Generic:: |
public | function |
Generates an ICS file for download Overrides AddToCalTypeInterface:: |
|
Generic:: |
public | function | Open the stream. | |
Generic:: |
private | function | Write data to the stream. | |
Generic:: |
function |
Add in the filesystem service for writing the ICS Overrides AddToCalTypeBase:: |
||
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. |