*** shindig_integrator/shindig_integrator.install	2009-10-23 14:28:34.000000000 +0200
--- shindig_integrator/shindig_integrator.install	2010-01-27 07:45:03.000000000 +0100
***************
*** 265,270 ****
--- 265,271 ----
        "person_service" => "ShindigIntegratorPeopleService",
        "activity_service" => "ShindigIntegratorActivitiesService",
        "app_data_service" => "ShindigIntegratorAppDataService",
+       "drupal_base_path" => "'.base_path().'",
      );
      $GLOBALS["shindigConfig"] = $shindigConfig;
  
*** shindig_integrator/shindig_files/ShindigIntegratorDbFetcher.php	2010-01-27 08:07:47.000000000 +0100
--- shindig_integrator/shindig_files/ShindigIntegratorDbFetcher.php	2010-01-27 08:07:52.000000000 +0100
***************
*** 8,36 ****
   * This module contains core shindig server
   */
  
- include(Config::get('settings_php'));
- include_once(Config::get('include_path')."/database.inc");
- include_once(Config::get('include_path')."/bootstrap.inc");
- include_once(Config::get('include_path')."/common.inc");
- 
- $url_array = parse_url($db_url);
- $GLOBALS['url_array'] = $url_array;
- include_once(Config::get('include_path')."/database.".$url_array['scheme'].".inc");
- 
- global $active_db;
- $active_db = db_connect($db_url);
- 
  class ShindigIntegratorDbFetcher {
  	private $db;
  	private $url_prefix;
! 	
  	// Singleton
  	private static $fetcher;
  
  	private function __construct()
  	{
! 		global $url_array;
! 		$this->url_prefix = "http://" . $_SERVER['HTTP_HOST'] . $url_array['path']."/";
  	}
  
  	private function __clone()
--- 8,52 ----
   * This module contains core shindig server
   */
  
  class ShindigIntegratorDbFetcher {
  	private $db;
  	private $url_prefix;
!  	private $drupal_dir;
! 
  	// Singleton
  	private static $fetcher;
  
  	private function __construct()
  	{
!     global $base_url;
!     //Initiliaze Drupal
!     if(!defined(DRUPAL_BOOTSTRAP_FULL)) {
!       $base_url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http') . '://'. $_SERVER['HTTP_HOST'];
!       try {
!         $base_path = Config::get('drupal_base_path');
!         if($base_path) {
!           $base_url .= $base_path;
!         }
!       }
!       catch(Exception $exc) {
!         //Ignore config exception for drupal_base_path
!       }
!       $dir = getcwd();
!       //Walk the directory tree to Drupal root
!       chdir(dirname(__FILE__));
!       $this->drupal_dir = getcwd();
!       chdir('..');
!       while(!file_exists('cron.php') && $this->drupal_dir != getcwd()) {
!         $this->drupal_dir = getcwd();
!         chdir('..');
!       }
!       $this->drupal_dir = getcwd();
!       require_once './includes/bootstrap.inc';
!       drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
!       //Return to original dir
!       chdir($dir);
!     }
! 		$this->url_prefix = $base_url;
  	}
  
  	private function __clone()
***************
*** 38,43 ****
--- 54,98 ----
  		// private, don't allow cloning of a singleton
  	}
  
+   /**
+    * @see drupal_alter
+    *
+    * @param <type> $type
+    * @param <type> $data
+    */
+   private function drupalAlter($type, &$data) {
+     $dir = getcwd();
+     chdir($this->drupal_dir);
+ 
+     // Hang onto a reference to the data array so that it isn't blown away later.
+     $args = array(&$data);
+ 
+     // Now, use func_get_args() to pull in any additional parameters passed into
+     // the drupalAlter() call.
+     $additional_args = func_get_args();
+     array_shift($additional_args);
+     array_shift($additional_args);
+     $args = array_merge($args, $additional_args);
+ 
+     foreach (module_implements($type .'_alter') as $module) {
+       $function = $module .'_'. $type .'_alter';
+       call_user_func_array($function, $args);
+     }
+ 
+     chdir($dir);
+   }
+ 
+   /**
+    * @see module_invoke_all
+    */
+   private function drupalInvokeAll() {
+     $dir = getcwd();
+     chdir($this->drupal_dir);
+     $return = call_user_func_array('module_invoke_all', func_get_args());
+     chdir($dir);
+     return $return;
+   }
+ 
  	static function get()
  	{
  		// This object is a singleton
***************
*** 71,83 ****
                      $ret[] = $row['requester_id'] ;
  			}
  		}
  		return $ret;
