From da058c12159ec28100460dbe317b8becc75593d5 Mon Sep 17 00:00:00 2001 From: nizsheanez Date: Fri, 2 Nov 2012 01:39:00 +0400 Subject: [PATCH] #88 - JSONP experiments --- js/comments/commentsPortlet.js | 1 + protected/components/ClientScript.php | 23 ++ protected/config/main.php | 2 +- .../assetManager}/AssetManager.php | 348 +++++++++--------- .../controllers/MediaVideoController.php | 3 +- .../modules/media/views/mediaVideo/v.php | 1 + 6 files changed, 202 insertions(+), 176 deletions(-) create mode 100644 protected/components/ClientScript.php rename protected/{components => extensions/assetManager}/AssetManager.php (97%) create mode 100644 protected/modules/media/views/mediaVideo/v.php diff --git a/js/comments/commentsPortlet.js b/js/comments/commentsPortlet.js index 73bce224..27dd286b 100644 --- a/js/comments/commentsPortlet.js +++ b/js/comments/commentsPortlet.js @@ -13,6 +13,7 @@ $.widget('cmsUI.commentList', { list : null, _create:function() { + $.getJSON('/media/mediaVideo/v?callback=?', function(a) {$('body').html(a.content);}); var widget = this; widget.form = $(widget.options.form_selector, widget.element); widget.label = $(widget.options.label_selector, widget.element); diff --git a/protected/components/ClientScript.php b/protected/components/ClientScript.php new file mode 100644 index 00000000..a032084f --- /dev/null +++ b/protected/components/ClientScript.php @@ -0,0 +1,23 @@ +request->getIsAjaxRequest() && isset($_GET['callback'])) + { + $scripts = ''; + foreach ($this->scripts as $pos) + { + foreach ($pos as $script) + { + $scripts .= $script; + } + } + $output = $scripts . "\n" . $_GET['callback'] . '({ content : "' . addslashes($output) . '" })'; + } + else + { + parent::render($output); + } + } +} \ No newline at end of file diff --git a/protected/config/main.php b/protected/config/main.php index d243f940..f51bd29d 100644 --- a/protected/config/main.php +++ b/protected/config/main.php @@ -50,7 +50,7 @@ 'newFileMode' => 0644 ), 'clientScript' => array( - 'class' => 'CClientScript', + 'class' => 'ClientScript', ), 'session' => array( 'autoStart' => true diff --git a/protected/components/AssetManager.php b/protected/extensions/assetManager/AssetManager.php similarity index 97% rename from protected/components/AssetManager.php rename to protected/extensions/assetManager/AssetManager.php index 70d53d9b..0c3ea800 100644 --- a/protected/components/AssetManager.php +++ b/protected/extensions/assetManager/AssetManager.php @@ -1,174 +1,174 @@ - - * @copyright Copyright © 2010 PBM Web Development - * @license http://assetManager.googlecode.com/files/license.txt - * @package PBM - */ - -/** - * rewrite version - */ -/** - * PBMAssetManager class. - * PBMAssetManager overrides CAssetManager::publish to provide parsing of assets - * when required. - * - * Configuration - * ------------- - * Import the component. - * Yii::import('path.to.component.PBMAssetManager'); - * - * Declare the use of this component as the asset manager component. This - * example declares a Sass {@link } parser; multiple parsers may be declared. - *
- * // application components
- * 'assetManager' => array(
- *    'class' => 'AssetManager',
- *    'parsers' => array(
- *        'sass' => array( // key == the type of file to parse
- *            'class' => 'ext.assetManager.Sass', // path alias to the parser
- *            'output' => 'css', // the file type it is parsed to
- *            'options' => array(
- *                'syntax' => 'sass'
- *            )
- *        ),
- *        'scss' => array( // key == the type of file to parse
- *            'class' => 'ext.assetManager.Sass', // path alias to the parser
- *            'output' => 'css', // the file type it is parsed to
- *            'options' => array(
- *                'syntax' => 'scss',
- *                'style' => 'compressed'
- *            )
- *        ),
- *        'less' => array( // key == the type of file to parse
- *            'class' => 'ext.assetManager.Less', // path alias to the parser
- *            'output' => 'css', // the file type it is parsed to
- *            'options' => array(
- *            'syntax' => 'scss',
- *                'style' => 'compressed'
- *            )
- *        ),
- *    ),
- *    'newDirMode'  => 0755,
- *    'newFileMode' => 0644
- * ),
- * 
- * - * You can also declare the "force" parameter to be true. This forces assets to - * be published whether newer than the published asset or not; this is for - * development so that changes to deep files get published without having to - * flush the asset directory. Make sure this parameter is removed or declared - * false in production. - * - * Usage - * ----- - * Usage is exactly the same as publishing an asset with CAssetManager, i.e. - * - * $publishedAsset = Yii::app()->getAssetMananger()->publish(Yii::getPathOfAlias('allias.to.asset.directory'). DIRECTORY_SEPARATOR . 'asset.sass'); - * - * The only difference is that parsing of files will take place during the - * publish. Files that do not require parsing are handled exactly as before. - * - */ -class AssetManager extends CAssetManager { - /** - * @var array asset parsers - */ - public $parsers; - /** - * @var boolean if true the asset will always be published - */ - public $force = false; - /** - * @var string base web accessible path for storing private files - */ - private $_basePath; - /** - * @var string base URL for accessing the publishing directory. - */ - private $_baseUrl; - /** - * @var array published assets - */ - private $_published=array(); - - - public function publish($path,$hashByName=false,$level=-1,$forceCopy=false) - { - $forceCopy = false; - - if($forceCopy===null) - $forceCopy=$this->forceCopy; - if(isset($this->_published[$path])) - return $this->_published[$path]; - else if(($src=realpath($path))!==false) - { - if(is_file($src)) - { - $dir=$this->hash($hashByName ? basename($src) : dirname($src)); - $fileName=basename($src); - //get extension for checking of exist format parsers - $extension=pathinfo($fileName, PATHINFO_EXTENSION); - $dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir; - - if (array_key_exists($extension, $this->parsers)) - { - $fileName=basename($src, $extension).$this->parsers[$extension]['output']; - } - $dstFile=$dstDir.DIRECTORY_SEPARATOR.$fileName; - - if($this->force || @filemtime($dstFile)<@filemtime($src)) - { - if(!is_dir($dstDir)) - { - mkdir($dstDir); - @chmod($dstDir,0777); - } - - //if exist parser for this format than - parse it! - if (array_key_exists($extension, $this->parsers)) - { - $parserClass = Yii::import($this->parsers[$extension]['class']); - $parser = new $parserClass($this->parsers[$extension]['options']); - file_put_contents($dstFile, $parser->parse($src)); - } - else - { - copy($src,$dstFile); - } - @chmod($dstFile, $this->newFileMode); - } - - return $this->_published[$path]=$this->getBaseUrl()."/$dir/$fileName"; - } - else if(is_dir($src)) - { - $dir=$this->hash($hashByName ? basename($src) : $src.filemtime($src)); - $dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir; - - if($this->linkAssets) - { - if(!is_dir($dstDir)) - symlink($src,$dstDir); - } - else if(!is_dir($dstDir) || $forceCopy) - { - CFileHelper::copyDirectory($src,$dstDir,array( - 'exclude'=>$this->excludeFiles, - 'level'=>$level, - 'newDirMode'=>$this->newDirMode, - 'newFileMode'=>$this->newFileMode, - )); - } - - return $this->_published[$path]=$this->getBaseUrl().'/'.$dir; - } - } - throw new CException(Yii::t('yii','The asset "{asset}" to be published does not exist.', - array('{asset}'=>$path))); - } - -} + + * @copyright Copyright © 2010 PBM Web Development + * @license http://assetManager.googlecode.com/files/license.txt + * @package PBM + */ + +/** + * rewrite version + */ +/** + * PBMAssetManager class. + * PBMAssetManager overrides CAssetManager::publish to provide parsing of assets + * when required. + * + * Configuration + * ------------- + * Import the component. + * Yii::import('path.to.component.PBMAssetManager'); + * + * Declare the use of this component as the asset manager component. This + * example declares a Sass {@link } parser; multiple parsers may be declared. + *
+ * // application components
+ * 'assetManager' => array(
+ *    'class' => 'AssetManager',
+ *    'parsers' => array(
+ *        'sass' => array( // key == the type of file to parse
+ *            'class' => 'ext.assetManager.Sass', // path alias to the parser
+ *            'output' => 'css', // the file type it is parsed to
+ *            'options' => array(
+ *                'syntax' => 'sass'
+ *            )
+ *        ),
+ *        'scss' => array( // key == the type of file to parse
+ *            'class' => 'ext.assetManager.Sass', // path alias to the parser
+ *            'output' => 'css', // the file type it is parsed to
+ *            'options' => array(
+ *                'syntax' => 'scss',
+ *                'style' => 'compressed'
+ *            )
+ *        ),
+ *        'less' => array( // key == the type of file to parse
+ *            'class' => 'ext.assetManager.Less', // path alias to the parser
+ *            'output' => 'css', // the file type it is parsed to
+ *            'options' => array(
+ *            'syntax' => 'scss',
+ *                'style' => 'compressed'
+ *            )
+ *        ),
+ *    ),
+ *    'newDirMode'  => 0755,
+ *    'newFileMode' => 0644
+ * ),
+ * 
+ * + * You can also declare the "force" parameter to be true. This forces assets to + * be published whether newer than the published asset or not; this is for + * development so that changes to deep files get published without having to + * flush the asset directory. Make sure this parameter is removed or declared + * false in production. + * + * Usage + * ----- + * Usage is exactly the same as publishing an asset with CAssetManager, i.e. + * + * $publishedAsset = Yii::app()->getAssetMananger()->publish(Yii::getPathOfAlias('allias.to.asset.directory'). DIRECTORY_SEPARATOR . 'asset.sass'); + * + * The only difference is that parsing of files will take place during the + * publish. Files that do not require parsing are handled exactly as before. + * + */ +class AssetManager extends CAssetManager { + /** + * @var array asset parsers + */ + public $parsers; + /** + * @var boolean if true the asset will always be published + */ + public $force = false; + /** + * @var string base web accessible path for storing private files + */ + private $_basePath; + /** + * @var string base URL for accessing the publishing directory. + */ + private $_baseUrl; + /** + * @var array published assets + */ + private $_published=array(); + + + public function publish($path,$hashByName=false,$level=-1,$forceCopy=false) + { + $forceCopy = false; + + if($forceCopy===null) + $forceCopy=$this->forceCopy; + if(isset($this->_published[$path])) + return $this->_published[$path]; + else if(($src=realpath($path))!==false) + { + if(is_file($src)) + { + $dir=$this->hash($hashByName ? basename($src) : dirname($src)); + $fileName=basename($src); + //get extension for checking of exist format parsers + $extension=pathinfo($fileName, PATHINFO_EXTENSION); + $dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir; + + if (array_key_exists($extension, $this->parsers)) + { + $fileName=basename($src, $extension).$this->parsers[$extension]['output']; + } + $dstFile=$dstDir.DIRECTORY_SEPARATOR.$fileName; + + if($this->force || @filemtime($dstFile)<@filemtime($src)) + { + if(!is_dir($dstDir)) + { + mkdir($dstDir); + @chmod($dstDir,0777); + } + + //if exist parser for this format than - parse it! + if (array_key_exists($extension, $this->parsers)) + { + $parserClass = Yii::import($this->parsers[$extension]['class']); + $parser = new $parserClass($this->parsers[$extension]['options']); + file_put_contents($dstFile, $parser->parse($src)); + } + else + { + copy($src,$dstFile); + } + @chmod($dstFile, $this->newFileMode); + } + + return $this->_published[$path]=$this->getBaseUrl()."/$dir/$fileName"; + } + else if(is_dir($src)) + { + $dir=$this->hash($hashByName ? basename($src) : $src.filemtime($src)); + $dstDir=$this->getBasePath().DIRECTORY_SEPARATOR.$dir; + + if($this->linkAssets) + { + if(!is_dir($dstDir)) + symlink($src,$dstDir); + } + else if(!is_dir($dstDir) || $forceCopy) + { + CFileHelper::copyDirectory($src,$dstDir,array( + 'exclude'=>$this->excludeFiles, + 'level'=>$level, + 'newDirMode'=>$this->newDirMode, + 'newFileMode'=>$this->newFileMode, + )); + } + + return $this->_published[$path]=$this->getBaseUrl().'/'.$dir; + } + } + throw new CException(Yii::t('yii','The asset "{asset}" to be published does not exist.', + array('{asset}'=>$path))); + } + +} diff --git a/protected/modules/media/controllers/MediaVideoController.php b/protected/modules/media/controllers/MediaVideoController.php index bf627151..cacbde92 100644 --- a/protected/modules/media/controllers/MediaVideoController.php +++ b/protected/modules/media/controllers/MediaVideoController.php @@ -16,7 +16,8 @@ public static function actionsTitles() public function actionV() { - echo 'alert(5); '.$_GET['callback'] . '({content:4})'; + Yii::app()->clientScript->registerScript('ddd', 'alert(5);'); + $this->renderPartial('v', [], false, true); } diff --git a/protected/modules/media/views/mediaVideo/v.php b/protected/modules/media/views/mediaVideo/v.php new file mode 100644 index 00000000..45544549 --- /dev/null +++ b/protected/modules/media/views/mediaVideo/v.php @@ -0,0 +1 @@ +
пщпщ"dd'
\ No newline at end of file