Source for file json.php

Documentation is available at json.php

  1. <?php
  2. /**
  3.  * @package     FrameworkOnFramework
  4.  * @subpackage  view
  5.  * @copyright   Copyright (C) 2010 - 2012 Akeeba Ltd. All rights reserved.
  6.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  7.  */
  8. // Protect from unauthorized access
  9. defined('_JEXEC'or die;
  10.  
  11. JLoader::import('joomla.application.component.view');
  12.  
  13. /**
  14.  * FrameworkOnFramework JSON View class. Renders the data as a JSON object or
  15.  * array. It can optionally output HAL links as well.
  16.  *
  17.  * @package  FrameworkOnFramework
  18.  * @since    2.0
  19.  */
  20. class FOFViewJson extends FOFViewHtml
  21. {
  22.     /**
  23.      * When set to true we'll add hypermedia to the output, implementing the
  24.      * HAL specification (http://stateless.co/hal_specification.html)
  25.      *
  26.      * @var   boolean 
  27.      */
  28.     public $useHypermedia = false;
  29.  
  30.     /**
  31.      * Public constructor
  32.      *
  33.      * @param   array  $config  The component's configuration array
  34.      */
  35.     public function __construct($config array())
  36.     {
  37.         parent::__construct($config);
  38.  
  39.         if (isset($config['use_hypermedia']))
  40.         {
  41.             $this->useHypermedia = (bool) $config['use_hypermedia'];
  42.         }
  43.     }
  44.  
  45.     /**
  46.      * The event which runs when we are displaying the record list JSON view
  47.      *
  48.      * @param   string  $tpl  The view sub-template to use
  49.      *
  50.      * @return  boolean  True to allow display of the view
  51.      */
  52.     protected function onDisplay($tpl null)
  53.     {
  54.         // Load the model
  55.         $model $this->getModel();
  56.  
  57.         $items $model->getItemList();
  58.         $this->assignRef('items'$items);
  59.  
  60.         $document FOFPlatform::getInstance()->getDocument();
  61.  
  62.         if ($document instanceof JDocument)
  63.         {
  64.             if ($this->useHypermedia)
  65.             {
  66.                 $document->setMimeEncoding('application/hal+json');
  67.             }
  68.             else
  69.             {
  70.                 $document->setMimeEncoding('application/json');
  71.             }
  72.         }
  73.  
  74.         if (is_null($tpl))
  75.         {
  76.             $tpl 'json';
  77.         }
  78.  
  79.         if (FOFPlatform::getInstance()->checkVersion(JVERSION'3.0''lt'))
  80.         {
  81.             FOFPlatform::getInstance()->setErrorHandling(E_ALL'ignore');
  82.         }
  83.  
  84.         $hasFailed false;
  85.  
  86.         try
  87.         {
  88.             $result $this->loadTemplate($tpltrue);
  89.  
  90.             if ($result instanceof Exception)
  91.             {
  92.                 $hasFailed true;
  93.             }
  94.         }
  95.         catch (Exception $e)
  96.         {
  97.             $hasFailed true;
  98.         }
  99.  
  100.         if (FOFPlatform::getInstance()->checkVersion(JVERSION'3.0''lt'))
  101.         {
  102.             if ($result instanceof Exception)
  103.             {
  104.                 $hasFailed true;
  105.             }
  106.         }
  107.  
  108.         if ($hasFailed)
  109.         {
  110.             // Default JSON behaviour in case the template isn't there!
  111.             if ($this->useHypermedia)
  112.             {
  113.                 $haldocument $this->_createDocumentWithHypermedia($items$model);
  114.                 $json $haldocument->render('json');
  115.             }
  116.             else
  117.             {
  118.                 $json json_encode($items);
  119.             }
  120.  
  121.             // JSONP support
  122.             $callback $this->input->getVar('callback'null);
  123.  
  124.             if (!empty($callback))
  125.             {
  126.                 echo $callback '(' $json ')';
  127.             }
  128.             else
  129.             {
  130.                 $defaultName $this->input->getCmd('view''joomla');
  131.                 $filename $this->input->getCmd('basename'$defaultName);
  132.  
  133.                 $document->setName($filename);
  134.                 echo $json;
  135.             }
  136.  
  137.             return false;
  138.         }
  139.         else
  140.         {
  141.             echo $result;
  142.  
  143.             return false;
  144.         }
  145.     }
  146.  
  147.     /**
  148.      * The event which runs when we are displaying a single item JSON view
  149.      *
  150.      * @param   string  $tpl  The view sub-template to use
  151.      *
  152.      * @return  boolean  True to allow display of the view
  153.      */
  154.     protected function onRead($tpl null)
  155.     {
  156.         $model $this->getModel();
  157.  
  158.         $item $model->getItem();
  159.         $this->assign('item'$item);
  160.  
  161.         $document FOFPlatform::getInstance()->getDocument();
  162.  
  163.         if ($document instanceof JDocument)
  164.         {
  165.             if ($this->useHypermedia)
  166.             {
  167.                 $document->setMimeEncoding('application/hal+json');
  168.             }
  169.             else
  170.             {
  171.                 $document->setMimeEncoding('application/json');
  172.             }
  173.         }
  174.  
  175.         if (is_null($tpl))
  176.         {
  177.             $tpl 'json';
  178.         }
  179.  
  180.         if (FOFPlatform::getInstance()->checkVersion(JVERSION'3.0''lt'))
  181.         {
  182.             FOFPlatform::getInstance()->setErrorHandling(E_ALL'ignore');
  183.         }
  184.  
  185.         $hasFailed false;
  186.  
  187.         try
  188.         {
  189.             $result $this->loadTemplate($tpltrue);
  190.  
  191.             if ($result instanceof Exception)
  192.             {
  193.                 $hasFailed true;
  194.             }
  195.         }
  196.         catch (Exception $e)
  197.         {
  198.             $hasFailed true;
  199.         }
  200.  
  201.         if (FOFPlatform::getInstance()->checkVersion(JVERSION'3.0''lt'))
  202.         {
  203.             if ($result instanceof Exception)
  204.             {
  205.                 $hasFailed true;
  206.             }
  207.         }
  208.  
  209.         if ($hasFailed)
  210.         {
  211.             // Default JSON behaviour in case the template isn't there!
  212.  
  213.             if ($this->useHypermedia)
  214.             {
  215.                 $haldocument $this->_createDocumentWithHypermedia($item$model);
  216.                 $json $haldocument->render('json');
  217.             }
  218.             else
  219.             {
  220.                 $json json_encode($item);
  221.             }
  222.  
  223.             // JSONP support
  224.             $callback $this->input->get('callback'null);
  225.  
  226.             if (!empty($callback))
  227.             {
  228.                 echo $callback '(' $json ')';
  229.             }
  230.             else
  231.             {
  232.                 $defaultName $this->input->getCmd('view''joomla');
  233.                 $filename $this->input->getCmd('basename'$defaultName);
  234.                 $document->setName($filename);
  235.                 echo $json;
  236.             }
  237.  
  238.             return false;
  239.         }
  240.         else
  241.         {
  242.             echo $result;
  243.  
  244.             return false;
  245.         }
  246.     }
  247.  
  248.     /**
  249.      * Creates a FOFHalDocument using the provided data
  250.      *
  251.      * @param   array     $data   The data to put in the document
  252.      * @param   FOFModel  $model  The model of this view
  253.      *
  254.      * @return  FOFHalDocument  A HAL-enabled document
  255.      */
  256.     protected function _createDocumentWithHypermedia($data$model null)
  257.     {
  258.         // Create a new HAL document
  259.  
  260.         if (is_array($data))
  261.         {
  262.             $count count($data);
  263.         }
  264.         else
  265.         {
  266.             $count null;
  267.         }
  268.  
  269.         if ($count == 1)
  270.         {
  271.             reset($data);
  272.             $document new FOFHalDocument(end($data));
  273.         }
  274.         else
  275.         {
  276.             $document new FOFHalDocument($data);
  277.         }
  278.  
  279.         // Create a self link
  280.         $uri = (string) (JUri::getInstance());
  281.         $uri $this->_removeURIBase($uri);
  282.         $uri JRoute::_($uri);
  283.         $document->addLink('self'new FOFHalLink($uri));
  284.  
  285.         // Create relative links in a record list context
  286.  
  287.         if (is_array($data&& ($model instanceof FOFModel))
  288.         {
  289.             $pagination $model->getPagination();
  290.  
  291.             if ($pagination->get('pages.total'1)
  292.             {
  293.                 // Try to guess URL parameters and create a prototype URL
  294.                 // NOTE: You are better off specialising this method
  295.                 $protoUri $this->_getPrototypeURIForPagination();
  296.  
  297.                 // The "first" link
  298.                 $uri clone $protoUri;
  299.                 $uri->setVar('limitstart'0);
  300.                 $uri JRoute::_((string) $uri);
  301.  
  302.                 $document->addLink('first'new FOFHalLink($uri));
  303.  
  304.                 // Do we need a "prev" link?
  305.  
  306.                 if ($pagination->get('pages.current'1)
  307.                 {
  308.                     $prevPage $pagination->get('pages.current'1;
  309.                     $limitstart ($prevPage 1$pagination->limit;
  310.                     $uri clone $protoUri;
  311.                     $uri->setVar('limitstart'$limitstart);
  312.                     $uri JRoute::_((string) $uri);
  313.  
  314.                     $document->addLink('prev'new FOFHalLink($uri));
  315.                 }
  316.  
  317.                 // Do we need a "next" link?
  318.  
  319.                 if ($pagination->get('pages.current'$pagination->get('pages.total'))
  320.                 {
  321.                     $nextPage $pagination->get('pages.current'1;
  322.                     $limitstart ($nextPage 1$pagination->limit;
  323.                     $uri clone $protoUri;
  324.                     $uri->setVar('limitstart'$limitstart);
  325.                     $uri JRoute::_((string) $uri);
  326.  
  327.                     $document->addLink('next'new FOFHalLink($uri));
  328.                 }
  329.  
  330.                 // The "last" link?
  331.                 $lastPage $pagination->get('pages.total');
  332.                 $limitstart ($lastPage 1$pagination->limit;
  333.                 $uri clone $protoUri;
  334.                 $uri->setVar('limitstart'$limitstart);
  335.                 $uri JRoute::_((string) $uri);
  336.  
  337.                 $document->addLink('last'new FOFHalLink($uri));
  338.             }
  339.         }
  340.  
  341.         return $document;
  342.     }
  343.  
  344.     /**
  345.      * Convert an absolute URI to a relative one
  346.      *
  347.      * @param   string  $uri  The URI to convert
  348.      *
  349.      * @return  string  The relative URL
  350.      */
  351.     protected function _removeURIBase($uri)
  352.     {
  353.         static $root null$rootlen 0;
  354.  
  355.         if (is_null($root))
  356.         {
  357.             $root rtrim(JURI::base()'/');
  358.             $rootlen strlen($root);
  359.         }
  360.  
  361.         if (substr($uri0$rootlen== $root)
  362.         {
  363.             $uri substr($uri$rootlen);
  364.         }
  365.  
  366.         return ltrim($uri'/');
  367.     }
  368.  
  369.     /**
  370.      * Returns a JUri instance with a prototype URI used as the base for the
  371.      * other URIs created by the JSON renderer
  372.      *
  373.      * @return  JUri  The prototype JUri instance
  374.      */
  375.     protected function _getPrototypeURIForPagination()
  376.     {
  377.         $protoUri new JUri('index.php');
  378.         $protoUri->setQuery($this->input->getData());
  379.         $protoUri->delVar('savestate');
  380.         $protoUri->delVar('base_path');
  381.  
  382.         return $protoUri;
  383.     }
  384. }

Documentation generated on Tue, 19 Nov 2013 15:06:16 +0100 by phpDocumentor 1.4.3