? signature_forum-474802-1.patch
? signature_forum-474802-2.patch
Index: signature_forum.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/signature_forum/signature_forum.install,v
retrieving revision 1.1.2.5.4.3
diff -u -p -r1.1.2.5.4.3 signature_forum.install
--- signature_forum.install	18 Nov 2008 19:49:33 -0000	1.1.2.5.4.3
+++ signature_forum.install	24 Jun 2009 19:11:05 -0000
@@ -22,9 +22,31 @@ function signature_forum_schema() {
         'type' => 'text',
         'not null' => FALSE,
         'default' => NULL),
+      'status' => array(
+        'type' => 'int',
+        'description' => 'A boolean indicator for whether or not a signature should be displayed by default: 1 means display, 0 means not displayed.',
+        'size' => 'tiny',
+        'not null' => FALSE),
     ),
     'primary key' => array('uid'),
   );
+  $schema['signature_comments'] = array(
+    'fields' => array(
+      'cid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'description' => 'The comment id from {comments}.cid',
+        'not null' => TRUE,
+        'disp-width' => '11'),
+      'status' => array(
+        'type' => 'int',
+        'description' => 'A boolean indicator for whether or not a signature should be displayed: 1 means display, 0 means not displayed.',
+        'size' => 'tiny',
+        'not null' => TRUE,
+        'disp-width' => '11'),
+    ),
+    'primary key' => array('cid'),
+  );
 
   return $schema;
 }
