This repository has been archived on 2020-02-18. You can view files and clone it, but cannot push or open issues or pull requests.
ical-drupal-calendar/includes/DrupalDatabaseEventReader.php

77 lines
2.8 KiB
PHP

<?php
namespace Pandy06269\iCalDrupal\includes;
class DrupalDatabaseEventReader extends MysqlConnector implements IEventReader
{
private $options = [];
public function __construct(DrupalConfigFileReader $configFileReader, array $options = [])
{
$this->config = $configFileReader->getDatabaseConfig();
$defaultOptions = [
'body_format' => 'content_editor_html',
'calendar_bundle' => 'private_event',
'end_date_field_name' => 'end_date',
'start_date_field_name' => 'start_date'
];
$this->options = array_merge($defaultOptions, $options);
}
public function getEvents()
{
$events = [];
$results = $this->executeQueryResult(
'SELECT n.nid, n.uuid, MAX(nr.vid) AS vid FROM `node` n INNER JOIN `node_revision` nr ON nr.nid = n.nid WHERE (n.`type` = ? OR n.`type` = ?) GROUP BY n.nid, n.uuid',
['s', 's'],
['private_event', 'public_event']
);
$startDateTableName = sprintf('node_revision__field_%s', $this->options['start_date_field_name']);
$startDateFieldName = sprintf('field_%s_value', $this->options['start_date_field_name']);
$endDateTableName = sprintf('node_revision__field_%s', $this->options['end_date_field_name']);
$endDateFieldName = sprintf('field_%s_value', $this->options['end_date_field_name']);
foreach ($results as $result)
{
$nodeResults = $this->executeQueryResult(
sprintf(
'SELECT * ' .
'FROM `node_field_data` n ' .
'LEFT OUTER JOIN `%s` ns ON ns.`entity_id` = n.`nid` AND ns.`revision_id` = n.`vid` ' .
'LEFT OUTER JOIN `%s` ne ON ne.`entity_id` = n.`nid` AND ne.`revision_id` = n.`vid` ' .
'LEFT OUTER JOIN `node_revision__body` nb ON nb.`entity_id` = n.`nid` AND nb.`revision_id` = n.`vid` ' .
'WHERE n.`nid` = ? AND n.`vid` = ?',
$startDateTableName,
$endDateTableName
),
['i', 'i'],
[$result['nid'], $result['vid']]
);
$event = new Event();
$event->setTitle($nodeResults[0]['title']);
$event->setDescription(trim($nodeResults[0]['body_value']));
if (!empty($nodeResults[0][$startDateFieldName]))
{
$event->setStartDate(new \DateTime($nodeResults[0][$startDateFieldName]));
}
if (!empty($nodeResults[0][$endDateFieldName]))
{
$event->setEndDate(new \DateTime($nodeResults[0][$endDateFieldName]));
}
$event->setUid($result['uuid']);
$events[] = $event;
}
return $events;
}
}