Author: sevein
Date: Sat Oct  9 09:19:31 2010
New Revision: 8037

Log:
Add support for checking updates with AJAX (XHR), thanks to JSONP. It is set as 
default way in favour of sfWebRequest. Outstanding issue: add 
updateCheck.js dinamically.

Added:
   trunk/js/updateCheck.js   (contents, props changed)
Modified:
   trunk/apps/qubit/modules/default/actions/updateCheckComponent.class.php
   trunk/apps/qubit/modules/default/templates/_updateCheck.php

Modified: 
trunk/apps/qubit/modules/default/actions/updateCheckComponent.class.php
==============================================================================
--- trunk/apps/qubit/modules/default/actions/updateCheckComponent.class.php     
Sat Oct  9 05:38:38 2010        (r8036)
+++ trunk/apps/qubit/modules/default/actions/updateCheckComponent.class.php     
Sat Oct  9 09:19:31 2010        (r8037)
@@ -29,52 +29,60 @@
 {
   public function execute($request)
   {
-    $data = array();
-  
-    if (!$this->getUser()->hasCredential('administrator') || 
!sfConfig::get('app_check_for_updates'))
+    if (!$this->getUser()->hasCredential('administrator') || 
!sfConfig::get('app_check_for_updates') || 
$request->getCookie('update_checked'))
     {
 
       return sfView::NONE;
     }
 
-    if (null === ($this->lastVersion = 
$this->getUser()->getAttribute('last_version')))
-    {
-      $data = array();
-
-      // Collect data
-      $data['address'] = $request->getUriPrefix() . $request->getScriptName() 
. $request->getPathInfo();
-      $data['site_description'] = sfConfig::get('app_siteDescription');
-      $data['site_title'] = sfConfig::get('app_siteTitle');
-      $data['version'] = qubitConfiguration::VERSION.' - 
'.sfConfig::get('app_version');
+    $this->currentVersion = qubitConfiguration::VERSION;
 
-      $packageXmlPath = sfConfig::get('sf_config_dir').'/package.xml';
-      if (file_exists($packageXmlPath))
-      {
-        require_once 
sfConfig::get('sf_root_dir').'/vendor/FluentDOM/FluentDOM.php';
+    $this->updateCheckUrl = 'http://updatecheck.qubit-toolkit.org/check/';
 
-        $fd = FluentDOM($packageXmlPath)
-          ->namespaces(array('p' => 'http://pear.php.net/dtd/package-2.0'));
+    $this->updateCheckData = array();
+    $this->updateCheckData['address'] = $request->getUriPrefix() . 
$request->getScriptName() . $request->getPathInfo();
+    $this->updateCheckData['version'] = qubitConfiguration::VERSION.' - 
'.sfConfig::get('app_version');
 
-        $data['distribution'] = $fd->find('/*/p:name')->item(0)->textContent;
-      }
+    $packageXmlPath = sfConfig::get('sf_config_dir').'/package.xml';
+    if (file_exists($packageXmlPath))
+    {
+      require_once 
sfConfig::get('sf_root_dir').'/vendor/FluentDOM/FluentDOM.php';
 
-      try
-      {
-        $browser = new sfWebBrowser;
-        $this->lastVersion = 
$browser->post('http://updatecheck.qubit-toolkit.org/check/', 
$data)->getResponseText();
-      }
-      catch (Exception $e)
-      {
-        $this->lastVersion = 0;
-      }
+      $fd = FluentDOM($packageXmlPath)
+        ->namespaces(array('p' => 'http://pear.php.net/dtd/package-2.0'));
 
-      $this->getUser()->setAttribute('last_version', $this->lastVersion);
+      $this->updateCheckData['distribution'] = 
$fd->find('/*/p:name')->item(0)->textContent;
     }
 
-    if (0 == $this->lastVersion || 1 > version_compare($this->lastVersion, 
qubitConfiguration::VERSION))
+    $this->updateCheckData['site_description'] = 
sfConfig::get('app_siteDescription');
+    $this->updateCheckData['site_title'] = sfConfig::get('app_siteTitle');
+
+    if (($request->getCookie('has_js')))
     {
+      // TODO QubitUpdateCheck::addAssets($this->response);
+    }
+    else
+    {
+      if (null === ($this->lastVersion = 
$this->getUser()->getAttribute('last_version')))
+      {
+        try
+        {
+          $browser = new sfWebBrowser;
+          $this->lastVersion = $browser->post($this->updateCheckUrl, 
$this->updateCheckData)->getResponseText();
+        }
+        catch (Exception $e)
+        {
+          $this->lastVersion = 0;
+        }
 
-      return sfView::NONE;
+        $this->getUser()->setAttribute('last_version', $this->lastVersion);
+      }
+
+      if (0 == $this->lastVersion || 1 > version_compare($this->lastVersion, 
qubitConfiguration::VERSION))
+      {
+
+        return sfView::NONE;
+      }
     }
   }
 }

Modified: trunk/apps/qubit/modules/default/templates/_updateCheck.php
==============================================================================
--- trunk/apps/qubit/modules/default/templates/_updateCheck.php Sat Oct  9 
05:38:38 2010        (r8036)
+++ trunk/apps/qubit/modules/default/templates/_updateCheck.php Sat Oct  9 
09:19:31 2010        (r8037)
@@ -1 +1,20 @@
-<div id="update-check"><span><?php echo __('A release %1% upgrade is 
available.', array('%1%' => $lastVersion)) ?></span></div>
+<? if ($sf_request->getCookie('has_js')): ?>
+
+  <?php use_helper('Javascript') ?>
+
+  <?php $data = json_encode($sf_data->getRaw('updateCheckData')) ?>
+  <?php $notificationMessage = __('A release %1% upgrade is available.') ?>
+
+  <?php echo javascript_tag(<<<EOF
+Qubit.updateCheck.url = '$updateCheckUrl';
+Qubit.updateCheck.currentVersion = '$currentVersion';
+Qubit.updateCheck.data = $data;
+Qubit.updateCheck.notificationMessage = '$notificationMessage';
+EOF
+) ?>
+
+<?php else: ?>
+
+  <div id="update-check"><span><?php echo __($notificationMessage, array('%1%' 
=> $lastVersion)) ?></span></div>
+
+<?php endif; ?>

Added: trunk/js/updateCheck.js
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ trunk/js/updateCheck.js     Sat Oct  9 09:19:31 2010        (r8037)
@@ -0,0 +1,71 @@
+// $Id$
+
+(function ($)
+  {
+    Qubit.updateCheck = Qubit.updateCheck || {};
+
+    Drupal.behaviors.updateCheck = {
+
+      attach: function (context)
+        {
+          $.ajax({
+            data: Qubit.updateCheck.data,
+            dataType: 'jsonp',
+            jsonpCallback: 'updateCheck',
+            timeout: 20000,
+            type: 'GET',
+            url: Qubit.updateCheck.url,
+            success: function(data)
+              {
+                if (1 > version_compare(Qubit.updateCheck.currentVersion, 
data.version))
+                {
+                  $('<div id="update-check"><span>' + 
Qubit.updateCheck.notificationMessage.replace('%1%', data.version) + 
'</span></div>').insertAfter('#languageMenu');
+                }
+
+                YAHOO.util.Cookie.set('update_checked', 1, { path: '/' });
+              }
+          });
+        }
+    }
+
+    function version_compare(v1, v2)
+    {
+      splitVersion = function (v)
+        {
+
+          return ('' + v).split('.');
+        };
+
+      v1 = splitVersion(v1);
+      v2 = splitVersion(v2);
+
+      var compare = 0;
+
+      for (i = 0; i < Math.max(v1.length, v2.length); i++)
+      {
+        if (v1[i] == v2[i])
+        {
+          continue;
+        }
+
+        v1[i] = parseInt(v1[i]);
+        v2[i] = parseInt(v2[i]);
+
+        if (v1[i] < v2[i])
+        {
+          compare = -1;
+
+          break;
+        }
+        else if(v1[i] > v2[i])
+        {
+          compare = 1;
+
+          break;
+        }
+      }
+
+      return compare;
+    }
+
+  })(jQuery);

-- 
You received this message because you are subscribed to the Google Groups 
"Qubit Toolkit Commits" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/qubit-commits?hl=en.

Reply via email to