Use CJavaScript::encode to encode flashvars, params and attributes - it´s much easier and provides javascript parameter / method usage via "js:" (eg. "js:document.URL").
<?php
class ESwfObject extends CWidget
{
public static $count = 0;
public $params;
public $flashvars;
public $attributes;
public $swfFile;
public $width;
public $height;
public $playerVersion;
private $expressInstallFile;
private $newLineJS;
private $baseUrl;
private $clientScript;
public $randomID;
/**
* Init the extension
*/
public function init()
{
$this->randomID = '_' . self::$count++;
$this->newLineJS = "\n";
parent::init();
}
/**
* Publishes the assets
*/
public function publishAssets()
{
$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'assets';
$this->baseUrl = Yii::app()->getAssetManager()->publish($dir);
}
/**
* Registers the external javascript files
*/
public function registerClientScripts()
{
// add the scripts
if ($this->baseUrl === '')
throw new CException(Yii::t('ESwfObject', 'baseUrl must be set. This is done automatically by calling publishAssets()'));
$this->clientScript = Yii::app()->getClientScript();
$this->clientScript->registerScriptFile($this->baseUrl.'/swfobject.js');
// set install express url
$this->expressInstallFile = $this->baseUrl.'/expressInstall.swf';
}
/**
* The javascript needed
*/
protected function createJsCode()
{
$js = '';
// add the flashvars array
$js .= 'var flashvars' . $this->randomID . ' = ';
$js .= CJavaScript::encode($this->flashvars);
$js .= ';' . $this->newLineJS;
// add the params array
$js .= 'var params' . $this->randomID . ' = ';
$js .= CJavaScript::encode($this->params);
$js .= ';' . $this->newLineJS;
// add the attributes array
$js .= 'var attributes' . $this->randomID . ' = ';
$js .= CJavaScript::encode($this->attributes);
$js .= ';' . $this->newLineJS;
// create the swfobject call
$js .= $this->newLineJS;
$js .= 'swfobject.embedSWF("' . $this->swfFile . '", "eswfobject_content' . $this->randomID . '", "' . $this->width . '", "' . $this->height . '", "' . $this->playerVersion . '","' . $this->expressInstallFile . '", flashvars' . $this->randomID . ', params' . $this->randomID . ', attributes' . $this->randomID . ');';
return $js;
}
/**
* The HTML object to receive the SWF OBJECT content
*/
public function createHtml(){
$html = '<div id="eswfobject_content' . $this->randomID . '"></div>';
return $html;
}
/**
* Run the widget
*/
public function run()
{
$this->publishAssets();
$this->registerClientScripts();
$js = $this->createJsCode();
$this->clientScript->registerScript('js_eswfobject' . $this->randomID, $js, CClientScript::POS_HEAD);
echo( $this->createHtml() );
parent::run();
}
}