Fix date range validation for Membership search.
authorSaurabh Batra <saurabh.batra96@gmail.com>
Fri, 4 Mar 2016 08:21:09 +0000 (13:51 +0530)
committerSaurabh Batra <saurabh.batra96@gmail.com>
Fri, 4 Mar 2016 08:21:09 +0000 (13:51 +0530)
CRM/Member/BAO/Query.php

index 970a14b485d5eb93e9ff5f1f58cef81c313e860a..9202fefd06ffb2b6a79411725f0e0a3e84e50578 100644 (file)
@@ -432,10 +432,15 @@ class CRM_Member_BAO_Query {
     $form->addElement('text', 'member_source', ts('Source'));
 
     CRM_Core_Form_Date::buildDateRange($form, 'member_join_date', 1, '_low', '_high', ts('From'), FALSE);
+    $form->addElement('hidden', 'member_join_date_range_error');
 
     CRM_Core_Form_Date::buildDateRange($form, 'member_start_date', 1, '_low', '_high', ts('From'), FALSE);
+    $form->addElement('hidden', 'member_start_date_range_error');
 
     CRM_Core_Form_Date::buildDateRange($form, 'member_end_date', 1, '_low', '_high', ts('From'), FALSE);
+    $form->addElement('hidden', 'member_end_date_range_error');
+
+    $form->addFormRule(array('CRM_Member_BAO_Query', 'formRule'), $form);
 
     $form->addYesNo('member_is_primary', ts('Primary Member?'), TRUE);
     $form->addYesNo('member_pay_later', ts('Pay Later?'), TRUE);
@@ -483,4 +488,41 @@ 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.
+   *
+   * @param array $fields
+   * @param array $files
+   * @param CRM_Core_Form $form
+   *
+   * @return bool|array
+   */
+  public static function formRule($fields, $files, $form) {
+    $errors = array();
+
+    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.');
+    }
+
+    return empty($errors) ? TRUE : $errors;
+  }
 }