Index: forward.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/forward/forward.module,v
retrieving revision 1.30.2.47
diff -u -r1.30.2.47 forward.module
--- forward.module	29 Apr 2008 18:22:37 -0000	1.30.2.47
+++ forward.module	9 May 2008 21:17:38 -0000
@@ -34,7 +34,7 @@
       'path'     => 'epostcard',
       'title'    => variable_get('forward_epostcard_title', t('Send an e-Postcard')),
       'callback' => 'drupal_get_form',
-      'callback arguments' => array('forward_form'),
+      'callback arguments' => array('forward_form', 'epostcard'),
       'access'   => (user_access('access epostcard')),
       'type'     => MENU_NORMAL_ITEM
     );
@@ -55,18 +55,16 @@
       'access'  => user_access('administer forward'),
       'type'    => MENU_NORMAL_ITEM
     );
+    $items[] = array(
+      'path'     => 'forward',
+      'title'    => variable_get('forward_page_title', t('Forward this page')),
+      'callback' => 'forward_page',
+      'access'   => (user_access('access forward')),
+      'type'     => MENU_CALLBACK
+    );
   }
   else {
-    if (is_numeric($arg1 = arg(1))) {
-      $items[] = array(
-        'path'     => 'forward',
-        'title'    => variable_get('forward_page_title', t('Forward this page')),
-        'callback' => 'drupal_get_form',
-        'callback arguments' => array('forward_form', $arg1, FALSE),
-        'access'   => (user_access('access forward')) && node_access('view', node_load($arg1)),
-        'type'     => MENU_CALLBACK
-      );
-    } else if (is_numeric($arg2 = arg(2))) {
+    if (is_numeric($arg2 = arg(2))) {
       $items[] = array(
         'path'     => 'forward/emailref',
         'title'    => t('Track email clickthrus'),
@@ -91,11 +89,16 @@
 /**
  * Implementation of hook_help
  */
-function forward_help($section) {
-  switch ($section) {
-    case 'epostcard':
-    case 'forward/'.arg(1):
-      return t(variable_get('forward_instructions', '<p>'. t('Thank you for your interest in spreading the word on') .' '. variable_get('site_name', 'drupal') .'.</p><p>'. t('NOTE: We only request your email address so that the person you are recommending the page to knows that you wanted them to see it, and that it is not junk mail. We do not capture any email address.') .'</p>'));
+function forward_help($path) {
+  if (user_access('access forward') && (preg_match("/^node\//i", $path) == 0) && (variable_get('forward_display_nonnode', false))) {
+    if ($path === NULL) {
+      $path = PRINT_PATH ."/". $_GET['q'];
+      $query = drupal_query_string_encode($_GET, array('q'));
+      if (empty($query)) {
+        $query = NULL;
+      }
+    }
+    return '<span class="forward">'. l(t('Email this page'), 'forward/'.$path, NULL, $query) .'</span>';
   }
 }
 
@@ -136,6 +139,13 @@
     '#default_value' => variable_get('forward_display_comments', false),
     '#description' => t('If checked, the link/form will appear on comments'),
   );
+  $form['forward_options']['forward_display_nonnode'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Show Forward on non-node pages'),
+    '#return_value' => 1,
+    '#default_value' => variable_get('forward_display_nonnode', false),
+    '#description' => t('If checked, the link/form will appear on non-node pages like views and other module pages'),
+  );
   $form['forward_options']['forward_form_type'] = array(
     '#type' => 'select',
     '#title' => t('Form Type'),
@@ -331,17 +341,68 @@
 /**
  * Form
  */
-function forward_form($nid = null, $nodeapi = FALSE) {
+function forward_page($nid = null) {
+  //print '<pre>'.print_r(menu_get_menu(),1).'</pre>'; exit;
+  
+  if (!is_numeric($nid)) {
+    // remove forward/ prefix from path
+    $args = substr($_GET['q'], strpos($_GET['q'], '/')+1);
+    $path = drupal_get_normal_path($args);
+    $ret = preg_match("/^node\/(.*)/i", $path, $matches);
+    if ($ret == 1) {
+      $nid = $matches[1];
+    }
+  }
+  if (is_numeric($nid)) {
+    // we have a node
+    $node = node_load(array('nid' => $nid));
+    if (!node_access('view', $node)) {
+      // Access is denied
+      return drupal_access_denied();
+    }
+    $path = 'node/'.$node->nid;
+  } else {
+    $_GET['q'] = $path;
+    _menu_append_contextual_items();
+  
+    menu_set_active_item($path);
+    
+    // Adapted from index.php.
+    $node = new stdClass();
+    $node->body = menu_execute_active_handler();
+    $node->title = menu_get_active_title();
+    
+    // It may happen that a drupal_not_found is called in the above call
+    if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) {
+      return;
+    }
+  
+    switch ($node->body) {
+      case MENU_NOT_FOUND:
+        return drupal_not_found();
+        break;
+      case MENU_ACCESS_DENIED:
+        return drupal_access_denied();
+        break;
+    }
+  }
+  //print "$path - $nid - $node->title"; exit;
+  return drupal_get_form('forward_form', $path, $node->title);
+}
+
+/**
+ * Form
+ */
+function forward_form($path = NULL, $title = NULL, $nodeapi = FALSE) {
   global $base_url, $user;
 
   $form = array();
 
-  if ($nid != '') {
-    $emailtype = 'email';
-    $form['node'] = array('#type' => 'value', '#value' => node_load((int)$nid));
+  if ($path == 'epostcard') {
+    $emailtype = 'postcard';
   }
   else {
-    $emailtype = 'postcard';
+    $emailtype = 'email';
   }
   
   if ($nodeapi == TRUE) {
@@ -353,7 +414,11 @@
       '#collapsible' => true,
     );
   }
-  $form['message']['yemail'] = array(
+  $form['instructions'] = array(
+    '#type' => 'item',
+    '#value' => t(variable_get('forward_instructions', '<p>'. t('Thank you for your interest in spreading the word on') .' '. variable_get('site_name', 'drupal') .'.</p><p>'. t('NOTE: We only request your email address so that the person you are recommending the page to knows that you wanted them to see it, and that it is not junk mail. We do not capture any email address.') .'</p>')),
+  );
+  $form['yemail'] = array(
     '#type' => 'textfield',
     '#title' => t('Your Email'),
     '#default_value' => $user->mail,
@@ -362,10 +427,10 @@
     '#required' => true,
   );
   if (($user->mail) && !user_access('override email address')) {
-    $form['message']['yemail']['#disabled'] = true;
-    $form['message']['yemail']['#value'] = $user->mail;
+    $form['yemail']['#disabled'] = true;
+    $form['yemail']['#value'] = $user->mail;
   }
-  $form['message']['yname'] = array(
+  $form['yname'] = array(
     '#type' => 'textfield',
     '#title' => t('Your Name'),
     '#default_value' => $user->name,
@@ -373,7 +438,7 @@
     '#maxlength' => 256,
     '#required' => true,
   );
-  $form['message']['recipients'] = array(
+  $form['recipients'] = array(
     '#type' => 'textarea',
     '#title' => t('Send To'),
     '#default_value' => str_replace(', ', "\n", $recipients),
@@ -383,25 +448,25 @@
     '#required' => true,
   );
   if (($emailtype == 'email') && ($nodeapi == FALSE)) {
-    $form['message']['page'] = array(
+    $form['page'] = array(
       '#type' => 'item',
       '#title' => t('You are going to email the following'),
-      '#value' => l($form['node']['#value']->title, "node/" . $form['node']['#value']->nid),
+      '#value' => l($title, $path),
     );
   }
-  $form['message']['subject'] = array(
+  $form['subject'] = array(
     '#type' => 'item',
     '#title' => t('Message Subject'),
     '#value' => t(variable_get('forward_'. $emailtype .'subject', '!name has sent you a message from !site'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
     '#description' => '',
   );
-  $form['message']['body'] = array(
+  $form['body'] = array(
     '#type' => 'item',
     '#title' => t('Message Body'),
     '#value' => t(variable_get('forward_'. $emailtype .'message', '!name thought you would like to see the !site web site.'), array('!name' => t('(Your Name)'), '!site' => variable_get('site_name', 'drupal'))),
     '#description' => '',
   );
-  $form['message']['message'] = array(
+  $form['message'] = array(
     '#type' => 'textarea',
     '#title' => t('Your Personal Message'),
     '#default_value' => '',
@@ -410,20 +475,35 @@
     '#description' => '',
     '#required' => true,
   );
-  $form['message']['nid'] = array(
+  $form['path'] = array(
     '#type' => 'hidden',
-    '#value' => $nid,
+    '#value' => $path,
   );
-  $form['message']['forward_footer'] = array(
+  $form['forward_footer'] = array(
     '#type' => 'hidden',
     '#value' => variable_get('forward_footer', ''),
   );
-  $form['message']['submit'] = array(
+  $form['submit'] = array(
     '#type' => 'submit',
     '#value' => t('Send Message'),
   );
-
-  return $form;
+  if ($nodeapi == TRUE) {
+    $nodeapiform['message'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Forward this page to a friend'),
+      '#description' => '',
+      '#collapsed' => true,
+      '#collapsible' => true,
+    );
+    foreach ($form as $key => $value) {
+      $nodeapiform['message'][$key] = $value;
+    }
+    //print '<pre>'.print_r($nodeapiform,1).'</pre>';
+    return $nodeapiform;
+  }
+  else {
+    return $form;
+  }
 }
 
 /**
@@ -434,7 +514,7 @@
     switch ($op) {
       case 'view':
         if (user_access('access forward')) {
-          $output = drupal_get_form('forward_form', $node->nid, TRUE);
+          $output = drupal_get_form('forward_form', 'node/'.$node->nid, $node->title, TRUE);
           $node->content['forward'] = array('#value' => $output, '#weight' => 10);
         }
         break;
@@ -465,7 +545,7 @@
   $yemail     = $form['edit']['yemail'];
   $recipients = $form['edit']['recipients'];
   $message    = $form['edit']['message'];
-  $nid        = $form['edit']['nid'];
+  $path       = $form['edit']['path'];
   $url        = $base_url .'/node/'. $nid;
 
   // normalize address entries
@@ -536,22 +616,50 @@
       break;
   }
 
-  if (!$edit['nid']) {
+  if ($edit['path'] == 'epostcard') {
     $emailtype = 'postcard';
-    $nid = '';
-    $content_title = '';
-    $content_name = '';
-    $content_teaser = '';
-    $returnurl = drupal_get_path_alias('');
   }
   else {
-    $nid = $edit['nid'];
     $emailtype = 'email';
-    //Get article information.
-    $content = node_load($nid);
-    $content->teaser = check_markup($content->teaser, $content->format, false);
-    $returnurl = drupal_get_path_alias('node/'. $nid);
+    $path = explode('/', $edit['path']);
+    if (($path[0] == 'node') && (is_numeric($path[1]))) {
+      // we have a node
+      $content = node_load($path[1]);
+      if (!node_access('view', $content)) {
+        // Access is denied
+        return drupal_access_denied();
+      }
+      $content->teaser = check_markup($content->teaser, $content->format, false);
+      $returnurl = drupal_get_path_alias('node/'. $nid);
+    } else {
+      $_GET['q'] = $edit['path'];
+      _menu_append_contextual_items();
+    
+      menu_set_active_item($edit['path']);
+      
+      // Adapted from index.php.
+      $content = new stdClass();
+      $content->body = menu_execute_active_handler();
+      $content->teaser = $content->body;
+      $content->title = menu_get_active_title();
+      $returnurl = $edit['path'];
+    
+      // It may happen that a drupal_not_found is called in the above call
+      if (preg_match('/404 Not Found/', drupal_get_headers()) == 1) {
+        return;
+      }
+    
+      switch ($content->body) {
+        case MENU_NOT_FOUND:
+          return drupal_not_found();
+          break;
+        case MENU_ACCESS_DENIED:
+          return drupal_access_denied();
+          break;
+      }
+    }
   }
+  
   $vars = array(
     'forward_header_image' => variable_get('forward_header_image', ''),
     'site_name' => variable_get('site_name', 'Drupal'),