@@ -39,6 +61,25 @@ function signature_forum_install() {
     FROM {users}
     WHERE signature<>''");
   db_query("UPDATE {users} SET signature=''");
+  signature_forum_build_signature_status();
+}
+
+/**
+ * Set all existing comments to not have a signature to avoid duplicate signatures
+ */
+function signature_forum_build_signature_status() {
+  // deal with nodecomments and standard nodes
+  if (module_exists('nodecomment')) {
+    db_query("INSERT INTO {signature_comments} (cid, status)
+      SELECT nid, 0
+      FROM {node}"); 
+  }
+  // deal with standard Drupal comments
+  else {
+    db_query("INSERT INTO {signature_comments} (cid, status)
+      SELECT cid, 0
+      FROM {comments}");
+  }
 }
 
 /**
@@ -61,3 +102,36 @@ function signature_forum_uninstall() {
   drupal_uninstall_schema('signature_forum');
   variable_del('signature_forum_settings');
 }
+
+/**
+ * Implementation of hook_update_n()
+ */
+
+function signature_forum_update_6100() {
+  $schema['signature_comments'] = array(
+    'fields' => array(
+      'cid' => array(
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'description' => 'The comment id from {comments}.cid',
+        'not null' => TRUE,
+        'disp-width' => '11'),
+      'status' => array(
+        'type' => 'int',
+        'description' => 'A boolean indicator for whether or not a signature should be displayed: 1 means display, 0 means not displayed.',
+        'size' => 'tiny',
+        'not null' => TRUE,
+        'disp-width' => '11'),
+    ),
+    'primary key' => array('cid'),
+  );
+  $ret = array();
+  db_add_field($ret, 'users_signature', 'status', array(
+        'type' => 'int',
+        'description' => 'A boolean indicator for whether or not a signature should be displayed by default: 1 means display, 0 means not displayed.',
+        'size' => 'tiny',
+        'not null' => FALSE));
+  db_create_table($ret, 'signature_comments', $schema['signature_comments']);
+  signature_forum_build_signature_status();
+  return $ret;
+}
\ No newline at end of file
Index: signature_forum.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/signature_forum/signature_forum.module,v
retrieving revision 1.1.2.11.4.13
diff -u -p -r1.1.2.11.4.13 signature_forum.module
--- signature_forum.module	17 Dec 2008 04:52:06 -0000	1.1.2.11.4.13
+++ signature_forum.module	24 Jun 2009 19:11:05 -0000
@@ -165,7 +165,28 @@ function signature_forum_admin_settings(
     '#options' => $roles,
     '#description' => t('Members of these roles will have their signatures shown in every post.')
   );
-
+  $form['signature_defaults'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Signature Defaults'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+    '#description' => t('Users will see a checkbox below the comment form allowing them to choose whether or not to use their signature. If signatures are not enabled for a content type, this setting will have no effect.'),
+  );
+  foreach (node_get_types('names') as $type => $name) {
+    $form['signature_defaults']['signature_forum_default_for_'. $type] = array(
+      '#type' => 'checkbox',
+      '#title' => $name,
+      '#return_value' => 1,
+      '#default_value' => (isset($settings['signature_forum_default_for_'. $type]) ? $settings['signature_forum_default_for_'. $type] : TRUE),
+    );
+  }
+  $form['signature_defaults']['signature_forum_allow_user_default'] = array(
+    '#type' => 'radios',
+    '#title' => t('Allow users to choose their own global default'),
+    '#default_value' => $settings['signature_forum_allow_user_default'],
+    '#options' => array(t('No'), t('Yes')),
+    '#description' => t("This will add an extra option in a user's profile settings where they can choose whether their comment should be used by default"),
+  );
   $form['signature_other'] = array(
     '#type' => 'fieldset',
     '#title' => t('Other options'),
@@ -211,6 +232,8 @@ function signature_forum_defaults() {
     'signature_forum_min_content_length_action' => MIN_CONTENT_ACTION_DO_NOT_DISPLAY,
     'signature_forum_min_content_length_filter' => FILTER_FORMAT_DEFAULT,
     'signature_forum_show_once_options' => SHOW_ONCE_OPTIONS_ALWAYS,
+    // do not automatically allow users to choose their own default signature attachment setting
+    'signature_forum_allow_user_default' => 1,
     // Do not automatically insert signatures into nodes if Advanced Forum is
     // installed (as AF does this in the theme layer).
     'signature_forum_auto_insert' => (module_exists('advanced_forum') ? FALSE : TRUE),
@@ -232,6 +255,9 @@ function signature_forum_admin_settings_
   foreach ($form_state['values'] as $form_value_key => $form_value_value) {
     if (drupal_substr($form_value_key, 0, drupal_strlen('signature_forum_show_for_')) == 'signature_forum_show_for_') {
       $settings[$form_value_key] = $form_value_value;
+    } 
+    elseif (drupal_substr($form_value_key, 0, drupal_strlen('signature_forum_default_for_')) == 'signature_forum_default_for_') {
+      $settings[$form_value_key] = $form_value_value;
     }
   }
   if ($form_state['values']['delete_signatures']) {
@@ -262,6 +288,7 @@ function signature_forum_admin_settings_
   $settings['signature_forum_show_once_options'] = $form_state['values']['show_once_options'];
   $settings['signature_forum_show_once_roles'] = $form_state['values']['show_once_roles'];
   $settings['signature_forum_auto_insert'] = $form_state['values']['auto_insert'];
+  $settings['signature_forum_allow_user_default'] = $form_state['values']['signature_forum_allow_user_default'];
   variable_set('signature_forum_settings', $settings);
   drupal_set_message(t('The configuration options have been saved.'));
 }
@@ -280,17 +307,18 @@ function signature_forum_user($op, &$edi
       }
       // If the user has a signature set update it, otherwise create a new entry.
       if (db_result(db_query("SELECT uid FROM {users_signature} WHERE uid = %d", $account->uid)) != FALSE) {
-        db_query("UPDATE {users_signature} SET signature='%s' WHERE uid = %d", array($edit['signature'], $account->uid));
+        db_query("UPDATE {users_signature} SET signature = '%s', status = %d WHERE uid = %d", array($edit['signature'], $edit['signature_default_status'], $account->uid));
       }
       else {
-        db_query("INSERT INTO {users_signature} (uid, signature) VALUES (%d, '%s')", array($account->uid, $edit['signature']));
+        db_query("INSERT INTO {users_signature} (uid, signature, status) VALUES (%d, '%s', %d)", array($account->uid, $edit['signature'], $edit['signature_default_status']));
       }
       unset($edit['signature']);
       break;
     case 'load':
-      $signature = db_result(db_query("SELECT signature FROM {users_signature} WHERE uid = %d", $account->uid));
+      $signature = db_fetch_object(db_query("SELECT signature, status FROM {users_signature} WHERE uid = %d", $account->uid));
       // Bug #190446 OG puts $account->signature into comments
-      $account->signature_forum = $signature;
+      $account->signature_forum = $signature->signature;
+      $account->signature_default_status = $signature->status;
       break;
     case 'validate':
       $settings = variable_get('signature_forum_settings', signature_forum_defaults());
@@ -324,8 +352,44 @@ function signature_forum_form_alter(&$fo
     else {
       $account = $form['_account']['#value'];
       $form['signature_settings']['signature']['#default_value'] = $account->signature_forum;
+      if ($settings['signature_forum_allow_user_default']) {
+        $form['signature_settings']['signature_default_status'] = array(
+          '#type' => 'checkbox',
+          '#title' => t('Use signature by default'),
+          '#description' => t('Whenever you add a comment you can choose not to use your signature. Use this to turn off your signature by default.'),
+          '#default_value' => $account->signature_default_status,
+        );
+      }
     }
   }
+  // add option to disable/enable signature for this comment, if this option is available
+  elseif (((($form_id == 'forum_reply_node_form' || (isset($form['type']) && isset($form['#node']) && $form['type']['#value'] .'_node_form' == $form_id)) && $settings['signature_forum_show_for_'. $form['type']['#value']] == 1 ) || $form_id == 'comment_form')  && variable_get('user_signatures', FALSE)) {
+    global $user;
+    // get default value for whether to use signature
+    // are we editing a standard drupal comment?
+    if ($form['cid']['#value'] > 0) { 
+      $use_signature = signature_forum_get_status($form['cid']['#value']);
+    }
+    // are we editing a nodecomment? type is necessary to avoid catching drupal default comments
+    elseif ($form['nid']['#value'] > 0 && isset($form['type']['#value'])) { // type is necessary to avoid catching drupal default comments
+      $use_signature = signature_forum_get_status($form['nid']['#value']);
+    } 
+    // we're not editing, so check if the user has set a global defalut
+    elseif (isset($user->signature_default_status)) {
+      $use_signature = $user->signature_default_status;
+    }
+    // get the default for this node type
+    else {
+      // get site-wide default setting
+      $use_signature = $settings['signature_forum_default_for_' . $form['type']['#value']];
+    }
+    $form['signature_forum'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Attach signature'),
+      '#description' => t('Signature can be changed in <a href="@signature_link">account settings</a>.', array('@signature_link' => url('user/' . $user->uid . '/edit'))),
+      '#default_value' => $use_signature,      
+    );
+  }
 }
 
 /**
@@ -338,6 +402,12 @@ function signature_forum_nodeapi(&$node,
       $node->content['body']['#value'] .= signature_forum_get_signature($node);
     }
   }
+  // save signature setting for this node
+  elseif ($op == 'insert' || $op == 'update') {
+    $cid = $node->nid;
+    $status = $node->signature_forum;
+    signature_forum_save_comment_status($cid, $status);
+  }
 }
 
 /**
@@ -350,6 +420,11 @@ function signature_forum_comment(&$comme
       $comment->comment .= signature_forum_get_signature($comment);
     }
   }
+  elseif ($op == 'insert' || $op == 'update') {
+    $cid = $comment['cid'];
+    $status = $comment['signature_forum'];
+    signature_forum_save_comment_status($cid, $status);
+  }
 }
 
 /**
@@ -446,6 +521,13 @@ function signature_forum_get_signature($
     return theme('signature_forum', '');
   }
 
+  // Check if signature is disabled for this comment/node 
+  $id = $a1->cid ? $a1->cid : $a1->nid;
+  $signature_status = signature_forum_get_status($id);
+  if (!$signature_status) {
+    return theme('signature_forum', '');
+  }
+
   if (isset($cache[$a1->uid])) {
     // If the signature is in the cache it has been displayed before.
     if ($settings['signature_forum_show_once_options'] == SHOW_ONCE_OPTIONS_ALWAYS || signature_forum_user_exception($a1->uid, 'show_once')) {
@@ -594,3 +676,32 @@ function signature_forum_theme() {
     ),
   );
 }
+
+/** 
+ * Save the signature status for a comment
+ * @param $cid 
+ *  An integer containing the id of a comment or node
+ * @param $status
+ *  A Boolean for whether or not the signature should be attached to this comment or node
+ */
+
+function signature_forum_save_comment_status($cid, $status = 0) {
+  if (!$old_status = db_fetch_object(db_query("SELECT cid, status FROM {signature_comments} WHERE cid = %d", array($cid)))) {
+    db_query("INSERT INTO {signature_comments} (cid, status) VALUES (%d, %d)", array($cid, $status));
+  }
+  elseif ($old_status->status != $status) {
+    db_query("UPDATE {signature_comments} SET status = %d WHERE cid = %d", array($status, $cid));
+  }
+}
+
+/**
+ * Get the status for whether signature should be used for a particular node or comment
+ * @param $cid
+ *  An integer containing the id of a comment or node
+ * @return 
+ *  bool 0|1 
+ */
+
+function signature_forum_get_status($cid) {
+  return db_result(db_query("SELECT status from {signature_comments} WHERE cid = %d", $cid));
+}
\ No newline at end of file
