From 79326ee2bc966665b74b4e6777164b32435edb11 Mon Sep 17 00:00:00 2001 From: Saurabh Batra Date: Mon, 7 Mar 2016 16:23:38 +0530 Subject: [PATCH] Add layer of abstraction --- CRM/Activity/BAO/Query.php | 9 +++------ CRM/Case/BAO/Query.php | 19 +++++-------------- CRM/Contribute/BAO/Query.php | 9 +++------ CRM/Event/BAO/Query.php | 2 ++ CRM/Mailing/BAO/Query.php | 7 ++----- CRM/Member/BAO/Query.php | 23 ++++------------------- CRM/Utils/Rule.php | 21 +++++++++++++++++++++ 7 files changed, 40 insertions(+), 50 deletions(-) diff --git a/CRM/Activity/BAO/Query.php b/CRM/Activity/BAO/Query.php index 30fde6d7fd..20677e4977 100644 --- a/CRM/Activity/BAO/Query.php +++ b/CRM/Activity/BAO/Query.php @@ -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; } diff --git a/CRM/Case/BAO/Query.php b/CRM/Case/BAO/Query.php index 51f1831316..f55b737d8e 100644 --- a/CRM/Case/BAO/Query.php +++ b/CRM/Case/BAO/Query.php @@ -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; } diff --git a/CRM/Contribute/BAO/Query.php b/CRM/Contribute/BAO/Query.php index ab886b8877..f26d00dc4e 100644 --- a/CRM/Contribute/BAO/Query.php +++ b/CRM/Contribute/BAO/Query.php @@ -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; } diff --git a/CRM/Event/BAO/Query.php b/CRM/Event/BAO/Query.php index 62b92014b8..4905cf56e4 100644 --- a/CRM/Event/BAO/Query.php +++ b/CRM/Event/BAO/Query.php @@ -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 diff --git a/CRM/Mailing/BAO/Query.php b/CRM/Mailing/BAO/Query.php index a0f4251bbd..56e1df0f62 100644 --- a/CRM/Mailing/BAO/Query.php +++ b/CRM/Mailing/BAO/Query.php @@ -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; } diff --git a/CRM/Member/BAO/Query.php b/CRM/Member/BAO/Query.php index 6505aec009..985d731026 100644 --- a/CRM/Member/BAO/Query.php +++ b/CRM/Member/BAO/Query.php @@ -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; } diff --git a/CRM/Utils/Rule.php b/CRM/Utils/Rule.php index c3b483b921..cc7dff3e1e 100644 --- a/CRM/Utils/Rule.php +++ b/CRM/Utils/Rule.php @@ -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)); + } + } + } -- 2.25.1