? spread-new_features.patch
Index: spread.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/spread/spread.module,v
retrieving revision 1.5
diff -u -p -r1.5 spread.module
--- spread.module	2 Oct 2008 17:01:19 -0000	1.5
+++ spread.module	8 May 2009 22:38:34 -0000
@@ -39,7 +39,42 @@ function spread_perm() {
 function spread_admin_settings() {
   $form = array();
   
-  $form['spread']['subject'] = array(
+  $form['spread_use_js'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Enable Javascript'),
+    '#default_value' => variable_get('spread_use_js', TRUE),
+  );
+  
+  $form['spread_destination'] = array(
+    '#type' => 'radios',
+    '#title' => t('Destination'),
+    '#description' => t('Select the destination where the user will be directed in the email.'),
+    '#options' => array(
+      'front' => 'Front page',
+      'current' => 'Current page',
+      'custom' => 'Custom',
+    ),
+    '#default_value' => variable_get('spread_destination', 'current'),
+  );
+  
+  $form['spread_custom_destination'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Custom destination'),
+    '#description' => t('Enter the custom destination for the link in the email here. You can use anything !url() accepts.', array('!url' => l('url', 'http://api.drupal.org/api/function/url', array('absolute' => TRUE)))),
+    '#size' => 40,
+    '#maxlength' => 255,
+    '#default_value' => variable_get('spread_current_destination', '<front>'),
+  );
+
+  $form['spread_thank_you'] = array(
+    '#title' => t('Thank you message'),
+    '#type'  => 'textfield',
+    '#required' => TRUE,
+    '#default_value' => variable_get('spread_thank_you', 'Thank you for spreading the word!'),
+    '#description' => t('The thank you message received after submission of the spread form.'),
+  );
+
+  $form['spread_subject'] = array(
     '#title' => t('Subject'),
     '#type'  => 'textfield',
     '#required' => TRUE,
@@ -47,15 +82,15 @@ function spread_admin_settings() {
     '#description' => t('The subject of the e-mail which will be sent.'),
   );
   
-  $form['spread']['body'] = array(
+  $form['spread_body'] = array(
     '#title' => t('Body'),
     '#type'  => 'textarea',
     '#required' => TRUE,
-    '#default_value' => variable_get('spread_body', t("Hi,\n!sender_email thinks that you should review this web page:\n!url")),
-    '#description' => t("This is the message which will be sent. You can use the following placeholders to adjust it:<br /><em>!sender_email</em>: user e-mail who spread the word<br /><em>!url</em>: web page from which the user spread the word"),
+    '#default_value' => variable_get('spread_body', t("Hi,\n\n!name (!sender_email) thinks that you may be interested in this web page:\n\n!url")),
+    '#description' => t("This is the message which will be sent. You can use the following placeholders to adjust it:<br /><em>!sender_email</em>: senders email address<br /><em>!url</em>: web page from which the user submitted the form or the destination URL if set"),
   );
   
-  $form['spread']['how_many']= array(
+  $form['spread_how_many'] = array(
     '#title' => t("How many e-mail fields?"),
     '#type'  => 'textfield',
     '#required' => TRUE,
@@ -71,20 +106,14 @@ function spread_admin_settings() {
 
 function spread_admin_settings_validate($form, &$form_state) {
   // Is entered value for field count is number?
-  if (!is_numeric($form_state['values']['how_many'])) {
-    form_set_error('how_many', t('You must enter a numeric value'));
+  if (!is_numeric($form_state['values']['spread_how_many'])) {
+    form_set_error('spread_how_many', t('You must enter a numeric value'));
   }
-  if ((int)$form_state['values']['how_many'] == 0) {
-    form_set_error('how_many', t('0 seems to be not enough!'));
+  if ((int)$form_state['values']['spread_how_many'] == 0) {
+    form_set_error('spread_how_many', t('0 seems to be not enough!'));
   }
 }
 
-function spread_admin_settings_submit($form, &$form_state) {
-  variable_set('spread_subject',  $form_state['values']['subject']);
-  variable_set('spread_body',     $form_state['values']['body']);
-  variable_set('spread_how_many', $form_state['values']['how_many']);
-}
-
 function spread_block($op = 'list', $delta = 0, $edit = array()) {
   switch ($op) {
     case 'list':
@@ -97,9 +126,7 @@ function spread_block($op = 'list', $del
           if (user_access('use spread form')) {
             $block['subject'] = t('Spread the word');
             $block['content'] = drupal_get_form('spread_form');
-            drupal_add_js("Drupal.q = '". $_GET['q'] ."'", 'inline');
-            drupal_add_js(drupal_get_path('module', 'spread') .'/spread.js');
-            drupal_add_css(drupal_get_path('module', 'spread') .'/spread.css');
+            spread_add_files();
           }
           break;
       }
@@ -107,99 +134,140 @@ function spread_block($op = 'list', $del
   }
 }
 
-function spread_form() {
+function spread_add_files() {
+  static $added = FALSE;
+  if ($added == FALSE) {
+    $module_path = drupal_get_path('module', 'spread');
+    if (variable_get('spread_use_js', TRUE)) {
+      drupal_add_js("Drupal.q = '". $_GET['q'] ."'", 'inline');
+      drupal_add_js($module_path .'/spread.js');
+    }
+    drupal_add_css($module_path .'/spread.css');
+    $added = TRUE;
+  }
+}
+
+function spread_form($form_state) {
   // We need this to get user's email
-  global $user;
-  
+  global $user;  
+  $total = variable_get('spread_how_many', 3);
+
   $form['thank_you'] = array(
-    '#prefix' => "<div id=\"spread-thank-you\">",
-    '#value'  => t('Thank you for spreading the word!'),
+    '#prefix' => '<div id="spread-thank-you" class="messages status">',
+    '#value'  => variable_get('spread_thank_you', 'Thank you for spreading the word!'),
     '#suffix' => "</div>",
   );
-  
-  $form['friends_label'] = array(
-    '#prefix' => '<label><strong>',
-    '#value'  => t("Your friend's e-mail:"),
-    '#suffix' => '</strong></label>'
+
+  $form['sender_name'] = array(
+    '#type'  => 'textfield',
+    '#title' => t('Your name'),
+    '#size'  => 20,
   );
-  
-  for ($i = 0; $i < variable_get('spread_how_many', 3); $i++) {
-    $form['to'][] = array(
+
+  $form['sender_mail'] = array(
+    '#type'  => 'textfield',
+    '#title' => t('Your email'),
+    '#size'  => 20,
+    '#default_value' => $user->mail,
+  );
+
+  for ($i = 0; $i < $total; $i++) {
+    $form['to']["spread-mail-$i"] = array(
       '#type' => 'textfield',
+      '#title' => t('Your friend\'s email'),
       '#size' => 20,
       '#name' => 'to[]',
-      '#default_value' => $_POST['to'][$i],
+      '#value' => $form_state['post']['to'][$i],
     );
   }
-  
-  $form['from'] = array(
-    '#type'  => 'textfield',
-    '#title' => t('Your e-mail'),
-    '#size'  => 20,
-    '#default_value' => $user->mail,
-  );
-  
+
   $form['submit'] = array(
     '#type'  => 'submit',
     '#value' => t('Spread the word'),
   );
-  
-  return $form ;
+
+  return $form;
 }
 
 function spread_form_validate($form, &$form_state) {
-  
-  $error = FALSE;
-  
-  foreach ($_POST['to'] as $key => $mail) {
-    $form_state['values']['to'][] = $mail;
-    
-    if (!valid_email_address($mail) && trim($mail) != '') {
-      $error = TRUE;
-      unset($_POST['to'][$key]);
-    }
-    
-    if (trim($mail) == '') {
-      unset($_POST['to'][$key]);
+  // Did the user enter their name?
+  if ($form_state['values']['sender_name'] == '') {
+    form_set_error('sender_name', t('Please enter your name'));
+  }
+
+  // Did the user enter their email address?
+  if ($form_state['values']['sender_mail'] == '') {
+    form_set_error('sender_mail', t('Please enter your email address'));
+  }
+
+  $match = FALSE;
+  foreach ($form_state['values'] as $key => $mail) {
+    if (strstr($key, 'spread-mail-')) {
+      if ($form_state['values'][$key] == '') {
+        unset($form_state['values'][$key]);
+      }
+      else {
+        $match = TRUE;
+        if (!valid_email_address($mail)) {
+          form_set_error($key, t('Email addresses must be valid'));        
+        }
+      }
     }
   }
-  
-  if (!valid_email_address($form_state['values']['from'])) {
-    $error = TRUE;
+  // If all inputs are empty throw an error.
+  if ($match == FALSE) {
+    form_set_error('spread-mail-0', t('Please enter at least one email address'));
   }
-  
-  if ($error == TRUE) {
-    form_set_error('', t('E-mail adresses must be valid'));
+
+  // Check we have a valid sender email address.
+  if (!valid_email_address($form_state['values']['sender_mail'])) {
+    form_set_error('sender_mail', t('Your email addresses is invalid'));
   }
-  
 }
 
 function spread_form_submit($form, &$form_state) {
-  spread_send_email($_POST['to'], $form_state['values']['from']);
+  foreach ($form_state['values'] as $key => $mail) {
+    if (strstr($key, 'spread-mail-')) {
+      $addresses[] = $mail;
+    }
+  }
+
+  spread_send_email($form_state['values']['sender_name'], $addresses, $form_state['values']['sender_mail']);
 }
 
-function spread_send_email($to = array(), $from = NULL, $js = FALSE) {
+function spread_send_email($name = NULL, $to = array(), $sender_mail = NULL, $js = FALSE) {
+  $destination = variable_get('spread_destination', 'current');
   $subject = variable_get("spread_subject", NULL);
-  
   $body = variable_get("spread_body", NULL);
-  
   $headers = array('Content-Type' => 'text/html; charset=UTF-8; format=flowed');
-  
-  if (!$js) {
-    $url = url($_GET['q'], array('query' => $query = NULL, 'fragment' => $fragment = NULL, 'absolute' => TRUE));
-  }
-  else {
-    $url = url($_POST['url'], array('query' => $query = NULL, 'fragment' => $fragment = NULL, 'absolute' => TRUE));
+
+  switch ($destination) {
+    case 'current':
+      if (!$js) {
+        $url = url($_GET['q'], array('absolute' => TRUE));
+      }
+      else {
+        $url = url($_POST['url']), array('absolute' => TRUE));
+      }
+      break;
+    case 'front':
+      $url = url('<front>', array('absolute' => TRUE));
+      break;
+    case 'custom':
+      $custom_destination = variable_get('spread_custom_destination', '<front>');
+      $url = url($custom_destination, array('absolute' => TRUE));
+      break;
   }
-  
+
   foreach ($to as $mail) {
-    
+
     $placeholders = array(
-      "!sender_email" => $from,
-      "!url"          => $url,
+      '!sender_email' => $sender_mail,
+      '!url'          => $url,
       '!mail'         => $mail,
+      '!name'         => $name,
     );
-    
+
     $object  = array();
     $context = array();
     
@@ -213,14 +281,14 @@ function spread_send_email($to = array()
       'headers'      => $headers,
     );
     
-    drupal_mail('spread', "spread", $mail, language_default(), $params, $from);
+    drupal_mail('spread', "spread", $mail, language_default(), $params, $sender_mail);
     
     $watchdog_message = t("!sender_email has spread the word to !mail on !url", $placeholders);
     watchdog('spread', $watchdog_message, $variables = array(), $severity = WATCHDOG_NOTICE, $link = NULL);
   }
   
   if (!$js) {
-    drupal_set_message(t('Thank you for spreading the word!'));
+    drupal_set_message(variable_get('spread_thank_you', 'Thank you for spreading the word!'));
   }
 }
 
@@ -232,13 +300,15 @@ function spread_mail($key, &$message, $p
 
 function spread_js() {
   global $user;
-  
+
   if (!user_access('use spread form')) {
     die();
   }
-  
-  if (($emails = spread_js_validate($_POST['to'], $_POST['from'])) != FALSE) {
-    spread_send_email($emails['to'], $emails['from'], $js = TRUE);
+
+  $js = variable_get('spread_use_js', TRUE);
+
+  if (($emails = spread_js_validate($_POST['to'], $_POST['sender_mail'])) != FALSE) {
+    spread_send_email($_POST['sender_name'], $emails['to'], $emails['from'], $js);
     print "TRUE";
   }
   else {
@@ -247,7 +317,7 @@ function spread_js() {
   die();
 }
 
-function spread_js_validate($adresses, $from) {
+function spread_js_validate($adresses, $sender_mail) {
   $error = FALSE;
   foreach ($adresses as $key => $mail) {
     $to[] = $mail;
@@ -259,7 +329,7 @@ function spread_js_validate($adresses, $
       unset($adresses[$key]);
     }
   }
-  if (!valid_email_address($from)) {
+  if (!valid_email_address($sender_mail)) {
     $error = TRUE;
   }
   
@@ -274,6 +344,6 @@ function spread_js_validate($adresses, $
   }
   return array(
     'to'   => $adresses,
-    'from' => $from,
+    'from' => $sender_mail,
   );
-}
\ No newline at end of file
+}
