Author: chabotc
Date: Sun Sep 27 10:52:04 2009
New Revision: 819283

URL: http://svn.apache.org/viewvc?rev=819283&view=rev
Log:
SHINDIG-1182 by Jacky Wang: Add support for os:Flash

Modified:
    incubator/shindig/trunk/php/src/gadgets/templates/TemplateParser.php

Modified: incubator/shindig/trunk/php/src/gadgets/templates/TemplateParser.php
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/php/src/gadgets/templates/TemplateParser.php?rev=819283&r1=819282&r2=819283&view=diff
==============================================================================
--- incubator/shindig/trunk/php/src/gadgets/templates/TemplateParser.php 
(original)
+++ incubator/shindig/trunk/php/src/gadgets/templates/TemplateParser.php Sun 
Sep 27 10:52:04 2009
@@ -151,7 +151,7 @@
       // And replace the node with the parsed output
       $ownerDocument = $node->ownerDocument;
       foreach ($ret->childNodes as $childNode) {
-               $importedNode = $ownerDocument->importNode($childNode, true);
+        $importedNode = $ownerDocument->importNode($childNode, true);
         $importedNode = $node->parentNode->insertBefore($importedNode, $node);
       }
       $node->parentNode->removeChild($node);
@@ -439,7 +439,58 @@
 
       /****** Extension - Tags ******/
 
-      case 'osx:flash':
+      case 'os:flash':
+        // handle expressions
+        $this->parseNodeAttributes($node);
+
+        // read swf config from attributes
+        $swfConfig = array('width' => '100px',
+            'height' => '100px', 'play' => 'immediate');
+        foreach ($node->attributes as $attr) {
+          $swfConfig[$attr->name] = $attr->value;
+        }
+
+        // attach security token in the flash var
+        $st = 'st=' . $_GET['st'];
+        if (array_key_exists('flashvars', $swfConfig)) {
+          $swfConfig['flashvars'] = $swfConfig['flashvars'] . '&' . $st;
+        } else {
+          $swfConfig['flashvars'] = $st;
+        }
+
+        // Restrict the content if sanitization is enabled
+        $sanitizationEnabled = Config::get('sanitize_views');
+        if ($sanitizationEnabled) {
+          $swfConfig['allowscriptaccess'] = 'never';
+          $swfConfig['swliveconnect'] = 'false';
+          $swfConfig['allownetworking'] = 'internal';
+        }
+
+        // Generate unique id for this swf
+        $ALT_CONTENT_PREFIX = 'os_Flash_alt_';
+        $altContentId = uniqid($ALT_CONTENT_PREFIX);
+
+        // Create a div wrapper around the provided alternate content, and add 
the alternate content to the holder
+        $altHolder = $node->ownerDocument->createElement('div');
+        $altHolder->setAttribute('id', $altContentId);
+        foreach ($node->childNodes as $childNode) {
+          $altHolder->appendChild($childNode);
+        }
+        $node->parentNode->insertBefore($altHolder, $node);
+
+        // Create the call to swfobject in header
+        $scriptCode = SwfConfig::buildSwfObjectCall($swfConfig, $altContentId);
+        $scriptBlock = $node->ownerDocument->createElement('script');
+        $scriptBlock->setAttribute('type', 'text/javascript');
+        $node->parentNode->insertBefore($scriptBlock, $node);
+        if ($swfConfig['play'] != 'immediate') {
+          // Add onclick handler to trigger call to swfobject
+          $scriptCode = "function {$altContentId}()\{{$scriptCode};\}";
+          $altHolder->setAttribute('onclick', "{$altContentId}()");
+        }
+        $scriptCodeNode = $node->ownerDocument->createTextNode($scriptCode);
+        $scriptBlock->appendChild($scriptCodeNode);
+        return $node;
         break;
 
       case 'osx:navigatetoapp':
@@ -471,3 +522,31 @@
     return true;
   }
 }
+
+class SwfConfig {
+  public static $FLASH_VER = '9.0.115';
+  public static $PARAMS = array('loop', 'menu', 'quality', 'scale', 'salign', 
'wmode', 'bgcolor',
+      'swliveconnect', 'flashvars', 'devicefont', 'allowscriptaccess', 
'seamlesstabbing',
+      'allowfullscreen', 'allownetworking');
+  public static $ATTRS = array('id', 'name', 'styleclass', 'align');
+
+  public static function buildSwfObjectCall($swfConfig, $altContentId, 
$flashVars = 'null') {
+    $params = SwfConfig::buildJsObj($swfConfig, SwfConfig::$PARAMS);
+    $attrs = SwfConfig::buildJsObj($swfConfig, SwfConfig::$ATTRS);
+    $flashVersion = SwfConfig::$FLASH_VER;
+    $swfObject = "swfobject.embedSWF(\"{$swfConfig['swf']}\", 
\"{$altContentId}\", \"{$swfConfig['width']}\", \"{$swfConfig['height']}\", 
\"{$flashVersion}\", null, {$flashVars}, {$params}, {$attrs});";
+    return $swfObject;
+  }
+
+  private static function buildJsObj($swfConfig, $keymap) {
+    $arr = array();
+    foreach ($swfConfig as $key => $value) {
+      if (in_array($key, $keymap)) {
+        $arr[] = "{$key}:\"{$value}\"";
+      }
+    }
+    $output = implode(",", $arr);
+    $output = '{' . $output . '}';
+    return $output;
+  }
+}


Reply via email to