web/lib/Zend/View/Helper/Json.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_View
       
    17  * @subpackage Helper
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @version    $Id: Json.php 23451 2010-11-28 13:10:03Z ramon $
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 /** Zend_Json */
       
    24 require_once 'Zend/Json.php';
       
    25 
       
    26 /** Zend_Controller_Front */
       
    27 require_once 'Zend/Controller/Front.php';
       
    28 
       
    29 /** Zend_View_Helper_Abstract.php */
       
    30 require_once 'Zend/View/Helper/Abstract.php';
       
    31 
       
    32 /**
       
    33  * Helper for simplifying JSON responses
       
    34  *
       
    35  * @package    Zend_View
       
    36  * @subpackage Helper
       
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    39  */
       
    40 class Zend_View_Helper_Json extends Zend_View_Helper_Abstract
       
    41 {
       
    42     /**
       
    43      * Encode data as JSON, disable layouts, and set response header
       
    44      *
       
    45      * If $keepLayouts is true, does not disable layouts.
       
    46      *
       
    47      * @param  mixed $data
       
    48      * @param  bool $keepLayouts
       
    49      * NOTE:   if boolean, establish $keepLayouts to true|false
       
    50      *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
       
    51      *         this array can contains a 'keepLayout'=>true|false
       
    52      *         that will not be passed to Zend_Json::encode method but will be used here
       
    53      * @return string|void
       
    54      */
       
    55     public function json($data, $keepLayouts = false)
       
    56     {
       
    57         $options = array();
       
    58         if (is_array($keepLayouts))
       
    59         {
       
    60             $options     = $keepLayouts;
       
    61             $keepLayouts = (array_key_exists('keepLayouts', $keepLayouts))
       
    62                             ? $keepLayouts['keepLayouts']
       
    63                             : false;
       
    64             unset($options['keepLayouts']);
       
    65         }
       
    66 
       
    67         $data = Zend_Json::encode($data, null, $options);
       
    68         if (!$keepLayouts) {
       
    69             require_once 'Zend/Layout.php';
       
    70             $layout = Zend_Layout::getMvcInstance();
       
    71             if ($layout instanceof Zend_Layout) {
       
    72                 $layout->disableLayout();
       
    73             }
       
    74         }
       
    75 
       
    76         $response = Zend_Controller_Front::getInstance()->getResponse();
       
    77         $response->setHeader('Content-Type', 'application/json', true);
       
    78         return $data;
       
    79     }
       
    80 }