web/lib/Zend/Controller/Action/Helper/Json.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Controller
    16  * @package    Zend_Controller
    17  * @subpackage Zend_Controller_Action_Helper
    17  * @subpackage Zend_Controller_Action_Helper
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Json.php 20096 2010-01-06 02:05:09Z bkarwin $
    20  * @version    $Id: Json.php 24829 2012-05-30 12:31:39Z adamlundrigan $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Controller_Action_Helper_Abstract
    24  * @see Zend_Controller_Action_Helper_Abstract
    25  */
    25  */
    30  *
    30  *
    31  * @uses       Zend_Controller_Action_Helper_Abstract
    31  * @uses       Zend_Controller_Action_Helper_Abstract
    32  * @category   Zend
    32  * @category   Zend
    33  * @package    Zend_Controller
    33  * @package    Zend_Controller
    34  * @subpackage Zend_Controller_Action_Helper
    34  * @subpackage Zend_Controller_Action_Helper
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    35  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    37  */
    37  */
    38 class Zend_Controller_Action_Helper_Json extends Zend_Controller_Action_Helper_Abstract
    38 class Zend_Controller_Action_Helper_Json extends Zend_Controller_Action_Helper_Abstract
    39 {
    39 {
    40     /**
    40     /**
    51      * used).
    51      * used).
    52      *
    52      *
    53      * @param  mixed   $data
    53      * @param  mixed   $data
    54      * @param  boolean $keepLayouts
    54      * @param  boolean $keepLayouts
    55      * @param  boolean|array $keepLayouts
    55      * @param  boolean|array $keepLayouts
       
    56      * @param  boolean $encodeData Provided data is already JSON
    56      * NOTE:   if boolean, establish $keepLayouts to true|false
    57      * NOTE:   if boolean, establish $keepLayouts to true|false
    57      *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
    58      *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
    58      *         if $keepLayouts and parmas for Zend_Json::encode are required
    59      *         if $keepLayouts and parmas for Zend_Json::encode are required
    59      *         then, the array can contains a 'keepLayout'=>true|false
    60      *         then, the array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false
    60      *         that will not be passed to Zend_Json::encode method but will be passed
    61      *         that will not be passed to Zend_Json::encode method but will be passed
    61      *         to Zend_View_Helper_Json
    62      *         to Zend_View_Helper_Json
    62      * @throws Zend_Controller_Action_Helper_Json
    63      * @throws Zend_Controller_Action_Helper_Json
    63      * @return string
    64      * @return string
    64      */
    65      */
    65     public function encodeJson($data, $keepLayouts = false)
    66     public function encodeJson($data, $keepLayouts = false, $encodeData = true)
    66     {
    67     {
    67         /**
    68         /**
    68          * @see Zend_View_Helper_Json
    69          * @see Zend_View_Helper_Json
    69          */
    70          */
    70         require_once 'Zend/View/Helper/Json.php';
    71         require_once 'Zend/View/Helper/Json.php';
    71         $jsonHelper = new Zend_View_Helper_Json();
    72         $jsonHelper = new Zend_View_Helper_Json();
    72         $data = $jsonHelper->json($data, $keepLayouts);
    73         $data = $jsonHelper->json($data, $keepLayouts, $encodeData);
    73 
    74 
    74         if (!$keepLayouts) {
    75         if (!$keepLayouts) {
    75             /**
    76             /**
    76              * @see Zend_Controller_Action_HelperBroker
    77              * @see Zend_Controller_Action_HelperBroker
    77              */
    78              */
    85     /**
    86     /**
    86      * Encode JSON response and immediately send
    87      * Encode JSON response and immediately send
    87      *
    88      *
    88      * @param  mixed   $data
    89      * @param  mixed   $data
    89      * @param  boolean|array $keepLayouts
    90      * @param  boolean|array $keepLayouts
       
    91      * @param  $encodeData Encode $data as JSON?
    90      * NOTE:   if boolean, establish $keepLayouts to true|false
    92      * NOTE:   if boolean, establish $keepLayouts to true|false
    91      *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
    93      *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
    92      *         if $keepLayouts and parmas for Zend_Json::encode are required
    94      *         if $keepLayouts and parmas for Zend_Json::encode are required
    93      *         then, the array can contains a 'keepLayout'=>true|false
    95      *         then, the array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false
    94      *         that will not be passed to Zend_Json::encode method but will be passed
    96      *         that will not be passed to Zend_Json::encode method but will be passed
    95      *         to Zend_View_Helper_Json
    97      *         to Zend_View_Helper_Json
    96      * @return string|void
    98      * @return string|void
    97      */
    99      */
    98     public function sendJson($data, $keepLayouts = false)
   100     public function sendJson($data, $keepLayouts = false, $encodeData = true)
    99     {
   101     {
   100         $data = $this->encodeJson($data, $keepLayouts);
   102         $data = $this->encodeJson($data, $keepLayouts, $encodeData);
   101         $response = $this->getResponse();
   103         $response = $this->getResponse();
   102         $response->setBody($data);
   104         $response->setBody($data);
   103 
   105 
   104         if (!$this->suppressExit) {
   106         if (!$this->suppressExit) {
   105             $response->sendResponse();
   107             $response->sendResponse();
   116      * response.
   118      * response.
   117      *
   119      *
   118      * @param  mixed   $data
   120      * @param  mixed   $data
   119      * @param  boolean $sendNow
   121      * @param  boolean $sendNow
   120      * @param  boolean $keepLayouts
   122      * @param  boolean $keepLayouts
       
   123      * @param  boolean $encodeData Encode $data as JSON?
   121      * @return string|void
   124      * @return string|void
   122      */
   125      */
   123     public function direct($data, $sendNow = true, $keepLayouts = false)
   126     public function direct($data, $sendNow = true, $keepLayouts = false, $encodeData = true)
   124     {
   127     {
   125         if ($sendNow) {
   128         if ($sendNow) {
   126             return $this->sendJson($data, $keepLayouts);
   129             return $this->sendJson($data, $keepLayouts, $encodeData);
   127         }
   130         }
   128         return $this->encodeJson($data, $keepLayouts);
   131         return $this->encodeJson($data, $keepLayouts, $encodeData);
   129     }
   132     }
   130 }
   133 }