Skip to content

Commit

Permalink
Merge pull request #79 from kamilmusial/export_changes
Browse files Browse the repository at this point in the history
EZS-1128 Items export for YOOCHOOSE - wrong header.
  • Loading branch information
kamilmusial authored Dec 7, 2016
2 parents 13ae826 + d927683 commit 7b9b4a3
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 82 deletions.
1 change: 1 addition & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ services:
calls:
- [setSiteAccess, [@ezpublish.siteaccess]]
- [setCustomerId, [$yoochoose.customer_id;ez_recommendation$]]
- [setLicenseKey, [$yoochoose.license_key;ez_recommendation$]]

ez_recommendation.rest.controller.export:
class: "%ez_recommendation.rest.controller.export.class%"
Expand Down
79 changes: 44 additions & 35 deletions Rest/Controller/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class ContentController extends BaseController
/** @var int */
protected $customerId;

/** @var string */
protected $licenseKey;

/** @var int $defaultAuthorId */
protected $defaultAuthorId;

Expand Down Expand Up @@ -96,16 +99,21 @@ public function setSiteAccess(SiteAccess $siteAccess)
}

/**
* Sets `customerId` option when service is created which allows to
* inject parameter value according to siteaccess configuration.
*
* @param string $value
* @param int $value
*/
public function setCustomerId($value)
{
$this->customerId = $value;
}

/**
* @param string $value
*/
public function setLicenseKey($value)
{
$this->licenseKey = $value;
}

