/** * 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'); } } }
/** * Loads the section $section from the config file encoded as JSON * * Sections are defined as properties of the main object * * In order to extend another section, a section defines the "_extends" * property having a value of the section name from which the extending * section inherits values. * * Note that the keys in $section will override any keys of the same * name in the sections that have been included via "_extends". * * @param string $json JSON file or string to process * @param mixed $section Section to process * @param boolean $options Whether modifiacations are allowed at runtime * @throws IfwPsn_Vendor_Zend_Config_Exception When JSON text is not set or cannot be loaded * @throws IfwPsn_Vendor_Zend_Config_Exception When section $sectionName cannot be found in $json */ public function __construct($json, $section = null, $options = false) { if (empty($json)) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Config/Exception.php'; throw new IfwPsn_Vendor_Zend_Config_Exception('Filename is not set'); } $allowModifications = false; if (is_bool($options)) { $allowModifications = $options; } elseif (is_array($options)) { foreach ($options as $key => $value) { switch (strtolower($key)) { case 'allow_modifications': case 'allowmodifications': $allowModifications = (bool) $value; break; case 'skip_extends': case 'skipextends': $this->_skipExtends = (bool) $value; break; case 'ignore_constants': case 'ignoreconstants': $this->_ignoreConstants = (bool) $value; break; default: break; } } } set_error_handler(array($this, '_loadFileErrorHandler')); // Warnings and errors are suppressed if ($json[0] != '{') { $json = file_get_contents($json); } restore_error_handler(); // Check if there was a error while loading file if ($this->_loadFileErrorStr !== null) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Config/Exception.php'; throw new IfwPsn_Vendor_Zend_Config_Exception($this->_loadFileErrorStr); } // Replace constants if (!$this->_ignoreConstants) { $json = $this->_replaceConstants($json); } // Parse/decode try { $config = IfwPsn_Vendor_Zend_Json::decode($json); } catch (IfwPsn_Vendor_Zend_Json_Exception $e) { // decode failed require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Config/Exception.php'; throw new IfwPsn_Vendor_Zend_Config_Exception("Error parsing JSON data"); } if ($section === null) { $dataArray = array(); foreach ($config as $sectionName => $sectionData) { $dataArray[$sectionName] = $this->_processExtends($config, $sectionName); } parent::__construct($dataArray, $allowModifications); } elseif (is_array($section)) { $dataArray = array(); foreach ($section as $sectionName) { if (!isset($config[$sectionName])) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Config/Exception.php'; throw new IfwPsn_Vendor_Zend_Config_Exception(sprintf('Section "%s" cannot be found', $sectionName)); } $dataArray = array_merge($this->_processExtends($config, $sectionName), $dataArray); } parent::__construct($dataArray, $allowModifications); } else { if (!isset($config[$section])) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Config/Exception.php'; throw new IfwPsn_Vendor_Zend_Config_Exception(sprintf('Section "%s" cannot be found', $section)); } $dataArray = $this->_processExtends($config, $section); if (!is_array($dataArray)) { // Section in the JSON data contains just one top level string $dataArray = array($section => $dataArray); } parent::__construct($dataArray, $allowModifications); } $this->_loadedSection = $section; }
/** * 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; }