protected function renderSingleItem($tpl)
{
// Load the model
/** @var DataModel $model */
$model = $this->getModel();
$result = '';
if (!$this->alreadyLoaded) {
$this->item = $model->find();
}
$document = $this->container->platform->getDocument();
/** @var \JDocumentJSON $document */
if ($document instanceof \JDocument) {
if ($this->useHypermedia) {
$document->setMimeEncoding('application/hal+json');
} else {
$document->setMimeEncoding('application/json');
}
}
if (is_null($tpl)) {
$tpl = 'json';
}
$hasFailed = false;
try {
$result = $this->loadTemplate($tpl, true);
if ($result instanceof \Exception) {
$hasFailed = true;
}
} catch (\Exception $e) {
$hasFailed = true;
}
if ($hasFailed) {
// Default JSON behaviour in case the template isn't there!
if ($this->useHypermedia) {
$haldocument = $this->_createDocumentWithHypermedia($this->item, $model);
$json = $haldocument->render('json');
} else {
if (is_object($this->item) && method_exists($this->item, 'toArray')) {
$data = $this->item->toArray();
} else {
$data = $this->item;
}
if (version_compare(PHP_VERSION, '5.4', 'ge')) {
$json = json_encode($data, JSON_PRETTY_PRINT);
} else {
$json = json_encode($data);
}
}
// JSONP support
$callback = $this->input->get('callback', null);
if (!empty($callback)) {
echo $callback . '(' . $json . ')';
} else {
$defaultName = $this->input->get('view', 'main', 'cmd');
$filename = $this->input->get('basename', $defaultName, 'cmd');
$document->setName($filename);
echo $json;
}
} else {
echo $result;
}
}