Add layer of abstraction
authorSaurabh Batra <saurabh.batra96@gmail.com>
Mon, 7 Mar 2016 10:53:38 +0000 (16:23 +0530)
committerSaurabh Batra <saurabh.batra96@gmail.com>
Mon, 7 Mar 2016 21:11:55 +0000 (02:41 +0530)
CRM/Activity/BAO/Query.php
CRM/Case/BAO/Query.php
CRM/Contribute/BAO/Query.php
CRM/Event/BAO/Query.php
CRM/Mailing/BAO/Query.php
CRM/Member/BAO/Query.php
CRM/Utils/Rule.php

index 30fde6d7fdea5b9e7d2651c2146c2d3001d12000..20677e49770a7fd1186e3b3a594bf17251bfdd16 100644 (file)
@@ -559,7 +559,7 @@ class CRM_Activity_BAO_Query {
   }
 
   /**
-   * Check if the values in the date range are in correct chronological order.
+   * Custom form rules.
    *
    * @param array $fields
    * @param array $files
@@ -573,12 +573,9 @@ class CRM_Activity_BAO_Query {
     if (empty($fields['activity_date_low']) || empty($fields['activity_date_high'])) {
       return TRUE;
     }
-    $lowDate = strtotime($fields['activity_date_low']);
-    $highDate = strtotime($fields['activity_date_high']);
 
-    if ($lowDate > $highDate) {
-      $errors['activity_date_range_error'] = ts('Please check that your Activity Date Range is in correct chronological order.');
-    }
+    CRM_Utils_Rule::validDateRange($fields, 'activity_date', $errors, ts('Activity Date'));
+
     return empty($errors) ? TRUE : $errors;
   }
 
index 51f1831316a0059c0a45d1e15051a0ffb3e94b35..f55b737d8e497adfa81ec7b00c20c8bb6a39bf0c 100644 (file)
@@ -688,8 +688,8 @@ case_relation_type.id = case_relationship.relationship_type_id )";
 
     CRM_Core_Form_Date::buildDateRange($form, 'case_from', 1, '_start_date_low', '_start_date_high', ts('From'), FALSE);
     CRM_Core_Form_Date::buildDateRange($form, 'case_to', 1, '_end_date_low', '_end_date_high', ts('From'), FALSE);
-    $form->addElement('hidden', 'case_from_date_range_error');
-    $form->addElement('hidden', 'case_to_date_range_error');
+    $form->addElement('hidden', 'case_from_start_date_range_error');
+    $form->addElement('hidden', 'case_to_end_date_range_error');
     $form->addFormRule(array('CRM_Case_BAO_Query', 'formRule'), $form);
 
     $form->assign('validCiviCase', TRUE);
@@ -742,7 +742,7 @@ case_relation_type.id = case_relationship.relationship_type_id )";
   }
 
   /**
-   * Check if the values in the date range are in correct chronological order.
+   * Custom form rules.
    *
    * @param array $fields
    * @param array $files
@@ -756,19 +756,10 @@ case_relation_type.id = case_relationship.relationship_type_id )";
     if ((empty($fields['case_from_start_date_low']) || empty($fields['case_from_start_date_high'])) && (empty($fields['case_to_end_date_low']) || empty($fields['case_to_end_date_high']))) {
       return TRUE;
     }
-    $lowDate = strtotime($fields['case_from_start_date_low']);
-    $highDate = strtotime($fields['case_from_start_date_high']);
 
-    if ($lowDate > $highDate) {
-      $errors['case_from_date_range_error'] = ts('Please check that your Case Start Date Range is in correct chronological order.');
-    }
-
-    $lowDate1 = strtotime($fields['case_to_end_date_low']);
-    $highDate1 = strtotime($fields['case_to_end_date_high']);
+    CRM_Utils_Rule::validDateRange($fields, 'case_from_start_date', $errors, ts('Case Start Date'));
+    CRM_Utils_Rule::validDateRange($fields, 'case_to_end_date', $errors, ts('Case End Date'));
 
-    if ($lowDate1 > $highDate1) {
-      $errors['case_to_date_range_error'] = ts('Please check that your Case End Date Range is in correct chronological order.');
-    }
     return empty($errors) ? TRUE : $errors;
   }
 
index ab886b8877eb6c0f36a2328eddc4e99bfbce9569..f26d00dc4e7e2dfa431ffc4e0d71e17562b28c5e 100644 (file)
@@ -1202,7 +1202,7 @@ class CRM_Contribute_BAO_Query {
   }
 
   /**
-   * Check if the values in the date range are in correct chronological order.
+   * Custom form rules.
    *
    * @param array $fields
    * @param array $files
@@ -1216,12 +1216,9 @@ class CRM_Contribute_BAO_Query {
     if (empty($fields['contribution_date_high']) || empty($fields['contribution_date_low'])) {
       return TRUE;
     }
-    $lowDate = strtotime($fields['contribution_date_low']);
-    $highDate = strtotime($fields['contribution_date_high']);
 
-    if ($lowDate > $highDate) {
-      $errors['contribution_date_range_error'] = ts('Please check that your Date Range is in correct chronological order.');
-    }
+    CRM_Utils_Rule::validDateRange($fields, 'contribution_date', $errors, ts('Date Received'));
+
     return empty($errors) ? TRUE : $errors;
   }
 
index 62b92014b879347dcd537b3874c5033f23c347a6..4905cf56e4c58fa8a636ce24b287a3e8b8ac780f 100644 (file)
@@ -684,6 +684,8 @@ class CRM_Event_BAO_Query {
   /**
    * Check if the values in the date range are in correct chronological order.
    *
+   * @todo Get this to work with CRM_Utils_Rule::validDateRange
+   *
    * @param array $fields
    * @param array $files
    * @param CRM_Core_Form $form
index a0f4251bbd6041dc17fed8e63dd50a9203e69102..56e1df0f62a2779b10012e8a6e66178f8ec5105b 100644 (file)
@@ -511,12 +511,9 @@ class CRM_Mailing_BAO_Query {
     if (empty($fields['mailing_date_high']) || empty($fields['mailing_date_low'])) {
       return TRUE;
     }
-    $lowDate = strtotime($fields['mailing_date_low']);
-    $highDate = strtotime($fields['mailing_date_high']);
 
-    if ($lowDate > $highDate) {
-      $errors['mailing_date_range_error'] = ts('Please check that your Date Range is in correct chronological order.');
-    }
+    CRM_Utils_Rule::validDateRange($fields, 'mailing_date', $errors, ts('Mailing Date'));
+
     return empty($errors) ? TRUE : $errors;
   }
 
index 6505aec009beff0bc0b07201971f1fe057da85e8..985d731026209b6623216439c04be193b8d2cb66 100644 (file)
@@ -489,8 +489,7 @@ class CRM_Member_BAO_Query {
   }
 
   /**
-   * Check if the values in the date range are in correct chronological order.
-   * @todo Find a better way to implement this validation when multiple date ranges are present.
+   * Custom form rules.
    *
    * @param array $fields
    * @param array $files
@@ -504,24 +503,10 @@ class CRM_Member_BAO_Query {
     if ((empty($fields['member_join_date_low']) || empty($fields['member_join_date_high'])) && (empty($fields['member_start_date_low']) || empty($fields['member_start_date_high'])) && (empty($fields['member_end_date_low']) || empty($fields['member_end_date_high']))) {
       return TRUE;
     }
-    $lowDate = strtotime($fields['member_join_date_low']);
-    $highDate = strtotime($fields['member_join_date_high']);
 
-    if ($lowDate > $highDate) {
-      $errors['member_join_date_range_error'] = ts('Please check that your Member Since Date Range is in correct chronological order.');
-    }
-
-    $lowDate1 = strtotime($fields['member_start_date_low']);
-    $highDate1 = strtotime($fields['member_start_date_high']);
-    if ($lowDate1 > $highDate1) {
-      $errors['member_start_date_range_error'] = ts('Please check that your Start Date Range is in correct chronological order.');
-    }
-
-    $lowDate2 = strtotime($fields['member_end_date_low']);
-    $highDate2 = strtotime($fields['member_end_date_high']);
-    if ($lowDate2 > $highDate2) {
-      $errors['member_end_date_range_error'] = ts('Please check that your End Date Range is in correct chronological order.');
-    }
+    CRM_Utils_Rule::validDateRange($fields, 'member_join_date', $errors, ts('Member Since'));
+    CRM_Utils_Rule::validDateRange($fields, 'member_start_date', $errors, ts('Start Date'));
+    CRM_Utils_Rule::validDateRange($fields, 'member_end_date', $errors, ts('End Date'));
 
     return empty($errors) ? TRUE : $errors;
   }
index c3b483b9218ee0687ae2cc5088b639c7098170a4..cc7dff3e1ede1b0f01e409590d2c2519a0c24a39 100644 (file)
@@ -812,4 +812,25 @@ class CRM_Utils_Rule {
     return ($key) ? CRM_Core_Key::valid($key) : FALSE;
   }
 
+  /**
+   * Check if the values in the date range are in correct chronological order.
+   *
+   * @param array $fields
+   *  Fields of the form.
+   * @param $fieldName
+   *  Name of date range field.
+   * @param $errors
+   *  The error array.
+   * @param $title
+   *  Title of the date range to be displayed in the error message.
+   */
+  public static function validDateRange($fields, $fieldName, &$errors, $title) {
+    $lowDate = strtotime($fields[$fieldName . '_low']);
+    $highDate = strtotime($fields[$fieldName . '_high']);
+
+    if ($lowDate > $highDate) {
+      $errors[$fieldName . '_range_error'] = ts('%1: Please check that your date range is in correct chronological order.', array(1 => $title));
+    }
+  }
+
 }