<?php
class QSWFObject {
protected $rootNode = 'div';
protected $swfObjectDivId = 'flashContent';
protected $swfObjectScriptBase = 'js/swfobject.js';
protected $config;
protected $object;
protected $properties;
protected $useSWFObject = false;
protected $useSWFObjectScript = false;
public function __construct($useSwfObject = true, $useSwfObjectScript = true, $classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000', $codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'){
if(!is_object($this->object) && !$this->object instanceof SimpleXMLElement){
$xml = '<?xml version="1.0" encoding="UTF-8"?><'.$this->rootNode.'></'.$this->rootNode.'>';
$this->object = new SimpleXMLElement($xml);
$this->properties['classid'] = $classid;
$this->properties['codebase'] = $codebase;
if($useSwfObject){
$this->useSWFObject();
}
if($useSwfObjectScript){
$this->useSWFObjectScript();
}
}
}
public function setFlashId($id){
$this->swfObjectDivId = (string) $id;
}
public function setSWFObjectScript($file){
$this->swfObjectScriptBase = (string) $file;
}
public function init(){
if($this->useSWFObject){
$this->initNoScript();
} else {
$this->initObject();
}
}
public function addConfig($config){
$this->config = $config;
}
protected function renderSWFObject(){
$array = array();
if($this->config){
$array[] = 'var so=new SWFObject("'.($this->config["path"]?$this->config["path"].'/open-flash-chart.swf':"").'","'.(($this->config["id"]?$this->config["id"]:"")).'","'.(($this->config["width"]?$this->config["width"]:"")).'","'.(($this->config["height"]?$this->config["height"]:"")).'","'.(($this->config["version"]?$this->config["version"]:"9")).'","'.(($this->config["bgcolor"]?$this->config["bgcolor"]:"#fffff")).'");';
$array[] = 'so.addVariable("data","'.(($this->config["url"]?$this->config["url"]:"")).'");';
$array[] = 'so.addVariable("allowScriptAccess","sameDomain");';
$array[] = 'so.write("'.$this->swfObjectDivId.'");';
return "\n".implode("\n", $array)."\n";
} else {
echo "Config Data is missing.";
}
}
protected function initSWFObject(){
$swfobject = $this->get();
$div = $swfobject->addChild("div");
$div->addAttribute('id', $this->swfObjectDivId);
if($this->useSWFObjectScript){
$script = $swfobject->addChild("script");
$script->addAttribute("type", "text/javascript");
$script->addAttribute("src", $this->config["path"].'/js/swfobject.js');
}
$script2 = $swfobject->addChild("script", $this->renderSWFObject());
$script2->addAttribute("type", "text/javascript");
}
protected function initNoScript(){
$swfobject = $this->get();
$this->initSWFObject();
$noscript = $swfobject->addChild("noscript");
$object = $noscript->addChild("object");
foreach($this->properties as $name => $value){
$object->addAttribute($name, $value);
}
}
protected function initObject(){
$swfobject = $this->get();
$object = $swfobject->addChild("object");
foreach($this->properties as $name => $value){
$object->addAttribute($name, $value);
}
}
public function useSWFObject(){
$this->useSWFObject = true;
}
public function useSWFObjectScript(){
$this->useSWFObjectScript = true;
}
public function get(){
return $this->object;
}
public function addAttributes($array){
foreach($array as $name => $value){
$this->setAttribute($name, $value);
}
}
public function setAttribute($name, $value){
$this->object->addAttribute($name, $value);
}
public function setParam($array){
$this->setNode("param", $array);
}
public function setEmbed($array){
$this->setNode("embed", $array);
}
protected function setNode($type, $array){
if($this->useSWFObject){
$$type = $this->get()->noscript->object->addChild($type);
} else {
$$type = $this->get()->object->addChild($type);
}
foreach($array as $name => $value){
$$type->addAttribute($name, $value);
}
}
protected function getRootNode(){
return $this->rootNode;
}
public function render(){
$domnode = dom_import_simplexml($this->get());
$dom = new DomDocument();
$domnode = $dom->importNode($domnode, true);
$dom->appendChild($domnode);
echo $dom->saveHTML();
}
}
?>