From 219829ec87832a4910acda6d276b049405935da8 Mon Sep 17 00:00:00 2001
From: Victor Kareh <vkareh@vkareh.net>
Date: Sat, 5 Mar 2011 00:39:54 -0500
Subject: [PATCH] #710266: Cleaned up the query generation.

---
 node_limitnumber.module |   49 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 42 insertions(+), 7 deletions(-)

diff --git a/node_limitnumber.module b/node_limitnumber.module
index e92f66f..ca160df 100644
--- a/node_limitnumber.module
+++ b/node_limitnumber.module
@@ -91,31 +91,39 @@ function node_limitnumber_rules_condition_info() {
 /**
  * Helper function to check limit
  */
-function _node_limitnumber_check_limit($time, $node, $user, $limit) {
+function _node_limitnumber_check_limit($time, $node, $user, $limit, $taxonomy_term = FALSE) {
+  $select = "SELECT n.nid";
+  $from = "FROM {node} n";
+  $where = "WHERE n.type = '%s' AND n.uid = %d";
+  if ($taxonomy_term) {
+    $from .= " INNER JOIN {term_node} t ON n.nid = t.nid";
+    $where .= " AND t.tid = " . db_escape_string($taxonomy_term);
+  }
   switch ($time) {
     case 'none':
-      $q = "SELECT nid FROM {node} WHERE type = '%s' AND uid = %d";
+      $q = "$select $from $where";
       $result = db_query($q, $node->type, $user->uid);
       break;
     case 'daily':
-      $q = "SELECT nid FROM {node} WHERE type = '%s' AND uid = %d AND created > %d";
+      $q = "$select $from $where AND n.created > %d";
       $day = strtotime(date('Y-m-d'));
       $result = db_query($q, $node->type, $user->uid, $day);
       break;
     case 'weekly':
-      $q = "SELECT nid FROM {node} WHERE type = '%s' AND uid = %d AND created > %d";
+      $q = "$select $from $where AND n.created > %d";
       $week = time() - 604800;
       $result = db_query($q, $node->type, $user->uid, $week);
       break;
     case 'monthly':
-      $q = "SELECT nid FROM {node} WHERE type = '%s' AND uid = %d AND created > %d";
+      $q = "$select $from $where AND n.created > %d";
       $month = strtotime(substr(date('Y-m-d'), 0, 8) .'01-01');
       $result = db_query($q, $node->type, $user->uid, $month);
       break;
     case 'annually':
-      $q = "SELECT nid FROM {node} WHERE type = '%s' AND uid = %d AND created > %d";
+      $q = "$select $from $where AND n.created > %d";
       $year = strtotime(substr(date('Y-m-d'), 0, 5) .'01-01');
       $result = db_query($q, $node->type, $user->uid, $year);
+      break;
   }
   $num = db_affected_rows();
   if ($num >= $limit) {
@@ -151,6 +159,14 @@ function node_limitnumber_rules_condition_limit($node, $user, $settings) {
       return FALSE;
     }
   }
+  if (module_exists('taxonomy') && intval($settings['taxonomy_term']) > 0) {
+    if (is_array($node->taxonomy) && array_search($settings['taxonomy_term'], $node->taxonomy)) {
+      return _node_limitnumber_check_limit($settings['time'], $node, $user, $settings['limit'], $settings['taxonomy_term']);
+    }
+    else {
+      return FALSE;
+    }
+  }
   // Continue checking as a simple limit since the OG check is over
   return _node_limitnumber_check_limit($settings['time'], $node, $user, $settings['limit']);
 }
@@ -158,7 +174,26 @@ function node_limitnumber_rules_condition_limit($node, $user, $settings) {
  * Condition set the limit configuration form.
  */
 function node_limitnumber_rules_condition_limit_form($settings, &$form) {
-  $settings += array('limit' => '', 'time' => '', 'og' => 'none');
+  $settings += array('taxonomy_term' => '', 'limit' => '', 'time' => '', 'og' => 'none');
+  if (module_exists('taxonomy')) {
+    $vocabularies = taxonomy_get_vocabularies();
+    $options = array(0 => t('- Any -'));
+    foreach ($vocabularies as $vocabulary) {
+      $terms = taxonomy_get_tree($vocabulary->vid);
+      if (is_array($terms)) {
+        foreach ($terms as $term) {
+          $options[$vocabulary->name][$term->tid] = $term->name;
+        }
+      }
+    }
+    $form['settings']['taxonomy_term'] = array(
+      '#type' => 'select',
+      '#title' => t('Taxonomy Term'),
+      '#default_value' => $settings['taxonomy_term'],
+      '#description' => t('Select the taxonomy term for this rule'),
+      '#options' => $options,
+    );
+  }
   $form['settings']['limit'] = array(
     '#type' => 'textfield',
     '#title' => t('Node Limit'),
-- 
1.7.1

