Skip to content
This repository has been archived by the owner on May 24, 2019. It is now read-only.

Commit

Permalink
Merge 7957096 into df70c8b
Browse files Browse the repository at this point in the history
  • Loading branch information
bertramakers committed Apr 18, 2019
2 parents df70c8b + 7957096 commit 35d830e
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 58 deletions.
19 changes: 18 additions & 1 deletion src/Command/ExportEventsAsPDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CultuurNet\UDB3\EventExport\Command;

use CultuurNet\UDB3\EventExport\EventExportQuery;
use CultuurNet\UDB3\EventExport\Format\HTML\WebArchive\WebArchiveTemplate;
use CultuurNet\UDB3\EventExport\Format\HTML\Properties\Footer;
use CultuurNet\UDB3\EventExport\Format\HTML\Properties\Publisher;
use CultuurNet\UDB3\EventExport\Format\HTML\Properties\Subtitle;
Expand Down Expand Up @@ -62,25 +63,33 @@ class ExportEventsAsPDF implements ExportEventsInterface
*/
private $publisher;

/**
* @var string
*/
private $template;

/**
* @param EventExportQuery $query
* @param SapiVersion $sapiVersion
* @param string $brand
* @param string $logo
* @param Title $title
* @param WebArchiveTemplate $template
*/
public function __construct(
EventExportQuery $query,
SapiVersion $sapiVersion,
string $brand,
string $logo,
Title $title
Title $title,
WebArchiveTemplate $template
) {
$this->brand = $brand;
$this->sapiVersion = $sapiVersion;
$this->logo = $logo;
$this->query = $query;
$this->title = $title;
$this->template = $template->toNative();
}

/**
Expand Down Expand Up @@ -231,6 +240,14 @@ public function getPublisher()
return $this->publisher;
}

/**
* @return WebArchiveTemplate
*/
public function getTemplate()
{
return WebArchiveTemplate::fromNative($this->template);
}

/**
* @return EventExportQuery The query.
*/
Expand Down
9 changes: 8 additions & 1 deletion src/Command/ExportEventsAsPDFJSONDeserializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CultuurNet\Deserializer\JSONDeserializer;
use CultuurNet\Deserializer\MissingValueException;
use CultuurNet\UDB3\EventExport\EventExportQuery;
use CultuurNet\UDB3\EventExport\Format\HTML\WebArchive\WebArchiveTemplate;
use CultuurNet\UDB3\EventExport\Format\HTML\Properties\Footer;
use CultuurNet\UDB3\EventExport\Format\HTML\Properties\Publisher;
use CultuurNet\UDB3\EventExport\Format\HTML\Properties\Subtitle;
Expand Down Expand Up @@ -83,12 +84,18 @@ public function deserialize(StringLiteral $data)

$title = new Title($customizations->title);

$template = WebArchiveTemplate::TIPS();
if (isset($customizations->template)) {
$template = WebArchiveTemplate::fromNative($customizations->template);
}

$command = new ExportEventsAsPDF(
$query,
$sapiVersion,
$brand,
$logo,
$title
$title,
$template
);

