Snippets

Create an account or login to be able to add, comment and rate snippets.

Navigation

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.
</div>
<?php use_helper('Flash'); ?>
<?php echo flash_object('flashcontent', array(
  'movie'=>'movie.swf', 
  'width'=>'200', 
  'height'=>'300', 
  'bgcolor'=> '#FFFFFF'); 
?>

FlashHelper.php

<?php
 /**
  * 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 http://www.bobbyvandersluis.com/ufo/
  */
function flash_object($id, $options = array())
{  
  sfContext::getInstance()->getResponse()->addJavascript('ufo.js');
 
  $options = _parse_attributes($options);
  $absolute = false;
  if (isset($options['absolute']))
  {
    unset($options['absolute']);
    $absolute = true;
  }  
  if(isset($options['size']))
  {
    list($options['width'], $options['height']) = split('x', $options['size'], 2);
    unset($options['size']);
  }
  if(!isset($options['majorversion'])) 
  {
    $options['majorversion'] = 7; 
  }  
  if(!isset($options['build'])) 
  {
    $options['build'] = 0; 
  }  
  // check for all required params  
  foreach(array('movie', 'width', 'height', 'majorversion', 'build') as $required)
  {
    if(!isset($options[$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 . '"';
  }
  sort($opts);
  // 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 

Comments on this snippet

gravatar icon
#1 Francois Zaninotto on 2007-01-29 at 09:12

Also to be noted

http://www.symfony-project.com/trac/wiki/sfSWFObjectHelperPlugin

gravatar icon
#2 Sylvain PAPET on 2007-01-29 at 09:14

Nice, You could also use SWFObject which is a javascript library which does almost the same thing but on client side : http://blog.deconcept.com/swfobject/ It replace a <div id="flashcontent">alternate content</div> with flash which is specied with a simple function call like your helper.

gravatar icon
#3 Stephen Leavitt on 2007-02-04 at 10:26

I've renamed the plugin to sfSwfObjectHelperPlugin in keeping with the Symfony naming standards. Also, the documentation has been updated significantly.

http://www.symfony-project.com/trac/wiki/sfSwfObjectHelperPlugin

You need to create an account or log in to post a comment or rate this snippet.