! 	}    
  
  	/**
! 	* Get User(s) 
  	* Return array of Person Model Objects
- 	* 
  	* @param
  	*   $ids of the userIds
  	* @param
--- 126,139 ----
                      $ret[] = $row['requester_id'] ;
  			}
  		}
+     //Invoke hook_friends_alter() implementations
+     $this->drupalAlter('friends', $ret, $user_id);
  		return $ret;
! 	}
  
  	/**
! 	* Get User(s)
  	* Return array of Person Model Objects
  	* @param
  	*   $ids of the userIds
  	* @param
***************
*** 121,127 ****
  			  }		
  			}
  		}
! 		
  		return $ret;
  	}
  	
--- 177,184 ----
  			  }		
  			}
  		}
!     	//Invoke hook_people_alter() implementations
! 		$this->drupalAlter('people', $ret, $profileDetails, $options);
  		return $ret;
  	}
  	
***************
*** 160,165 ****
--- 217,226 ----
  		$user_id = $user_id;
  		$app_id = $app_id;
  		
+     //Invoke hook_shindig_save_appdata() implementations
+     $modules_results = array_filter($this->drupalInvokeAll('shindig_save_appdata', $userId, $key, $value, $appId));
+     //If no implementation returned a TRUE value, save the appdata ourself
+     if(empty($modules_results)) {
  		if (empty($value)) {
  			if (! db_query("DELETE FROM {application_settings} WHERE application_id = %d AND user_id = %d AND name = '%s'", $appId, $userId, $key)) {
  				return false;
***************
*** 169,174 ****
--- 230,236 ----
  				return false;
  			}
  		}
+     }
  		return true;
  	}
      
***************
*** 213,218 ****
--- 275,282 ----
          $data[$user_id][$key] = $value;
        }
      }
+     //Invoke hook_appdata_alter
+     $this->drupalAlter('appdata', $data, $app_id);
      return $data;
    }
      
***************
*** 230,235 ****
--- 294,304 ----
  	{
  		$userId = $userId;
  		$appId = $appId;
+ 
+     //Invoke hook_shindig_create_activites implementations
+     $modules_results = array_filter($this->drupalInvokeAll('shindig_delete_appdata',$appId, $userId, $key));
+     //If no implementation returned a TRUE value, delete the appdata ourself
+     if(empty($modules_results)) {
  		if($key == "*") {
  		  if(!db_query("DELETE FROM {application_settings} WHERE application_id = %d AND user_id = %d ", $appId, $userId)) {
  			return false;
***************
*** 239,244 ****
--- 308,314 ----
  			return false;
  		  }
  		}
+     }
  		return true;
  
  	}
***************
*** 258,267 ****
  		$title = isset($activity['title']) ? trim($activity['title']) : '';
  		$body = isset($activity['body']) ? trim($activity['body']) : '';
  		$time = time();
! 		db_query("INSERT INTO activities (id, user_id, app_id, title, body, created) VALUES (0, %d, %d, '%s', '%s', $time)", $user_id, $app_id, $title, $body);
! 		if (! ($activityId = db_last_insert_id('activities', 'id'))) {
! 			return false;
! 		}
  		$mediaItems = isset($activity['mediaItems']) ? $activity['mediaItems'] :  array();
  		if (count($mediaItems)) {
  			foreach ($mediaItems as $mediaItem) {
--- 328,347 ----
  		$title = isset($activity['title']) ? trim($activity['title']) : '';
  		$body = isset($activity['body']) ? trim($activity['body']) : '';
  		$time = time();
!     //Invoke hook_shindig_create_activites implementations
!     $modules_results = array_filter($this->drupalInvokeAll('shindig_create_activites', $user_id, $app_id, $title, $body));
!     //The activities has been saved, the first non-FALSE results is the activity ID
!     if(!empty($modules_results)) {
!       $activityId = reset($modules_results);
!     }
!     //If no implementation returned a TRUE value, save the activities ourself
!     else {
!       db_query("INSERT INTO activities (id, user_id, app_id, title, body, created) VALUES (0, %d, %d, '%s', '%s', $time)", $user_id, $app_id, $title, $body);
!       $activityId = db_last_insert_id('activities', 'id');
!     }
!     if (!$activityId) {
!       return false;
!     }
  		$mediaItems = isset($activity['mediaItems']) ? $activity['mediaItems'] :  array();
  		if (count($mediaItems)) {
  			foreach ($mediaItems as $mediaItem) {
***************
*** 271,277 ****
  				$type = trim($type);
  				$mimeType = trim($mimeType);
  				$url = trim($url);
! 				db_query("INSERT INTO activity_media_items (id, activity_id, mime_type, media_type, url) VALUES (0, %d, '%s', '%s', '%s')", $activityId, $mimeType, $type, $url);			
  			}
  		}
  		return true;
--- 351,362 ----
  				$type = trim($type);
  				$mimeType = trim($mimeType);
  				$url = trim($url);
!         //Invoke hook_shindig_create_mediaitem implementations
!         $modules_results = array_filter($this->drupalInvokeAll('shindig_create_mediaitem', $activityId, $mimeType, $type, $url));
!         //If no implementation returned a TRUE value, save the mediaitem ourself
!         if(empty($modules_results)) {
!           db_query("INSERT INTO activity_media_items (id, activity_id, mime_type, media_type, url) VALUES (0, %d, '%s', '%s', '%s')", $activityId, $mimeType, $type, $url);
!         }
  			}
  		}
  		return true;
***************
*** 329,334 ****
--- 414,421 ----
  			$activity->setMediaItems($this->getMediaItems($row['activity_id']));
  			$activities[] = $activity;
  		}
+     	//Invoke hook_activities_alter() implementations
+     	$this->drupalAlter('activities', $activities, $appId);
  		return $activities;
  	}
  
***************
*** 348,353 ****
--- 435,442 ----
  		while ($row = db_fetch_array($res)) {
  			$media[] = new MediaItem($row['mime_type'], strtoupper($row['media_type']), $row['url']);
  		}
+     	//Invoke hook_mediaitems_alter() implementations
+     	$this->drupalAlter('mediaitems', $media, $activity_id);
  		return $media;
  	}
  
