From 65f36f559f829c88a50baa9965055bf5e7d02c16 Mon Sep 17 00:00:00 2001
From: Victor Kareh <vkareh@vkareh.net>
Date: Sat, 5 Mar 2011 12:26:00 -0500
Subject: [PATCH] #348731: Add date and time comparison condition.

---
 rules/modules/rules.rules.inc       |   52 +++++++++++++++++++++++++++++++++++
 rules/modules/rules.rules_forms.inc |   31 +++++++++++++++++++++
 2 files changed, 83 insertions(+), 0 deletions(-)

diff --git a/rules/modules/rules.rules.inc b/rules/modules/rules.rules.inc
index a5c36f1..ec9049f 100644
--- a/rules/modules/rules.rules.inc
+++ b/rules/modules/rules.rules.inc
@@ -163,6 +163,14 @@ function rules_rules_condition_info() {
       ),
       'module' => 'Rules',
     ),
+    'rules_condition_date_compare' => array(
+      'label' => t('Date/Time comparison'),
+      'arguments' => array(
+        'datetime1' => array('label' => t('Date/Time 1'), 'type' => 'date'),
+        'datetime2' => array('label' => t('Date/Time 2'), 'type' => 'date'),
+      ),
+      'module' => 'Rules',
+    ),
   );
 }
 
@@ -200,6 +208,50 @@ function rules_condition_check_boolean($boolean) {
 }
 
 /**
+ * Condition implementation: Date/Time comparison.
+ */
+function rules_condition_date_compare($datetime1, $datetime2, $settings) {
+  $timestamp1 = strtotime($datetime1);
+  $timestamp2 = strtotime($datetime2);
+  switch ($settings['granularity']) {
+    case 'year':
+      $timestamp1 = strtotime(date('Y', strtotime($datetime1)));
+      $timestamp2 = strtotime(date('Y', strtotime($datetime2)));
+      break;
+    case 'month':
+      $timestamp1 = strtotime(date('Y-m', strtotime($datetime1)));
+      $timestamp2 = strtotime(date('Y-m', strtotime($datetime2)));
+      break;
+    case 'day':
+      $timestamp1 = strtotime(date('Y-m-d', strtotime($datetime1)));
+      $timestamp2 = strtotime(date('Y-m-d', strtotime($datetime2)));
+      break;
+    case 'hour':
+      $timestamp1 = strtotime(substr(date('c', strtotime($datetime1)), 0, 14) . '00:00');
+      $timestamp2 = strtotime(substr(date('c', strtotime($datetime2)), 0, 14) . '00:00');
+      break;
+    case 'minute':
+      $timestamp1 = strtotime(substr(date('c', strtotime($datetime1)), 0, 17) . '00');
+      $timestamp2 = strtotime(substr(date('c', strtotime($datetime2)), 0, 17) . '00');
+      break;
+    case 'second':
+      $timestamp1 = strtotime($datetime1);
+      $timestamp2 = strtotime($datetime2);
+      break;
+  }
+  switch ($settings['operation']) {
+    case 'before':
+      return $timestamp1 < $timestamp2;
+
+    case 'on':
+      return $timestamp1 == $timestamp2;
+
+    case 'after':
+      return $timestamp1 > $timestamp2;
+  }
+}
+
+/**
  * Implementation of hook_rules_action_info().
  */
 function rules_rules_action_info() {
diff --git a/rules/modules/rules.rules_forms.inc b/rules/modules/rules.rules_forms.inc
index 3d47ece..d910aaf 100644
--- a/rules/modules/rules.rules_forms.inc
+++ b/rules/modules/rules.rules_forms.inc
@@ -42,6 +42,37 @@ function rules_condition_check_boolean_help() {
 }
 
 /**
+ * Condition: Date/Time comparision configuration form
+ */
+function rules_condition_date_compare_form($settings = array(), &$form) {
+  $form['settings']['datetime1']['#weight'] = -2;
+  $form['settings']['datetime2']['#weight'] = 2;
+
+  $form['settings']['operation'] = array(
+    '#type' => 'select',
+    '#title' => t('Operation'),
+    '#options' => array('before' => t('Is before'), 'on' => t('Is on'), 'after' => t('Is after')),
+    '#default_value' => isset($settings['operation']) ? $settings['operation'] : 'on',
+    '#description' => t('Select the comparison operator. It will compare the dates based on the selected granularity.'),
+  );
+  $form['settings']['granularity'] = array(
+    '#type' => 'select',
+    '#title' => t('Granularity'),
+    '#options' => array(
+      'year' => t('Year'),
+      'month' => t('Month'),
+      'day' => t('Day'),
+      'hour' => t('Hour'),
+      'minute' => t('Minute'),
+      'second' => t('Second'),
+    ),
+    '#default_value' => isset($settings['granularity']) ? $settings['granularity'] : 'second',
+    '#description' => t('Select what the granularity should be for date comparison.'),
+    '#weight' => 10,
+  );
+}
+
+/**
  * Also add in the invoked rule set when exporting for features.
  */
 function rules_action_invoke_set_features_export(&$export, &$pipe, $settings, $element) {
-- 
1.7.1

