Fix date range validation for Events
authorSaurabh Batra <saurabh.batra96@gmail.com>
Fri, 4 Mar 2016 08:33:37 +0000 (14:03 +0530)
committerSaurabh Batra <saurabh.batra96@gmail.com>
Fri, 4 Mar 2016 08:33:37 +0000 (14:03 +0530)
CRM/Event/BAO/Query.php

index cddba764ad964bd9befbda984cdccfa88f506d3d..457652a2ae451a70bba6ffe76784183629e243b0 100644 (file)
@@ -611,6 +611,10 @@ class CRM_Event_BAO_Query {
 
     CRM_Core_Form_Date::buildDateRange($form, 'participant', 1, '_register_date_low', '_register_date_high', ts('From'), FALSE);
 
+    $form->addElement('hidden', 'event_date_range_error');
+    $form->addElement('hidden', 'participant_date_range_error');
+    $form->addFormRule(array('CRM_Event_BAO_Query', 'formRule'), $form);
+
     $form->addElement('checkbox', "event_include_repeating_events", NULL, ts('Include participants from all events in the %1 series', array(1 => '<em>%1</em>')));
 
     $form->addSelect('participant_status_id',
@@ -677,4 +681,35 @@ class CRM_Event_BAO_Query {
     }
   }
 
+  /**
+   * 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['event_start_date_low']) || empty($fields['event_end_date_high'])) && (empty($fields['participant_register_date_low']) || empty($fields['participant_register_date_high']))) {
+      return TRUE;
+    }
+    $lowDate = strtotime($fields['event_start_date_low']);
+    $highDate = strtotime($fields['event_end_date_high']);
+
+    if ($lowDate > $highDate) {
+      $errors['event_date_range_error'] = ts('Please check that your Event Date Range is in correct chronological order.');
+    }
+
+    $lowDate1 = strtotime($fields['participant_register_date_low']);
+    $highDate1 = strtotime($fields['participant_register_date_high']);
+
+    if ($lowDate1 > $highDate1) {
+      $errors['participant_date_range_error'] = ts('Please check that your Registration Date Range is in correct chronological order.');
+    }
+    return empty($errors) ? TRUE : $errors;
+  }
+
 }