Author: ornicar2
Date: 2010-01-11 01:23:15 +0100 (Mon, 11 Jan 2010)
New Revision: 26466

Added:
   plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/
   plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapi.php
   
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapiException.php
   
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapiReportEntry.php
   plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/lib/
   
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/lib/dmGoogleAnalyticsForm.php
Removed:
   plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/dmGapi.php
   plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/dmGapiReportEntry.php
Modified:
   plugins/diemPlugin/trunk/dmAdminPlugin/config/dm/services.yml
   
plugins/diemPlugin/trunk/dmAdminPlugin/config/dmAdminPluginConfiguration.class.php
   plugins/diemPlugin/trunk/dmAdminPlugin/lib/chart/generic/dmChart.php
   plugins/diemPlugin/trunk/dmAdminPlugin/lib/chart/generic/dmGaChart.php
   
plugins/diemPlugin/trunk/dmAdminPlugin/lib/service/dmAdminBaseServiceContainer.php
   
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/actions/actions.class.php
   
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/templates/indexSuccess.php
   plugins/diemPlugin/trunk/dmCorePlugin/lib/config/dmConfig.php
   plugins/diemPlugin/trunk/dmCorePlugin/lib/task/dmDataTask.class.php
   plugins/diemPlugin/trunk/dmCorePlugin/lib/vendor/gapi/gapi.class.php
Log:
[Diem]
- improved google api service
- no more stores google analytics logins but authentication token
- enhanced admin google analytics module

Modified: plugins/diemPlugin/trunk/dmAdminPlugin/config/dm/services.yml
===================================================================
--- plugins/diemPlugin/trunk/dmAdminPlugin/config/dm/services.yml       
2010-01-11 00:18:00 UTC (rev 26465)
+++ plugins/diemPlugin/trunk/dmAdminPlugin/config/dm/services.yml       
2010-01-11 00:23:15 UTC (rev 26466)
@@ -98,7 +98,7 @@
   gapi:
     class:                    %gapi.class%
     shared:                   true
-    arguments:                [ %gapi.email%, %gapi.password%, 
%gapi.auth_token% ]
+    arguments:                [ @cache_manager ]
 
   link_tag:
     class:                    %link_tag.class%

Modified: 
plugins/diemPlugin/trunk/dmAdminPlugin/config/dmAdminPluginConfiguration.class.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/config/dmAdminPluginConfiguration.class.php
  2010-01-11 00:18:00 UTC (rev 26465)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/config/dmAdminPluginConfiguration.class.php
  2010-01-11 00:23:15 UTC (rev 26466)
@@ -56,5 +56,4 @@
       'sf_csrf_secret' => false,   // csrf is useless because all admin app is 
secured
     ));
   }
-
 }
\ No newline at end of file

Modified: plugins/diemPlugin/trunk/dmAdminPlugin/lib/chart/generic/dmChart.php
===================================================================
--- plugins/diemPlugin/trunk/dmAdminPlugin/lib/chart/generic/dmChart.php        
2010-01-11 00:18:00 UTC (rev 26465)
+++ plugins/diemPlugin/trunk/dmAdminPlugin/lib/chart/generic/dmChart.php        
2010-01-11 00:23:15 UTC (rev 26466)
@@ -110,12 +110,12 @@
   protected function getDefaultOptions()
   {
     return array(
-      'width' => 500,
-      'height' => 300,
-      'name' => get_class($this),
-      'key' => preg_replace('|(\w+)Chart|', '$1', get_class($this)),
+      'width'       => 500,
+      'height'      => 300,
+      'name'        => get_class($this),
+      'key'         => preg_replace('|(\w+)Chart|', '$1', get_class($this)),
       'credentials' => 'see_chart',
-      'lifetime' => 60 * 60 * 24
+      'lifetime'    => 60 * 60 * 24
     );
   }
 

Modified: plugins/diemPlugin/trunk/dmAdminPlugin/lib/chart/generic/dmGaChart.php
===================================================================
--- plugins/diemPlugin/trunk/dmAdminPlugin/lib/chart/generic/dmGaChart.php      
2010-01-11 00:18:00 UTC (rev 26465)
+++ plugins/diemPlugin/trunk/dmAdminPlugin/lib/chart/generic/dmGaChart.php      
2010-01-11 00:23:15 UTC (rev 26466)
@@ -9,9 +9,9 @@
   {
     try
     {
-      $this->gapi = $this->serviceContainer->getGapi();
+      $this->gapi = 
$this->serviceContainer->getService('gapi')->authenticate(null, null, 
dmConfig::get('ga_token'));
     }
-    catch(Exception $e)
+    catch(dmGapiException $e)
     {
       $this->available = false;
       
@@ -20,6 +20,7 @@
         throw $e;
       }
     }
