* @copyright Copyright (c) 2010, ConsultorPC * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html * @link http://smartclientphp.com/ * @since Version 0.1b * @filesource */ /** * Smart Client General Helper * */ class ScGeneral { /** * @var String */ protected $_removeQuotesStr; /** * * @var string */ protected $_jsTagHeader; /** * * @var string */ protected $_jsTagFooter; /** * * @var bool */ protected $_showJsTags; /** * * @var bool */ protected $_indentJson; /** * Chars that are automatically removed from final json * * @var array */ protected $_removeChars; /** * Set $remove_quotes_str */ public function __construct() { // Set initial values $this->_removeQuotesStr = '{NQ}'; $this->_jsTagHeader = ''; $this->_showJsTags = true; $this->_indentJson = false; $this->_removeChars = array( '\n' , '\t' , '\r' ); } /** * * Return instance * */ public function ScGeneral() { return $this; } /** * Create the scripts for include * * @param string $sc_path Path where SmartClient is located * @param string $theme SmartClient theme * @param boolean $useCompression * @param string $scCompressPath Path where ScCompress.php is located. Only used if $useCompression is true. * @return string */ public function includeScripts( $scPath = 'js/isomorphic/', $theme = 'SmartClient', $useCompression = false, $scCompressPath = 'js/' ) { // Check if we have to use compression if( $useCompression ) { $includeScripts = ' '; } else { $includeScripts = ' '; } return $includeScripts; } /** * * Add QuotesStr to String * * @param string $str * @return string */ public function addQuotesStr( $str ) { return $this->_removeQuotesStr . $str . $this->_removeQuotesStr; } /** * Remove quotes from specific vars that are not supposed to be quoted using * the $removeQuotesStr var * * @param string $json String in json format * @return string */ public function removeQuotes( $json ) { $newJson = $json; // Removes quotes replacing remove_quotes_str $newJson = str_replace( $this->_removeQuotesStr . '"', '', $newJson ); $newJson = str_replace( '"' . $this->_removeQuotesStr, '', $newJson ); return $newJson; } /** * Set javascript tag header * * @param string $value * @return void */ public function setJsTagHeader( $value ) { $this->_jsTagHeader = $value; } /** * Set javascript tag footer * * @param string $value * @return void */ public function setJsTagFooter( $value ) { $this->_jsTagFooter = $value; } /** * Set removeChars * * @param array $value * @return void */ public function setRemoveChars( $value ) { $this->_removeChars = $value; } /** * Set removeQuotesStr * * @param string $value * @return void */ public function setRemoveQuotesStr( $value ) { $this->_removeQuotesStr = $value; } /** * Define if javascript tags should show up * * @param boolean $value * @return void */ public function showJsTags( $value ) { $this->_showJsTags = (bool) $value; } /** * Define if json code should be indented * * @param boolean $value * @return void */ public function indentJson( $value ) { $this->_indentJson = (bool) $value; } /** * Add script tags if showJsTags is true * * @param string $script * @return string */ public function addScriptTags( $script ) { if( $this->_showJsTags ) { $script = $this->_jsTagHeader . $script . $this->_jsTagFooter; } return $script; } /** * Encode array into json. Watch out when modifying this function as it is used by all components * * @param array $array Array to be encoded * @return string */ protected function encode( $array ) { $json = json_encode( $array ); // Remove slashes from double quotes, which are created by json_encode $json = str_replace( '\"' , '"' , $json ); // Automatically remove some chars that are not necessary or may cause problems $json = str_replace( $this->_removeChars , '' , $json ); // Remove quotes based on $_removeQuotesStr $json = $this->removeQuotes( $json ); // Check if we have to indent json if( $this->_indentJson ) { $json = $this->indent( $json ); } return $json; } /** * Indents a flat JSON string to make it more human-readable * * Thanks to http://recurser.com/articles/2008/03/11/format-json-with-php/ * * @param string $json The original JSON string to process * @return string Indented version of the original JSON string */ public function indent( $json ) { $result = ''; $pos = 0; $strLen = strlen( $json ); $indentStr = ' '; $newLine = "\n"; for( $i = 0; $i <= $strLen; $i ++ ) { // Grab the next character in the string $char = substr( $json, $i, 1 ); // If this character is the end of an element, // output a new line and indent the next line if( $char == '}' || $char == ']' ) { $result .= $newLine; $pos --; for( $j = 0; $j < $pos; $j ++ ) { $result .= $indentStr; } } // Add the character to the result string $result .= $char; // If the last character was the beginning of an element, // output a new line and indent the next line if( $char == ',' || $char == '{' || $char == '[' ) { $result .= $newLine; if( $char == '{' || $char == '[' ) { $pos ++; } for( $j = 0; $j < $pos; $j ++ ) { $result .= $indentStr; } } } return $result; } }