Index: notifications.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/notifications/notifications.module,v
retrieving revision 1.6.2.6
diff -u -r1.6.2.6 notifications.module
--- notifications.module	19 Mar 2008 18:13:59 -0000	1.6.2.6
+++ notifications.module	24 Mar 2008 04:15:25 -0000
@@ -7,15 +7,15 @@
  *
  * This is the base module of the notifications framework. It handles event processing, queueing,
  * message composition and sending.
- * 
+ *
  * Different subscriptions types are provided by plug-in modules implementing hook_notifications()
  * Most of the UI is implemented in notifications_ui module
  * The messaging framework is used for message delivery
- * Token module is used for token replacement in messages 
- * 
+ * Token module is used for token replacement in messages
+ *
  * This is based on the previous subscriptions module
- * 
- * Development Seed, http://www.developmentseed.org, 2007 
+ *
+ * Development Seed, http://www.developmentseed.org, 2007
  *
  */
 
@@ -45,13 +45,13 @@
       'type' => MENU_DEFAULT_LOCAL_TASK,
       'weight' => -10,
     );
-    $items[] = array('path' => 'admin/notifications/status/queue', 
+    $items[] = array('path' => 'admin/notifications/status/queue',
       'title' => t('Queue'),
       'description' => t('Subscriptions queue.'),
       'callback' => 'notifications_admin_queue',
       'type' => MENU_LOCAL_TASK
     );
-    
+
     // Site settings
     $items[] = array(
       'path' => 'admin/notifications/settings',
@@ -93,7 +93,7 @@
     if (arg(0) == 'notifications' || arg(1) == 'notifications' || arg(2) == 'notifications') {
       include_once drupal_get_path('module', 'notifications') .'/notifications.admin.inc';
     }
-    
+
     if ($user->uid && arg(0) == 'user' && is_numeric(arg(1)) && ($user->uid == arg(1) && user_access('maintain own subscriptions') || user_access('administer notifications'))) {
       $account = ($user->uid == arg(1)) ? $user : user_load(array('uid' => arg(1)));
       $items[] = array(
@@ -116,7 +116,7 @@
 
 /**
  * Implementation of hook_perms()
- * 
+ *
  */
 function notifications_perm() {
   return array_merge(array('administer notifications', 'maintain own subscriptions'));
@@ -150,7 +150,7 @@
           '#title' => t('Default send interval'),
           '#options' => _notifications_send_intervals(),
           '#default_value' => notifications_user_setting('send_interval', $user),
-        );      
+        );
         /*
         $form['notifications']['notifications_teaser'] = array(
           '#type'          => 'checkbox',
@@ -167,7 +167,7 @@
 
 /**
  * Gets a user setting, defaults to default system setting for each
- * 
+ *
  * @param $name
  *   Setting name
  * @param $account
@@ -218,14 +218,14 @@
  *   Event array.
  */
 function notifications_queue($event) {
-  
+
   // Build query fields for this event type. If no arguments retrieved, skip this step
   if ($query_args = notifications_module_info('query', 'event', $event->type, $event)) {
     $query['args'] = array($event->eid, $event->created, $event->type);
     foreach ($query_args as $query_params) {
       $query = notifications_query_build($query_params, $query);
     }
-    
+
     // We throw in all the conditions and check the number of matching conditions
     // that must be equal to the subscription conditions number
     $sql = 'INSERT INTO {notifications_queue} (uid, sid, module, eid, send_interval, send_method, cron, created, conditions) '.
@@ -240,7 +240,7 @@
 
   // Modules can do cleanup operations or modify the queue
   notifications_module_invoke('event queued', $event);
-  
+
   // Optional immediate sending.
   if (variable_get('notifications_send_immediate', 0)) {
     require_once drupal_get_path('module', 'notifications') .'/notifications.cron.inc';
@@ -250,7 +250,7 @@
 
 /**
  * Query builder for subscriptions
- * 
+ *
  * @param $params
  *   Array of query conditions
  */
@@ -274,7 +274,7 @@
 
 /**
  * Stores new events
- * 
+ *
  * @param $event
  *   Event object
  * @ TODO: Avoid to and from array conversion
@@ -289,7 +289,7 @@
 
 /**
  * Get subscription for a given user
- * 
+ *
  * @param $uid
  *   User id
  * @param $event_type
@@ -298,9 +298,9 @@
  *   Object id for caching. I.e. for a node it will be nid
  * @param $object
  *   Object to check subscriptions to. I.e. $node
- * 
+ *
  * @return
- *   Array of subscriptions for this user and object indexed by sid 
+ *   Array of subscriptions for this user and object indexed by sid
  */
 function notifications_user_get_subscriptions($uid, $event_type, $oid, $object = NULL, $refresh = FALSE) {
   static $subscriptions;
@@ -317,7 +317,7 @@
     }
 
     $sql = 'SELECT s.*, f.* FROM {notifications} s INNER JOIN {notifications_fields} f ON s.sid = f.sid '.
-      implode(' ', $query['join']). 
+      implode(' ', $query['join']).
       " WHERE s.uid = %d AND event_type = '%s' AND ((".implode(') OR (', $query['where']).'))';
     $result = db_query($sql, $query['args']);
     while ($sub = db_fetch_object($result)) {
@@ -333,11 +333,11 @@
 
 /**
  * Update or create subscription
- * 
+ *
  * This function checks for duplicated subscriptions before saving.
  * If a similar subscription is found it will be updated.
  * If no subscription is found and it is new, the sid will be added into the object.
- * 
+ *
  * @param $subscription
  *   Subscription object or array
  */
@@ -354,7 +354,7 @@
   }
   // The cron parameter will be always enabled because we are not managing push/pull methods anymore at this level.
   $subscription->cron = 1;
-  
+
   if ($subscription->sid) {
     $op = 'update';
     db_query("UPDATE {notifications} SET uid = %d, type = '%s', event_type = '%s', conditions = %d, send_interval = '%d', send_method = '%s', cron = %d, module = '%s', status = %d WHERE sid = %d", $subscription->uid, $subscription->type, $subscription->event_type, $subscription->conditions, $subscription->send_interval, $subscription->send_method, $subscription->cron, $subscription->module, $subscription->status, $subscription->sid);
@@ -370,14 +370,16 @@
     while (array_shift($duplicate)) {
       notifications_delete_subscription($duplicate->sid);
     }
-    return notifications_save_subscription($subscription);    
+    return notifications_save_subscription($subscription);
   } else {
     $op = 'insert';
     $subscription->sid = db_next_id('{notifications}_sid');
     db_query("INSERT INTO {notifications} (sid, uid, type, event_type, conditions, send_interval, send_method, cron, module, status) VALUES(%d, %d, '%s', '%s', %d, %d, '%s', %d, '%s', %d)", $subscription->sid, $subscription->uid, $subscription->type, $subscription->event_type, $subscription->conditions, $subscription->send_interval, $subscription->send_method, $subscription->cron, $subscription->module, $subscription->status);
   }
-  foreach ($subscription->fields as $name => $value) {
-    db_query("INSERT INTO {notifications_fields} (sid, field, value) VALUES(%d, '%s', '%s')", $subscription->sid, $name, $value);
+  if (is_array($subscription->fields)) {
+    foreach ($subscription->fields as $name => $value) {
+      db_query("INSERT INTO {notifications_fields} (sid, field, value) VALUES(%d, '%s', '%s')", $subscription->sid, $name, $value);
+    }
   }
   notifications_module_invoke($op, $subscription);
 }
@@ -418,9 +420,9 @@
 
 /**
  * Delete subscription and clean up related data.
- * 
- * It also removes pending notifications related to that subscription 
- * 
+ *
+ * It also removes pending notifications related to that subscription
+ *
  * @param $sid
  *   Id of subscriptin to delete
  */
@@ -432,9 +434,9 @@
 
 /**
  * Delete multiple subscriptions and clean up related data (pending notifications, fields).
- * 
+ *
  * Warning: It will delete also subscriptions with more conditions than the fields passed.
- * 
+ *
  * @param array $params
  *   Array of multiple conditions in the notifications table to delete subscriptions
  * @param array $conditions
@@ -483,8 +485,8 @@
  * @param $limit
  *   Whether to limit the result to subscriptions with exatly that condition fields
  * @param $key
- *   Optional key field to use as the array index. Will default to sid 
- * 
+ *   Optional key field to use as the array index. Will default to sid
+ *
  * @return
  *   Array of subscriptions indexed by uid, module, field, value, author
  */
@@ -505,15 +507,15 @@
       }
       $index++;
     }
-  }  
+  }
   foreach ($params as $field => $value) {
     $name = strstr($field, '.') ? $field : 's.'.$field;
     $where[] = is_numeric($value) ? $name.' = %d' : "$name = '%s'";
   }
-  
+
   $sql = 'SELECT * FROM {notifications} s '. implode(' ', $join) .
   $sql .= ' WHERE '.implode(' AND ', $where);
-  
+
   if ($pager) {
     $sql .= ' ORDER BY s.sid';
     $result = pager_query($sql, $pager, 0, NULL, $params);
@@ -545,7 +547,7 @@
     $types = notifications_module_info('subscription types');
     notifications_alter('subscription_types', $types);
   }
-  
+
   if ($field && $type) {
     return isset($types[$type][$field]) ? $types[$type][$field] : NULL;
   }
@@ -606,7 +608,7 @@
         'name' => t('Notifications digest'),
       );
       return $info;
-    case 'message keys':      
+    case 'message keys':
       $type = $arg1;
       switch ($type) {
         case 'notifications-event':
@@ -650,7 +652,7 @@
           'footer' => array(
             t('This is an automatic message from [site-name])'),
             t('To manage your subscriptions, browse to [subscriptions-manage]'),
-          ),        
+          ),
         );
       }
       break;