+    
     parent::setup();
   }
   

Modified: 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/service/dmAdminBaseServiceContainer.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/service/dmAdminBaseServiceContainer.php
  2010-01-11 00:18:00 UTC (rev 26465)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/service/dmAdminBaseServiceContainer.php
  2010-01-11 00:23:15 UTC (rev 26466)
@@ -12,20 +12,7 @@
   protected function connectServices()
   {
     parent::connectServices();
-    
+
     $this->getService('bread_crumb')->connect();
   }
-  
-  
-  public function getGapi()
-  {
-    $user = $this->getService('user');
-    $this->setParameter('gapi.email', dmConfig::get('ga_email'));
-    $this->setParameter('gapi.password', dmConfig::get('ga_password'));
-    $this->setParameter('gapi.auth_token', 
$user->getAttribute('gapi_auth_token'));
-    $gapi = $this->getGapiService();
-    $gapi->setCacheManager($this->getCacheManagerService());
-    $user->setAttribute('gapi_auth_token', $gapi->getAuthToken());
-    return $gapi;
-  }
 }
\ No newline at end of file

Deleted: plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/dmGapi.php
===================================================================
--- plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/dmGapi.php    
2010-01-11 00:18:00 UTC (rev 26465)
+++ plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/dmGapi.php    
2010-01-11 00:23:15 UTC (rev 26466)
@@ -1,290 +0,0 @@
-<?php
-
-require_once(dmOs::join(sfConfig::get('dm_core_dir'), 
'lib/vendor/gapi/gapi.class.php'));
-
-class dmGapi extends gapi
-{
-  protected
-  $cacheManager,
-  $reportId,
-  $defaultReportOptions;
-  
-  /**
-   * Constructor function for all new gapi instances
-   * 
-   * Set up authenticate with Google and get auth_token
-   *
-   * @param String $email
-   * @param String $password
-   * @param String $token
-   * @return gapi
-   */
-  public function __construct($email, $password, $token=null)
-  {
-    if (!($email && $password) && !$token)
-    {
-      throw new dmException('No google analytics account configured');
-    }
-    
-    parent::__construct($email, $password, $token);
-  }
-  
-  public function getTotalPageViews()
-  {
-    $report = $this->getReport(array(
-      'dimensions'  => array('year'),
-      'metrics'     => array('pageviews')
-    ));
-    
-    $pageviews = 0;
-    foreach($report as $entry)
-    {
-      $pageviews += $entry->get('pageviews');
-    }
-    
-    unset($report);
-    
-    return $pageviews;
-  }
-  
-  public function getReport(array $options)
-  {
-    $options = array_merge($this->getDefaultReportOptions(), $options);
-    
-    return $this->requestReportData(
-    $this->getReportId(),
-    $options['dimensions'],
-    $options['metrics'],
-    $options['sort_metric'],
-    $options['filter'],
-    $options['start_date'],
-    $options['end_date'],
-    $options['start_index'],
-    $options['max_results']
-    );
-  }
-  
-  public function getDefaultReportOptions()
-  {
-    if (null === $this->defaultReportOptions)
-    {
-      $this->defaultReportOptions = array(
-        'dimensions'  => array(),
-        'metrics'     => array(),
-        'sort_metric' => null,
-        'filter'      => null,
-        'start_date'  => date('Y-m-d',strtotime('11 months ago')),
-        'end_date'    => null,
-        'start_index' => 1,
-        'max_results' => 30
-      );
-    }
-    
-    return $this->defaultReportOptions;
-  }
-  
-  public function getReportId()
-  {
-    if ($this->reportId)
-    {
-      return $this->reportId;
-    }
-    
-    if (!$gaKey = dmConfig::get('ga_key'))
-    {
-      throw new dmException('You must configure a ga_key in the configuration 
panel');
-    }
-    
-    foreach($this->requestAccountData() as $account)
-    {
-      if ($account->getWebPropertyId() === $gaKey)
-      {
-        return $this->reportId = $account->getProfileId();
-      }
-    }
-    
-    throw new dmException('Current report not found for ga key : '.$gaKey);
-  }
-  
-  public function setCacheManager(dmCacheManager $cacheManager)
-  {
-    $this->cacheManager = $cacheManager;
-  }
-  
-  /**
-   * Request account data from Google Analytics
-   *
-   * @param Int $start_index OPTIONAL: Start index of results
-   * @param Int $max_results OPTIONAL: Max results returned
-   */
-  public function requestAccountData($start_index=1, $max_results=20)
-  {
-    if ($this->cacheManager)
-    {
-      $cacheKey = 'account-data-'.$start_index.'-to-'.$max_results;
-      
-      if ($this->cacheManager->getCache('gapi/request')->has($cacheKey))
-      {
-        return $this->cacheManager->getCache('gapi/request')->get($cacheKey);
-      }
-    }
-    
-    $result = parent::requestAccountData($start_index, $max_results);
-
-    if ($this->cacheManager)
-    {
-      $this->cacheManager->getCache('gapi/request')->set($cacheKey, $result);
-    }
-    
-    return $result;
-  }
-  
-  /**
-   * Request report data from Google Analytics
-   *
-   * $report_id is the Google report ID for the selected account
-   * 
-   * $parameters should be in key => value format
-   * 
-   * @param String $report_id
-   * @param Array $dimensions Google Analytics dimensions e.g. array('browser')
-   * @param Array $metrics Google Analytics metrics e.g. array('pageviews')
-   * @param Array $sort_metric OPTIONAL: Dimension or dimensions to sort by 
e.g.('-visits')
-   * @param String $filter OPTIONAL: Filter logic for filtering results
-   * @param String $start_date OPTIONAL: Start of reporting period
-   * @param String $end_date OPTIONAL: End of reporting period
-   * @param Int $start_index OPTIONAL: Start index of results
-   * @param Int $max_results OPTIONAL: Max results returned
-   */
-  public function requestReportData($report_id, $dimensions, $metrics, 
$sort_metric=null, $filter=null, $start_date=null, $end_date=null, 
$start_index=1, $max_results=30)
-  {
-    if ($this->cacheManager)
-    {
-      $cacheKey = 'report-data-'.md5(serialize(func_get_args()));
-      
-      if ($this->cacheManager->getCache('gapi/request')->has($cacheKey))
-      {
-        return $this->cacheManager->getCache('gapi/request')->get($cacheKey);
-      }
-    }
-    
-    $result = parent::requestReportData($report_id, $dimensions, $metrics, 
$sort_metric, $filter, $start_date, $end_date, $start_index, $max_results);
-
-    if ($this->cacheManager)
-    {
-      $this->cacheManager->getCache('gapi/request')->set($cacheKey, $result);
-    }
-    
-    return $result;
-  }
-  
-  /**
-   * Authenticate Google Account with Google
-   *
-   * @param String $email
-   * @param String $password
-   */
-  protected function authenticateUser($email, $password)
-  {
-    try
-    {
-      return parent::authenticateUser($email, $password);
-    }
-    catch(Exception $e)
-    {
-      throw new dmException('GAPI: Failed to authenticate with email 
'.$email.'. Please configure email and password in the admin configuration 
panel');
-    }
-  }
-  
-  /**
-   * Report Object Mapper to convert the XML to array of useful PHP objects
-   *
-   * @param String $xml_string
-   * @return Array of gapiReportEntry objects
-   */
-  protected function reportObjectMapper($xml_string)
-  {
-    $xml = simplexml_load_string($xml_string);
-    
-    $this->results = null;
-    $results = array();
-    
-    $report_root_parameters = array();
-    $report_aggregate_metrics = array();
-    
-    //Load root parameters
-    
-    $report_root_parameters['updated'] = strval($xml->updated);
-    $report_root_parameters['generator'] = strval($xml->generator);
-    $report_root_parameters['generatorVersion'] = 
strval($xml->generator->attributes());
-    
-    $open_search_results = 
$xml->children('http://a9.com/-/spec/opensearchrss/1.0/');
-    
-    foreach($open_search_results as $key => $open_search_result)
-    {
-      $report_root_parameters[$key] = intval($open_search_result);
-    }
-    
-    $google_results = 
$xml->children('http://schemas.google.com/analytics/2009');
-
-    foreach($google_results->dataSource->property as $property_attributes)
-    {
-      
$report_root_parameters[str_replace('ga:','',$property_attributes->attributes()->name)]
 = strval($property_attributes->attributes()->value);
-    }
-    
-    $report_root_parameters['startDate'] = strval($google_results->startDate);
-    $report_root_parameters['endDate'] = strval($google_results->endDate);
-    
-    //Load result aggregate metrics
-    
-    foreach($google_results->aggregates->metric as $aggregate_metric)
-    {
-      $metric_value = strval($aggregate_metric->attributes()->value);
-      
-      //Check for float, or value with scientific notation
-      if(preg_match('/^(\d+\.\d+)|(\d+E\d+)|(\d+.\d+E\d+)$/',$metric_value))
-      {
-        
$report_aggregate_metrics[str_replace('ga:','',$aggregate_metric->attributes()->name)]
 = floatval($metric_value);
-      }
-      else
-      {
-        
$report_aggregate_metrics[str_replace('ga:','',$aggregate_metric->attributes()->name)]
 = intval($metric_value);
-      }
-    }
-    
-    //Load result entries
-    
-    foreach($xml->entry as $entry)
-    {
-      $metrics = array();
-      
foreach($entry->children('http://schemas.google.com/analytics/2009')->metric as 
$metric)
-      {
-        $metric_value = strval($metric->attributes()->value);
-        
-        //Check for float, or value with scientific notation
-        if(preg_match('/^(\d+\.\d+)|(\d+E\d+)|(\d+.\d+E\d+)$/',$metric_value))
-        {
-          $metrics[str_replace('ga:','',$metric->attributes()->name)] = 
floatval($metric_value);
-        }
-        else
-        {
-          $metrics[str_replace('ga:','',$metric->attributes()->name)] = 
intval($metric_value);
-        }
-      }
-      
-      $dimensions = array();
-      
foreach($entry->children('http://schemas.google.com/analytics/2009')->dimension 
as $dimension)
-      {
-        $dimensions[str_replace('ga:','',$dimension->attributes()->name)] = 
strval($dimension->attributes()->value);
-      }
-      
-      $results[] = new dmGapiReportEntry($metrics,$dimensions);
-    }
-    
-    $this->report_root_parameters = $report_root_parameters;
-    $this->report_aggregate_metrics = $report_aggregate_metrics;
-    $this->results = $results;
-    
-    return $results;
-  }
-}
\ No newline at end of file

Deleted: 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/dmGapiReportEntry.php
===================================================================
--- plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/dmGapiReportEntry.php 
2010-01-11 00:18:00 UTC (rev 26465)
+++ plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/dmGapiReportEntry.php 
2010-01-11 00:23:15 UTC (rev 26466)
@@ -1,30 +0,0 @@
-<?php
-
-class dmGapiReportEntry extends gapiReportEntry
-{
-  
-  public function getMetric($name)
-  {
-    return $this->metrics[$name];
-  }
-  
-  public function getDimension($name)
-  {
-    return $this->dimensions[$name];
-  }
-  
-  public function get($name)
-  {
-    if (isset($this->metrics[$name]))
-    {
-      return $this->metrics[$name];
-    }
-    elseif (isset($this->dimensions[$name]))
-    {
-      return $this->dimensions[$name];
-    }
-
-    throw new Exception('No valid metric or dimension called "' . $name . '"');
-  }
-  
-}
\ No newline at end of file

Copied: plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapi.php 
(from rev 26351, 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/dmGapi.php)
===================================================================
--- plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapi.php       
                        (rev 0)
+++ plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapi.php       
2010-01-11 00:23:15 UTC (rev 26466)
@@ -0,0 +1,315 @@
+<?php
+
+require_once(dmOs::join(sfConfig::get('dm_core_dir'), 
'lib/vendor/gapi/gapi.class.php'));
+
+class dmGapi extends gapi
+{
+  protected
+  $cacheManager,
+  $reportId,
+  $defaultReportOptions;
+
+  public function __construct(dmCacheManager $cacheManager)
+  {
+    $this->cacheManager = $cacheManager;
+
+    $this->cacheManager = false;
+  }
+
+  /**
+   * Set up authenticate with Google and get auth_token
+   *
+   * @param String $email
+   * @param String $password
+   * @param String $token
+   * @return gapi
+   */
+  public function authenticate($email, $password, $token = null)
+  {
+    if (!($email && $password) && !$token)
+    {
+      throw new dmGapiException('No google analytics account configured');
+    }
+
+    $this->reportId = null;
+
+    if($token !== null)
+    {
+      $this->auth_token = $token;
+    }
+    else
+    {
+      $this->authenticateUser($email, $password);
+    }
+
+    $this->getReportId();
+
+    return $this;
+  }
+  
+  public function getTotalPageViews()
+  {
+    $report = $this->getReport(array(
+      'dimensions'  => array('year'),
+      'metrics'     => array('pageviews')
+    ));
+    
+    $pageviews = 0;
+    foreach($report as $entry)
+    {
+      $pageviews += $entry->get('pageviews');
+    }
+    
+    unset($report);
+    
+    return $pageviews;
+  }
+  
+  public function getReport(array $options)
+  {
+    $options = array_merge($this->getDefaultReportOptions(), $options);
+    
+    return $this->requestReportData(
+    $this->getReportId(),
+    $options['dimensions'],
+    $options['metrics'],
+    $options['sort_metric'],
+    $options['filter'],
+    $options['start_date'],
+    $options['end_date'],
+    $options['start_index'],
+    $options['max_results']
+    );
+  }
+  
+  public function getDefaultReportOptions()
+  {
+    if (null === $this->defaultReportOptions)
+    {
+      $this->defaultReportOptions = array(
+        'dimensions'  => array(),
+        'metrics'     => array(),
+        'sort_metric' => null,
+        'filter'      => null,
+        'start_date'  => date('Y-m-d',strtotime('11 months ago')),
+        'end_date'    => null,
+        'start_index' => 1,
+        'max_results' => 30
+      );
+    }
+    
+    return $this->defaultReportOptions;
+  }
+  
+  public function getReportId()
+  {
+    if ($this->reportId)
+    {
+      return $this->reportId;
+    }
+    
+    if (!$gaKey = dmConfig::get('ga_key'))
+    {
+      throw new dmGapiException('You must configure a ga_key in the 
configuration panel');
+    }
+    
+    foreach($this->requestAccountData() as $account)
+    {
+      if ($account->getWebPropertyId() === $gaKey)
+      {
+        return $this->reportId = $account->getProfileId();
+      }
+    }
+    
+    throw new dmGapiException('Current report not found for ga key : '.$gaKey);
+  }
+  
+  public function setCacheManager(dmCacheManager $cacheManager)
+  {
+    $this->cacheManager = $cacheManager;
+  }
+  
+  /**
+   * Request account data from Google Analytics
+   *
+   * @param Int $start_index OPTIONAL: Start index of results
+   * @param Int $max_results OPTIONAL: Max results returned
+   */
+  public function requestAccountData($start_index=1, $max_results=20)
+  {
+    if ($this->cacheManager)
+    {
+      $cacheKey = 'account-data-'.$start_index.'-to-'.$max_results;
+      
+      if ($this->cacheManager->getCache('gapi/request')->has($cacheKey))
+      {
+        return $this->cacheManager->getCache('gapi/request')->get($cacheKey);
+      }
+    }
+
+    try
+    {
+      $result = parent::requestAccountData($start_index, $max_results);
+    }
+    catch(Exception $e)
+    {
+      throw new dmGapiException($e->getMessage());
+    }
+    
+    if ($this->cacheManager)
+    {
+      $this->cacheManager->getCache('gapi/request')->set($cacheKey, $result);
+    }
+    
+    return $result;
+  }
+  
+  /**
+   * Request report data from Google Analytics
+   *
+   * $report_id is the Google report ID for the selected account
+   * 
+   * $parameters should be in key => value format
+   * 
+   * @param String $report_id
+   * @param Array $dimensions Google Analytics dimensions e.g. array('browser')
+   * @param Array $metrics Google Analytics metrics e.g. array('pageviews')
+   * @param Array $sort_metric OPTIONAL: Dimension or dimensions to sort by 
e.g.('-visits')
+   * @param String $filter OPTIONAL: Filter logic for filtering results
+   * @param String $start_date OPTIONAL: Start of reporting period
+   * @param String $end_date OPTIONAL: End of reporting period
+   * @param Int $start_index OPTIONAL: Start index of results
+   * @param Int $max_results OPTIONAL: Max results returned
+   */
+  public function requestReportData($report_id, $dimensions, $metrics, 
$sort_metric=null, $filter=null, $start_date=null, $end_date=null, 
$start_index=1, $max_results=30)
+  {
+    if ($this->cacheManager)
+    {
+      $cacheKey = 'report-data-'.md5(serialize(func_get_args()));
+      
+      if ($this->cacheManager->getCache('gapi/request')->has($cacheKey))
+      {
+        return $this->cacheManager->getCache('gapi/request')->get($cacheKey);
+      }
+    }
+    
+    $result = parent::requestReportData($report_id, $dimensions, $metrics, 
$sort_metric, $filter, $start_date, $end_date, $start_index, $max_results);
+
+    if ($this->cacheManager)
+    {
+      $this->cacheManager->getCache('gapi/request')->set($cacheKey, $result);
+    }
+    
+    return $result;
+  }
+  
+  /**
+   * Authenticate Google Account with Google
+   *
+   * @param String $email
+   * @param String $password
+   */
+  protected function authenticateUser($email, $password)
+  {
+    try
+    {
+      return parent::authenticateUser($email, $password);
+    }
+    catch(Exception $e)
+    {
+      throw new dmGapiException('GAPI: Failed to authenticate with email 
'.$email.'. Please configure email and password in the admin configuration 
panel');
+    }
+  }
+  
+  /**
+   * Report Object Mapper to convert the XML to array of useful PHP objects
+   *
+   * @param String $xml_string
+   * @return Array of gapiReportEntry objects
+   */
+  protected function reportObjectMapper($xml_string)
+  {
+    $xml = simplexml_load_string($xml_string);
+    
+    $this->results = null;
+    $results = array();
+    
+    $report_root_parameters = array();
+    $report_aggregate_metrics = array();
+    
+    //Load root parameters
+    
+    $report_root_parameters['updated'] = strval($xml->updated);
+    $report_root_parameters['generator'] = strval($xml->generator);
+    $report_root_parameters['generatorVersion'] = 
strval($xml->generator->attributes());
+    
+    $open_search_results = 
$xml->children('http://a9.com/-/spec/opensearchrss/1.0/');
+    
+    foreach($open_search_results as $key => $open_search_result)
+    {
+      $report_root_parameters[$key] = intval($open_search_result);
+    }
+    
+    $google_results = 
$xml->children('http://schemas.google.com/analytics/2009');
+
+    foreach($google_results->dataSource->property as $property_attributes)
+    {
+      
$report_root_parameters[str_replace('ga:','',$property_attributes->attributes()->name)]
 = strval($property_attributes->attributes()->value);
+    }
+    
+    $report_root_parameters['startDate'] = strval($google_results->startDate);
+    $report_root_parameters['endDate'] = strval($google_results->endDate);
+    
+    //Load result aggregate metrics
+    
+    foreach($google_results->aggregates->metric as $aggregate_metric)
+    {
+      $metric_value = strval($aggregate_metric->attributes()->value);
+      
+      //Check for float, or value with scientific notation
+      if(preg_match('/^(\d+\.\d+)|(\d+E\d+)|(\d+.\d+E\d+)$/',$metric_value))
+      {
+        
$report_aggregate_metrics[str_replace('ga:','',$aggregate_metric->attributes()->name)]
 = floatval($metric_value);
+      }
+      else
+      {
+        
$report_aggregate_metrics[str_replace('ga:','',$aggregate_metric->attributes()->name)]
 = intval($metric_value);
+      }
+    }
+    
+    //Load result entries
+    
+    foreach($xml->entry as $entry)
+    {
+      $metrics = array();
+      
foreach($entry->children('http://schemas.google.com/analytics/2009')->metric as 
$metric)
+      {
+        $metric_value = strval($metric->attributes()->value);
+        
+        //Check for float, or value with scientific notation
+        if(preg_match('/^(\d+\.\d+)|(\d+E\d+)|(\d+.\d+E\d+)$/',$metric_value))
+        {
+          $metrics[str_replace('ga:','',$metric->attributes()->name)] = 
floatval($metric_value);
+        }
+        else
+        {
+          $metrics[str_replace('ga:','',$metric->attributes()->name)] = 
intval($metric_value);
+        }
+      }
+      
+      $dimensions = array();
+      
foreach($entry->children('http://schemas.google.com/analytics/2009')->dimension 
as $dimension)
+      {
+        $dimensions[str_replace('ga:','',$dimension->attributes()->name)] = 
strval($dimension->attributes()->value);
+      }
+      
+      $results[] = new dmGapiReportEntry($metrics,$dimensions);
+    }
+    
+    $this->report_root_parameters = $report_root_parameters;
+    $this->report_aggregate_metrics = $report_aggregate_metrics;
+    $this->results = $results;
+    
+    return $results;
+  }
+}
\ No newline at end of file

Added: 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapiException.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapiException.php  
                            (rev 0)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapiException.php  
    2010-01-11 00:23:15 UTC (rev 26466)
@@ -0,0 +1,6 @@
+<?php
+
+class dmGapiException extends dmException
+{
+  
+}
\ No newline at end of file

Copied: 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapiReportEntry.php
 (from rev 26351, 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/dmGapiReportEntry.php)
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapiReportEntry.php
                            (rev 0)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/lib/webService/gapi/dmGapiReportEntry.php
    2010-01-11 00:23:15 UTC (rev 26466)
@@ -0,0 +1,30 @@
+<?php
+
+class dmGapiReportEntry extends gapiReportEntry
+{
+  
+  public function getMetric($name)
+  {
+    return $this->metrics[$name];
+  }
+  
+  public function getDimension($name)
+  {
+    return $this->dimensions[$name];
+  }
+  
+  public function get($name)
+  {
+    if (isset($this->metrics[$name]))
+    {
+      return $this->metrics[$name];
+    }
+    elseif (isset($this->dimensions[$name]))
+    {
+      return $this->dimensions[$name];
+    }
+
+    throw new Exception('No valid metric or dimension called "' . $name . '"');
+  }
+  
+}
\ No newline at end of file

Modified: 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/actions/actions.class.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/actions/actions.class.php
  2010-01-11 00:18:00 UTC (rev 26465)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/actions/actions.class.php
  2010-01-11 00:23:15 UTC (rev 26466)
@@ -5,28 +5,31 @@
   
   public function executeIndex(dmWebRequest $request)
   {
-    $settings = dmDb::query('DmSetting s')
-    ->whereIn('s.name', array('ga_key', 'ga_email', 'ga_password'))
-    ->fetchRecords()->getData();
-    
-    foreach($settings as $index => $setting)
+    if($this->getUser()->can('google_analytics'))
     {
-      if (!$this->getUser()->can($setting->get('credentials')))
+      $this->form = new dmGoogleAnalyticsForm();
+      $this->form->setGapi($this->getService('gapi'));
+      
+      if ($request->isMethod('post') && $this->form->bindAndValid($request))
       {
-        unset($settings[$index]);
+        $this->form->save();
+        return $this->redirect('@dm_google_analytics');
       }
     }
+
+    $this->gapiConnected = false;
     
-    if(!empty($settings))
+    if(dmConfig::get('ga_token'))
     {
-      $this->form = new dmConfigForm;
-      $this->form->addSettings($settings);
-      
-      if ($request->isMethod('post') && $this->form->bindAndValid($request))
+      try
       {
-        $this->form->save();
-        return $this->redirect('@dm_google_analytics');
+        $this->getService('gapi')->authenticate(null, null, 
dmConfig::get('ga_token'));
+        $this->gapiConnected = true;
       }
+      catch(dmGapiException $e)
+      {
+        // bad token
+      }
     }
   }
   

Added: 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/lib/dmGoogleAnalyticsForm.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/lib/dmGoogleAnalyticsForm.php
                              (rev 0)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/lib/dmGoogleAnalyticsForm.php
      2010-01-11 00:23:15 UTC (rev 26466)
@@ -0,0 +1,64 @@
+<?php
+
+class dmGoogleAnalyticsForm extends dmForm
+{
+  protected
+  $gapi;
+
+  public function setGapi(dmGapi $gapi)
+  {
+    $this->gapi = $gapi;
+  }
+
+  public function configure()
+  {
+    $this->widgetSchema['key'] = new sfWidgetFormInputText();
+    $this->validatorSchema['key'] = new sfValidatorString(array('required' => 
false));
+    $this->widgetSchema->setHelp('key', 
dmDb::table('DmSetting')->findOneByName('ga_key')->description);
+    $this->setDefault('key', dmConfig::get('ga_key'));
+    
+    $this->widgetSchema['email'] = new sfWidgetFormInputText();
+    $this->validatorSchema['email'] = new sfValidatorEmail(array('required' => 
false));
+    $this->widgetSchema->setHelp('email', 'Required to display google 
analytics data into Diem');
+
+    $this->widgetSchema['password'] = new sfWidgetFormInputPassword();
+    $this->validatorSchema['password'] = new 
sfValidatorString(array('required' => false));
+    $this->widgetSchema->setHelp('password', 'Required to display google 
analytics data into Diem');
+
+    $this->mergePostValidator(new sfValidatorCallback(array('callback' => 
array($this, 'tokenize'))));
+  }
+
+  public function tokenize($validator, $values)
+  {
+    $values['token'] = null;
+    
+    if($values['email'] || $values['password'])
+    {
+      try
+      {
+        $this->gapi->authenticate($values['email'], $values['password']);
+
+        // save token
+        $values['token'] = $this->gapi->getAuthToken();
+      }
+      catch(dmGapiException $e)
+      {
+        // probably bad email/password
+        // throw an error bound to the password field
+        throw new sfValidatorErrorSchema($validator, array('email' => new 
sfValidatorError($validator, 'Bad email or password')));
+      }
+    }
+    
+    return $values;
+  }
+
+  public function save()
+  {
+    dmConfig::set('ga_key', $this->getValue('key'));
+    
+    if($this->getValue('token'))
+    {
+      dmConfig::set('ga_token', $this->getValue('token'));
+    }
+  }
+}
\ No newline at end of file

Modified: 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/templates/indexSuccess.php
===================================================================
--- 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/templates/indexSuccess.php
 2010-01-11 00:18:00 UTC (rev 26465)
+++ 
plugins/diemPlugin/trunk/dmAdminPlugin/modules/dmGoogleAnalytics/templates/indexSuccess.php
 2010-01-11 00:23:15 UTC (rev 26466)
@@ -18,11 +18,16 @@
     echo £('div.dm_box_inner',
       $form->open('.dm_form.list.little').
       £('li', £('h3', __('Send reports'))).
-      $form['ga_key']->renderRow().
+      $form['key']->renderRow().
       £('li.separator', '&nbsp;').
       £('li', £('h3', __('Receive reports'))).
-      $form['ga_email']->renderRow().
-      $form['ga_password']->renderRow().
+      £('div.mb10 style="text-align: center"',
+        ($gapiConnected
+        ? £('span.s16.s16_tick', __('Connected'))
+        : £('span.s16.s16_cross', __('Not connected')))
+      ).
+      $form['email']->renderRow().
+      $form['password']->renderRow().
       $form->renderSubmitTag(__('Save')).
       $form->close()
     );

Modified: plugins/diemPlugin/trunk/dmCorePlugin/lib/config/dmConfig.php
===================================================================
--- plugins/diemPlugin/trunk/dmCorePlugin/lib/config/dmConfig.php       
2010-01-11 00:18:00 UTC (rev 26465)
+++ plugins/diemPlugin/trunk/dmCorePlugin/lib/config/dmConfig.php       
2010-01-11 00:23:15 UTC (rev 26466)
@@ -73,13 +73,16 @@
     $setting->set('value', $value);
 
     $setting->save();
+
+    self::$config[$name] = $value;
     
     self::$dispatcher->notify(new sfEvent(null, 'dm.config.updated', array(
-      'setting'  => $setting,
-      'culture' => self::$culture
+      'setting'   => $setting,
+      'culture'   => self::$culture
     )));
 
-    return self::$config[$name] = $value;
+    // reassign setting value as it may have changed
+    return self::$config[$name] = $setting->get('value');
   }
 
   /**

Modified: plugins/diemPlugin/trunk/dmCorePlugin/lib/task/dmDataTask.class.php
===================================================================
--- plugins/diemPlugin/trunk/dmCorePlugin/lib/task/dmDataTask.class.php 
2010-01-11 00:18:00 UTC (rev 26465)
+++ plugins/diemPlugin/trunk/dmCorePlugin/lib/task/dmDataTask.class.php 
2010-01-11 00:23:15 UTC (rev 26466)
@@ -79,17 +79,12 @@
       ),
       'ga_key' => array(
         'description' => 'The google analytics key without javascript stuff ( 
e.g. UA-9876614-1 )',
-        'group_name' =>'tracking',
-        'credentials' => 'google_analytics'
-      ),
-      'ga_email' => array(
-        'description' => 'Required to display google analytics data into Diem',
         'group_name' => 'tracking',
         'credentials' => 'google_analytics'
       ),
-      'ga_password' => array(
-        'description' => 'Required to display google analytics data into Diem',
-        'group_name' => 'tracking',
+      'ga_token' => array(
+        'description' => 'Auth token gor Google Analytics, computed from 
password',
+        'group_name' => 'internal',
         'credentials' => 'google_analytics'
       ),
       'gwt_key' => array(
@@ -185,7 +180,7 @@
       if (!isset($existingSettings[$name]))
       {
         $setting = new DmSetting;
-        $setting->name = $name;
+        $setting->set('name', $name);
         $setting->fromArray($config);
 
         $setting->save();

Modified: plugins/diemPlugin/trunk/dmCorePlugin/lib/vendor/gapi/gapi.class.php
===================================================================
--- plugins/diemPlugin/trunk/dmCorePlugin/lib/vendor/gapi/gapi.class.php        
2010-01-11 00:18:00 UTC (rev 26465)
+++ plugins/diemPlugin/trunk/dmCorePlugin/lib/vendor/gapi/gapi.class.php        
2010-01-11 00:23:15 UTC (rev 26466)
@@ -34,7 +34,7 @@
   const interface_name = 'GAPI-1.3';
   const dev_mode = false;
   
-  private $auth_token = null;
+  protected $auth_token = null;
   private $account_entries = array();
   private $account_root_parameters = array();
   private $report_aggregate_metrics = array();

-- 
You received this message because you are subscribed to the Google Groups 
"symfony SVN" 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/symfony-svn?hl=en.


Reply via email to