Add ability to add external script files.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 30 Jul 2009 16:16:07 +0000 (12:16 -0400)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Thu, 30 Jul 2009 16:16:07 +0000 (12:16 -0400)
(Code ported from Ansel's Ansel_Script_Files)

framework/Core/lib/Horde.php
framework/Core/lib/Horde/Script/Files.php

index 32056f9..ba1cb72 100644 (file)
@@ -295,15 +295,17 @@ HTML;
             return;
         }
 
-        $js_tocache = $js_force = array();
+        $js_tocache = $js_force = $js_external = array();
         $mtime = array(0);
 
         $s_list = $hsf->listFiles();
         foreach ($s_list as $app => $files) {
             foreach ($files as $file) {
-                if ($file['d'] && ($file['f'][0] != '/')) {
+                if ($file['d'] && ($file['f'][0] != '/') && empty($file['e'])) {
                     $js_tocache[$file['p'] . $file['f']] = false;
                     $mtime[] = filemtime($file['p'] . $file['f']);
+                } elseif (!empty($file['e'])) {
+                    $js_external[] = $file['u'];
                 } else {
                     $js_force[] = $file['u'];
                 }
@@ -358,12 +360,24 @@ HTML;
             }
         }
 
-        foreach (array_merge(array($js_url), $js_force) as $val) {
+        foreach (array_merge($js_external, array($js_url), $js_force) as $val) {
             echo '<script type="text/javascript" src="' . $val . "\"></script>\n";
         }
     }
 
     /**
+     * Inlude script files from external sources.
+     *
+     * @param string $url  The url to the external script file.
+     * @param string $app  The app scope
+     */
+    static public function addExternalScriptFile($url, $app = null)
+    {
+         $hsf = Horde_Script_Files::singleton();
+         $hsf->addExternal($url, $app);
+    }
+
+    /**
      * Get a token for protecting a form.
      *
      * @param string $slug  Slug name.
index a1cc7af..115bfc7 100644 (file)
@@ -86,6 +86,31 @@ class Horde_Script_Files
     }
 
     /**
+     * Adds an external script file
+     *
+     * @param string $url  The url to the external script file.
+     * @param string $app  The app scope
+     */
+    public function addExternal($url, $app)
+    {
+        if (empty($app)) {
+            $app = $GLOBALS['registry']->getApp();
+        }
+
+        // Don't include scripts multiple times.
+        if (!empty($this->_included[$app][$url])) {
+            return false;
+        }
+
+        $this->_included[$app][$url] = true;
+
+        $this->_files[$app][] = array('f' => basename($url),
+                                      'u' => $url,
+                                      'd' => false,
+                                      'e' => true);
+    }
+
+    /**
      * Helper function to determine if given file needs to be output.
      */
     public function _add($file, $app, $direct, $full = false)