Finished implementing the import into the start/end date fields in Drupal

This commit is contained in:
Andy Heathershaw 2017-09-26 21:19:12 +01:00
parent bb66308a6f
commit 2c5b7642f1
3 changed files with 102 additions and 5 deletions

View File

@ -78,13 +78,30 @@ try
$target = new DrupalDatabaseEventWriter($drupalReader); $target = new DrupalDatabaseEventWriter($drupalReader);
$target->open(); $target->open();
$numberCompleted = 0;
/** @var Event $event */ /** @var Event $event */
foreach ($events as $event) foreach ($events as $event)
{
echo sprintf('Uploading event %s ... ', $event->getUid());
try
{ {
$target->upload($event); $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(); $target->close();
echo 'Done' . PHP_EOL;
} }
catch (\Exception $e) catch (\Exception $e)
{ {

View File

@ -4,9 +4,20 @@ namespace Pandy06269\iCalImporter\includes;
class DrupalDatabaseEventWriter extends MysqlEventWriter class DrupalDatabaseEventWriter extends MysqlEventWriter
{ {
public function __construct(DrupalConfigFileReader $configFileReader) private $options = [];
public function __construct(DrupalConfigFileReader $configFileReader, array $options = [])
{ {
$this->config = $configFileReader->getDatabaseConfig(); $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) 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'], ['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] [$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('<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() private function guidv4()

View File

@ -27,8 +27,7 @@ abstract class MysqlEventWriter implements IEventWriter
$this->config['host'], $this->config['host'],
$this->config['username'], $this->config['username'],
$this->config['password'], $this->config['password'],
//$this->config['database'], $this->config['database'],
'drupal_osborne_2',
$this->config['port'] $this->config['port']
); );