if (isset($json->email)) {
Expand Down
15 changes: 13 additions & 2 deletions src/EventExportCommandHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CultuurNet\UDB3\EventExport\Format\TabularData\OOXML\OOXMLFileFormat;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Twig_Environment;

class EventExportCommandHandler extends CommandHandler implements LoggerAwareInterface
{
Expand All @@ -40,22 +41,30 @@ class EventExportCommandHandler extends CommandHandler implements LoggerAwareInt
*/
protected $calendarSummaryRepository;

/**
* @var Twig_Environment|null
*/
private $twig;

/**
* @param EventExportServiceCollection $eventExportServiceCollection
* @param string $princeXMLBinaryPath
* @param EventInfoServiceInterface|null $uitpas
* @param CalendarSummaryRepositoryInterface $calendarSummaryRepository
* @param Twig_Environment|null $twig
*/
public function __construct(
EventExportServiceCollection $eventExportServiceCollection,
$princeXMLBinaryPath,
EventInfoServiceInterface $uitpas = null,
CalendarSummaryRepositoryInterface $calendarSummaryRepository = null
CalendarSummaryRepositoryInterface $calendarSummaryRepository = null,
Twig_Environment $twig = null
) {
$this->eventExportServiceCollection = $eventExportServiceCollection;
$this->princeXMLBinaryPath = $princeXMLBinaryPath;
$this->uitpas = $uitpas;
$this->calendarSummaryRepository = $calendarSummaryRepository;
$this->twig = $twig;
}

/**
Expand Down Expand Up @@ -109,14 +118,16 @@ public function handleExportEventsAsPDF(
): void {
$fileFormat = new PDFWebArchiveFileFormat(
$this->princeXMLBinaryPath,
$exportEvents->getTemplate(),
$exportEvents->getBrand(),
$exportEvents->getLogo(),
$exportEvents->getTitle(),
$exportEvents->getSubtitle(),
$exportEvents->getFooter(),
$exportEvents->getPublisher(),
$this->uitpas,
$this->calendarSummaryRepository
$this->calendarSummaryRepository,
$this->twig
);

$this->eventExportServiceCollection->delegateToServiceWithAppropriateSapiVersion(
Expand Down
7 changes: 7 additions & 0 deletions src/Format/HTML/HTMLEventFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public function formatEvent($eventId, $eventString)
'municipality' => $this->getAddressField($event, 'addressLocality')
];
}

if (property_exists($event->location, 'geo')) {
$address += [
'latitude' => $event->location->geo->latitude,
'longitude' => $event->location->geo->longitude,
];
}
}

if (!empty($address)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Format/HTML/HTMLFileWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function getHTML($events)
{
$variables = $this->variables;

$variables['events'] = $events;
$variables['events'] = iterator_to_array($events);

return $this->twig->render($this->template, $variables);
}
Expand Down
10 changes: 8 additions & 2 deletions src/Format/HTML/PDF/PDFWebArchiveFileFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use CultuurNet\UDB3\EventExport\FileFormatInterface;
use CultuurNet\UDB3\EventExport\Format\HTML\Uitpas\EventInfo\EventInfoServiceInterface;
use CultuurNet\UDB3\EventExport\Format\HTML\WebArchive\WebArchiveFileFormat;
use CultuurNet\UDB3\EventExport\Format\HTML\WebArchive\WebArchiveTemplate;
use Twig_Environment;

class PDFWebArchiveFileFormat extends WebArchiveFileFormat implements FileFormatInterface
{
Expand All @@ -26,6 +28,7 @@ class PDFWebArchiveFileFormat extends WebArchiveFileFormat implements FileFormat

/**
* @param string $princeXMLBinaryPath
* @param WebArchiveTemplate $template
* @param string $brand
* @param string $logo
* @param string $title
Expand All @@ -34,19 +37,22 @@ class PDFWebArchiveFileFormat extends WebArchiveFileFormat implements FileFormat
* @param string|null $publisher
* @param EventInfoServiceInterface|null $uitpas
* @param CalendarSummaryRepositoryInterface|null $calendarSummaryRepository
* @param Twig_Environment|null $twig
*/
public function __construct(
$princeXMLBinaryPath,
WebArchiveTemplate $template,
$brand,
$logo,
$title,
$subTitle = null,
$footer = null,
$publisher = null,
EventInfoServiceInterface $uitpas = null,
CalendarSummaryRepositoryInterface $calendarSummaryRepository = null
CalendarSummaryRepositoryInterface $calendarSummaryRepository = null,
Twig_Environment $twig = null
) {
parent::__construct($brand, $logo, $title, $subTitle, $footer, $publisher);
parent::__construct($template, $brand, $logo, $title, $subTitle, $footer, $publisher, $twig);
$this->princeXMLBinaryPath = $princeXMLBinaryPath;
$this->uitpas = $uitpas;
$this->calendarSummaryRepository = $calendarSummaryRepository;
Expand Down
52 changes: 52 additions & 0 deletions src/Format/HTML/Twig/GoogleMapUrlGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace CultuurNet\UDB3\EventExport\Format\HTML\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

class GoogleMapUrlGenerator extends AbstractExtension
{
const STATIC_MAP_URL = 'https://maps.googleapis.com/maps/api/staticmap';

/**
* @var string
*/
private $googleMapsApiKey;

public function __construct(string $googleMapsApiKey)
{
$this->googleMapsApiKey = $googleMapsApiKey;
}

public function getFunctions()
{
return [
new TwigFunction('googleMapUrl', function (array $coordinates, int $widthInPixels, int $heightInPixels) {
return $this->generateGoogleMapUrl($coordinates, $widthInPixels, $heightInPixels);
}),
];
}

/**
* @param string[] $markers
* @param int $widthInPixels
* @param int $heightInPixels
* @return string
*/
public function generateGoogleMapUrl(array $markers, int $widthInPixels, int $heightInPixels): string
{
$markers = array_unique($markers);

$url = self::STATIC_MAP_URL;
$url .= '?size=' . $widthInPixels . 'x' . $heightInPixels;

foreach ($markers as $marker) {
$url .= '&markers=' . $marker;
}

$url .= '&key=' . $this->googleMapsApiKey;

return $url;
}
}
12 changes: 8 additions & 4 deletions src/Format/HTML/WebArchive/WebArchiveFileFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CultuurNet\UDB3\EventExport\Format\HTML\WebArchive;

use CultuurNet\UDB3\EventExport\Format\HTML\HTMLFileWriter;
use Twig_Environment;

abstract class WebArchiveFileFormat
{
Expand All @@ -12,22 +13,24 @@ abstract class WebArchiveFileFormat
protected $htmlFileWriter;

/**
* @param WebArchiveTemplate $template
* @param string $brand
* @param string $logo
* @param string $title
* @param string|null $subtitle
* @param string|null $footer
* @param string|null $publisher
* @param string|null $partner
* @param Twig_Environment|null $twig
*/
public function __construct(
WebArchiveTemplate $template,
$brand,
$logo,
$title,
$subtitle = null,
$footer = null,
$publisher = null,
$partner = null
Twig_Environment $twig = null
) {
$variables = [
'brand' => $brand,
Expand All @@ -36,9 +39,10 @@ public function __construct(
'subtitle' => $subtitle,
'footer' => $footer,
'publisher' => $publisher,
'partner' => !in_array($brand, array('uit', 'vlieg', 'uitpas', 'paspartoe'))
'partner' => !in_array($brand, array('uit', 'vlieg', 'uitpas', 'paspartoe')),
'showMap' => $template->sameValueAs(WebArchiveTemplate::MAP()),
];
$this->htmlFileWriter = new HTMLFileWriter('export.html.twig', $variables);
$this->htmlFileWriter = new HTMLFileWriter('export.html.twig', $variables, $twig);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Format/HTML/WebArchive/WebArchiveTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace CultuurNet\UDB3\EventExport\Format\HTML\WebArchive;

use ValueObjects\Enum\Enum;

/**
* @method static WebArchiveTemplate TIPS()
* @method static WebArchiveTemplate MAP()
*/
final class WebArchiveTemplate extends Enum
{
const TIPS = 'tips';
const MAP = 'map';
}
15 changes: 15 additions & 0 deletions src/Format/HTML/templates/export.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@
</div>
</div>

{%- if showMap %}
{% set coordinates = [] %}
{% for event in events %}
{% if event.address.latitude and event.address.longitude %}
{% set coordinates = coordinates|merge([event.address.latitude ~ ',' ~ event.address.longitude]) %}
{% endif %}
{% endfor %}
{% if coordinates is not empty -%}
<div class="map">
<img src="{{ googleMapUrl(coordinates, 600, 300) }}" alt="Google Map"/>
</div>
{%- endif %}
{%- endif %}


<div class="columns">
{%- for event in events %}

Expand Down
22 changes: 10 additions & 12 deletions tests/Command/ExportEventsAsPDFJSONDeserializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use CultuurNet\Deserializer\MissingValueException;
use CultuurNet\UDB3\EventExport\EventExportQuery;
use CultuurNet\UDB3\EventExport\Format\HTML\WebArchive\WebArchiveTemplate;
use CultuurNet\UDB3\EventExport\Format\HTML\Properties\Footer;
use CultuurNet\UDB3\EventExport\Format\HTML\Properties\Publisher;
use CultuurNet\UDB3\EventExport\Format\HTML\Properties\Subtitle;
Expand Down Expand Up @@ -48,7 +49,8 @@ public function it_returns_a_PDF_export_command()
$this->sapiVersion,
'vlieg',
'http://foo.bar/logo.svg',
new Title('a title')
new Title('a title'),
WebArchiveTemplate::TIPS()
),
$command
);
Expand Down Expand Up @@ -93,21 +95,17 @@ public function it_expects_a_title_property()

/**
* @test
* @dataProvider exportPropertyDataProvider
* @param $propertyName
* @param $expectedValue
* @param $getter
*/
public function it_includes_optional_properties(
$propertyName,
$expectedValue,
$getter
) {
public function it_includes_optional_properties()
{
$exportData = $this->getJSONStringFromFile('export_pdf_data.json');
$command = $this->deserializer->deserialize($exportData);

$this->assertEquals($expectedValue, $command->{$getter}());

$this->assertEquals(new Subtitle('a subtitle'), $command->getSubtitle());
$this->assertEquals(new Publisher('a publisher'), $command->getPublisher());
$this->assertEquals(new Footer('a footer'), $command->getFooter());
$this->assertEquals(new EmailAddress('john@doe.com'), $command->getAddress());
$this->assertEquals(WebArchiveTemplate::MAP(), $command->getTemplate());
}

/**
Expand Down

0 comments on commit 35d830e

Please sign in to comment.