/**
* Prepares content for ContentDataValue class.
*
Expand Down Expand Up @@ -166,37 +174,38 @@ protected function prepareContent($data, Request $request)
$requestedFields = $request->get('fields');

$content = array();

foreach ($data as $contentValue) {
$contentValue = $contentValue->valueObject;
$contentType = $this->contentTypeService->loadContentType($contentValue->contentInfo->contentTypeId);
$location = $this->locationService->loadLocation($contentValue->contentInfo->mainLocationId);
$language = (null === $requestLanguage) ? $location->contentInfo->mainLanguageCode : $requestLanguage;
$this->value->setFieldDefinitionsList($contentType);

$content[$contentValue->id] = array(
'contentId' => $contentValue->id,
'contentTypeId' => $contentType->id,
'identifier' => $contentType->identifier,
'language' => $language,
'publishedDate' => $contentValue->contentInfo->publishedDate->format('c'),
'author' => $this->getAuthor($contentValue, $contentType),
'uri' => $this->generator->generate($location, array(), false),
'mainLocation' => array(
'href' => '/api/ezp/v2/content/locations' . $location->pathString,
),
'locations' => array(
'href' => '/api/ezp/v2/content/objects/' . $contentValue->id . '/locations',
),
'categoryPath' => $location->pathString,
'fields' => array(),
);

$fields = $this->prepareFields($contentType, $requestedFields);
if (!empty($fields)) {
foreach ($fields as $field) {
$field = $this->value->getConfiguredFieldIdentifier($field, $contentType);
$content[$contentValue->id]['fields'][] = $this->value->getFieldValue($contentValue, $field, $language);
foreach ($data as $contentTypeId => $items) {
foreach ($items as $contentValue) {
$contentValue = $contentValue->valueObject;
$contentType = $this->contentTypeService->loadContentType($contentValue->contentInfo->contentTypeId);
$location = $this->locationService->loadLocation($contentValue->contentInfo->mainLocationId);
$language = (null === $requestLanguage) ? $location->contentInfo->mainLanguageCode : $requestLanguage;
$this->value->setFieldDefinitionsList($contentType);

$content[$contentTypeId][$contentValue->id] = array(
'contentId' => $contentValue->id,
'contentTypeId' => $contentType->id,
'identifier' => $contentType->identifier,
'language' => $language,
'publishedDate' => $contentValue->contentInfo->publishedDate->format('c'),
'author' => $this->getAuthor($contentValue, $contentType),
'uri' => $this->generator->generate($location, array(), false),
'mainLocation' => array(
'href' => '/api/ezp/v2/content/locations' . $location->pathString,
),
'locations' => array(
'href' => '/api/ezp/v2/content/objects/' . $contentValue->id . '/locations',
),
'categoryPath' => $location->pathString,
'fields' => array(),
);

$fields = $this->prepareFields($contentType, $requestedFields);
if (!empty($fields)) {
foreach ($fields as $field) {
$field = $this->value->getConfiguredFieldIdentifier($field, $contentType);
$content[$contentTypeId][$contentValue->id]['fields'][] = $this->value->getFieldValue($contentValue, $field, $language);
}
}
}
}
Expand Down
41 changes: 24 additions & 17 deletions Rest/Controller/ContentTypeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,12 @@ public function getContentType($contentTypeIdList, $responseType, Request $reque
'responseType' => $responseType,
'chunkSize' => $request->get('page_size', $this->getDeafultPageSize($responseType)),
'documentRoot' => $request->server->get('DOCUMENT_ROOT'),
'webHook' => $request->get('webHook'),
'transaction' => $request->get('transaction', date('YmdHis', time())),
'lang' => $request->get('lang'),
'host' => $request->getSchemeAndHttpHost(),
'customerId' => $this->customerId,
'licenseKey' => $this->licenseKey,
'contentTypeIds' => $contentTypeIds,
]);
}
Expand All @@ -67,30 +71,33 @@ protected function prepareContentByContentTypeIds($contentTypeIds, $responseType
$offset = $page * $pageSize - $pageSize;
$path = $request->get('path');
$hidden = $request->get('hidden');
$contentItems = array();

$criteria = array(new Criterion\ContentTypeId($contentTypeIds));
foreach ($contentTypeIds as $contentTypeId) {
$criteria = array(new Criterion\ContentTypeId($contentTypeId));

if ($path) {
$criteria[] = new Criterion\Subtree($path);
}
if ($path) {
$criteria[] = new Criterion\Subtree($path);
}

if (!$hidden) {
$criteria[] = new Criterion\Visibility(Criterion\Visibility::VISIBLE);
}
if (!$hidden) {
$criteria[] = new Criterion\Visibility(Criterion\Visibility::VISIBLE);
}

$siteAccess = $request->get('sa', $this->siteAccess->name);
$rootLocationId = $this->configResolver->getParameter('content.tree_root.location_id', null, $siteAccess);
$criteria[] = new Criterion\Subtree($this->locationService->loadLocation($rootLocationId)->pathString);
$siteAccess = $request->get('sa', $this->siteAccess->name);
$rootLocationId = $this->configResolver->getParameter('content.tree_root.location_id', null, $siteAccess);
$criteria[] = new Criterion\Subtree($this->locationService->loadLocation($rootLocationId)->pathString);

$query = new Query();
$query->query = new Criterion\LogicalAnd($criteria);
$query = new Query();
$query->query = new Criterion\LogicalAnd($criteria);

if ($responseType != 'export') {
$query->limit = $pageSize;
$query->offset = $offset;
}
if ($responseType != 'export') {
$query->limit = $pageSize;
$query->offset = $offset;
}

$contentItems = $this->searchService->findContent($query)->searchHits;
$contentItems[$contentTypeId] = $this->searchService->findContent($query)->searchHits;
}

return $this->prepareContent($contentItems, $request);
}
Expand Down
67 changes: 38 additions & 29 deletions Rest/Response/ExportResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,25 @@ public function render(Generator $generator, $data)
}

$chunkDir = $this->createChunkDir($data->options['documentRoot']);
$chunkDirPath = $data->options['documentRoot'] . '/var/export' . $chunkDir;
$chunks = array_chunk($data->contents, $data->options['chunkSize']);

$urls = [];
touch($data->options['documentRoot'] . '/var/export/.lock');
$chunkDirPath = $data->options['documentRoot'] . '/var/export' . $chunkDir;
foreach ($data->contents as $contentTypeId => $items) {
$chunks = array_chunk($items, $data->options['chunkSize']);

foreach ($chunks as $id => $chunk) {
$chunkPath = $chunkDirPath . $id;
touch($data->options['documentRoot'] . '/var/export/.lock');

$generator->reset();
$generator->startDocument($chunk);
foreach ($chunks as $id => $chunk) {
$chunkPath = $chunkDirPath . $contentTypeId . $id;

$this->contentListElementGenerator->generateElement($generator, $chunk);
$generator->reset();
$generator->startDocument($chunk);

file_put_contents($chunkPath, $generator->endDocument($chunk));
$this->contentListElementGenerator->generateElement($generator, $chunk);

$urls[] = sprintf('%s/api/ezp/v2/ez_recommendation/v1/exportDownload%s%s', $data->options['host'], $chunkDir, $id);
file_put_contents($chunkPath, $generator->endDocument($chunk));
$urls[$contentTypeId][] = sprintf('%s/api/ezp/v2/ez_recommendation/v1/exportDownload%s%s', $data->options['host'], $chunkDir, $contentTypeId . $id);
}
}

unlink($data->options['documentRoot'] . '/var/export/.lock');
Expand Down Expand Up @@ -80,40 +82,47 @@ private function createChunkDir($documentRoot)
}

/**
* @param string $urls
* @param array $urls
* @param array $options
* @param string $chunkDirPath
*
* @return \Psr\Http\Message\StreamInterface|string
*/
private function sendYCResponse($urls, $options, $chunkDirPath)
private function sendYCResponse(array $urls, $options, $chunkDirPath)
{
$guzzle = new Client([
'base_uri' => $this->apiEndpoint . '/api/',
'base_uri' => $options['webHook'],
]);

$contentTypeId = count($options['contentTypeIds']) == 1
? array_pop($options['contentTypeIds'])
: $options['contentTypeIds'];
$events = [];
$password = $this->secureDir($chunkDirPath);

foreach ($urls as $contentTypeId => $urlList) {
$events[] = [
'action' => 'FULL',
'format' => 'EZ',
'contentTypeId' => $contentTypeId,
'lang' => $options['lang'],
'uri' => $urlList,
'credentials' => [
'login' => 'yc',
'password' => $password,
],
];
}

try {
$response = $guzzle->send(
new Request(
'POST',
sprintf('%s/items', $options['customerId']),
[],
'',
[
'Content-Type' => 'application/json',
'Authorization' => 'Basic ' . base64_encode($options['customerId'] . ':' . $options['licenseKey']),
],
json_encode([
'transaction' => null,
'events' => [
'action' => 'FULL',
'format' => 'EZ',
'contentTypeId' => $contentTypeId,
'uri' => $urls,
'credentials' => [
'login' => 'yc',
'password' => $this->secureDir($chunkDirPath),
],
],
'transaction' => $options['transaction'],
'events' => $events,
])
)
)->getBody();
Expand Down
9 changes: 8 additions & 1 deletion Rest/Response/HttpResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ class HttpResponse extends Response
{
public function render(Generator $generator, $data)
{
return $this->contentListElementGenerator->generateElement($generator, $data->contents);
$contents = array();
foreach ($data->contents as $contentTypes) {
foreach ($contentTypes as $contentType) {
$contents[] = $contentType;
}
}

return $this->contentListElementGenerator->generateElement($generator, $contents);
}
}

0 comments on commit 7b9b4a3

Please sign in to comment.