Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

  • CGoogleApi
  • CHtml
  • CJavaScript
  • CJavaScriptExpression
  • CJSON
  • X2Html
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * CJavaScript helper class file.
  4:  *
  5:  * @author Qiang Xue <qiang.xue@gmail.com>
  6:  * @link http://www.yiiframework.com/
  7:  * @copyright 2008-2013 Yii Software LLC
  8:  * @license http://www.yiiframework.com/license/
  9:  */
 10: 
 11: /**
 12:  * CJavaScript is a helper class containing JavaScript-related handling functions.
 13:  *
 14:  * @author Qiang Xue <qiang.xue@gmail.com>
 15:  * @package system.web.helpers
 16:  * @since 1.0
 17:  */
 18: class CJavaScript
 19: {
 20:     /**
 21:      * Quotes a javascript string.
 22:      * After processing, the string can be safely enclosed within a pair of
 23:      * quotation marks and serve as a javascript string.
 24:      * @param string $js string to be quoted
 25:      * @param boolean $forUrl whether this string is used as a URL
 26:      * @return string the quoted string
 27:      */
 28:     public static function quote($js,$forUrl=false)
 29:     {
 30:         if($forUrl)
 31:             return strtr($js,array('%'=>'%25',"\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\','</'=>'<\/'));
 32:         else
 33:             return strtr($js,array("\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\','</'=>'<\/'));
 34:     }
 35: 
 36:     /**
 37:      * Encodes a PHP variable into javascript representation.
 38:      *
 39:      * Example:
 40:      * <pre>
 41:      * $options=array('key1'=>true,'key2'=>123,'key3'=>'value');
 42:      * echo CJavaScript::encode($options);
 43:      * // The following javascript code would be generated:
 44:      * // {'key1':true,'key2':123,'key3':'value'}
 45:      * </pre>
 46:      *
 47:      * For highly complex data structures use {@link jsonEncode} and {@link jsonDecode}
 48:      * to serialize and unserialize.
 49:      *
 50:      * If you are encoding user input, make sure $safe is set to true.
 51:      *
 52:      * @param mixed $value PHP variable to be encoded
 53:      * @param boolean $safe If true, 'js:' will not be allowed. In case of
 54:      * wrapping code with {@link CJavaScriptExpression} JavaScript expression
 55:      * will stay as is no matter what value this parameter is set to.
 56:      * Default is false. This parameter is available since 1.1.11.
 57:      * @return string the encoded string
 58:      */
 59:     public static function encode($value,$safe=false)
 60:     {
 61:         if(is_string($value))
 62:         {
 63:             if(strpos($value,'js:')===0 && $safe===false)
 64:                 return substr($value,3);
 65:             else
 66:                 return "'".self::quote($value)."'";
 67:         }
 68:         elseif($value===null)
 69:             return 'null';
 70:         elseif(is_bool($value))
 71:             return $value?'true':'false';
 72:         elseif(is_integer($value))
 73:             return "$value";
 74:         elseif(is_float($value))
 75:         {
 76:             if($value===-INF)
 77:                 return 'Number.NEGATIVE_INFINITY';
 78:             elseif($value===INF)
 79:                 return 'Number.POSITIVE_INFINITY';
 80:             else
 81:                 return str_replace(',','.',(float)$value);  // locale-independent representation
 82:         }
 83:         elseif($value instanceof CJavaScriptExpression)
 84:             return $value->__toString();
 85:         elseif(is_object($value))
 86:             return self::encode(get_object_vars($value),$safe);
 87:         elseif(is_array($value))
 88:         {
 89:             $es=array();
 90:             if(($n=count($value))>0 && array_keys($value)!==range(0,$n-1))
 91:             {
 92:                 foreach($value as $k=>$v)
 93:                     $es[]="'".self::quote($k)."':".self::encode($v,$safe);
 94:                 return '{'.implode(',',$es).'}';
 95:             }
 96:             else
 97:             {
 98:                 foreach($value as $v)
 99:                     $es[]=self::encode($v,$safe);
100:                 return '['.implode(',',$es).']';
101:             }
102:         }
103:         else
104:             return '';
105:     }
106: 
107:     /**
108:      * Returns the JSON representation of the PHP data.
109:      * @param mixed $data the data to be encoded
110:      * @return string the JSON representation of the PHP data.
111:      */
112:     public static function jsonEncode($data)
113:     {
114:         return CJSON::encode($data);
115:     }
116: 
117:     /**
118:      * Decodes a JSON string.
119:      * @param string $data the data to be decoded
120:      * @param boolean $useArray whether to use associative array to represent object data
121:      * @return mixed the decoded PHP data
122:      */
123:     public static function jsonDecode($data,$useArray=true)
124:     {
125:         return CJSON::decode($data,$useArray);
126:     }
127: }
128: 
API documentation generated by ApiGen 2.8.0