Flash and Joomla integration
One of the most requested things in the content management world is to make dynamic content to be displayed inside a flash movie. Flash has a feature that allows it to display content from a xml file, in this article I'm gonna explain how to display Joomla! content in your flash pages. With a little imagination you can change this "how-to" and make it work with other CMSes like Drupal, Xoops, or even your own CMS
You can check a integration example accessing: http://www.mmoraes.com
Before we start:
- This is a script I wrote a while ago, it is probably not the best (or even the right) way to do this integration, but it works.
- You can use, change, set fire... do whatever you want with these classes but please keep my name and URL in the credits.
- Suggestions are always welcome
ok, let's roll
I'm not flash expert. In fact, I didn't even know how to program in ActionScript before my boss asked me to create this integration. All I knew is that Flash could "read" xml files. The logic to create the integration with Joomla! is pretty simple: Export Joomla!'s content in a way that the Flash movie can read it. This means that we will generate a dynamic xml file with whatever we want to export and create a .swf that accesses it and badda-bing!
In this proof-of-concept, I'm gonna export the menu items and create them dynamically in my movie. Ok, enough talking.
First step: My Array-to-XML class
Function: Convert any given array to xml. This class gives flexibility to adapt this technique to virtually any other CMS.
-
<?php
-
/**
-
* XML-Generator
-
* @author Matheus Mendes aka bigodines - http://www.joomla.com.br
-
* @date July, 2006
-
* @license GNU GPL v2.0
-
* @note: this class uses a different naming for each part of a xml:
-
* <channel />
-
* <node />
-
* <item />attribute value</item>
-
* <attribute2 property="property value">item value</item2>
-
* </node>
-
* <node2 />
-
* <item />attribute value</attribute>
-
* <item2 property="property value">item value</item2>
-
* </node2>
-
* </channel>
-
*
-
* I know these names aren't the standard. But that's the way i've learned
suggestions are always welcome -
* -bigo
-
*/
-
-
class XmlGenerator {
-
-
var $channel = null;
-
var $xml = null;
-
var $level = null; // indent helper
-
var $nodeName = null;
-
-
function XmlGenerator($channel, $arrTree, $level = 0, $arrAttributes=null, $nodeName = "item" ) {
-
-
$this->nodeName = $nodeName;
-
$this->channel = $channel;
-
$this->level = $level;
-
-
foreach( $arrTree as $name => $value) {
-
$this->addNode( $this->nodeName, $value, $level+1, $arrAttributes[$name] );
-
} else {
-
if ($arrAttributes[$name]) {
-
$this->attributes[$name] = $this->getAttributes($arrAttributes[$name]);
-
$this->attributes[$name];
-
}
-
$this->addItem($name, $value );
-
}
-
}
-
}
-
-
-
function addNode( $channel, $arrAttribs, $level, $attributes=null ) {
-
$this->nodes[] = new XmlGenerator( $channel, $arrAttribs, $level, $attributes );
-
-
}
-
-
function setNodeName($name) {
-
$this->nodeName = $name;
-
}
-
-
/* creates a new item */
-
function addItem($itemName, $itemValue ) {
-
$this->items[$itemName] = $itemValue;
-
}
-
-
/* transform an array into a string of attributes */
-
function getAttributes($arr) {
-
$attrString = "";
-
foreach( $arr as $k => $v) {
-
$attrString .= ' '.$k.'="'.$v.'"';
-
}
-
return $attrString;
-
}
-
-
function getItems() {
-
return $this->items;
-
}
-
-
function createXml() {
-
-
//$this->insertHeader();
-
$this->insertBody();
-
return $this->getXml();
-
}
-
-
function struct() {
-
}
-
-
function insertBody() {
-
-
$this->xml .= $this->tab($this->level) . "<" . $this->channel . ">\n";
-
foreach($this->items as $item => $value) {
-
$attribs = "";
-
if($this->attributes[$item]) {
-
$attribs = $this->attributes[$item];
-
}
-
$this->xml .= $this->tab($this->level+1) . $this->toXml($item, $value, $attribs);
-
}
-
foreach( $this->nodes as $node) {
-
$node->insertBody();
-
$this->xml .= $node->getXml();
-
}
-
$this->xml .= $this->tab($this->level) ."</" . $this->channel . ">\n";
-
-
-
}
-
-
function tab($level) {
-
$r = "";
-
for($i=0;$i<$level;$i++) $r .= " ";
-
return $r;
-
}
-
-
function toXml($item, $value, $attribs=null) {
-
return "<" . $item . $attribs.">" . $value . "</" . $item . ">\n";
-
}
-
-
function insertHeader($encoding = "utf-8") {
-
$this->xml .= "<?xml version=\"1.0\" encoding=\"".$encoding."\"?>\n";
-
}
-
-
function getXml() {
-
return $this->xml;
-
}
-
-
}
-
-
//$xml = new XmlGenerator("mainmenu", $mainTree, 0, $attributes, 'nodezinho' );
-
//echo $xml->createXml();
-
-
?>
Second step: Create an export file that will be accessed by the flash movie
Function: We've gotta create one for each "part" we want to export (menus, news, polls, shopping items....). If you don't use Joomla! and uses another opensource CMS and has done (or are trying to...) this integration, please mail me so I can update this post
Here we go:
-
<?php
-
/**
-
* Joomla! MainMenu using XmlGenerator
-
* @author Matheus Mendes aka bigodines - http://www.joomla.com.br
-
* @date July, 2006
-
* @package xmlgen
-
*/
-
-
// begin - configuration
-
$menutype = 'mainmenu';
-
-
// end - configuration
-
-
-
-
// joomla stuff
-
include_once("../configuration.php");
-
include_once("../includes/database.php");
-
// XmlGenerator
-
include_once("clsXmlGen.php");
-
-
-
-
/* database object */
-
$database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
-
-
$sql = "SELECT id, name, link, type, parent FROM #__menu " .
-
"\n WHERE menutype = '$menutype' " .
-
"\n AND published = 1 " .
-
"\n ORDER BY parent, ordering";
-
-
$database->setQuery($sql);
-
$rows = $database->loadAssocList();
-
// establish the hierarchy of the menu
-
// first pass - collect children
-
foreach ($rows as $v ) {
-
$pt = $v->parent;
-
$familyTree[$pt] = $list1;
-
}
-
-
//print_r($familyTree); // debug
-
-
/* XmlGenerator params: channel, array with structure, start level, first node's name */
-
$xml = new XmlGenerator("mainmenu", $familyTree, 0, null,'menuItems' );
-
-
?>
Pretty simple isn't it? Basically, when this file is accessed, it collects our menu items and create a XML file using our XmlGenerator class.
Now, the final part: a Flash Movie to read everything.
As I said earlier, I'm no ActionScript developer neither a good designer. So this .fla may be VERY frustrating if you were expecting a lot of eye-candy effects or fancy colour combination
-
/**
-
Creating a flash menu for Joomla! using XMLGenerator!
-
@author Matheus Mendes aka bigodines - http://www.joomla.com.br
-
@date july, 2006
-
*/
-
-
myXml = new XML();
-
myXml.onLoad = XmlHandler;
-
myXml.ignoreWhite = true;
-
myXml.load("http://www.joomla.com.br/xmlgen/xgen-mainmenu.php");
-
//myXml.load("test.xml ");
-
-
function XmlHandler(ok) {
-
if (ok == true) {
-
parseChannel(this.firstChild.firstChild); // parses the channel
-
}
-
}
-
-
function parseChannel(channelNode) {
-
if (channelNode.nodeName.toUpperCase () == "MENUITEMS") {
-
menuItem = channelNode.firstChild;
-
i=0;
-
while (menuItem != null) {
-
if (menuItem.nodeName.toUpperCase() != "ITEM") break;
-
id = "";
-
name = "";
-
link = "";
-
parent = "";
-
element = menuItem.firstChild;
-
while(element != null) {
-
if (element.nodeName.toUpperCase() == "ID") {
-
id = element.firstChild.nodeValue;
-
-
}
-
if ( element.nodeName.toUpperCase() == "NAME") {
-
name = element.firstChild.nodeValue;
-
}
-
if (element.nodeName.toUpperCase() == "LINK") {
-
link = element.firstChild.nodeValue;
-
}
-
if (element.nodeName.toUpperCase() == "PARENT") {
-
parent = element.firstChild.nodeValue;
-
}
-
-
element = element.nextSibling;
-
}
-
// do something!!
-
if (i <13) doSomething(id, name, link, parent, i++);
-
menuItem = menuItem.nextSibling;
-
} // while menuItem
-
} // nodeName == "MAINMENU"
-
}
-
-
var nMenu_x = 80;
-
var target = _root;
-
var yPos = 60;
-
-
function doSomething(id, name, link, parent, n) {
-
myItem = target.attachMovie("myMenu", "menuitem"+n, 100+n);
-
myItem._x = nMenu_x;
-
myItem._y = yPos;
-
myItem.title = name;
-
myItem.onRelease = function() {
-
myItem.getURL('http://www.joomla.com.br/'+link , '_blank');
-
}
-
yPos += target["menuitem"+n]._height;
-
}
You can download the .fla here
I hope it helps someone
-bigo
15 Comments so far
Leave a reply

