? flag_abuse-reset.patch
Index: flag_abuse.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flag_abuse/flag_abuse.module,v
retrieving revision 1.1.2.1
diff -u -p -r1.1.2.1 flag_abuse.module
--- flag_abuse.module	12 May 2009 00:35:49 -0000	1.1.2.1
+++ flag_abuse.module	8 Sep 2009 01:50:15 -0000
@@ -39,14 +39,18 @@ function flag_abuse_perm() {
 function flag_abuse_preprocess_flag(&$vars) {
   global $user;
   
-  // permmission check instead of a role
+  // permission check
   if (user_access('reset abuse flags', $user)) {
     // is this one of our abuse flags
     // @todo: should be dynamic
     if (in_array($vars['flag']->name, array('abuse_node', 'abuse_comment', 'abuse_user'))) {
-      $vars['action'] = 'reset';
-      $vars['link_text'] = t('Reset flags');
-      $vars['link_title'] = t('Remove all flags on this content');
+      // is this content even flagged?
+      $count = flag_get_counts($vars['flag']->content_type, $vars['content_id']);
+      if (!empty($count) && $count[$vars['flag']->name] > 0) {
+        $vars['link_href'] = str_replace('unflag', 'reset', $vars['link_href']);
+        $vars['link_text'] = t('Reset flags(!count)', array('!count' => $count[$vars['flag']->name]));
+        $vars['link_title'] = t('Remove all flags on this content');
+      }
     }
   }
 }
@@ -54,23 +58,27 @@ function flag_abuse_preprocess_flag(&$va
 /**
  * Implementation of hook_flag().
  *
- * If a user with appropriate permission/role flags this content from our view
+ * If a user with appropriate permissions flags this content from our view
  * we want to remove all flags. http://drupal.org/node/327901#comment-1085685
  *
+ * Note that since this runs _after_ the actual flagging has already happened,
+ * we must somehow veryify that this is a reset and not a flag/unflag. This
+ * would be possible if http://drupal.org/node/571100 goes in.
+ *
  * @todo: When $flag->access() goes in, use this to limit access to a flag that
  * an administrator has already acted upon. http://drupal.org/node/322034
  */
 function flag_abuse_flag($event, $flag, $content_id, $account) {
-  // permmission check instead of a role
-  if (user_access('reset abuse flags', $account)) {
-    // is this one of our abuse flags
-    // @todo: should be dynamic
-    if (in_array($flag->name, array('abuse_node', 'abuse_comment', 'abuse_user'))) {
-      // remove all flag on this content
-      $query = db_query("SELECT uid FROM {flag_content} WHERE fid = %d", $flag->fid);
-      while ($result = db_fetch_object($query)) {
-        // Supposed to pass in a full $account here, let's see if we can fake it
-        $flag->flag('unflag', $content_id, $result, TRUE);
+  if ($event == 'reset') {
+    // permission check, name check
+    if (user_access('reset abuse flags', $account) && in_array($flag->name, array('abuse_node', 'abuse_comment', 'abuse_user'))) {
+      // make sure there are flags to be reset
+      $count = flag_get_counts($flag->content_type, $content_id);
+      if (!empty($count) && $count[$flag->name] > 0) {
+        // remove all flags on this content
+        db_query('DELETE FROM {flag_content} WHERE content_id = %d', $content_id);
+        // reset the count
+        db_query('UPDATE {flag_counts} SET count = 0 WHERE content_id = %d', $content_id);
       }
     }
   }
