137 lines
6.4 KiB
PHP
137 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace Pandy06269\iCalImporter\includes;
|
|
|
|
class DrupalDatabaseEventWriter extends MysqlEventWriter
|
|
{
|
|
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 upload(Event $event)
|
|
{
|
|
// First insert the `node` record
|
|
$this->executeQuery(
|
|
'INSERT INTO `node` (`vid`, `type`, `uuid`, `langcode`) VALUES (?, ?, ?, ?)',
|
|
['i', 's', 's', 's'],
|
|
[0, 'private_event', $this->guidv4(), 'en']
|
|
);
|
|
$nodeID = $this->getLastInsertedID();
|
|
|
|
// Now add a `node_revision` record
|
|
$this->executeQuery(
|
|
'INSERT INTO `node_revision` (`nid`, `langcode`, `revision_timestamp`, `revision_uid`) VALUES (?, ?, ?, ?)',
|
|
['i', 's', 'i', 'i'],
|
|
[$nodeID, 'en', time(), 1]
|
|
);
|
|
$revisionID = $this->getLastInsertedID();
|
|
|
|
// Update the original node record with the vid
|
|
$this->executeQuery(
|
|
'UPDATE `node` SET `vid` = ? WHERE `nid` = ?',
|
|
['i', 'i'],
|
|
[$revisionID, $nodeID]
|
|
);
|
|
|
|
// Now add a `node_field_data` record
|
|
$this->executeQuery(
|
|
'INSERT INTO `node_field_data` (`nid`, `vid`, `type`, `langcode`, `title`, `uid`, `status`, `created`, `changed`, `promote`, `sticky`, `revision_translation_affected`, `default_langcode`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
['i', 'i', 's', 's', 's', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'],
|
|
[$nodeID, $revisionID, 'private_event', 'en', $event->getTitle(), 1, 1, time(), time(), 0, 0, 1, 1]
|
|
);
|
|
|
|
// Now add a `node_field_revision` record
|
|
$this->executeQuery(
|
|
'INSERT INTO `node_field_revision` (`nid`, `vid`, `langcode`, `title`, `uid`, `status`, `created`, `changed`, `promote`, `sticky`, `revision_translation_affected`, `default_langcode`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
['i', 'i', 's', 's', 'i', 'i', 'i', 'i', 'i', 'i', 'i', 'i'],
|
|
[$nodeID, $revisionID, 'en', $event->getTitle(), 1, 1, time(), time(), 0, 0, 1, 1]
|
|
);
|
|
|
|
// Add the summary to the `node_revision__body` table
|
|
$this->executeQuery(
|
|
'INSERT INTO `node_revision__body` (`bundle`, `deleted`, `entity_id`, `revision_id`, `langcode`, `delta`, `body_value`, `body_summary`, `body_format`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
['s', 'i', 'i', 'i', 's', 'i', 's', 's', 's'],
|
|
[$this->options['calendar_bundle'], 0, $nodeID, $revisionID, 'en', 0, sprintf('<p>%s</p>', $event->getDescription()), '', $this->options['body_format']]
|
|
);
|
|
|
|
// Add the start date to the `node__field_X` table
|
|
$startDateTableName = sprintf('node__field_%s', $this->options['start_date_field_name']);
|
|
$startDateFieldName = sprintf('field_%s_value', $this->options['start_date_field_name']);
|
|
$this->executeQuery(
|
|
sprintf(
|
|
'INSERT INTO `%s` (`bundle`, `deleted`, `entity_id`, `revision_id`, `langcode`, `delta`, `%s`) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
$startDateTableName,
|
|
$startDateFieldName
|
|
),
|
|
['s', 'i', 'i', 'i', 's', 's', 's'],
|
|
[$this->options['calendar_bundle'], 0, $nodeID, $revisionID, 'en', 0, $event->getStartDate()->format('Y-m-d\\TH:i:s')]
|
|
);
|
|
|
|
// Add the start date to the `node_revision__field_X` table
|
|
$startDateTableName = sprintf('node_revision__field_%s', $this->options['start_date_field_name']);
|
|
$startDateFieldName = sprintf('field_%s_value', $this->options['start_date_field_name']);
|
|
$this->executeQuery(
|
|
sprintf(
|
|
'INSERT INTO `%s` (`bundle`, `deleted`, `entity_id`, `revision_id`, `langcode`, `delta`, `%s`) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
$startDateTableName,
|
|
$startDateFieldName
|
|
),
|
|
['s', 'i', 'i', 'i', 's', 's', 's'],
|
|
[$this->options['calendar_bundle'], 0, $nodeID, $revisionID, 'en', 0, $event->getStartDate()->format('Y-m-d\\TH:i:s')]
|
|
);
|
|
|
|
// Default to adding 30 minutes to the end date
|
|
$endDate = $event->getStartDate()->add(new \DateInterval('PT30M'));
|
|
|
|
// Add the end date to the `node__field_X` table
|
|
$endDateTableName = sprintf('node__field_%s', $this->options['end_date_field_name']);
|
|
$endDateFieldName = sprintf('field_%s_value', $this->options['end_date_field_name']);
|
|
|
|
$this->executeQuery(
|
|
sprintf(
|
|
'INSERT INTO `%s` (`bundle`, `deleted`, `entity_id`, `revision_id`, `langcode`, `delta`, `%s`) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
$endDateTableName,
|
|
$endDateFieldName
|
|
),
|
|
['s', 'i', 'i', 'i', 's', 's', 's'],
|
|
[$this->options['calendar_bundle'], 0, $nodeID, $revisionID, 'en', 0, $endDate->format('Y-m-d\\TH:i:s')]
|
|
);
|
|
|
|
// Add the end date to the `node_revision__field_X` table
|
|
$endDateTableName = sprintf('node_revision__field_%s', $this->options['end_date_field_name']);
|
|
$endDateFieldName = sprintf('field_%s_value', $this->options['end_date_field_name']);
|
|
|
|
$this->executeQuery(
|
|
sprintf(
|
|
'INSERT INTO `%s` (`bundle`, `deleted`, `entity_id`, `revision_id`, `langcode`, `delta`, `%s`) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
|
$endDateTableName,
|
|
$endDateFieldName
|
|
),
|
|
['s', 'i', 'i', 'i', 's', 's', 's'],
|
|
[$this->options['calendar_bundle'], 0, $nodeID, $revisionID, 'en', 0, $endDate->format('Y-m-d\\TH:i:s')]
|
|
);
|
|
}
|
|
|
|
private function guidv4()
|
|
{
|
|
if (function_exists('com_create_guid') === true)
|
|
return trim(com_create_guid(), '{}');
|
|
|
|
$data = openssl_random_pseudo_bytes(16);
|
|
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
|
|
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
|
|
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
|
}
|
|
} |