? modules/sms_legacy
Index: sms.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/smsframework/sms.module,v
retrieving revision 1.8.2.9.2.1
diff -u -p -r1.8.2.9.2.1 sms.module
--- sms.module	10 Mar 2009 19:13:28 -0000	1.8.2.9.2.1
+++ sms.module	6 May 2009 10:19:52 -0000
@@ -10,48 +10,141 @@
 /**
  * Sends a message using the active gateway.
  * 
- * @param $number
- *   The destination number.
  * 
- * @param $message
- *   The text of the messsage to send.
- * 
- * @param $options
- *   An array of dditional properties as defined by gateway modules.
- */
-function sms_send($number, $message, $options = array()) {
-  $gateway = sms_default_gateway();
+ * @param $sms
+ *   a sms message object, containing at least $sms->type
+ *   - for legacy this can also be the former variable $number, an is recognized so, if $a1 is set to string
+ * @param $a1
+ *   a optional variable to provide legacy behaviour, this will be recognized as $sms->message, so message text
+ *   do NOT! set in none-legacy cases
+ * @param $a2
+ *   for legacy reasons: an array of dditional properties as defined by gateway modules.
+ */
+function sms_send(&$sms, $a1 = NULL, $a2 = NULL) {
+  $legacy = FALSE;
+  if (isset($a1)) {
+  	$number = $sms;
+    $sms = new stdClass();
+    $sms->recipient = $number;
+    $sms->text = $a2;
+    $sms->gateway = array('options' => $a2);
+    $sms->type = 'sms_legacy';
+    $legacy = TRUE;
+  }
+  //ensure $sms->direction is set to 'out' for sending messages
+  $sms->direction = 'out';
+  
+  //add the default gateway
+  //TODO: maybe gateway handling shall be rewritten too, and default gateway implement during 'pre send' if none present?
+  $sms->gateway = (object)sms_default_gateway();
+  
+  //PRE SEND
+  _sms_invokebyref('pre send',$sms,'sms');
   
+  //SEND
+  /** MOVED to sms_legacy.module
   foreach (module_implements('sms_send') as $module) {
     $function = $module .'_sms_send';
     $function($number, $message, $options, $gateway);
   }
+  */
+  _sms_invokebyref('send',$sms,'sms');
+  
+  //finally SEND through gateway
+  //provide legacy send method for marked $gateways
+  if (isset($sms->gateway->legacy) && $sms->gateway->legacy) {
+  	$gateway = (array)$sms->gateway;
+    $options = $gateway->options;
+    $number = $sms->recipient;
+    $sms = $sms->text;
+    if (function_exists($gateway['send'])) {
+      $response = $gateway['send']($number, $sms, $options);
+      $sms->response = $response;//response has to fetched in legacy
+    }
+  }
+  else {
+  	if (isset($sms->gateway->send) && $sms->gateway->send) {
+  		$function = $sms->gateway->send;
+      $function($sms);//response will be set by non-legacy within the function
+  	}
+  }
+  //POST SEND
+  _sms_invokebyref('post send',$sms,'sms');
+  
+  //sms_handle_result moved to sms_sms('post send'):
+  //legacy messages would expect a boolean value
+  // else none
+  if ($legacy) {
+    return ($sms->response['status'] == TRUE);  
+  }
+  else {
+  	//TODO: necessary??
+  }
+}
+
+/**
+ * Implementation of hook_sms()
+ */
+function sms_sms($op,&$sms) {
+	if ($op = 'post send') {
+    $response = $sms->respone;
+    $number = $sms->recipient;
+		return sms_handle_result($response, $number, $sms);
+	}
+}
 
-  if (function_exists($gateway['send'])) {
-    $response = $gateway['send']($number, $message, $options);
+/**
+ * Helper function to invoke $sms object by ref through all ops
+ * 
+ * @param $op
+ *   the operator for hook_sms
+ * @param $sms
+ *   the sms object
+ */
+function _sms_invokebyref($op,&$sms,$hook = 'sms') {
+  foreach (module_implements($hook) as $module) {
+    $function = $module."_$hook";
+    $function($op,$sms);	
   }
-  return sms_handle_result($response, $number, $message);
 }
 
+
 /**
  * Callback for incoming messages. Allows gateways modules to pass messages in
  * a standard format for processing.
  * 
- * @param $number
- *   The sender's mobile number.
- * 
- * @param $message
- *   The content of the text message.
- */
-function sms_incoming($number, $message, $options = array()) {
+ * @param $sms
+ *   the message object
+ *   - for legacy cases this could be the sender's mobile number
+ * @param $a1
+ *   only use in legacy case: The content of the text message
+ * @param $a2
+ *   only used in legacy case: an $options array
+ */
+function sms_incoming($sms, $a1 = NULL, $a2 = array()) {
+  
+  if (isset($a1)) {
+    $number = $sms;
+    $sms = new stdClass();
+    if (is_array($a2)) $sms = (object)$a2;
+    $sms->sender = $number;
+    $sms->text = $a1;
+  }
+  $sms->direction = 'in'; //ensure correct direction
+  //TODO: rethink rules implementation with additional ops
   if (module_exists('rules')) {
-    rules_invoke_event('sms_incoming', $number, $message);
+    rules_invoke_event('sms_incoming', $sms->sender, $sms->text);
   }
 
   // Execute three phases
+  /** moved to sms_legacy.module
   module_invoke_all('sms_incoming', 'pre process', $number, $message, $options);
   module_invoke_all('sms_incoming', 'process', $number, $message, $options);
   module_invoke_all('sms_incoming', 'post process', $number, $message, $options);
+  */
+  _sms_invokebyref('pre receive',$sms,'sms');
+  _sms_invokebyref('receive',$sms,'sms');
+  _sms_invokebyref('post receive',$sms,'sms');
 }
 
 /**
@@ -63,6 +156,8 @@ function sms_default_gateway() {
 
 /**
  * Implementation of hook_gateway_info().
+ * 
+ * Provides a default gateway for testing cases
  */
 function sms_gateway_info() {
   return array(
@@ -73,7 +168,16 @@ function sms_gateway_info() {
   );
 }
 
-function sms_send_log($number, $message, $options) {
+/**
+ * Send function for Log-Gateway.
+ * 
+ * simply sends recipient and text to watchdog, so it can be stored with dblog.module
+ * @param $sms
+ *   sms object
+ */
+function sms_send_log($sms) {
+  $number = $sms->recipient;
+  $message = $sms->text;
   watchdog('sms', 'SMS message sent to %number with the text: @message', array('%number' => $number, '@message' => $message), WATCHDOG_INFO);
   return array('status' => TRUE);
 }
