2017-09-25 21:14:04 +01:00
|
|
|
<?php
|
|
|
|
|
2018-03-11 21:03:39 +00:00
|
|
|
namespace Pandy06269\iCalDrupal;
|
2017-09-25 21:14:04 +01:00
|
|
|
|
2018-03-11 21:03:39 +00:00
|
|
|
use Pandy06269\iCalDrupal\includes\DrupalConfigFileReader;
|
|
|
|
use Pandy06269\iCalDrupal\includes\DrupalDatabaseEventWriter;
|
|
|
|
use Pandy06269\iCalDrupal\includes\Event;
|
|
|
|
use Pandy06269\iCalDrupal\includes\ICSFileReader;
|
2017-09-25 21:14:04 +01:00
|
|
|
|
|
|
|
//define('DEBUG', true);
|
|
|
|
|
|
|
|
function autoload_include_file($class)
|
|
|
|
{
|
2018-03-11 21:03:39 +00:00
|
|
|
$class = substr($class, strlen('Pandy06269\\iCalDrupal\\'));
|
2017-09-25 21:14:04 +01:00
|
|
|
|
|
|
|
$filePath = sprintf('%s/%s.php', __DIR__, str_replace('\\', DIRECTORY_SEPARATOR, $class));
|
|
|
|
require_once $filePath;
|
|
|
|
}
|
|
|
|
|
2018-03-11 21:03:39 +00:00
|
|
|
spl_autoload_register('\Pandy06269\iCalDrupal\autoload_include_file');
|
2017-09-25 21:14:04 +01:00
|
|
|
|
|
|
|
if ($argc !== 3)
|
|
|
|
{
|
|
|
|
echo sprintf('Usage: %s <path to ical file> <path to Drupal config file>' . PHP_EOL, $argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$pathToICSFile = trim($argv[1]);
|
|
|
|
$pathToDrupalConfig = trim($argv[2]);
|
|
|
|
|
|
|
|
$icsReader = new ICSFileReader($pathToICSFile);
|
2017-09-25 22:24:55 +01:00
|
|
|
$drupalReader = new DrupalConfigFileReader($pathToDrupalConfig);
|
2017-09-25 21:14:04 +01:00
|
|
|
|
|
|
|
echo sprintf('Reading iCal data from %s' . PHP_EOL, $icsReader->getPath());
|
|
|
|
echo sprintf('Using Drupal connection details from %s' . PHP_EOL, $drupalReader->getPath());
|
|
|
|
echo '---' . PHP_EOL;
|
|
|
|
|
|
|
|
$events = $icsReader->getEvents();
|
|
|
|
|
|
|
|
echo sprintf('%d event%s found' . PHP_EOL, count($events), count($events) == 1 ? '' : 's');
|
|
|
|
echo '---' . PHP_EOL;
|
|
|
|
|
|
|
|
/** @var Event $event */
|
|
|
|
$index = 0;
|
|
|
|
foreach ($events as $event)
|
|
|
|
{
|
|
|
|
echo sprintf('%d: %s | %s' . PHP_EOL, $index, $event->getUid(), $event->getStartDate()->format('H:i:s d/M/Y'));
|
|
|
|
echo $event->getTitle() . PHP_EOL;
|
|
|
|
|
|
|
|
if (strlen($event->getDescription()))
|
|
|
|
{
|
|
|
|
echo PHP_EOL . $event->getDescription() . PHP_EOL;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '---' . PHP_EOL;
|
|
|
|
$index++;
|
|
|
|
}
|
2017-09-25 22:24:55 +01:00
|
|
|
|
|
|
|
echo PHP_EOL . '*** Imported ICS file ***' . PHP_EOL;
|
|
|
|
|
|
|
|
if (count($events) == 0)
|
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '*** Ready to upload to Drupal ***' . PHP_EOL . PHP_EOL;
|
|
|
|
|
|
|
|
$character = readline('Do you want to continue? ');
|
|
|
|
if (strtolower($character) !== 'y')
|
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
echo PHP_EOL;
|
|
|
|
|
|
|
|
$target = new DrupalDatabaseEventWriter($drupalReader);
|
|
|
|
$target->open();
|
|
|
|
|
2017-09-26 21:19:12 +01:00
|
|
|
$numberCompleted = 0;
|
|
|
|
|
2017-09-25 22:24:55 +01:00
|
|
|
/** @var Event $event */
|
|
|
|
foreach ($events as $event)
|
|
|
|
{
|
2017-09-26 21:19:12 +01:00
|
|
|
echo sprintf('Uploading event %s ... ', $event->getUid());
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2018-03-11 21:03:39 +00:00
|
|
|
$target->write($event);
|
2017-09-26 21:19:12 +01:00
|
|
|
echo 'OK' . PHP_EOL;
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
echo 'failed' . PHP_EOL;
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
|
|
|
|
$numberCompleted++;
|
2017-09-25 22:24:55 +01:00
|
|
|
}
|
|
|
|
|
2017-09-26 21:19:12 +01:00
|
|
|
echo sprintf('%d event%s added successfully, disconnecting...' . PHP_EOL, $numberCompleted, $numberCompleted == 1 ? '' : 's');
|
2017-09-25 22:24:55 +01:00
|
|
|
$target->close();
|
2017-09-26 21:19:12 +01:00
|
|
|
echo 'Done' . PHP_EOL;
|
2017-09-25 21:14:04 +01:00
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
|
|
|
echo sprintf('Caught exception: %s' . PHP_EOL, $e->getMessage());
|
|
|
|
echo $e->getTraceAsString();
|
|
|
|
}
|