FOF30\View\DataView\Json::onBeforeBrowse PHP Метод

onBeforeBrowse() публичный Метод

The event which runs when we are displaying the record list JSON view
public onBeforeBrowse ( string $tpl = null )
$tpl string The sub-template to use
    public function onBeforeBrowse($tpl = null)
    {
        // Load the model
        /** @var DataModel $model */
        $model = $this->getModel();
        $result = '';
        if (!$this->alreadyLoaded) {
            $this->limitStart = $model->getState('limitstart', 0);
            $this->limit = $model->getState('limit', 0);
            $this->items = $model->get(true, $this->limitStart, $this->limit);
            $this->total = $model->count();
        }
        $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) {
                $data = array();
                foreach ($this->items as $item) {
                    if (is_object($item) && method_exists($item, 'toArray')) {
                        $data[] = $item->toArray();
                    } else {
                        $data[] = $item;
                    }
                }
                $HalDocument = $this->_createDocumentWithHypermedia($data, $model);
                $json = $HalDocument->render('json');
            } else {
                $result = array();
                foreach ($this->items as $item) {
                    if (is_object($item) && method_exists($item, 'toArray')) {
                        $result[] = $item->toArray();
                    } else {
                        $result[] = $item;
                    }
                }
                if (version_compare(PHP_VERSION, '5.4', 'ge')) {
                    $json = json_encode($result, JSON_PRETTY_PRINT);
                } else {
                    $json = json_encode($result);
                }
            }
            // JSONP support
            $callback = $this->input->get('callback', null, 'raw');
            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;
        }
    }