|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2017 |
|
6
|
|
|
* @package Client |
|
7
|
|
|
* @subpackage JsonApi |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Client\JsonApi\Attribute; |
|
12
|
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* JSON API standard client |
|
19
|
|
|
* |
|
20
|
|
|
* @package Client |
|
21
|
|
|
* @subpackage JsonApi |
|
22
|
|
|
*/ |
|
23
|
|
|
class Standard |
|
24
|
|
|
extends \Aimeos\Client\JsonApi\Base |
|
25
|
|
|
implements \Aimeos\Client\JsonApi\Iface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Returns the resource or the resource list |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
31
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
32
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
33
|
|
|
*/ |
|
34
|
|
|
public function get( ServerRequestInterface $request, ResponseInterface $response ) |
|
35
|
|
|
{ |
|
36
|
|
|
$view = $this->getView(); |
|
37
|
|
|
|
|
38
|
|
|
try |
|
39
|
|
|
{ |
|
40
|
|
|
if( $view->param( 'id' ) != '' ) { |
|
41
|
|
|
$response = $this->getItem( $view, $request, $response ); |
|
42
|
|
|
} else { |
|
43
|
|
|
$response = $this->getItems( $view, $request, $response ); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$status = 200; |
|
47
|
|
|
} |
|
48
|
|
|
catch( \Aimeos\MShop\Exception $e ) |
|
49
|
|
|
{ |
|
50
|
|
|
$status = 404; |
|
51
|
|
|
$view->errors = $this->getErrorDetails( $e, 'mshop' ); |
|
52
|
|
|
} |
|
53
|
|
|
catch( \Exception $e ) |
|
54
|
|
|
{ |
|
55
|
|
|
$status = 500; |
|
56
|
|
|
$view->errors = $this->getErrorDetails( $e ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** client/jsonapi/attribute/standard/template |
|
60
|
|
|
* Relative path to the attribute lists JSON API template |
|
61
|
|
|
* |
|
62
|
|
|
* The template file contains the code and processing instructions |
|
63
|
|
|
* to generate the result shown in the JSON API body. The |
|
64
|
|
|
* configuration string is the path to the template file relative |
|
65
|
|
|
* to the templates directory (usually in client/jsonapi/templates). |
|
66
|
|
|
* |
|
67
|
|
|
* You can overwrite the template file configuration in extensions and |
|
68
|
|
|
* provide alternative templates. These alternative templates should be |
|
69
|
|
|
* named like the default one but with the string "standard" replaced by |
|
70
|
|
|
* an unique name. You may use the name of your project for this. If |
|
71
|
|
|
* you've implemented an alternative client class as well, "standard" |
|
72
|
|
|
* should be replaced by the name of the new class. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string Relative path to the template creating the body for the GET method of the JSON API |
|
75
|
|
|
* @since 2017.03 |
|
76
|
|
|
* @category Developer |
|
77
|
|
|
*/ |
|
78
|
|
|
$tplconf = 'client/jsonapi/attribute/standard/template'; |
|
79
|
|
|
$default = 'attribute/standard.php'; |
|
80
|
|
|
|
|
81
|
|
|
$body = $view->render( $view->config( $tplconf, $default ) ); |
|
82
|
|
|
|
|
83
|
|
|
return $response->withHeader( 'Allow', 'GET,OPTIONS' ) |
|
84
|
|
|
->withHeader( 'Content-Type', 'application/vnd.api+json' ) |
|
85
|
|
|
->withBody( $view->response()->createStreamFromString( $body ) ) |
|
86
|
|
|
->withStatus( $status ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Returns the available REST verbs and the available parameters |
|
92
|
|
|
* |
|
93
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
94
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
95
|
|
|
* @param string|null $prefix Form parameter prefix when nesting parameters is required |
|
96
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
97
|
|
|
*/ |
|
98
|
|
|
public function options( ServerRequestInterface $request, ResponseInterface $response, $prefix = null ) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->getOptionsResponse( $request, $response, $prefix, 'GET,OPTIONS' ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Retrieves the item and adds the data to the view |
|
106
|
|
|
* |
|
107
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
|
108
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
109
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
110
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getItem( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
$ref = $view->param( 'include', [] ); |
|
115
|
|
|
|
|
116
|
|
|
if( is_string( $ref ) ) { |
|
117
|
|
|
$ref = explode( ',', $ref ); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'attribute' ); |
|
121
|
|
|
|
|
122
|
|
|
$view->items = $cntl->getItem( $view->param( 'id' ), $ref ); |
|
123
|
|
|
$view->total = 1; |
|
124
|
|
|
|
|
125
|
|
|
return $response; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Retrieves the items and adds the data to the view |
|
131
|
|
|
* |
|
132
|
|
|
* @param \Aimeos\MW\View\Iface $view View instance |
|
133
|
|
|
* @param \Psr\Http\Message\ServerRequestInterface $request Request object |
|
134
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response Response object |
|
135
|
|
|
* @return \Psr\Http\Message\ResponseInterface Modified response object |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function getItems( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request, ResponseInterface $response ) |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
/** client/jsonapi/attribute/types |
|
140
|
|
|
* List of attribute types that should be displayed in this order in the catalog filter |
|
141
|
|
|
* |
|
142
|
|
|
* The attribute section in the catalog filter component can display |
|
143
|
|
|
* all attributes a visitor can use to reduce the listed products |
|
144
|
|
|
* to those that contains one or more attributes. By default, all |
|
145
|
|
|
* available attributes will be displayed and ordered by their |
|
146
|
|
|
* attribute type. |
|
147
|
|
|
* |
|
148
|
|
|
* With this setting, you can limit the attribute types to only thoses |
|
149
|
|
|
* whose names are part of the setting value. Furthermore, a particular |
|
150
|
|
|
* order for the attribute types can be enforced that is different |
|
151
|
|
|
* from the standard order. |
|
152
|
|
|
* |
|
153
|
|
|
* @param array List of attribute type codes |
|
154
|
|
|
* @since 2017.03 |
|
155
|
|
|
* @category Developer |
|
156
|
|
|
*/ |
|
157
|
|
|
$attrTypes = $this->getContext()->getConfig()->get( 'client/jsonapi/attribute/types', [] ); |
|
158
|
|
|
|
|
159
|
|
|
$total = 0; |
|
160
|
|
|
$attrMap = []; |
|
161
|
|
|
|
|
162
|
|
|
$ref = $view->param( 'include', [] ); |
|
163
|
|
|
|
|
164
|
|
|
if( is_string( $ref ) ) { |
|
165
|
|
|
$ref = explode( ',', $ref ); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$cntl = \Aimeos\Controller\Frontend\Factory::createController( $this->getContext(), 'attribute' ); |
|
169
|
|
|
|
|
170
|
|
|
$filter = $cntl->createFilter(); |
|
171
|
|
|
$filter = $this->initCriteriaConditions( $filter, $view->param() ); |
|
172
|
|
|
$filter = $cntl->addFilterTypes( $filter, $attrTypes ); |
|
173
|
|
|
|
|
174
|
|
|
$items = $cntl->searchItems( $filter, $ref, $total ); |
|
175
|
|
|
|
|
176
|
|
|
foreach( $items as $id => $item ) { |
|
177
|
|
|
$attrMap[$item->getType()][$id] = $item; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
if( !empty( $attrTypes ) ) |
|
181
|
|
|
{ |
|
182
|
|
|
$sorted = []; |
|
183
|
|
|
|
|
184
|
|
|
foreach( $attrTypes as $type ) |
|
185
|
|
|
{ |
|
186
|
|
|
if( isset( $attrMap[$type] ) ) { |
|
187
|
|
|
$sorted = array_merge( $sorted, $attrMap[$type] ); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$items = $sorted; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$view->items = $items; |
|
195
|
|
|
$view->total = $total; |
|
196
|
|
|
|
|
197
|
|
|
return $response; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.