Skip to content

Commit

Permalink
Merge pull request ezsystems#31 from kamilmusial/EZS-212
Browse files Browse the repository at this point in the history
Path and hidden query parameters
  • Loading branch information
lserwatka committed Sep 3, 2015
2 parents 7d68c7a + f535239 commit 09b1922
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
43 changes: 29 additions & 14 deletions Rest/Controller/ContentController.php
Expand Up @@ -9,6 +9,8 @@
namespace EzSystems\RecommendationBundle\Rest\Controller;

use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\REST\Server\Controller as BaseController;
use eZ\Publish\API\Repository\ContentService;
Expand Down Expand Up @@ -73,43 +75,56 @@ public function __construct(
*
* @return \EzSystems\RecommendationBundle\Rest\Values\ContentData
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundExceptionif the content, version with the given id and languages or content type does not exist
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the content, version with the given id and languages or content type does not exist
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the user has no access to read content and in case of un-published content: read versions
*/
public function getContent($contentIdList)
{
$contentIds = explode(',', $contentIdList);
$content = $this->prepareContent($contentIds);
$lang = $this->request->get('lang');

return new ContentDataValue($content);
$criteria = array(new Criterion\ContentId($contentIds));

if (!$this->request->get('hidden')) {
$criteria[] = new Criterion\Visibility(Criterion\Visibility::VISIBLE);
}
if ($lang) {
$criteria[] = new Criterion\LanguageCode($lang);
}

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

$contentItems = $this->searchService->findContent($query)->searchHits;

$data = $this->prepareContent($contentItems);

return new ContentDataValue($data);
}

/**
* Prepare content array.
*
* @param array $contentIds
* @param array $data
*
* @return array
*/
protected function prepareContent($contentIds)
protected function prepareContent($data)
{
$requestLanguage = $this->request->get('lang');
$requestedFields = $this->request->get('fields');

$content = array();

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

$content[$contentId] = array(
'contentId' => $contentId,
$content[$contentValue->id] = array(
'contentId' => $contentValue->id,
'contentTypeId' => $contentType->id,
'identifier' => $contentType->identifier,
'language' => $language,
Expand All @@ -120,7 +135,7 @@ protected function prepareContent($contentIds)
'href' => '/api/ezp/v2/content/locations' . $location->pathString,
),
'locations' => array(
'href' => '/api/ezp/v2/content/objects/' . $contentId . '/locations',
'href' => '/api/ezp/v2/content/objects/' . $contentValue->id . '/locations',
),
'categoryPath' => $location->pathString,
'fields' => array(),
Expand All @@ -130,7 +145,7 @@ protected function prepareContent($contentIds)
if (!empty($fields)) {
foreach ($fields as $field) {
$field = $this->value->getConfiguredFieldIdentifier($field, $contentType);
$content[$contentId]['fields'][] = $this->value->getFieldValue($contentValue, $field, $language);
$content[$contentValue->id]['fields'][] = $this->value->getFieldValue($contentValue, $field, $language);
}
}
}
Expand Down
27 changes: 13 additions & 14 deletions Rest/Controller/ContentTypeController.php
Expand Up @@ -47,27 +47,26 @@ protected function prepareContentByContentTypeIds($contentTypeIds)
$pageSize = $this->request->get('page_size', 10);
$page = $this->request->get('page', 1);
$offset = $page * $pageSize - $pageSize;
$path = $this->request->get('path');
$hidden = $this->request->get('hidden');

$contentTypesCriterion = array_map(function ($contentId) {
return new Criterion\ContentTypeId($contentId);
}, $contentTypeIds);
$criteria = array(new Criterion\ContentTypeId($contentTypeIds));

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

$query = new Query();
$query->criterion = new Criterion\LogicalAnd(array(
new Criterion\LogicalOr(
$contentTypesCriterion
),
new Criterion\Visibility(Criterion\Visibility::VISIBLE),
));
$query->query = new Criterion\LogicalAnd($criteria);

$query->limit = $pageSize;
$query->offset = $offset;

$content = array();
foreach ($this->searchService->findContent($query)->searchHits as $hit) {
$content[] = $hit->valueObject->id;
}
$contentItems = $this->searchService->findContent($query)->searchHits;

return $this->prepareContent($content);
return $this->prepareContent($contentItems);
}
}

0 comments on commit 09b1922

Please sign in to comment.