Fix date range search for Activity
authorSaurabh Batra <saurabh.batra96@gmail.com>
Fri, 4 Mar 2016 08:43:02 +0000 (14:13 +0530)
committerSaurabh Batra <saurabh.batra96@gmail.com>
Fri, 4 Mar 2016 08:43:02 +0000 (14:13 +0530)
CRM/Activity/BAO/Query.php

index 2f241f9a48f3611c7326d3a2be4d07aa63667fcc..52c98c257d62218db376135cee166a5639df33fc 100644 (file)
@@ -415,6 +415,9 @@ class CRM_Activity_BAO_Query {
     );
 
     CRM_Core_Form_Date::buildDateRange($form, 'activity_date', 1, '_low', '_high', ts('From'), FALSE, FALSE);
+    $form->addElement('hidden', 'activity_date_range_error');
+    $form->addFormRule(array('CRM_Activity_BAO_Query', 'formRule'), $form);
+
     $followUpActivity = array(
       1 => ts('Yes'),
       2 => ts('No'),
@@ -555,4 +558,27 @@ class CRM_Activity_BAO_Query {
     return $properties;
   }
 
+  /**
+   * Check if the values in the date range are in correct chronological order.
+   *
+   * @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['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.');
+    }
+    return empty($errors) ? TRUE : $errors;
+  }
 }