/** * Encode data as JSON, disable layouts, and set response header * * If $keepLayouts is true, does not disable layouts. * If $encodeJson is false, does not JSON-encode $data * * @param mixed $data * @param bool $keepLayouts * NOTE: if boolean, establish $keepLayouts to true|false * if array, admit params for IfwPsn_Vendor_Zend_Json::encode as enableJsonExprFinder=>true|false * this array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false * that will not be passed to IfwPsn_Vendor_Zend_Json::encode method but will be used here * @param bool $encodeData * @return string|void */ public function json($data, $keepLayouts = false, $encodeData = true) { $options = array(); if (is_array($keepLayouts)) { $options = $keepLayouts; $keepLayouts = false; if (array_key_exists('keepLayouts', $options)) { $keepLayouts = $options['keepLayouts']; unset($options['keepLayouts']); } if (array_key_exists('encodeData', $options)) { $encodeData = $options['encodeData']; unset($options['encodeData']); } } if ($encodeData) { $data = IfwPsn_Vendor_Zend_Json::encode($data, null, $options); } if (!$keepLayouts) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Layout.php'; $layout = IfwPsn_Vendor_Zend_Layout::getMvcInstance(); if ($layout instanceof IfwPsn_Vendor_Zend_Layout) { $layout->disableLayout(); } } $response = IfwPsn_Vendor_Zend_Controller_Front::getInstance()->getResponse(); $response->setHeader('Content-Type', 'application/json', true); return $data; }
/** * Converts an associative array to a string of tag attributes. * * @access public * * @param array $attribs From this array, each key-value pair is * converted to an attribute name and value. * * @return string The XHTML for the attributes. */ protected function _htmlAttribs($attribs) { $xhtml = ''; foreach ((array) $attribs as $key => $val) { $key = $this->view->escape($key); if ('on' == substr($key, 0, 2) || 'constraints' == $key) { // Don't escape event attributes; _do_ substitute double quotes with singles if (!is_scalar($val)) { // non-scalar data should be cast to JSON first require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Json.php'; $val = IfwPsn_Vendor_Zend_Json::encode($val); } // Escape single quotes inside event attribute values. // This will create html, where the attribute value has // single quotes around it, and escaped single quotes or // non-escaped double quotes inside of it $val = str_replace('\'', ''', $val); } else { if (is_array($val)) { $val = implode(' ', $val); } $val = $this->view->escape($val); } if ('id' == $key) { $val = $this->_normalizeId($val); } if (strpos($val, '"') !== false) { $xhtml .= " {$key}='{$val}'"; } else { $xhtml .= " {$key}=\"{$val}\""; } } return $xhtml; }
/** * JSON post processing * * JSON serialize view variables to response body * * @return void */ public function postJsonContext() { if (!$this->getAutoJsonSerialization()) { return; } $viewRenderer = IfwPsn_Vendor_Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); $view = $viewRenderer->view; if ($view instanceof IfwPsn_Vendor_Zend_View_Interface) { /** * @see IfwPsn_Vendor_Zend_Json */ if (method_exists($view, 'getVars')) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Json.php'; $vars = IfwPsn_Vendor_Zend_Json::encode($view->getVars()); $this->getResponse()->setBody($vars); } else { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Controller/Action/Exception.php'; throw new IfwPsn_Vendor_Zend_Controller_Action_Exception('View does not implement the getVars() method needed to encode the view into JSON'); } } }
/** * Process submitted AJAX data * * Checks if provided $data is valid, via {@link isValidPartial()}. If so, * it returns JSON-encoded boolean true. If not, it returns JSON-encoded * error messages (as returned by {@link getMessages()}). * * @param array $data * @return string JSON-encoded boolean true or error messages */ public function processAjax(array $data) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Json.php'; if ($this->isValidPartial($data)) { return IfwPsn_Vendor_Zend_Json::encode(true); } $messages = $this->getMessages(); return IfwPsn_Vendor_Zend_Json::encode($messages); }
/** * Render a IfwPsn_Vendor_Zend_Config into a JSON config string. * * @since 1.10 * @return string */ public function render() { $data = $this->_config->toArray(); $sectionName = $this->_config->getSectionName(); $extends = $this->_config->getExtends(); if (is_string($sectionName)) { $data = array($sectionName => $data); } foreach ($extends as $section => $parentSection) { $data[$section][IfwPsn_Vendor_Zend_Config_Json::EXTENDS_NAME] = $parentSection; } // Ensure that each "extends" section actually exists foreach ($data as $section => $sectionData) { if (is_array($sectionData) && isset($sectionData[IfwPsn_Vendor_Zend_Config_Json::EXTENDS_NAME])) { $sectionExtends = $sectionData[IfwPsn_Vendor_Zend_Config_Json::EXTENDS_NAME]; if (!isset($data[$sectionExtends])) { // Remove "extends" declaration if section does not exist unset($data[$section][IfwPsn_Vendor_Zend_Config_Json::EXTENDS_NAME]); } } } $out = IfwPsn_Vendor_Zend_Json::encode($data); if ($this->prettyPrint()) { $out = IfwPsn_Vendor_Zend_Json::prettyPrint($out); } return $out; }