From 2c5b7642f1caf75954d988e43c7133b11dab7185 Mon Sep 17 00:00:00 2001 From: Andy Heathershaw Date: Tue, 26 Sep 2017 21:19:12 +0100 Subject: [PATCH] Finished implementing the import into the start/end date fields in Drupal --- import.php | 19 +++++- includes/DrupalDatabaseEventWriter.php | 85 +++++++++++++++++++++++++- includes/MysqlEventWriter.php | 3 +- 3 files changed, 102 insertions(+), 5 deletions(-) diff --git a/import.php b/import.php index d819233..186588d 100644 --- a/import.php +++ b/import.php @@ -78,13 +78,30 @@ try $target = new DrupalDatabaseEventWriter($drupalReader); $target->open(); + $numberCompleted = 0; + /** @var Event $event */ foreach ($events as $event) { - $target->upload($event); + echo sprintf('Uploading event %s ... ', $event->getUid()); + + try + { + $target->upload($event); + echo 'OK' . PHP_EOL; + } + catch (\Exception $e) + { + echo 'failed' . PHP_EOL; + throw $e; + } + + $numberCompleted++; } + echo sprintf('%d event%s added successfully, disconnecting...' . PHP_EOL, $numberCompleted, $numberCompleted == 1 ? '' : 's'); $target->close(); + echo 'Done' . PHP_EOL; } catch (\Exception $e) { diff --git a/includes/DrupalDatabaseEventWriter.php b/includes/DrupalDatabaseEventWriter.php index 049fe1b..8b131a0 100644 --- a/includes/DrupalDatabaseEventWriter.php +++ b/includes/DrupalDatabaseEventWriter.php @@ -4,9 +4,20 @@ namespace Pandy06269\iCalImporter\includes; class DrupalDatabaseEventWriter extends MysqlEventWriter { - public function __construct(DrupalConfigFileReader $configFileReader) + 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) @@ -40,7 +51,77 @@ class DrupalDatabaseEventWriter extends MysqlEventWriter ['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] ); - exit(); + + // 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('

%s

', $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() diff --git a/includes/MysqlEventWriter.php b/includes/MysqlEventWriter.php index b0c7c69..33a4bad 100644 --- a/includes/MysqlEventWriter.php +++ b/includes/MysqlEventWriter.php @@ -27,8 +27,7 @@ abstract class MysqlEventWriter implements IEventWriter $this->config['host'], $this->config['username'], $this->config['password'], - //$this->config['database'], - 'drupal_osborne_2', + $this->config['database'], $this->config['port'] );