From 4055598054d778916963e9ca4d7f43d018dfbd52 Mon Sep 17 00:00:00 2001 From: Saurabh Batra Date: Fri, 4 Mar 2016 13:51:09 +0530 Subject: [PATCH] Fix date range validation for Membership search. --- CRM/Member/BAO/Query.php | 42 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/CRM/Member/BAO/Query.php b/CRM/Member/BAO/Query.php index 970a14b485..9202fefd06 100644 --- a/CRM/Member/BAO/Query.php +++ b/CRM/Member/BAO/Query.php @@ -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; + } } -- 2.25.1