@@ -667,13 +669,13 @@
       return $tokens;
     case 'pull':
       require_once drupal_get_path('module', 'notifications') .'/notifications.cron.inc';
-      return notifications_process_pull($arg1, $arg2, $arg3, $arg4);      
+      return notifications_process_pull($arg1, $arg2, $arg3, $arg4);
   }
 }
 
 /**
  * Implementation of hook_token_values()
- * 
+ *
  * @ TODO: Work out event tokens
  */
 function notifications_token_values($type, $object = NULL, $options = array()) {
@@ -682,7 +684,7 @@
       $values = array();
       if ($subscription = $object) {
         $link = notifications_get_link('unsubscribe', array('sid' => $subscription->sid, 'signed' => TRUE, 'destination' => FALSE));
-        $values['unsubscribe-url'] = url($link['href'], $link['query'], NULL, TRUE);      
+        $values['unsubscribe-url'] = url($link['href'], $link['query'], NULL, TRUE);
       }
       return $values;
     case 'user':
@@ -766,7 +768,7 @@
     case 'unsubscribe':
       $elements = array(
         'unsubscribe',
-        $params['sid']        
+        $params['sid']
       );
       break;
   }
@@ -789,10 +791,10 @@
 
 /**
  * Check access to objects
- * 
+ *
  * This will check permissions for subscriptions and events before subscribing
  * and before getting updates.
- * 
+ *
  * @param $type
  *   Type of object to check for access. Possible values:
  *   - 'event', will check access to event objects
@@ -801,7 +803,7 @@
 function notifications_user_allowed($type, $account, $object = NULL) {
 
   // Invoke notifications hook and check for a FALSE return value
-  
+
   $permissions = notifications_module_info('access', $type, $account, $object);
 
   if ($permissions) {
@@ -814,7 +816,7 @@
 
 /**
  * Implementation of notifications_hook()
- * 
+ *
  * Check access permissions to subscriptions
  */
 function notifications_notifications($op, &$arg0, $arg1 = NULL, $arg2 = NULL) {
@@ -880,7 +882,7 @@
     'send_method' => notifications_user_setting('send_method', $account, ''),
     'module' => 'notifications',
     'status' => 1,
-  ); 
+  );
 }
 
 /**
@@ -894,13 +896,13 @@
 
 /**
  * Callback for module dependent data
- * 
+ *
  * Some data stored in the notifications system is meant to be processed by other modules and
  * this is indicated by a module column in the data.
- * 
+ *
  * This function calls the module function if available, defaulting to the notifications provided
  * function when not. The arguments are passed as is
- * 
+ *
  * @param $module
  *   Module name
  * @param $function
@@ -917,11 +919,11 @@
    }
    return call_user_func_array($callback, $args);
  }
- 
+
  /**
  * This dispatch function hands off structured Drupal arrays to type-specific
  * _notifications_*_alter implementations. Modelled after Drupal 6 drupal_alter()
- * 
+ *
  * @param $type
  *   The data type of the structured array. 'form', 'links',
  *   'node_content', and so on are several examples.
