From 8d657ddef99c0cc33d03e89d871509faad8877f3 Mon Sep 17 00:00:00 2001 From: Andrew Hunt Date: Tue, 17 Jun 2014 21:05:20 -0400 Subject: [PATCH] CRM-14879: scheduled reminders - add anniversary mode and custom fields for contact ---------------------------------------- * CRM-14879: Scheduled reminders for contact date fields https://issues.civicrm.org/jira/browse/CRM-14879 --- CRM/Admin/Form/ScheduleReminders.php | 11 +++- CRM/Core/BAO/ActionSchedule.php | 77 ++++++++++++++++++++++++---- 2 files changed, 77 insertions(+), 11 deletions(-) diff --git a/CRM/Admin/Form/ScheduleReminders.php b/CRM/Admin/Form/ScheduleReminders.php index 5d971f6684..4d089cfeba 100644 --- a/CRM/Admin/Form/ScheduleReminders.php +++ b/CRM/Admin/Form/ScheduleReminders.php @@ -244,12 +244,21 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { */ static function formRule($fields) { $errors = array(); - if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] == 0) || + if ((array_key_exists(1, $fields['entity']) && $fields['entity'][1][0] === 0) || (array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] == 0) ) { $errors['entity'] = ts('Please select appropriate value'); } + if (array_key_exists(1, $fields['entity']) && !is_numeric($fields['entity'][1][0])) { + if (count($fields['entity'][1]) > 1) { + $errors['entity'] = ts('You may only select one contact field per reminder'); + } + elseif (!(array_key_exists(2, $fields['entity']) && $fields['entity'][2][0] > 0)) { + $errors['entity'] = ts('Please select whether the reminder is sent each year.'); + } + } + if (!empty($fields['is_active']) && CRM_Utils_System::isNull($fields['subject']) ) { diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 585c95cd50..e1e48608e4 100755 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -68,6 +68,21 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { return $mapping; } + /** + * Get all fields of the type Date + */ + + static function getDateFields() { + $allFields = CRM_Core_BAO_CustomField::getFields(''); + $dateFields = array('birth_date' => ts('Birth Date')); + foreach ($allFields as $fieldID => $field) { + if ($field['data_type'] == 'Date') { + $dateFields["custom_$fieldID"] = $field['label']; + } + } + return $dateFields; + } + /** * Retrieve list of selections/drop downs for Scheduled Reminder form * @@ -88,7 +103,9 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { $eventTemplate = CRM_Event_PseudoConstant::eventTemplates(); $autoRenew = CRM_Core_OptionGroup::values('auto_renew_options'); $membershipType = CRM_Member_PseudoConstant::membershipType(); - $dateFields = array('birth_date' => ts('Birth Date')); + $dateFieldParams = array('data_type' => 'Date'); + $dateFields = self::getDateFields(); + $contactOptions = CRM_Core_OptionGroup::values('contact_date_reminder_options'); asort($activityType); @@ -150,11 +167,11 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { $sel2[$key] = $valueLabel + $membershipType; break; - case 'birth_date': + case 'civicrm_contact': if ($value['entity'] == 'civicrm_contact') { $sel1Val = ts('Contact'); } - $sel2[$key] = $valueLabel + $dateFields; + $sel2[$key] = $dateFields; break; } $sel1[$key] = $sel1Val; @@ -221,16 +238,12 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { } break; - case 'none': + case 'contact_date_reminder_options': foreach ($sel3[$id] as $kkey => & $vval) { - $vval = $statusLabel; + $vval = $contactOptions; } break; - case null: - $sel3[$id] = array('' => 'hello there'); - break; - case '': $sel3[$id] = ''; break; @@ -320,14 +333,17 @@ class CRM_Core_BAO_ActionSchedule extends CRM_Core_DAO_ActionSchedule { $civicrm_event = CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )"); $civicrm_participant_status_type = CRM_Event_PseudoConstant::participantStatus(NULL, NULL, 'label'); $event_template = CRM_Event_PseudoConstant::eventTemplates(); + $civicrm_contact = self::getDateFields(); $auto_renew_options = CRM_Core_OptionGroup::values('auto_renew_options'); + $contact_date_reminder_options = CRM_Core_OptionGroup::values('contact_date_reminder_options'); $civicrm_membership_type = CRM_Member_PseudoConstant::membershipType(); $entity = array( 'civicrm_activity' => 'Activity', 'civicrm_participant' => 'Event', 'civicrm_membership' => 'Member', + 'civicrm_contact' => 'Contact', ); $query = " @@ -902,6 +918,8 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL ); $status = implode(',', $status); + $anniversary = false; + if (!CRM_Utils_System::isNull($mapping->entity_recipient)) { $recipientOptions = CRM_Core_OptionGroup::values($mapping->entity_recipient, FALSE, FALSE, FALSE, NULL, 'name'); } @@ -1021,7 +1039,39 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL } if ($mapping->entity == 'civicrm_contact') { + if ($value == 'birth_date') { + $dateDBField = 'birth_date'; + $table = 'civicrm_contact e'; + $contactField = 'e.id'; + $where[] = 'e.is_deleted = 0'; + $where[] = 'e.is_deceased = 0'; + } + else { + //custom field + $customFieldParams = array('id' => substr($value, 7)); + $customGroup = $customField = array(); + CRM_Core_BAO_CustomField::retrieve($customFieldParams, $customField); + $dateDBField = $customField['column_name']; + $customGroupParams = array('id' => $customField['custom_group_id'], $customGroup); + CRM_Core_BAO_CustomGroup::retrieve($customGroupParams, $customGroup); + $from = $table = "{$customGroup['table_name']} e"; + $contactField = 'e.entity_id'; + $where[] = '1'; // possible to have no "where" in this case + } + + $status_ = explode(',', $status); + if (in_array(2, $status_)) { + // anniversary mode: + $dateField = 'DATE_ADD(e.' . $dateDBField . ', INTERVAL ROUND(DATEDIFF(NOW(), e.' . $dateDBField . ') / 365) YEAR)'; + $anniversary = true; + } + else { + // regular mode: + $dateField = 'e.' . $dateDBField; + } // TODO get this working + + // TODO: Make sure everything's provided for repetition, etc. } // CRM-13577 Introduce Smart Groups Handling @@ -1095,7 +1145,14 @@ reminder.entity_id = e.id AND reminder.entity_table = '{$mapping->entity}' AND reminder.action_schedule_id = %1"; - $join[] = "INNER JOIN civicrm_contact c ON c.id = {$contactField} AND c.is_deleted = 0 AND c.is_deceased = 0 "; + if ($anniversary) { + // only consider reminders less than 11 months ago + $reminderJoinClause .= " AND reminder.action_date_time > DATE_SUB({$now}, INTERVAL 11 MONTH)"; + } + + if ($table != 'civicrm_contact e') { + $join[] = "INNER JOIN civicrm_contact c ON c.id = {$contactField} AND c.is_deleted = 0 AND c.is_deceased = 0 "; + } if ($actionSchedule->start_action_date) { $startDateClause = array(); -- 2.25.1