1 <?php
2 3 4 5 6 7
8
9 defined('FOF_INCLUDED') or die;
10
11 12 13 14 15 16 17
18 class FOFViewJson extends FOFViewHtml
19 {
20 21 22 23 24 25
26 public $useHypermedia = false;
27
28 29 30 31 32
33 public function __construct($config = array())
34 {
35 parent::__construct($config);
36
37 if (isset($config['use_hypermedia']))
38 {
39 $this->useHypermedia = (bool) $config['use_hypermedia'];
40 }
41 }
42
43 44 45 46 47 48 49
50 protected function onDisplay($tpl = null)
51 {
52
53 $model = $this->getModel();
54
55 $items = $model->getItemList();
56 $this->items = $items;
57
58 $document = FOFPlatform::getInstance()->getDocument();
59
60 if ($document instanceof JDocument)
61 {
62 if ($this->useHypermedia)
63 {
64 $document->setMimeEncoding('application/hal+json');
65 }
66 else
67 {
68 $document->setMimeEncoding('application/json');
69 }
70 }
71
72 if (is_null($tpl))
73 {
74 $tpl = 'json';
75 }
76
77 FOFPlatform::getInstance()->setErrorHandling(E_ALL, 'ignore');
78
79 $hasFailed = false;
80
81 try
82 {
83 $result = $this->loadTemplate($tpl, true);
84
85 if ($result instanceof Exception)
86 {
87 $hasFailed = true;
88 }
89 }
90 catch (Exception $e)
91 {
92 $hasFailed = true;
93 }
94
95 if ($hasFailed)
96 {
97
98 if ($this->useHypermedia)
99 {
100 $haldocument = $this->_createDocumentWithHypermedia($items, $model);
101 $json = $haldocument->render('json');
102 }
103 else
104 {
105 $json = json_encode($items);
106 }
107
108
109 $callback = $this->input->get('callback', null, 'raw');
110
111 if (!empty($callback))
112 {
113 echo $callback . '(' . $json . ')';
114 }
115 else
116 {
117 $defaultName = $this->input->getCmd('view', 'joomla');
118 $filename = $this->input->getCmd('basename', $defaultName);
119
120 $document->setName($filename);
121 echo $json;
122 }
123
124 return false;
125 }
126 else
127 {
128 echo $result;
129
130 return false;
131 }
132 }
133
134 135 136 137 138 139 140
141 protected function onRead($tpl = null)
142 {
143 $model = $this->getModel();
144
145 $item = $model->getItem();
146 $this->item = $item;
147
148 $document = FOFPlatform::getInstance()->getDocument();
149
150 if ($document instanceof JDocument)
151 {
152 if ($this->useHypermedia)
153 {
154 $document->setMimeEncoding('application/hal+json');
155 }
156 else
157 {
158 $document->setMimeEncoding('application/json');
159 }
160 }
161
162 if (is_null($tpl))
163 {
164 $tpl = 'json';
165 }
166
167 FOFPlatform::getInstance()->setErrorHandling(E_ALL, 'ignore');
168
169 $hasFailed = false;
170
171 try
172 {
173 $result = $this->loadTemplate($tpl, true);
174
175 if ($result instanceof Exception)
176 {
177 $hasFailed = true;
178 }
179 }
180 catch (Exception $e)
181 {
182 $hasFailed = true;
183 }
184
185 if ($hasFailed)
186 {
187
188
189 if ($this->useHypermedia)
190 {
191 $haldocument = $this->_createDocumentWithHypermedia($item, $model);
192 $json = $haldocument->render('json');
193 }
194 else
195 {
196 $json = json_encode($item);
197 }
198
199
200 $callback = $this->input->get('callback', null);
201
202 if (!empty($callback))
203 {
204 echo $callback . '(' . $json . ')';
205 }
206 else
207 {
208 $defaultName = $this->input->getCmd('view', 'joomla');
209 $filename = $this->input->getCmd('basename', $defaultName);
210 $document->setName($filename);
211 echo $json;
212 }
213
214 return false;
215 }
216 else
217 {
218 echo $result;
219
220 return false;
221 }
222 }
223
224 225 226 227 228 229 230 231
232 protected function _createDocumentWithHypermedia($data, $model = null)
233 {
234
235
236 if (is_array($data))
237 {
238 $count = count($data);
239 }
240 else
241 {
242 $count = null;
243 }
244
245 if ($count == 1)
246 {
247 reset($data);
248 $document = new FOFHalDocument(end($data));
249 }
250 else
251 {
252 $document = new FOFHalDocument($data);
253 }
254
255
256 $uri = (string) (JUri::getInstance());
257 $uri = $this->_removeURIBase($uri);
258 $uri = JRoute::_($uri);
259 $document->addLink('self', new FOFHalLink($uri));
260
261
262
263 if (is_array($data) && ($model instanceof FOFModel))
264 {
265 $pagination = $model->getPagination();
266
267 if ($pagination->get('pages.total') > 1)
268 {
269
270
271 $protoUri = $this->_getPrototypeURIForPagination();
272
273
274 $uri = clone $protoUri;
275 $uri->setVar('limitstart', 0);
276 $uri = JRoute::_((string) $uri);
277
278 $document->addLink('first', new FOFHalLink($uri));
279
280
281
282 if ($pagination->get('pages.current') > 1)
283 {
284 $prevPage = $pagination->get('pages.current') - 1;
285 $limitstart = ($prevPage - 1) * $pagination->limit;
286 $uri = clone $protoUri;
287 $uri->setVar('limitstart', $limitstart);
288 $uri = JRoute::_((string) $uri);
289
290 $document->addLink('prev', new FOFHalLink($uri));
291 }
292
293
294
295 if ($pagination->get('pages.current') < $pagination->get('pages.total'))
296 {
297 $nextPage = $pagination->get('pages.current') + 1;
298 $limitstart = ($nextPage - 1) * $pagination->limit;
299 $uri = clone $protoUri;
300 $uri->setVar('limitstart', $limitstart);
301 $uri = JRoute::_((string) $uri);
302
303 $document->addLink('next', new FOFHalLink($uri));
304 }
305
306
307 $lastPage = $pagination->get('pages.total');
308 $limitstart = ($lastPage - 1) * $pagination->limit;
309 $uri = clone $protoUri;
310 $uri->setVar('limitstart', $limitstart);
311 $uri = JRoute::_((string) $uri);
312
313 $document->addLink('last', new FOFHalLink($uri));
314 }
315 }
316
317 return $document;
318 }
319
320 321 322 323 324 325 326
327 protected function _removeURIBase($uri)
328 {
329 static $root = null, $rootlen = 0;
330
331 if (is_null($root))
332 {
333 $root = rtrim(FOFPlatform::getInstance()->URIbase(), '/');
334 $rootlen = strlen($root);
335 }
336
337 if (substr($uri, 0, $rootlen) == $root)
338 {
339 $uri = substr($uri, $rootlen);
340 }
341
342 return ltrim($uri, '/');
343 }
344
345 346 347 348 349 350
351 protected function ()
352 {
353 $protoUri = new JUri('index.php');
354 $protoUri->setQuery($this->input->getData());
355 $protoUri->delVar('savestate');
356 $protoUri->delVar('base_path');
357
358 return $protoUri;
359 }
360 }
361