Skip to content

Commit

Permalink
Created helpers for parsing field values
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamil Musiał committed Sep 1, 2015
1 parent 9616d9a commit 99ff4ff
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 181 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -58,7 +58,7 @@ The URI your site's REST API can be accessed from.
### `recommender.included_content_types`
This allows you to define content types on which tracking script will be shown. Go to the Tracking section to get more details.

If content's author or image are stored in different field, you can specify it in __default_settings.yml__
If content's author or image are stored in different field, you can specify it in __parameters.yml__

```yaml
ez_recommendation.field_identifiers:
Expand Down
18 changes: 16 additions & 2 deletions Resources/config/services.yml
Expand Up @@ -102,16 +102,30 @@ services:
ez_recommendation.rest.controller.content:
class: %ez_recommendation.rest.controller.content.class%
arguments:
- @ezpublish.config.resolver.core
- @ezpublish.image_alias.imagine.alias_generator
- @ezpublish.urlalias_router
- @ezpublish.api.service.content
- @ezpublish.api.service.location
- @ezpublish.api.service.content_type
- @ezpublish.api.service.search
- @ez_recommendation.rest.field.value
parent: ezpublish_rest.controller.base

ez_recommendation.rest.controller.contenttype:
class: %ez_recommendation.rest.controller.contenttype.class%
parent: ez_recommendation.rest.controller.content

ez_recommendation.rest.field.value:
class: EzSystems\RecommendationBundle\Rest\Field\Value
arguments:
- @ezpublish.api.service.content
- @ezpublish.api.service.content_type
- @ez_recommendation.rest.field.type_value
- {fieldIdentifiers: %ez_recommendation.field_identifiers%}

ez_recommendation.rest.field.type_value:
class: EzSystems\RecommendationBundle\Rest\Field\TypeValue
arguments:
- @request_stack
- @ezpublish.config.resolver.core
- @ezpublish.fieldtype.ezxmltext.converter.html5
- @ezpublish.image_alias.imagine.alias_generator
192 changes: 14 additions & 178 deletions Rest/Controller/ContentController.php
Expand Up @@ -8,16 +8,14 @@
*/
namespace EzSystems\RecommendationBundle\Rest\Controller;

use eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator as ImageVariationService;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Field;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\Core\MVC\ConfigResolverInterface;
use eZ\Publish\Core\REST\Server\Controller as BaseController;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\LocationService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\SearchService;
use EzSystems\RecommendationBundle\Rest\Field\Value as FieldValue;
use EzSystems\RecommendationBundle\Rest\Values\ContentData as ContentDataValue;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface as UrlGenerator;

Expand All @@ -26,12 +24,6 @@
*/
class ContentController extends BaseController
{
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
protected $configResolver;

/** @var \eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator */
protected $imageVariationService;

/** @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface */
protected $generator;

Expand All @@ -47,31 +39,31 @@ class ContentController extends BaseController
/** @var \eZ\Publish\Core\Repository\SearchService */
protected $searchService;

/** @var \EzSystems\RecommendationBundle\Rest\Field\Value */
protected $value;

/**
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
* @param \eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator $imageVariationService
* @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $generator
* @param \eZ\Publish\API\Repository\ContentService $contentService
* @param \eZ\Publish\API\Repository\LocationService $locationService
* @param \eZ\Publish\API\Repository\ContentTypeService $contentTypeService
* @param \eZ\Publish\API\Repository\SearchService $searchService
* @param \EzSystems\RecommendationBundle\Rest\Field\Value $value
*/
public function __construct(
ConfigResolverInterface $configResolver,
ImageVariationService $imageVariationService,
UrlGenerator $generator,
ContentService $contentService,
LocationService $locationService,
ContentTypeService $contentTypeService,
SearchService $searchService
SearchService $searchService,
FieldValue $value
) {
$this->configResolver = $configResolver;
$this->imageVariationService = $imageVariationService;
$this->generator = $generator;
$this->contentService = $contentService;
$this->locationService = $locationService;
$this->contentTypeService = $contentTypeService;
$this->searchService = $searchService;
$this->value = $value;
}

/**
Expand Down Expand Up @@ -114,6 +106,7 @@ protected function prepareContent($contentIds)
$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,
Expand All @@ -134,30 +127,10 @@ protected function prepareContent($contentIds)
);

$fields = $this->prepareFields($contentType, $requestedFields);
$imageFieldIdentifier = $this->getImageFieldIdentifier($contentId, $language);

if (!empty($fields)) {
foreach ($fields as $field) {
$fieldValue = $contentValue->getFieldValue($field, $language);

$relatedField = $this->getRelation($contentValue, $field, $imageFieldIdentifier, $language);

if (null === $fieldValue && !$relatedField) {
continue;
}

if ($relatedField) {
$fieldValue = $this->getRelatedFieldValue($relatedField, $language, $imageFieldIdentifier);
$field = $imageFieldIdentifier;
} elseif ($field == $imageFieldIdentifier) {
$fieldObj = $contentValue->getFieldsByLanguage($language);
$fieldValue = $this->imageVariations($fieldObj[$field], $contentValue->versionInfo, $this->request->get('image'));
}

$content[$contentId]['fields'][] = array(
'key' => $field,
'value' => (string) $fieldValue,
);
$field = $this->value->getConfiguredFieldIdentifier($field, $contentType);
$content[$contentId]['fields'][] = $this->value->getFieldValue($contentValue, $field, $language);
}
}
}
Expand Down Expand Up @@ -189,113 +162,6 @@ protected function prepareFields(ContentType $contentType, $fields = null)
return $fields;
}

/**
* Returns image uri based on variation provided in url.
* If none is set original is returned.
*
* @param \eZ\Publish\API\Repository\Values\Content\Field $fieldValue
* @param \eZ\Publish\API\Repository\Values\Content\VersionInfo $versionInfo
* @param string|null $variation
*
* @return string
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidVariationException
* @throws \eZ\Publish\Core\MVC\Exception\SourceImageNotFoundException
*/
private function imageVariations(Field $fieldValue, $versionInfo, $variation = null)
{
if (!isset($fieldValue->value->id)) {
return '';
}

$variations = $this->configResolver->getParameter('image_variations');

if ((null === $variation) || !in_array($variation, array_keys($variations))) {
$variation = 'original';
}

return $this->imageVariationService->getVariation($fieldValue, $versionInfo, $variation)->uri;
}

/**
* Return uri of the related image field.
*
* @param mixed $contentId
* @param string $language
* @param string $imageFieldIdentifier
*
* @return string
*/
private function getRelatedFieldValue($contentId, $language, $imageFieldIdentifier)
{
$content = $this->contentService->loadContent($contentId);
$fieldObj = $content->getFieldsByLanguage($language);

if (!isset($fieldObj[$imageFieldIdentifier])) {
return '';
}

return $this->imageVariations($fieldObj[$imageFieldIdentifier], $content->versionInfo, $this->request->get('image'));
}

/**
* Return identifier of a field of ezimage type.
*
* @param mixed $contentId
* @param string $language
*
* @return string
*/
private function getImageFieldIdentifier($contentId, $language)
{
$content = $this->contentService->loadContent($contentId);
$contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId);

if ($identifier = $this->getConfiguredFieldIdentifier('image', $contentType)) {
return $identifier;
}

foreach ($contentType->fieldDefinitions as $fieldDefinition) {
if ($fieldDefinition->fieldTypeIdentifier == 'ezimage') {
return $fieldDefinition->identifier;
} elseif ($fieldDefinition->fieldTypeIdentifier == 'ezobjectrelation') {
$field = $content->getFieldValue($fieldDefinition->identifier, $language);

return $this->getImageFieldIdentifier($field->destinationContentId, $language);
}
}

return false;
}

/**
* Checks if content has image relation field, returns its ID if true.
*
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
* @param string $field
* @param string $imageFieldIdentifier
* @param string $language
*
* @return int|null
*/
private function getRelation(Content $content, $field, $imageFieldIdentifier, $language)
{
$contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId);

foreach ($contentType->fieldDefinitions as $fieldDef) {
$isRelation = ($fieldDef->fieldTypeIdentifier == 'ezobjectrelation' && $field == $fieldDef->identifier);
if ($isRelation || $field == $imageFieldIdentifier) {
$fieldValue = $content->getFieldValue($fieldDef->identifier, $language);

if (isset($fieldValue->destinationContentId)) {
return $fieldValue->destinationContentId;
}
}
}

return false;
}

/**
* Returns author of the content.
*
Expand All @@ -306,7 +172,9 @@ private function getRelation(Content $content, $field, $imageFieldIdentifier, $l
*/
private function getAuthor(Content $contentValue, ContentType $contentType)
{
$author = $contentValue->getFieldValue($this->getConfiguredFieldIdentifier('author', $contentType));
$author = $contentValue->getFieldValue(
$this->value->getConfiguredFieldIdentifier('author', $contentType)
);

if (null === $author) {
$userContentInfo = $this->contentService->loadContentInfo($contentValue->contentInfo->ownerId);
Expand All @@ -315,36 +183,4 @@ private function getAuthor(Content $contentValue, ContentType $contentType)

return (string) $author;
}

/**
* Returns field name.
*
* To define another field name for specific value (e. g. author) add it to default_settings.yml
*
* For example:
*
* ez_recommendation.field_identifiers:
* author:
* blog_post: authors
* image:
* blog_post: thumbnail
*
* @param string $fieldName
* @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType
*
* @return string
*/
private function getConfiguredFieldIdentifier($fieldName, ContentType $contentType)
{
$contentTypeName = $contentType->identifier;
if ($this->container->hasParameter('ez_recommendation.field_identifiers')) {
$fieldDefinitions = $this->container->getParameter('ez_recommendation.field_identifiers');

if (isset($fieldDefinitions[$fieldName]) && !empty($fieldDefinitions[$fieldName][$contentTypeName])) {
return $fieldDefinitions[$fieldName][$contentTypeName];
}
} else {
return $fieldName;
}
}
}

0 comments on commit 99ff4ff

Please sign in to comment.