10
10
use eZ \Publish \API \Repository \LocationService ;
11
11
use eZ \Publish \API \Repository \SearchService ;
12
12
use eZ \Publish \API \Repository \Values \Content \Content ;
13
+ use eZ \Publish \API \Repository \Values \Content \Location ;
13
14
use eZ \Publish \API \Repository \Values \Content \Query ;
14
15
use eZ \Publish \API \Repository \Values \Content \Search \SearchHit ;
15
16
use eZ \Publish \API \Repository \Values \ContentType \FieldDefinition ;
21
22
/**
22
23
* Executes a query and returns the results.
23
24
*/
24
- final class QueryFieldService implements QueryFieldServiceInterface
25
+ final class QueryFieldService implements QueryFieldServiceInterface, QueryFieldLocationService
25
26
{
26
27
/** @var \eZ\Publish\Core\QueryType\QueryTypeRegistry */
27
28
private $ queryTypeRegistry ;
@@ -48,29 +49,33 @@ public function __construct(
48
49
$ this ->queryTypeRegistry = $ queryTypeRegistry ;
49
50
}
50
51
51
- /**
52
- * @param \eZ\Publish\API\Repository\Values\Content\Content $content
53
- * @param string $fieldDefinitionIdentifier
54
- *
55
- * @return \eZ\Publish\API\Repository\Values\Content\Content[]
56
- *
57
- * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
58
- */
59
52
public function loadContentItems (Content $ content , string $ fieldDefinitionIdentifier ): iterable
60
53
{
61
- $ query = $ this ->prepareQuery ($ content , $ fieldDefinitionIdentifier );
54
+ $ query = $ this ->prepareQuery ($ content , $ content -> contentInfo -> getMainLocation (), $ fieldDefinitionIdentifier );
62
55
63
- return array_map (
64
- function (SearchHit $ searchHit ) {
65
- return $ searchHit ->valueObject ;
66
- },
67
- $ this ->searchService ->findContent ($ query )->searchHits
68
- );
56
+ return $ this ->executeQueryAndMapResult ($ query );
57
+ }
58
+
59
+ public function loadContentItemsForLocation (Location $ location , string $ fieldDefinitionIdentifier ): iterable
60
+ {
61
+ $ query = $ this ->prepareQuery ($ location ->getContent (), $ location , $ fieldDefinitionIdentifier );
62
+
63
+ return $ this ->executeQueryAndMapResult ($ query );
69
64
}
70
65
71
66
public function countContentItems (Content $ content , string $ fieldDefinitionIdentifier ): int
72
67
{
73
- $ query = $ this ->prepareQuery ($ content , $ fieldDefinitionIdentifier );
68
+ $ query = $ this ->prepareQuery ($ content , $ content ->contentInfo ->getMainLocation (), $ fieldDefinitionIdentifier );
69
+ $ query ->limit = 0 ;
70
+
71
+ $ count = $ this ->searchService ->findContent ($ query )->totalCount - $ query ->offset ;
72
+
73
+ return $ count < 0 ? 0 : $ count ;
74
+ }
75
+
76
+ public function countContentItemsForLocation (Location $ location , string $ fieldDefinitionIdentifier ): int
77
+ {
78
+ $ query = $ this ->prepareQuery ($ location ->getContent (), $ location , $ fieldDefinitionIdentifier );
74
79
$ query ->limit = 0 ;
75
80
76
81
$ count = $ this ->searchService ->findContent ($ query )->totalCount - $ query ->offset ;
@@ -80,34 +85,35 @@ public function countContentItems(Content $content, string $fieldDefinitionIdent
80
85
81
86
public function loadContentItemsSlice (Content $ content , string $ fieldDefinitionIdentifier , int $ offset , int $ limit ): iterable
82
87
{
83
- $ query = $ this ->prepareQuery ($ content , $ fieldDefinitionIdentifier );
84
- $ query ->offset + = $ offset ;
88
+ $ query = $ this ->prepareQuery ($ content , $ content -> contentInfo -> getMainLocation (), $ fieldDefinitionIdentifier );
89
+ $ query ->offset = $ offset ;
85
90
$ query ->limit = $ limit ;
86
91
87
- return array_map (
88
- function (SearchHit $ searchHit ) {
89
- return $ searchHit ->valueObject ;
90
- },
91
- $ this ->searchService ->findContent ($ query )->searchHits
92
- );
92
+ return $ this ->executeQueryAndMapResult ($ query );
93
+ }
94
+
95
+ public function loadContentItemsSliceForLocation (Location $ location , string $ fieldDefinitionIdentifier , int $ offset , int $ limit ): iterable
96
+ {
97
+ $ query = $ this ->prepareQuery ($ location ->getContent (), $ location , $ fieldDefinitionIdentifier );
98
+ $ query ->offset = $ offset ;
99
+ $ query ->limit = $ limit ;
100
+
101
+ return $ this ->executeQueryAndMapResult ($ query );
93
102
}
94
103
95
104
public function getPaginationConfiguration (Content $ content , string $ fieldDefinitionIdentifier ): int
96
105
{
97
106
$ fieldDefinition = $ this ->loadFieldDefinition ($ content , $ fieldDefinitionIdentifier );
98
107
99
108
if ($ fieldDefinition ->fieldSettings ['EnablePagination ' ] === false ) {
100
- return false ;
109
+ return 0 ;
101
110
}
102
111
103
112
return $ fieldDefinition ->fieldSettings ['ItemsPerPage ' ];
104
113
}
105
114
106
115
/**
107
116
* @param array $expressions parameters that may include expressions to be resolved
108
- * @param array $variables
109
- *
110
- * @return array
111
117
*/
112
118
private function resolveParameters (array $ expressions , array $ variables ): array
113
119
{
@@ -130,11 +136,6 @@ private function isExpression($expression): bool
130
136
}
131
137
132
138
/**
133
- * @param string $expression
134
- * @param array $variables
135
- *
136
- * @return mixed
137
- *
138
139
* @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException if $expression is not an expression.
139
140
*/
140
141
private function resolveExpression (string $ expression , array $ variables )
@@ -146,20 +147,10 @@ private function resolveExpression(string $expression, array $variables)
146
147
return (new ExpressionLanguage ())->evaluate (substr ($ expression , 2 ), $ variables );
147
148
}
148
149
149
- /**
150
- * @param \eZ\Publish\API\Repository\Values\Content\Content $content
151
- * @param string $fieldDefinitionIdentifier
152
- *
153
- * @return \eZ\Publish\API\Repository\Values\Content\Query
154
- *
155
- * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
156
- * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
157
- */
158
- private function prepareQuery (Content $ content , string $ fieldDefinitionIdentifier , array $ extraParameters = []): Query
150
+ private function prepareQuery (Content $ content , Location $ location , string $ fieldDefinitionIdentifier , array $ extraParameters = []): Query
159
151
{
160
152
$ fieldDefinition = $ this ->loadFieldDefinition ($ content , $ fieldDefinitionIdentifier );
161
153
162
- $ location = $ this ->locationService ->loadLocation ($ content ->contentInfo ->mainLocationId );
163
154
$ queryType = $ this ->queryTypeRegistry ->getQueryType ($ fieldDefinition ->fieldSettings ['QueryType ' ]);
164
155
$ parameters = $ this ->resolveParameters (
165
156
$ fieldDefinition ->fieldSettings ['Parameters ' ],
@@ -168,7 +159,8 @@ private function prepareQuery(Content $content, string $fieldDefinitionIdentifie
168
159
[
169
160
'content ' => $ content ,
170
161
'contentInfo ' => $ content ->contentInfo ,
171
- 'mainLocation ' => $ location ,
162
+ 'location ' => $ location ,
163
+ 'mainLocation ' => $ location ->id === $ content ->contentInfo ->mainLocationId ? $ location : $ content ->contentInfo ->getMainLocation (),
172
164
'returnedType ' => $ fieldDefinition ->fieldSettings ['ReturnedType ' ],
173
165
]
174
166
)
@@ -178,11 +170,6 @@ private function prepareQuery(Content $content, string $fieldDefinitionIdentifie
178
170
}
179
171
180
172
/**
181
- * @param \eZ\Publish\API\Repository\Values\Content\Content $content
182
- * @param string $fieldDefinitionIdentifier
183
- *
184
- * @return \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition|null
185
- *
186
173
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
187
174
*/
188
175
private function loadFieldDefinition (Content $ content , string $ fieldDefinitionIdentifier ): FieldDefinition
@@ -199,4 +186,19 @@ private function loadFieldDefinition(Content $content, string $fieldDefinitionId
199
186
200
187
return $ fieldDefinition ;
201
188
}
189
+
190
+ /**
191
+ * @return \eZ\Publish\API\Repository\Values\Content\Content[]
192
+ *
193
+ * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
194
+ */
195
+ private function executeQueryAndMapResult (Query $ query ): array
196
+ {
197
+ return array_map (
198
+ static function (SearchHit $ searchHit ): Content {
199
+ return $ searchHit ->valueObject ;
200
+ },
201
+ $ this ->searchService ->findContent ($ query )->searchHits
202
+ );
203
+ }
202
204
}
0 commit comments