From 2adf5d9a1da1591fba2de2a78fea1398f28df9e1 Mon Sep 17 00:00:00 2001
From: Victor Kareh <vkareh@vkareh.net>
Date: Wed, 2 Mar 2011 17:07:17 -0500
Subject: [PATCH] Issue #710266: Node limit per taxonomy term.

---
 node_limitnumber.module |   92 +++++++++++++++++++++++++++++++++-------------
 1 files changed, 66 insertions(+), 26 deletions(-)

diff --git a/node_limitnumber.module b/node_limitnumber.module
index e92f66f..d017d04 100644
--- a/node_limitnumber.module
+++ b/node_limitnumber.module
@@ -91,31 +91,44 @@ function node_limitnumber_rules_condition_info() {
 /**
  * Helper function to check limit
  */
-function _node_limitnumber_check_limit($time, $node, $user, $limit) {
-  switch ($time) {
-    case 'none':
-      $q = "SELECT nid FROM {node} WHERE type = '%s' AND uid = %d";
-      $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";
-      $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";
-      $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";
-      $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";
-      $year = strtotime(substr(date('Y-m-d'), 0, 5) .'01-01');
-      $result = db_query($q, $node->type, $user->uid, $year);
+function _node_limitnumber_check_limit($time, $node, $user, $limit, $taxonomy_term = FALSE) {
+  $query = "SELECT n.nid FROM {node} n";
+  if ($taxonomy_term) {
+    $query .= " INNER JOIN {term_node} t ON n.nid = t.nid WHERE n.type = '%s' AND n.uid = %d AND t.tid = %d";
+  }
+  else {
+    $query .= " WHERE n.type = '%s' AND n.uid = %d";
+  }
+  if ($time == 'none') {
+    if ($taxonomy_term) {
+      db_query($query, $node->type, $user->uid, $taxonomy_term);
+    }
+    else {
+      db_query($query, $node->type, $user->uid);
+    }
+  }
+  else {
+    $query .= " AND n.created > %d";
+    switch ($time) {
+      case 'daily':
+        $period = strtotime(date('Y-m-d'));
+        break;
+      case 'weekly':
+        $period = time() - 604800;
+        break;
+      case 'monthly':
+        $period = strtotime(substr(date('Y-m-d'), 0, 8) .'01-01');
+        break;
+      case 'annually':
+        $period = strtotime(substr(date('Y-m-d'), 0, 5) .'01-01');
+        break;
+    }
+    if ($taxonomy_term) {
+      $result = db_query($query, $node->type, $user->uid, $taxonomy_term, $period);
+    }
+    else {
+      $result = db_query($query, $node->type, $user->uid, $period);
+    }
   }
   $num = db_affected_rows();
   if ($num >= $limit) {
@@ -151,6 +164,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 +179,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

