Index: storm.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/storm/storm.module,v
retrieving revision 1.10.4.52
diff -u -r1.10.4.52 storm.module
--- storm.module	17 Jan 2010 13:49:57 -0000	1.10.4.52
+++ storm.module	11 Mar 2010 21:30:34 -0000
@@ -951,3 +951,166 @@
   }
   return $options;
 }
+
+
+
+
+/**
+ * Implementation of hook_nodeapi().
+ */
+//I pick up the modifications or the creation of ticket, task or of projects
+function storm_mail_nodeapi(&$node, $op, $teaser, $page) {
+  switch ($op) {
+case 'update':
+  if ($node->type == "stormproject") {
+  storm_mail_all($node, "project", "up");
+  }
+  if ($node->type == "stormtask") {
+  storm_mail_all($node, "task", "up");
+  }
+  if ($node->type == "stormticket") {
+  storm_mail_all($node, "ticket", "up");
+  }
+  break;
+  case 'insert':
+  if ($node->type == "stormproject") {
+  storm_mail_all($node, "project", "ins");
+  }
+  if ($node->type == "stormtask") {
+  storm_mail_all($node, "task", "ins");
+  }
+  if ($node->type == "stormticket") {
+  storm_mail_all($node, "ticket", "ins");
+  }
+  break;
+  }
+}
+
+
+
+
+/**
+ * @function
+ * Prepare the data necessary for shipment of the mail
+ */
+function storm_mail_all($node, $type, $oper) {
+//Grab the ticket
+$result = db_query("SELECT * FROM {%s} WHERE nid = %d ", $node->type, $node->nid);
+if ($data = db_fetch_object($result)) {
+$assign = $data->assigned_nid;
+//Check who has been assigned to it
+$sql = db_query("SELECT * FROM {node} WHERE nid = %d ", $assign);
+$type= db_fetch_object($sql);
+
+  if ($type->type == 'stormteam') {
+//getTicket Type
+$type= $type->type;
+//get Team name
+$name = $type->title;
+//get members
+$result = db_query("SELECT * FROM {stormteam} WHERE nid = %d ", $assign);
+$teammembers= db_fetch_object($result);
+$teammembers= unserialize($teammembers->members);
+$emailTo='';
+
+$counter_teammembers=0;
+foreach($teammembers as $key => $teammember) {
+if($counter_teammembers!=0) $emailTo .= ' , ';
+$counter_teammembers++;
+$result = db_query("SELECT * FROM {stormperson} WHERE nid = %d ", $key);
+if ($data = db_fetch_object($result)) {
+$emailTo .= $data->email;
+
+}
+}
+}
+else {
+$result = db_query("SELECT * FROM {stormperson} WHERE nid = %d ", $assign);
+if ($data = db_fetch_object($result)) {
+$emailTo = $data->email;
+$name = $data->fullname;
+}
+};
+
+
+}
+//Check if pathauto module exists and it's enabled
+  $exist_alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s' ", 'node/' . $node->nid));
+    if ($exist_alias) {
+  $result = db_query("SELECT * FROM {url_alias} WHERE src = '%s' ", 'node/' . $node->nid);
+  if ($data = db_fetch_object($result)) {
+  $url = $data->dst;
+  }
+    }
+  else $url = 'node/' . $node->nid;
+  //I obtain URL of the corresponding page to the rows unloaded
+  global $base_url;
+  $url_down = $base_url . '/' . $url;
+  $result = db_query("SELECT * FROM {node} WHERE nid = %d", $node->nid);
+    if ($data = db_fetch_object($result)) {
+  $titolo = $data->title;
+    }
+  //It passes the parameters to the mail
+  $params = array(
+'name' => $name,
+'url' => $url_down,
+'titolo' => $titolo,
+'type' => $type
+  );
+
+
+  //shipment mail all' awardee of the task
+  if ($node->send_mail == 'yes' && $emailTo != '' && $oper == "up") {
+drupal_mail('storm_mail', 'assigned_to_update', $emailTo, language_default(), $params);
+drupal_set_message ('update mail sent ');
+  }
+  if ($node->send_mail == 'yes' && $emailTo != '' && $oper == "ins") {
+drupal_mail('storm_mail', 'assigned_to', $emailTo, language_default(), $params);
+drupal_set_message ('mail sent ');
+
+  }
+  }
+
+/**
+ * Implementation of hook_mail().
+ */
+//send the mail to the recipients
+function storm_mail_mail($key, &$message, $params) {
+  switch ($key) {
+    case 'assigned_to':
+      // note: data can be passed to this function in the $params array
+      $message['subject'] = t('assignment ') . $params['type'];
+      $message['body'] = t('Hello ') . $params['name'] . t(', ')
+                        . t('The e\' state as soon as assigned the following one ') . $params['type'] . (': ') . $params['url'] . t(' dal titolo: "') . $params['titolo'] . t('"');
+      break;
+      case 'assigned_to_update':
+      // note: data can be passed to this function in the $params array
+      $message['subject'] = t('update ') . $params['type'];
+      $message['body'] = t('Hello ') . $params['name'] . t(', ')
+                        . t('The ') . $params['type'] . (' "') . $params['titolo'] . t('", to which you are assigned has been modified: ') . $params['url'];
+      break;
+  }
+}
+
+/**
+ * Implementation of hook_form_alter().
+ */
+  function storm_mail_form_alter(&$form, $form_state, $form_id) {
+    switch ($form_id) {
+        case 'stormproject_node_form':
+        case 'stormtask_node_form':
+        case 'stormticket_node_form':
+            $options = array(
+                'no' => t('No'),
+                'yes' => t('Yes')
+            );
+            $form['group5']['send_mail'] = array(
+                '#type' => 'select',
+                '#title' => t('Send an email'),
+                '#options' => $options,
+                '#default_value' => 'yes',
+            );
+            return $form;
+        break;
+  }
+  }
\ No newline at end of file

