--- petition_HEAD.module	2007-03-14 14:28:03.739969472 -0800
+++ petition.module	2007-03-14 14:21:52.174470175 -0800
@@ -11,8 +11,6 @@
  */
 function petition_help($section) {
   switch ($section) {
-    case 'admin/modules#description':
-      return t('Enables users to create online petitions and for other users to sign it.');
 
     case 'node/add#petition':
       return t('Creates a new petition for users to sign and support.');
@@ -47,44 +45,52 @@ function petition_menu($may_cache) {
   $items = array();
 
   if ($may_cache) {
-    $items[] = array('title'   => t('petition'),
+    $items[] = array('title'   => t('Petition'),
                      'path'    => 'node/add/petition',
                      'access'  => user_access('create petition'),
     );
-    $items[] = array('title'    => t('petitions'),
+    $items[] = array('title'    => t('Petitions'),
                      'path'     => 'petition',
                      'callback' => 'petition_overview',
                      'access'   => user_access('respond to petition'),
     );
-    $items[] = array('title'    => t('petition statistics'),
-                     'path'     => 'admin/petition', 
+    $items[] = array('title'    => t('Petitions'),
+                     'path'     => 'admin/content/petition', 
                      'callback' => 'petition_results_overview',
+                     'description' => t('List petition data.'),
                      'access'   => user_access('access petition stats'),
     );
-    $items[] = array('title'    => t('comments'),
+    $items[] = array('title'    => t('Comments'),
                      'path'     => 'admin/petition/comments', 
                      'callback' => 'petition_results_comments',
                      'access'   => user_access('access petition stats'),
                      'type'     => MENU_CALLBACK,
     );
-    $items[] = array('title'    => t('signatories'),
+    $items[] = array('title'    => t('Signatories'),
                      'path'     => 'admin/petition/signatories', 
                      'callback' => 'petition_results_signatories',
                      'access'   => user_access('access petition stats'),
                      'type'     => MENU_CALLBACK,
     );
-    $items[] = array('title'    => t('signatories (csv)'),
+    $items[] = array('title'    => t('Signatories (csv)'),
                      'path'     => 'admin/petition/signatories/csv',
                      'callback' => 'petition_results_signatories_csv',
                      'access'   => user_access('access petition stats'),
                      'type'     => MENU_CALLBACK,
     );
-    $items[] = array('title'    => t('trending'),
+    $items[] = array('title'    => t('Trending'),
                      'path'     => 'admin/petition/trending', 
                      'callback' => 'petition_results_trending',
                      'access'   => user_access('access petition stats'),
                      'type'     => MENU_CALLBACK,
     );
+    $items[] = array('title' => t('Petition settings'),
+                     'path' => 'admin/settings/petition',
+                     'callback' => 'drupal_get_form',
+                     'callback arguments' => array('petition_admin_settings'),
+                     'description' => t('Configure petition settings.'),
+                     'access' => user_access('administer site configuration'),
+    );
   }
 
   return $items;
@@ -94,7 +100,13 @@ function petition_menu($may_cache) {
  * Implementation of hook_node_info().
  */
 function petition_node_info() {
-  return array('petition' => array('name' => t('petition'), 'base' => 'petition'));
+  return array(
+    'petition' => array(
+      'name' => t('Petition'), 
+      'module' => 'petition',
+      'description' => t('An online petition that users can sign.'),
+    )
+  );
 }
 
 /**
@@ -178,8 +190,8 @@ function petition_form(&$node) {
   // emails enabled, so allow message customization.
   if (variable_get('petition_default_autorespond', 1)) {
     $profile_fields = _petition_adjust_fields(crm_uf_get_profile_fields(variable_get('petition_profile', 1)));
-    $wildcards = array('%petition_title'); // we'll use the node->title here when it comes time.
-    foreach ($profile_fields as $key => $value) { $wildcards[] = "%$key"; }
+    $wildcards = array('@petition_title'); // we'll use the node->title here when it comes time.
+    foreach ($profile_fields as $key => $value) { $wildcards[] = "@$key"; }
     $wildcards = implode(', ', $wildcards); // for #description display.
 
     $form['defaultibles']['autorespond_from'] = array(
@@ -244,8 +256,35 @@ function petition_load($node) {
  * Implementation of hook_view().
  */
 function petition_view(&$node, $teaser = FALSE, $page = FALSE) {
+  global $user;
+  $signed = FALSE;
   civicrm_initialize(TRUE);
+ 
+  if ($user->uid) {
+    $contact = crm_get_contact(array('email' => $user->mail));
+    $signatory = db_result(db_query("SELECT cid FROM {petition_signatories} WHERE pid = %d AND cid = %d", $node->nid, $contact->contact_id));
+    if (!empty($signatory)) {
+      $signed = TRUE;
+    }
+  }
 
+  $node = node_prepare($node, $teaser);
+  if ($signed) {
+    $node->petition_signup_form = t('You have already signed this petition.');
+  }
+  else {
+    $node->petition_signup_form = drupal_get_form('petition_signup_form_data', $node);
+  }
+  $node->appeal = check_markup($node->appeal, $node->format, FALSE);
+  $node->personal = $node->personal ? check_markup($node->personal, $node->format, FALSE) : NULL;
+  $node->content['body'] = array(
+    '#value' => theme('petition_view', $node),
+    '#weight' => 0,
+  );
+  return $node;
+}
+
+function petition_signup_form_data($node) {
   // generate the signup form. forms only support textfields.
   $profile_id = variable_get('petition_profile', 1);
   $fields     = crm_uf_get_profile_fields($profile_id, true);
@@ -270,18 +309,48 @@ function petition_view(&$node, $teaser =
       );
     }
   }
+  if (is_null($form['email'])) {
+    $form['email'] = array(
+      '#type'     => 'textfield',
+      '#title'    => t('Email'),
+      '#required' => TRUE,
+    );
+  }
   $form['comment'] = array(
     '#type'     => 'textarea',
     '#title'    => t('Your comment to leaders and the press')
   );
   $form['pid']     = array( '#type' => 'hidden', '#value' => $node->nid, );
   $form['submit']  = array( '#type' => 'submit', '#value' => t('Submit'), );
-  $node->petition_signup_form = drupal_get_form('petition_signup_form_data', $form);
-  $node->body = check_markup($node->body, $node->format, FALSE);
-  $node->teaser = check_markup($node->teaser, $node->format, FALSE);
-  $node->appeal = check_markup($node->appeal, $node->format, FALSE);
-  $node->personal = $node->personal ? check_markup($node->personal, $node->format, FALSE) : NULL;
-  $node->body = theme('petition_view', $node);
+  return $form;
+}
+
+/*
+ * Validate petition signup.
+ */
+function petition_signup_form_data_validate($form_id, $form_values) {
+  global $user;
+
+  if (!$user->uid) {
+    // Don't allow anonymous users to sign if the email has an associated Drupal account.
+    $email = db_result(db_query("SELECT mail FROM {users} WHERE mail = '%s'", $form_values['email']));
+    if (!empty($email)) {
+      form_set_error('email', 'There is a Drupal account associated with that email address.  Please '. l(t('log in'), 'user/login', array(), 'destination=node/'. $form_values['pid']) .' to sign the petition.'); 
+    }
+  
+    // Don't allow anonymous users to sign the petition twice with the same email address.
+    civicrm_initialize(TRUE);
+    $crm_fields    = array('email' => $form_values['email']);
+    $contact_check = crm_contact_search($crm_fields, array('email'));
+    if (count($contact_check[0]) > 0) {
+      $cid = array_shift($contact_check[0]);
+      $cid = $cid['contact_id'];
+    }
+    $signatory = db_result(db_query("SELECT cid FROM {petition_signatories} WHERE pid = %d AND cid = %d", $form_values['pid'], $cid));
+    if (!empty($signatory)) {
+      form_set_error('email', 'Someone has already signed this petition with that email address.');
+    }
+  }
 }
 
 /**
@@ -352,15 +421,15 @@ function petition_signup_form_data_submi
   if (variable_get('petition_default_autorespond', 1) && $form_values['email']) { 
     $wildcards = _petition_adjust_fields(crm_uf_get_profile_fields(variable_get('petition_profile', 1)));
     foreach ($wildcards as $key => $field) {
-      $wildcards["%$key"] = $form_values[$key];
+      $wildcards["@$key"] = $form_values[$key];
       unset($wildcards[$key]);
     }
-    $wildcards['%petition_title'] = $node->title;
+    $wildcards['@petition_title'] = $node->title;
     $from    = $node->autorespond_from ? $node->autorespond_from : variable_get('site_mail', ini_get('sendmail_from'));
     $subject = $node->autorespond_subject ? $node->autorespond_subject : variable_get('petition_default_autorespond_subject', t('Thanks for signing %petition_title'));
     $message = $node->autorespond_message ? $node->autorespond_message : variable_get('petition_default_autorespond_message', t('Thanks for signing this petition, %first_name!'));
     $headers = "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from";
-    user_mail($form_values['email'], t($subject, $wildcards), t($message, $wildcards), $headers);
+    drupal_mail('petition_mail', $form_values['email'], t($subject, $wildcards), t($message, $wildcards), $headers);
   }
 
   // and send 'em along home, danno.
@@ -383,7 +452,7 @@ function petition_register_signatory($pi
  * Displays the entire petition to the user.
  */
 function theme_petition_view($node) {
-  theme_add_style(drupal_get_path('module', 'petition').'/petition.css');
+  drupal_add_css(drupal_get_path('module', 'petition') .'/petition.css');
   $output  = '<div id="petition">';
 
   if ($node->statistics) {
@@ -438,7 +507,7 @@ function theme_petition_overview($node) 
 /**
  * Implementation of hook_settings().
  */
-function petition_settings() {
+function petition_admin_settings() {
   civicrm_initialize(true);
 
   $form['petition'] = array(
@@ -461,20 +530,20 @@ function petition_settings() {
 
   // wildcards, like %thing, can be used to customize the email autoresponse.
   $profile_fields = _petition_adjust_fields(crm_uf_get_profile_fields(variable_get('petition_profile', 1)));
-  $wildcards = array('%petition_title'); // we'll use the node->title here when it comes time.
-  foreach ($profile_fields as $key => $value) { $wildcards[] = "%$key"; }
+  $wildcards = array('@petition_title'); // we'll use the node->title here when it comes time.
+  foreach ($profile_fields as $key => $value) { $wildcards[] = "@$key"; }
   $wildcards = implode(', ', $wildcards); // for #description display.
 
   $form['petition']['petition_default_autorespond_subject'] = array(
     '#type'          => 'textfield',
     '#title'         => t('Default Email Auto-Respond Subject'),
-    '#default_value' => variable_get('petition_default_autorespond_subject', t('Thanks for signing %petition_title')),
+    '#default_value' => variable_get('petition_default_autorespond_subject', t('Thanks for signing @petition_title')),
     '#description'   => t('Subject of auto-response email message to be sent. You may include the following wildcards: %wild.', array('%wild' => $wildcards)),
   );
   $form['petition']['petition_default_autorespond_message'] = array(
     '#type'          => 'textarea',
     '#title'         => t('Default Email Auto-Respond Message'),
-    '#default_value' => variable_get('petition_default_autorespond_message', t('Thanks for signing this petition, %first_name!')),
+    '#default_value' => variable_get('petition_default_autorespond_message', t('Thanks for signing this petition, @first_name!')),
     '#description'   => t('Body of auto-response email message to be sent. You may include the following wildcards: %wild.', array('%wild' => $wildcards)),
   );
 
@@ -522,7 +591,7 @@ function petition_settings() {
     '#description'   => t('Select the CiviCRM profile for petition signers to use.'),
   );
 
-  return $form;
+  return system_settings_form($form);
 }
 
 /**
@@ -642,7 +711,7 @@ function petition_results_trending($pid 
  *
  */
 function theme_petition_trend($trend = 'pos', $data) {
-  theme_add_style(drupal_get_path('module', 'petition').'/petition.css');
+  drupal_add_css(drupal_get_path('module', 'petition') .'/petition.css');
   $output = ($trend == 'pos')
     ? '<span class="trend_positive">'
     : '<span class="trend_negative">';
