Code snippets for symfony 1.x


Refine Tags

Snippets tagged "flash"

LightWindow, SwfObject and Flash together

Hi, buddies!

It's my first snippet here, and, please, let me know if there's some bad or wrong code in your testings. Also, let me know if I make any mistakes about concepts here, or anything else. :-)

Well, this snippet is about how to get LightWindow, a wonderfull library, and Flash (I mean Adobe's) working together. This can be accomplished by using sfUFOPlugin as unobstrusive Flash content placement and ActionScript 2 inside the Flash animation, but I'll use sfSwfObjectHelperPlugin and ActionScript 3, which are my favorite ones. So, I'm assuming that you have these installed on your Symfony project:

and also knows how to program in ActionScript 3.

Now, let's start with the LightWindow portion of this snippet. If you are already using this plugin's helpers in the view, you don't need to do anything, since the library and CSS will be already loaded. If not, use the helper function, to load the resources, inside the view:

use_helper('LightWindow'); // load the LW helpers
_lwAddResources(); // load the LW resources (JS and CSS)

Alright, now the library is available for use by some JavaScript calls that we'll put inside our Flash project. But wait, there's a little catch here: I do not advise one to put a direct call to a LightWindow JS function as a command under an event listener, on Flash, because by the time the ActionScript is read in the compiled SWF the LW library may not be fully loaded; Instead of the direct call, I recommend you to create a simple JS function, right inside the view, that will delegate the call to the LW function, such as this:

echo javascript_tag('
function showWindowFromJS()
   myLightWindow.activateWindow({href: "path/to/my/image.extension"}); // auxiliary function to be called from inside ActionScript

The path can be assigned to a PHP variable, of course – normally I use $sf_request->getRelativeUrlRoot() to get to the root of the web folders, so that I can access my images easilly -. Also, read the API documentation on LW site to know more about the options to go inside the argument's object.

Now, how about some ActionScript? Let's say:

function showWindowFromFlash(event:MouseEvent):void
{'showWindowFromJS'); // using EI to communicate to the outside world
mMyLightWindowLauncher.buttonMode = true; // just to make the user sure that it's clickable.
mMyLightWindow.addEventListener(MouseEvent.CLICK, showWindowFromFlash); // assigning the function to the button event

We're almost there! Now, just load the Flash movie (SWF file) inside the view using sfSwfObjectHelperPlugin:

$id = 'flash_container_id'; // div to be replaced by Flash content.
$params = array( 'id'               => 'flash_object_id',
                 'movie'            => 'path/to/my/movie.swf'
                 'size'             => 'WIDTHxHEIGHT', // replace the dimensions here
                 'version'          => '9',
                 'background_color' => '#ffffff',
                 'params'           => array(
                   'allowScriptAccess' => 'sameDomain',
                   'wmode'             => 'transparent',
                   'quality'           => 'high' ),
                 'variables'        => array(
                   // a bunch of variables, can be an array with links to several images, just catch the vars inside the movie and use them passing as arguments in the ExternalInterface code.
                 'create_proxy'     => true );

That's it!

See you later, guys!

by Diogo Baeder on 2008-03-24, tagged ajax  flash  lightwindow  plugin  swfobject 
(1 comment)

Upload Multiple Files with Flash

you can create a multi-file uploader in Flash using ActionScript's FileReference and FileReferenceList classes, but you have to jump through some hoops to make it work. The upload() method doesn't send the user's session cookie with the request, so you have to send it in the URL and set the cookie manually on the server. If you are using ufo.js, the first argument to UFO.create() should look like this:

  movie: '/flash/uploader.swf', 
  id: 'uploader',
  name: 'uploader',
  flashvars: 'cookie=' + document.cookie,
  // other options ...

If you are dealing with multilple cookies, you need to parse document.cookie and send only the desired cookie.

Next, add the cookie to the URL:

var list:Array = myFileRefList.fileList;
var item:FileReference;
var url:String = _root.uploadURL + '?cookie=' + _root.cookie;
for (var i:Number = 0; i < list.length; i++) {
  item.upload (url);

Now add the following class to your symfony project:

class mySessionStorage extends sfSessionStorage
  public function initialize($context, $parameters = null)
    if ( /* whatever the condition is when we want to do this */ ) {
      if ($cookie = $context->getRequest()->getParameter('cookie')) {
        $name = 'symfony';
        preg_match('/^' . $name.'=(.*)$/', $cookie, $asMatch);
        $value = $asMatch[1];
    parent::initialize($context, $parameters);

Now edit factories.yml:

    class: mySessionStorage
      session_name: symfony

And you're done!

My blog post on this subject seemed popular, so I made it into a snippet. Hope it helps!

by Rob Rosenbaum on 2007-09-11, tagged flash  session  upload 

Flash object helper

Simple helper for embeding flash objects using Unobtrusive Flash Objects (UFO). Download and install ufo.js to js/ufo.js and place your movies (*.swf) to web/swf, FlashHelper.php in your lib/helper directory.

In your template

<div id="flashcontent">
  This text is replaced by the Flash movie.
<?php use_helper('Flash'); ?>
<?php echo flash_object('flashcontent', array(
  'bgcolor'=> '#FFFFFF'); 


  * Flash object helper - embeds Flash objects (files with the .swf extension)
  * Uses Unobtrusive Flash Objects (UFO) 
  * @param string $id - The ID od the div which will be replaced by flash movie
  * @param mixed  $options - Flash object options 
  * @see
function flash_object($id, $options = array())
  $options = _parse_attributes($options);
  $absolute = false;
  if (isset($options['absolute']))
    $absolute = true;
    list($options['width'], $options['height']) = split('x', $options['size'], 2);
    $options['majorversion'] = 7; 
    $options['build'] = 0; 
  // check for all required params  
  foreach(array('movie', 'width', 'height', 'majorversion', 'build') as $required)
      throw new sfException("{FlashHelper} Required parameter \"$required\" is missing.");
  $options['movie'] = flash_path($options['movie'], $absolute);  
  $opts = array();
  foreach ($options as $key => $value)
    $opts[] = $key . ': "' . $value . '"';
  // javascript variable name
  $name = $id . '_var';  
  $js = 'var ' . $name . " = {".join(', ', $opts)."};\n";   
  $js .= 'UFO.create('.$name.', "'.$id.'");'."\n";   
  return content_tag('script', "\n//".cdata_section("\n$js\n//")."\n", array('type' => 'text/javascript')); 
 * Returns the path to a flash swf movie.
 * <b>Example:</b>
 * <code>
 *  echo flash_path('mymovie');
 *    => /swf/mymovie.swf
 * </code>
 * <b>Note:</b> The asset name can be supplied as a...
 * - full path, like "/swf/movie.swf"
 * - file name, like "movie.swf", that gets expanded to "/swf/movie.swf"
 * - file name without extension, like "movie", that gets expanded to "/swf/movie.css"
 * @param  string asset name
 * @param  bool return absolute path ?
 * @return string file path to the flash movie file
function flash_path($source, $absolute = false)
  return _compute_public_path($source, 'swf', 'swf', $absolute);
by mishal on 2007-01-26, tagged flash  helper  swf  ufo