OK, could you show an example of how content would be added using the xmlgenerator, say for the front page?
@FBI, you can check this website: http://www.mmoraes.com/templates/home/index-en.htm, as you can see, this is just a regular .htm (with flash, of course) that is including joomla content.
you might note two things:
1 - I’ve created a small hack to assign an image to each new. That image that you see in the News Scroll in the site I mentioned isn’t static. Its dynamic but just appears in the flash (objectively, it is just a new field to fill when the journalists insert news).
2 - There is a bug with the nl2br() as you can see… If they insert content correctly (not hitting “enter” after the intro text) it would works well..
BTW: The example in the article I wrote doesn’t have anything to do with the frontpage. As you may have noticed, it is a menu and a [really ugly] dynamic flash loader, but the principle is the same for every application.
Cheers,
bigo
Well, I don’t know the first thing about xml files. I do understand some php and flash action script. Could the export file you have stated above (for the menu) be modified for news, polls, and whatnot? Or would the coding be different for each section I would want to export? For example, if changed
// begin - configuration
$menutype = ‘mainmenu’;
// end - configuration
and
$sql = “SELECT id, name, link, type, parent FROM #__menu ” .
“\n WHERE menutype = ‘$menutype’ ” .
“\n AND published = 1 ” .
“\n ORDER BY parent, ordering”;
to news where ($menutype = ‘mainmenu’;)=($contenttype = ‘news’;)
and
$sql = “SELECT id, name, link, type, parent FROM #__menu ” .
“\n WHERE menutype = ‘$menutype’ ” .
“\n AND published = 1 ” .
“\n ORDER BY parent, ordering”;
is changed to
$sql = “SELECT id, name, link, type, parent FROM #__content” .
“\n WHERE contenttype = ‘$contenttype’ ” .
“\n AND published = 1 ” .
“\n ORDER BY parent, ordering”;
would this work? Would you be willing to post an example script for an export file for use with news? I seem to learn better when I can see how it should look and then work backwards (reverse engineer as some would say) the script to see how it interacts with on another.
Thanks
The XmlGenerator class will work without modifications converting any given array to an XML file, so if you want to export different parts of your joomla, you will need to create files that use this class. These files will generate arrays with the info you would like to convert to XML and then you pass the generated XML to the flash.
I’ll try to find some time this week to blog a different example exporting something else. but the principles are the same.
cheers,
bigo
setQuery($sql);
$rows = $database->loadAssocList();
// establish the hierarchy of the menu
$familyTree = array();
// first pass - collect children
foreach ($rows as $v ) {
$pt = $v->parent;
$list1 = @$familyTree[$pt] ? $familyTree[$pt] : array();
array_push( $list1, $v );
$familyTree[$pt] = $list1;
}
//print_r($familyTree); // debug
/* XmlGenerator params: channel, array with structure, start level, first node’s name */
$xml = new XmlGenerator(”news”, $familyTree, 0, null,’contentItems’ );
echo $xml->createXml();
?>
sorry, code didn’t come out right.
there’s an easier way. I’ll blog (will write right now) an example and post here to show you. (inserting code as comments is pretty buggy in wp :/
ok, one more try with the coding.
setQuery($sql);
$rows = $database->loadAssocList();
// establish the hierarchy of the menu
$familyTree = array();
// first pass - collect children
foreach ($rows as $v ) {
$pt = $v->parent;
$list1 = @$familyTree[$pt] ? $familyTree[$pt] : array();
array_push( $list1, $v );
$familyTree[$pt] = $list1;
}
//print_r($familyTree); // debug
/* XmlGenerator params: channel, array with structure, start level, first node’s name */
$xml = new XmlGenerator(”news”, $familyTree, 0, null,’contentItems’ );
echo $xml->createXml();
?>
If this dosen’t show then the site won’t allow me to post the full code.
oh, ok. sorry for all the posts.
[…] how easy is to export Joomla! content to a XML format and then use it inside your flash movies (as you learned in the first part of this “article”). Today I’m gonna show a quick-and-dirty way to export your frontpage […]
hello, I’m trying to test this tutorial, but I can’t see the menuitems.. If I test the xml via webbrowser in a joomla localhost site, I get the items, but when I try to see them trough flash, they just don’t appear.. After some “trace”, It seems that the problem is in the actionscript. Here: if (channelNode.nodeName.toUpperCase() == “MENUITEMS”) {
.. It doesn’t recognize the “MENUITEMS”.. don’t kwnow why….
I did this test:
if (channelNode.nodeName.toUpperCase() != “MENUITEMS”) {
trace (”yes”);
}
/* and I do get a trace …*/
if (channelNode.nodeName.toUpperCase() == “MENUITEMS”) {
trace (”yes”);
}
/* and I do NOT get a trace …*/
please If anyone know what is going on, I would appreciate a lot… thanx..
people are stranger
For anyone trying to figure this out here is how to do it.
I had troubles because the directions are slightly vague but once I figured it out it was clear. The content example worked right away but this one did not.
I used a macro to remove all the numbers in the code above with TextPad… quick and easy.
1. Create a new directory in your Joomla root directory called xmlgen
2. First chunk of code above, put it in a file called clsXmlGen.php and save it into your Joomla/xmlgen directory.
3. Second chunk of code above, put it in a file called xgen-mainmenu.php and save it into your Joomla/xmlgen directory.
4. Third chunk of code above, just ignore that and download the .fla file below it.
5. Open up the .fla file in Flash and change line 10 to point to your xmlgen/xgen-mainmenu.php file.
6. On line 64 update the URL to your Joomla site including trailing slash.
7. On line 64 you will probably want to change _blank to _self so it doesn’t open in a new window every time.
8. Export the Flash movie and add the code to call the Flash file to your template or use a module to show the flash. There are some free ones on the extensions.joomla.org site.
Now, if everything went well, you should have a crude lookin menu that you can customize by editing the Flash file
I realized that on line 64 in step 6 above you should also add the Itemid as well so it detects permissions and whatnot properly. So to use the one above for an example on line 64 you would add the Itemid like the following:
#
myItem.getURL(’http://www.joomla.com.br/’+link+”&Itemid=”+id , ‘_self’);
#
I whanna use this for joomla 1.5 but i can’t get it to work can you help me out?
This does work but its only for a menu, is it also possible to show content in a swf??