forked from ezsystems/EzSystemsRecommendationBundle
/
TypeValue.php
148 lines (131 loc) · 4.98 KB
/
TypeValue.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\RecommendationBundle\Rest\Field;
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver;
use eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator as ImageVariationService;
use eZ\Publish\Core\MVC\Exception\SourceImageNotFoundException;
use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Field;
use eZ\Bundle\EzPublishCoreBundle\FieldType\RichText\Converter\Html5 as RichHtml5;
use eZ\Publish\Core\FieldType\XmlText\Converter\Html5 as XmlHtml5;
use Symfony\Component\HttpFoundation\RequestStack;
class TypeValue
{
/** @var \Symfony\Component\HttpFoundation\RequestStack */
private $request;
/** @var \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver */
private $configResolver;
/** @var \eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator */
protected $imageVariationService;
/** @var \eZ\Bundle\EzPublishCoreBundle\FieldType\RichText\Converter\Html5 */
private $richHtml5Converter;
/** @var \eZ\Publish\Core\FieldType\XmlText\Converter\Html5 */
private $xmlHtml5Converter;
/**
* @param \Symfony\Component\HttpFoundation\RequestStack $request
* @param \eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Configuration\ConfigResolver $configResolver
* @param \eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator $imageVariationService
* @param \eZ\Bundle\EzPublishCoreBundle\FieldType\RichText\Converter\Html5 $richHtml5Converter
* @param \eZ\Publish\Core\FieldType\XmlText\Converter\Html5 $xmlHtml5Converter
*/
public function __construct(
RequestStack $request,
ConfigResolver $configResolver,
ImageVariationService $imageVariationService,
RichHtml5 $richHtml5Converter,
XmlHtml5 $xmlHtml5Converter = null
) {
$this->request = $request;
$this->configResolver = $configResolver;
$this->imageVariationService = $imageVariationService;
$this->richHtml5Converter = $richHtml5Converter;
$this->xmlHtml5Converter = $xmlHtml5Converter;
}
/**
* Default field value parsing.
*
* @param string $fieldName
* @param mixed $args
*
* @return string
*/
public function __call($fieldName, $args)
{
$field = array_shift($args);
return (string) $field->value;
}
/**
* Method for parsing ezxmltext field.
*
* @param \eZ\Publish\API\Repository\Values\Content\Field $field
*
* @return string
*/
public function ezxmltext(Field $field)
{
return '<![CDATA[' . $this->xmlHtml5Converter->convert($field->value->xml) . ']]>';
}
/**
* Method for parsing ezrichtext field.
*
* @param \eZ\Publish\API\Repository\Values\Content\Field $field
*
* @return string
*/
public function ezrichtext(Field $field)
{
return '<![CDATA[' . $this->richHtml5Converter->convert($field->value->xml)->saveHTML() . ']]>';
}
/**
* Method for parsing ezimage field.
*
* @param \eZ\Publish\API\Repository\Values\Content\Field $field
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
*
* @return string
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidVariationException
* @throws \eZ\Publish\Core\MVC\Exception\SourceImageNotFoundException
*/
public function ezimage(Field $field, Content $content)
{
if (!isset($field->value->id)) {
return '';
}
$variations = $this->configResolver->getParameter('image_variations');
$variation = 'original';
$requestedVariation = $this->request->getCurrentRequest()->get('image');
if ((null !== $requestedVariation) || in_array($requestedVariation, array_keys($variations))) {
$variation = $requestedVariation;
}
try {
return $this->imageVariationService->getVariation($field, $content->versionInfo, $variation)->uri;
} catch (SourceImageNotFoundException $exception) {
return '';
}
}
/**
* Method for parsing ezobjectrelation field.
* For now related fields refer to images.
*
* @param \eZ\Publish\API\Repository\Values\Content\Field $field
* @param \eZ\Publish\API\Repository\Values\Content\Content $content
* @param string $language
* @param string $imageFieldIdentifier
*
* @return string
*/
public function ezobjectrelation(Field $field, Content $content, $language, $imageFieldIdentifier)
{
$fields = $content->getFieldsByLanguage($language);
foreach ($fields as $type => $field) {
if ($type == $imageFieldIdentifier) {
return $this->ezimage($field, $content);
}
}
return '';
}
}