Guest User

unzend.com_208

a guest
May 5th, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 27.97 KB | None | 0 0
  1. <?php
  2. // ionCube version 9 Decoder unzend.com - Email: unzend@gmail.com
  3. // http://www.unzend.com
  4. /**
  5.  * CClientScript class file.
  6.  *
  7.  * @author Qiang Xue <qiang.xue@gmail.com>
  8.  * @link http://www.yiiframework.com/
  9.  * @copyright 2008-2013 Yii Software LLC
  10.  * @license http://www.yiiframework.com/license/
  11.  */
  12.  
  13. /**
  14.  * CClientScript manages JavaScript and CSS stylesheets for views.
  15.  *
  16.  * @property string $coreScriptUrl The base URL of all core javascript files.
  17.  *
  18.  * @author Qiang Xue <qiang.xue@gmail.com>
  19.  * @package system.web
  20.  * @since 1.0
  21.  */
  22. class CClientScript extends CApplicationComponent
  23. {
  24.     /**
  25.      * The script is rendered in the head section right before the title element.
  26.      */
  27.     const POS_HEAD=0;
  28.     /**
  29.      * The script is rendered at the beginning of the body section.
  30.      */
  31.     const POS_BEGIN=1;
  32.     /**
  33.      * The script is rendered at the end of the body section.
  34.      */
  35.     const POS_END=2;
  36.     /**
  37.      * The script is rendered inside window onload function.
  38.      */
  39.     const POS_LOAD=3;
  40.     /**
  41.      * The body script is rendered inside a jQuery ready function.
  42.      */
  43.     const POS_READY=4;
  44.  
  45.     /**
  46.      * @var boolean whether JavaScript should be enabled. Defaults to true.
  47.      */
  48.     public $enableJavaScript=true;
  49.     /**
  50.      * @var array the mapping between script file names and the corresponding script URLs.
  51.      * The array keys are script file names (without directory part) and the array values are the corresponding URLs.
  52.      * If an array value is false, the corresponding script file will not be rendered.
  53.      * If an array key is '*.js' or '*.css', the corresponding URL will replace all
  54.      * JavaScript files or CSS files, respectively.
  55.      *
  56.      * This property is mainly used to optimize the generated HTML pages
  57.      * by merging different scripts files into fewer and optimized script files.
  58.      */
  59.     public $scriptMap=array();
  60.     /**
  61.      * @var array list of custom script packages (name=>package spec).
  62.      * This property keeps a list of named script packages, each of which can contain
  63.      * a set of CSS and/or JavaScript script files, and their dependent package names.
  64.      * By calling {@link registerPackage}, one can register a whole package of client
  65.      * scripts together with their dependent packages and render them in the HTML output.
  66.      *
  67.      * The array structure is as follows:
  68.      * <pre>
  69.      * array(
  70.      *   'package-name'=>array(
  71.      *     'basePath'=>'alias of the directory containing the script files',
  72.      *     'baseUrl'=>'base URL for the script files',
  73.      *     'js'=>array(list of js files relative to basePath/baseUrl),
  74.      *     'css'=>array(list of css files relative to basePath/baseUrl),
  75.      *     'depends'=>array(list of dependent packages),
  76.      *   ),
  77.      *   ......
  78.      * )
  79.      * </pre>
  80.      *
  81.      * The JS and CSS files listed are relative to 'basePath'.
  82.      * For example, if 'basePath' is 'application.assets', a script named 'comments.js'
  83.      * will refer to the file 'protected/assets/comments.js'.
  84.      *
  85.      * When a script is being rendered in HTML, it will be prefixed with 'baseUrl'.
  86.      * For example, if 'baseUrl' is '/assets', the 'comments.js' script will be rendered
  87.      * using URL '/assets/comments.js'.
  88.      *
  89.      * If 'baseUrl' does not start with '/', the relative URL of the application entry
  90.      * script will be inserted at the beginning. For example, if 'baseUrl' is 'assets'
  91.      * and the current application runs with the URL 'http://localhost/demo/index.php',
  92.      * then the 'comments.js' script will be rendered using URL '/demo/assets/comments.js'.
  93.      *
  94.      * If 'baseUrl' is not set, the script will be published by {@link CAssetManager}
  95.      * and the corresponding published URL will be used.
  96.      *
  97.      * When calling {@link registerPackage} to register a script package,
  98.      * this property will be checked first followed by {@link corePackages}.
  99.      * If a package is found, it will be registered for rendering later on.
  100.      *
  101.      * @since 1.1.7
  102.      */
  103.     public $packages=array();
  104.     /**
  105.      * @var array list of core script packages (name=>package spec).
  106.      * Please refer to {@link packages} for details about package spec.
  107.      *
  108.      * By default, the core script packages are specified in 'framework/web/js/packages.php'.
  109.      * You may configure this property to customize the core script packages.
  110.      *
  111.      * When calling {@link registerPackage} to register a script package,
  112.      * {@link packages} will be checked first followed by this property.
  113.      * If a package is found, it will be registered for rendering later on.
  114.      *
  115.      * @since 1.1.7
  116.      */
  117.     public $corePackages;
  118.     /**
  119.      * @var array the registered JavaScript code blocks (position, key => code)
  120.      */
  121.     public $scripts=array();
  122.     /**
  123.      * @var array the registered CSS files (CSS URL=>media type).
  124.      */
  125.     protected $cssFiles=array();
  126.     /**
  127.      * @var array the registered JavaScript files (position, key => URL)
  128.      */
  129.     protected $scriptFiles=array();
  130.     /**
  131.      * @var array the registered head meta tags. Each array element represents an option array
  132.      * that will be passed as the last parameter of {@link CHtml::metaTag}.
  133.      * @since 1.1.3
  134.      */
  135.     protected $metaTags=array();
  136.     /**
  137.      * @var array the registered head link tags. Each array element represents an option array
  138.      * that will be passed as the last parameter of {@link CHtml::linkTag}.
  139.      * @since 1.1.3
  140.      */
  141.     protected $linkTags=array();
  142.     /**
  143.      * @var array the registered css code blocks (key => array(CSS code, media type)).
  144.      * @since 1.1.3
  145.      */
  146.     protected $css=array();
  147.     /**
  148.      * @var boolean whether there are any javascript or css to be rendered.
  149.      * @since 1.1.7
  150.      */
  151.     protected $hasScripts=false;
  152.     /**
  153.      * @var array the registered script packages (name => package spec)
  154.      * @since 1.1.7
  155.      */
  156.     protected $coreScripts=array();
  157.     /**
  158.      * @var integer Where the scripts registered using {@link registerCoreScript} or {@link registerPackage}
  159.      * will be inserted in the page. This can be one of the CClientScript::POS_* constants.
  160.      * Defaults to CClientScript::POS_HEAD.
  161.      * @since 1.1.3
  162.      */
  163.     public $coreScriptPosition=self::POS_HEAD;
  164.     /**
  165.      * @var integer Where the scripts registered using {@link registerScriptFile} will be inserted in the page.
  166.      * This can be one of the CClientScript::POS_* constants.
  167.      * Defaults to CClientScript::POS_HEAD.
  168.      * @since 1.1.11
  169.      */
  170.     public $defaultScriptFilePosition=self::POS_HEAD;
  171.     /**
  172.      * @var integer Where the scripts registered using {@link registerScript} will be inserted in the page.
  173.      * This can be one of the CClientScript::POS_* constants.
  174.      * Defaults to CClientScript::POS_READY.
  175.      * @since 1.1.11
  176.      */
  177.     public $defaultScriptPosition=self::POS_READY;
  178.  
  179.     private $_baseUrl;
  180.  
  181.     /**
  182.      * Cleans all registered scripts.
  183.      */
  184.     public function reset()
  185.     {
  186.         $this->hasScripts=false;
  187.         $this->coreScripts=array();
  188.         $this->cssFiles=array();
  189.         $this->css=array();
  190.         $this->scriptFiles=array();
  191.         $this->scripts=array();
  192.         $this->metaTags=array();
  193.         $this->linkTags=array();
  194.  
  195.         $this->recordCachingAction('clientScript','reset',array());
  196.     }
  197.  
  198.     /**
  199.      * Renders the registered scripts.
  200.      * This method is called in {@link CController::render} when it finishes
  201.      * rendering content. CClientScript thus gets a chance to insert script tags
  202.      * at <code>head</code> and <code>body</code> sections in the HTML output.
  203.      * @param string $output the existing output that needs to be inserted with script tags
  204.      */
  205.     public function render(&$output)
  206.     {
  207.         if(!$this->hasScripts)
  208.             return;
  209.  
  210.         $this->renderCoreScripts();
  211.  
  212.         if(!empty($this->scriptMap))
  213.             $this->remapScripts();
  214.  
  215.         $this->unifyScripts();
  216.  
  217.         $this->renderHead($output);
  218.         if($this->enableJavaScript)
  219.         {
  220.             $this->renderBodyBegin($output);
  221.             $this->renderBodyEnd($output);
  222.         }
  223.     }
  224.  
  225.     /**
  226.      * Removes duplicated scripts from {@link scriptFiles}.
  227.      * @since 1.1.5
  228.      */
  229.     protected function unifyScripts()
  230.     {
  231.         if(!$this->enableJavaScript)
  232.             return;
  233.         $map=array();
  234.         if(isset($this->scriptFiles[self::POS_HEAD]))
  235.             $map=$this->scriptFiles[self::POS_HEAD];
  236.  
  237.         if(isset($this->scriptFiles[self::POS_BEGIN]))
  238.         {
  239.             foreach($this->scriptFiles[self::POS_BEGIN] as $scriptFile=>$scriptFileValue)
  240.             {
  241.                 if(isset($map[$scriptFile]))
  242.                     unset($this->scriptFiles[self::POS_BEGIN][$scriptFile]);
  243.                 else
  244.                     $map[$scriptFile]=true;
  245.             }
  246.         }
  247.  
  248.         if(isset($this->scriptFiles[self::POS_END]))
  249.         {
  250.             foreach($this->scriptFiles[self::POS_END] as $key=>$scriptFile)
  251.             {
  252.                 if(isset($map[$key]))
  253.                     unset($this->scriptFiles[self::POS_END][$key]);
  254.             }
  255.         }
  256.     }
  257.  
  258.     /**
  259.      * Uses {@link scriptMap} to re-map the registered scripts.
  260.      */
  261.     protected function remapScripts()
  262.     {
  263.         $cssFiles=array();
  264.         foreach($this->cssFiles as $url=>$media)
  265.         {
  266.             $name=basename($url);
  267.             if(isset($this->scriptMap[$name]))
  268.             {
  269.                 if($this->scriptMap[$name]!==false)
  270.                     $cssFiles[$this->scriptMap[$name]]=$media;
  271.             }
  272.             elseif(isset($this->scriptMap['*.css']))
  273.             {
  274.                 if($this->scriptMap['*.css']!==false)
  275.                     $cssFiles[$this->scriptMap['*.css']]=$media;
  276.             }
  277.             else
  278.                 $cssFiles[$url]=$media;
  279.         }
  280.         $this->cssFiles=$cssFiles;
  281.  
  282.         $jsFiles=array();
  283.         foreach($this->scriptFiles as $position=>$scriptFiles)
  284.         {
  285.             $jsFiles[$position]=array();
  286.             foreach($scriptFiles as $scriptFile=>$scriptFileValue)
  287.             {
  288.                 $name=basename($scriptFile);
  289.                 if(isset($this->scriptMap[$name]))
  290.                 {
  291.                     if($this->scriptMap[$name]!==false)
  292.                         $jsFiles[$position][$this->scriptMap[$name]]=$this->scriptMap[$name];
  293.                 }
  294.                 elseif(isset($this->scriptMap['*.js']))
  295.                 {
  296.                     if($this->scriptMap['*.js']!==false)
  297.                         $jsFiles[$position][$this->scriptMap['*.js']]=$this->scriptMap['*.js'];
  298.                 }
  299.                 else
  300.                     $jsFiles[$position][$scriptFile]=$scriptFileValue;
  301.             }
  302.         }
  303.         $this->scriptFiles=$jsFiles;
  304.     }
  305.  
  306.     /**
  307.      * Composes script HTML block from the given script values,
  308.      * attempting to group scripts at single 'script' tag if possible.
  309.      * @param array $scripts script values to process.
  310.      * @return string HTML output
  311.      */
  312.     protected function renderScriptBatch(array $scripts)
  313.     {
  314.         $html = '';
  315.         $scriptBatches = array();
  316.         foreach($scripts as $scriptValue)
  317.         {
  318.             if(is_array($scriptValue))
  319.             {
  320.                 $scriptContent = $scriptValue['content'];
  321.                 unset($scriptValue['content']);
  322.                 $scriptHtmlOptions = $scriptValue;
  323.             }
  324.             else
  325.             {
  326.                 $scriptContent = $scriptValue;
  327.                 $scriptHtmlOptions = array();
  328.             }
  329.             $key=serialize(ksort($scriptHtmlOptions));
  330.             $scriptBatches[$key]['htmlOptions']=$scriptHtmlOptions;
  331.             $scriptBatches[$key]['scripts'][]=$scriptContent;
  332.         }
  333.         foreach($scriptBatches as $scriptBatch)
  334.             if(!empty($scriptBatch['scripts']))
  335.                 $html.=CHtml::script(implode("\n",$scriptBatch['scripts']),$scriptBatch['htmlOptions'])."\n";
  336.         return $html;
  337.     }
  338.  
  339.     /**
  340.      * Renders the specified core javascript library.
  341.      */
  342.     public function renderCoreScripts()
  343.     {
  344.         if($this->coreScripts===null)
  345.             return;
  346.         $cssFiles=array();
  347.         $jsFiles=array();
  348.         foreach($this->coreScripts as $name=>$package)
  349.         {
  350.             $baseUrl=$this->getPackageBaseUrl($name);
  351.             if(!empty($package['js']))
  352.             {
  353.                 foreach($package['js'] as $js)
  354.                     $jsFiles[$baseUrl.'/'.$js]=$baseUrl.'/'.$js;
  355.             }
  356.             if(!empty($package['css']))
  357.             {
  358.                 foreach($package['css'] as $css)
  359.                     $cssFiles[$baseUrl.'/'.$css]='';
  360.             }
  361.         }
  362.         // merge in place
  363.         if($cssFiles!==array())
  364.         {
  365.             foreach($this->cssFiles as $cssFile=>$media)
  366.                 $cssFiles[$cssFile]=$media;
  367.             $this->cssFiles=$cssFiles;
  368.         }
  369.         if($jsFiles!==array())
  370.         {
  371.             if(isset($this->scriptFiles[$this->coreScriptPosition]))
  372.             {
  373.                 foreach($this->scriptFiles[$this->coreScriptPosition] as $url => $value)
  374.                     $jsFiles[$url]=$value;
  375.             }
  376.             $this->scriptFiles[$this->coreScriptPosition]=$jsFiles;
  377.         }
  378.     }
  379.  
  380.     /**
  381.      * Inserts the scripts in the head section.
  382.      * @param string $output the output to be inserted with scripts.
  383.      */
  384.     public function renderHead(&$output)
  385.     {
  386.         $html='';
  387.         foreach($this->metaTags as $meta)
  388.             $html.=CHtml::metaTag($meta['content'],null,null,$meta)."\n";
  389.         foreach($this->linkTags as $link)
  390.             $html.=CHtml::linkTag(null,null,null,null,$link)."\n";
  391.         foreach($this->cssFiles as $url=>$media)
  392.             $html.=CHtml::cssFile($url,$media)."\n";
  393.         foreach($this->css as $css)
  394.             $html.=CHtml::css($css[0],$css[1])."\n";
  395.         if($this->enableJavaScript)
  396.         {
  397.             if(isset($this->scriptFiles[self::POS_HEAD]))
  398.             {
  399.                 foreach($this->scriptFiles[self::POS_HEAD] as $scriptFileValueUrl=>$scriptFileValue)
  400.                 {
  401.                     if(is_array($scriptFileValue))
  402.                         $html.=CHtml::scriptFile($scriptFileValueUrl,$scriptFileValue)."\n";
  403.                     else
  404.                         $html.=CHtml::scriptFile($scriptFileValueUrl)."\n";
  405.                 }
  406.             }
  407.  
  408.             if(isset($this->scripts[self::POS_HEAD]))
  409.                 $html.=$this->renderScriptBatch($this->scripts[self::POS_HEAD]);
  410.         }
  411.  
  412.         if($html!=='')
  413.         {
  414.             $count=0;
  415.             $output=preg_replace('/(<title\b[^>]*>|<\\/head\s*>)/is','<###head###>$1',$output,1,$count);
  416.             if($count)
  417.                 $output=str_replace('<###head###>',$html,$output);
  418.             else
  419.                 $output=$html.$output;
  420.         }
  421.     }
  422.  
  423.     /**
  424.      * Inserts the scripts at the beginning of the body section.
  425.      * @param string $output the output to be inserted with scripts.
  426.      */
  427.     public function renderBodyBegin(&$output)
  428.     {
  429.         $html='';
  430.         if(isset($this->scriptFiles[self::POS_BEGIN]))
  431.         {
  432.             foreach($this->scriptFiles[self::POS_BEGIN] as $scriptFileUrl=>$scriptFileValue)
  433.             {
  434.                 if(is_array($scriptFileValue))
  435.                     $html.=CHtml::scriptFile($scriptFileUrl,$scriptFileValue)."\n";
  436.                 else
  437.                     $html.=CHtml::scriptFile($scriptFileUrl)."\n";
  438.             }
  439.         }
  440.         if(isset($this->scripts[self::POS_BEGIN]))
  441.             $html.=$this->renderScriptBatch($this->scripts[self::POS_BEGIN]);
  442.  
  443.         if($html!=='')
  444.         {
  445.             $count=0;
  446.             $output=preg_replace('/(<body\b[^>]*>)/is','$1<###begin###>',$output,1,$count);
  447.             if($count)
  448.                 $output=str_replace('<###begin###>',$html,$output);
  449.             else
  450.                 $output=$html.$output;
  451.         }
  452.     }
  453.  
  454.     /**
  455.      * Inserts the scripts at the end of the body section.
  456.      * @param string $output the output to be inserted with scripts.
  457.      */
  458.     public function renderBodyEnd(&$output)
  459.     {
  460.         if(!isset($this->scriptFiles[self::POS_END]) && !isset($this->scripts[self::POS_END])
  461.             && !isset($this->scripts[self::POS_READY]) && !isset($this->scripts[self::POS_LOAD]))
  462.             return;
  463.  
  464.         $fullPage=0;
  465.         $output=preg_replace('/(<\\/body\s*>)/is','<###end###>$1',$output,1,$fullPage);
  466.         $html='';
  467.         if(isset($this->scriptFiles[self::POS_END]))
  468.         {
  469.             foreach($this->scriptFiles[self::POS_END] as $scriptFileUrl=>$scriptFileValue)
  470.             {
  471.                 if(is_array($scriptFileValue))
  472.                     $html.=CHtml::scriptFile($scriptFileUrl,$scriptFileValue)."\n";
  473.                 else
  474.                     $html.=CHtml::scriptFile($scriptFileUrl)."\n";
  475.             }
  476.         }
  477.         $scripts=isset($this->scripts[self::POS_END]) ? $this->scripts[self::POS_END] : array();
  478.         if(isset($this->scripts[self::POS_READY]))
  479.         {
  480.             if($fullPage)
  481.                 $scripts[]="jQuery(function($) {\n".implode("\n",$this->scripts[self::POS_READY])."\n});";
  482.             else
  483.                 $scripts[]=implode("\n",$this->scripts[self::POS_READY]);
  484.         }
  485.         if(isset($this->scripts[self::POS_LOAD]))
  486.         {
  487.             if($fullPage)
  488.                 $scripts[]="jQuery(window).on('load',function() {\n".implode("\n",$this->scripts[self::POS_LOAD])."\n});";
  489.             else
  490.                 $scripts[]=implode("\n",$this->scripts[self::POS_LOAD]);
  491.         }
  492.         if(!empty($scripts))
  493.             $html.=$this->renderScriptBatch($scripts);
  494.  
  495.         if($fullPage)
  496.             $output=str_replace('<###end###>',$html,$output);
  497.         else
  498.             $output=$output.$html;
  499.     }
  500.  
  501.     /**
  502.      * Returns the base URL of all core javascript files.
  503.      * If the base URL is not explicitly set, this method will publish the whole directory
  504.      * 'framework/web/js/source' and return the corresponding URL.
  505.      * @return string the base URL of all core javascript files
  506.      */
  507.     public function getCoreScriptUrl()
  508.     {
  509.         if($this->_baseUrl!==null)
  510.             return $this->_baseUrl;
  511.         else
  512.             return $this->_baseUrl=Yii::app()->getAssetManager()->publish(YII_PATH.'/web/js/source');
  513.     }
  514.  
  515.     /**
  516.      * Sets the base URL of all core javascript files.
  517.      * This setter is provided in case when core javascript files are manually published
  518.      * to a pre-specified location. This may save asset publishing time for large-scale applications.
  519.      * @param string $value the base URL of all core javascript files.
  520.      */
  521.     public function setCoreScriptUrl($value)
  522.     {
  523.         $this->_baseUrl=$value;
  524.     }
  525.  
  526.     /**
  527.      * Returns the base URL for a registered package with the specified name.
  528.      * If needed, this method may publish the assets of the package and returns the published base URL.
  529.      * @param string $name the package name
  530.      * @return string the base URL for the named package. False is returned if the package is not registered yet.
  531.      * @see registerPackage
  532.      * @since 1.1.8
  533.      */
  534.     public function getPackageBaseUrl($name)
  535.     {
  536.         if(!isset($this->coreScripts[$name]))
  537.             return false;
  538.         $package=$this->coreScripts[$name];
  539.         if(isset($package['baseUrl']))
  540.         {
  541.             $baseUrl=$package['baseUrl'];
  542.             if($baseUrl==='' || $baseUrl[0]!=='/' && strpos($baseUrl,'://')===false)
  543.                 $baseUrl=Yii::app()->getRequest()->getBaseUrl().'/'.$baseUrl;
  544.             $baseUrl=rtrim($baseUrl,'/');
  545.         }
  546.         elseif(isset($package['basePath']))
  547.             $baseUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias($package['basePath']));
  548.         else
  549.             $baseUrl=$this->getCoreScriptUrl();
  550.  
  551.         return $this->coreScripts[$name]['baseUrl']=$baseUrl;
  552.     }
  553.  
  554.     /**
  555.      * Registers a script package that is listed in {@link packages}.
  556.      * This method is the same as {@link registerCoreScript}.
  557.      * @param string $name the name of the script package.
  558.      * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
  559.      * @since 1.1.7
  560.      * @see renderCoreScript
  561.      */
  562.     public function registerPackage($name)
  563.     {
  564.         return $this->registerCoreScript($name);
  565.     }
  566.  
  567.     /**
  568.      * Registers a script package that is listed in {@link packages}.
  569.      * @param string $name the name of the script package.
  570.      * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
  571.      * @see renderCoreScript
  572.      */
  573.     public function registerCoreScript($name)
  574.     {
  575.         if(isset($this->coreScripts[$name]))
  576.             return $this;
  577.         if(isset($this->packages[$name]))
  578.             $package=$this->packages[$name];
  579.         else
  580.         {
  581.             if($this->corePackages===null)
  582.                 $this->corePackages=require(YII_PATH.'/web/js/packages.php');
  583.             if(isset($this->corePackages[$name]))
  584.                 $package=$this->corePackages[$name];
  585.         }
  586.         if(isset($package))
  587.         {
  588.             if(!empty($package['depends']))
  589.             {
  590.                 foreach($package['depends'] as $p)
  591.                     $this->registerCoreScript($p);
  592.             }
  593.             $this->coreScripts[$name]=$package;
  594.             $this->hasScripts=true;
  595.             $params=func_get_args();
  596.             $this->recordCachingAction('clientScript','registerCoreScript',$params);
  597.         }
  598.         return $this;
  599.     }
  600.  
  601.     /**
  602.      * Registers a CSS file
  603.      * @param string $url URL of the CSS file
  604.      * @param string $media media that the CSS file should be applied to. If empty, it means all media types.
  605.      * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
  606.      */
  607.     public function registerCssFile($url,$media='')
  608.     {
  609.         $this->hasScripts=true;
  610.         $this->cssFiles[$url]=$media;
  611.         $params=func_get_args();
  612.         $this->recordCachingAction('clientScript','registerCssFile',$params);
  613.         return $this;
  614.     }
  615.  
  616.     /**
  617.      * Registers a piece of CSS code.
  618.      * @param string $id ID that uniquely identifies this piece of CSS code
  619.      * @param string $css the CSS code
  620.      * @param string $media media that the CSS code should be applied to. If empty, it means all media types.
  621.      * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
  622.      */
  623.     public function registerCss($id,$css,$media='')
  624.     {
  625.         $this->hasScripts=true;
  626.         $this->css[$id]=array($css,$media);
  627.         $params=func_get_args();
  628.         $this->recordCachingAction('clientScript','registerCss',$params);
  629.         return $this;
  630.     }
  631.  
  632.     /**
  633.      * Registers a javascript file.
  634.      * @param string $url URL of the javascript file
  635.      * @param integer $position the position of the JavaScript code. Valid values include the following:
  636.      * <ul>
  637.      * <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
  638.      * <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
  639.      * <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
  640.      * </ul>
  641.      * @param array $htmlOptions additional HTML attributes
  642.      * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
  643.      */
  644.     public function registerScriptFile($url,$position=null,array $htmlOptions=array())
  645.     {
  646.         if($position===null)
  647.             $position=$this->defaultScriptFilePosition;
  648.         $this->hasScripts=true;
  649.         if(empty($htmlOptions))
  650.             $value=$url;
  651.         else
  652.         {
  653.             $value=$htmlOptions;
  654.             $value['src']=$url;
  655.         }
  656.         $this->scriptFiles[$position][$url]=$value;
  657.         $params=func_get_args();
  658.         $this->recordCachingAction('clientScript','registerScriptFile',$params);
  659.         return $this;
  660.     }
  661.  
  662.     /**
  663.      * Registers a piece of javascript code.
  664.      * @param string $id ID that uniquely identifies this piece of JavaScript code
  665.      * @param string $script the javascript code
  666.      * @param integer $position the position of the JavaScript code. Valid values include the following:
  667.      * <ul>
  668.      * <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
  669.      * <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
  670.      * <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
  671.      * <li>CClientScript::POS_LOAD : the script is inserted in the window.onload() function.</li>
  672.      * <li>CClientScript::POS_READY : the script is inserted in the jQuery's ready function.</li>
  673.      * </ul>
  674.      * @param array $htmlOptions additional HTML attributes
  675.      * Note: HTML attributes are not allowed for script positions "CClientScript::POS_LOAD" and "CClientScript::POS_READY".
  676.      * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
  677.      */
  678.     public function registerScript($id,$script,$position=null,array $htmlOptions=array())
  679.     {
  680.         if($position===null)
  681.             $position=$this->defaultScriptPosition;
  682.         $this->hasScripts=true;
  683.         if(empty($htmlOptions))
  684.             $scriptValue=$script;
  685.         else
  686.         {
  687.             if($position==self::POS_LOAD || $position==self::POS_READY)
  688.                 throw new CException(Yii::t('yii','Script HTML options are not allowed for "CClientScript::POS_LOAD" and "CClientScript::POS_READY".'));
  689.             $scriptValue=$htmlOptions;
  690.             $scriptValue['content']=$script;
  691.         }
  692.         $this->scripts[$position][$id]=$scriptValue;
  693.         if($position===self::POS_READY || $position===self::POS_LOAD)
  694.             $this->registerCoreScript('jquery');
  695.         $params=func_get_args();
  696.         $this->recordCachingAction('clientScript','registerScript',$params);
  697.         return $this;
  698.     }
  699.  
  700.     /**
  701.      * Registers a meta tag that will be inserted in the head section (right before the title element) of the resulting page.
  702.      *
  703.      * <b>Note:</b>
  704.      * Each call of this method will cause a rendering of new meta tag, even if their attributes are equal.
  705.      *
  706.      * <b>Example:</b>
  707.      * <pre>
  708.      *    $cs->registerMetaTag('example', 'description', null, array('lang' => 'en'));
  709.      *    $cs->registerMetaTag('beispiel', 'description', null, array('lang' => 'de'));
  710.      * </pre>
  711.      * @param string $content content attribute of the meta tag
  712.      * @param string $name name attribute of the meta tag. If null, the attribute will not be generated
  713.      * @param string $httpEquiv http-equiv attribute of the meta tag. If null, the attribute will not be generated
  714.      * @param array $options other options in name-value pairs (e.g. 'scheme', 'lang')
  715.      * @param string $id Optional id of the meta tag to avoid duplicates
  716.      * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
  717.      */
  718.     public function registerMetaTag($content,$name=null,$httpEquiv=null,$options=array(),$id=null)
  719.     {
  720.         $this->hasScripts=true;
  721.         if($name!==null)
  722.             $options['name']=$name;
  723.         if($httpEquiv!==null)
  724.             $options['http-equiv']=$httpEquiv;
  725.         $options['content']=$content;
  726.         $this->metaTags[null===$id?count($this->metaTags):$id]=$options;
  727.         $params=func_get_args();
  728.         $this->recordCachingAction('clientScript','registerMetaTag',$params);
  729.         return $this;
  730.     }
  731.  
  732.     /**
  733.      * Registers a link tag that will be inserted in the head section (right before the title element) of the resulting page.
  734.      * @param string $relation rel attribute of the link tag. If null, the attribute will not be generated.
  735.      * @param string $type type attribute of the link tag. If null, the attribute will not be generated.
  736.      * @param string $href href attribute of the link tag. If null, the attribute will not be generated.
  737.      * @param string $media media attribute of the link tag. If null, the attribute will not be generated.
  738.      * @param array $options other options in name-value pairs
  739.      * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.5).
  740.      */
  741.     public function registerLinkTag($relation=null,$type=null,$href=null,$media=null,$options=array())
  742.     {
  743.         $this->hasScripts=true;
  744.         if($relation!==null)
  745.             $options['rel']=$relation;
  746.         if($type!==null)
  747.             $options['type']=$type;
  748.         if($href!==null)
  749.             $options['href']=$href;
  750.         if($media!==null)
  751.             $options['media']=$media;
  752.         $this->linkTags[serialize($options)]=$options;
  753.         $params=func_get_args();
  754.         $this->recordCachingAction('clientScript','registerLinkTag',$params);
  755.         return $this;
  756.     }
  757.  
  758.     /**
  759.      * Checks whether the CSS file has been registered.
  760.      * @param string $url URL of the CSS file
  761.      * @return boolean whether the CSS file is already registered
  762.      */
  763.     public function isCssFileRegistered($url)
  764.     {
  765.         return isset($this->cssFiles[$url]);
  766.     }
  767.  
  768.     /**
  769.      * Checks whether the CSS code has been registered.
  770.      * @param string $id ID that uniquely identifies the CSS code
  771.      * @return boolean whether the CSS code is already registered
  772.      */
  773.     public function isCssRegistered($id)
  774.     {
  775.         return isset($this->css[$id]);
  776.     }
  777.  
  778.     /**
  779.      * Checks whether the JavaScript file has been registered.
  780.      * @param string $url URL of the javascript file
  781.      * @param integer $position the position of the JavaScript code. Valid values include the following:
  782.      * <ul>
  783.      * <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
  784.      * <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
  785.      * <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
  786.      * </ul>
  787.      * @return boolean whether the javascript file is already registered
  788.      */
  789.     public function isScriptFileRegistered($url,$position=self::POS_HEAD)
  790.     {
  791.         return isset($this->scriptFiles[$position][$url]);
  792.     }
  793.  
  794.     /**
  795.      * Checks whether the JavaScript code has been registered.
  796.      * @param string $id ID that uniquely identifies the JavaScript code
  797.      * @param integer $position the position of the JavaScript code. Valid values include the following:
  798.      * <ul>
  799.      * <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
  800.      * <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
  801.      * <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
  802.      * <li>CClientScript::POS_LOAD : the script is inserted in the window.onload() function.</li>
  803.      * <li>CClientScript::POS_READY : the script is inserted in the jQuery's ready function.</li>
  804.      * </ul>
  805.      * @return boolean whether the javascript code is already registered
  806.      */
  807.     public function isScriptRegistered($id,$position=self::POS_READY)
  808.     {
  809.         return isset($this->scripts[$position][$id]);
  810.     }
  811.  
  812.     /**
  813.      * Records a method call when an output cache is in effect.
  814.      * This is a shortcut to Yii::app()->controller->recordCachingAction.
  815.      * In case when controller is absent, nothing is recorded.
  816.      * @param string $context a property name of the controller. It refers to an object
  817.      * whose method is being called. If empty it means the controller itself.
  818.      * @param string $method the method name
  819.      * @param array $params parameters passed to the method
  820.      * @see COutputCache
  821.      */
  822.     protected function recordCachingAction($context,$method,$params)
  823.     {
  824.         if(($controller=Yii::app()->getController())!==null)
  825.             $controller->recordCachingAction($context,$method,$params);
  826.     }
  827.  
  828.     /**
  829.      * Adds a package to packages list.
  830.      *
  831.      * @param string $name the name of the script package.
  832.      * @param array $definition the definition array of the script package,
  833.      * @see CClientScript::packages.
  834.      * @return CClientScript the CClientScript object itself (to support method chaining, available since version 1.1.10).
  835.      *
  836.      * @since 1.1.9
  837.      */
  838.     public function addPackage($name,$definition)
  839.     {
  840.         $this->packages[$name]=$definition;
  841.         return $this;
  842.     }
  843. }
Add Comment
Please, Sign In to add comment