Commit | Line | Data |
---|---|---|
546a1ecc TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
546a1ecc | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
546a1ecc TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
546a1ecc TO |
12 | |
13 | /** | |
14 | * Class CRM_Contact_ActionMapping | |
15 | * | |
16 | * This defines the scheduled-reminder functionality for contact | |
17 | * entities. It is useful for, e.g., sending a reminder based on | |
18 | * birth date, modification date, or other custom dates on | |
19 | * the contact record. | |
20 | */ | |
21 | class CRM_Contact_ActionMapping extends \Civi\ActionSchedule\Mapping { | |
22 | ||
46f5566c TO |
23 | /** |
24 | * The value for civicrm_action_schedule.mapping_id which identifies the | |
25 | * "Contact" mapping. | |
26 | * | |
27 | * Note: This value is chosen to match legacy DB IDs. | |
28 | */ | |
29 | const CONTACT_MAPPING_ID = 6; | |
30 | ||
31 | /** | |
32 | * Register Contact-related action mappings. | |
33 | * | |
34 | * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations | |
35 | */ | |
36 | public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent $registrations) { | |
be2fb01f | 37 | $registrations->register(CRM_Contact_ActionMapping::create([ |
46f5566c TO |
38 | 'id' => CRM_Contact_ActionMapping::CONTACT_MAPPING_ID, |
39 | 'entity' => 'civicrm_contact', | |
40 | 'entity_label' => ts('Contact'), | |
41 | 'entity_value' => 'civicrm_contact', | |
42 | 'entity_value_label' => ts('Date Field'), | |
43 | 'entity_status' => 'contact_date_reminder_options', | |
44 | 'entity_status_label' => ts('Annual Options'), | |
45 | 'entity_date_start' => 'date_field', | |
be2fb01f | 46 | ])); |
46f5566c TO |
47 | } |
48 | ||
be2fb01f | 49 | private $contactDateFields = [ |
546a1ecc TO |
50 | 'birth_date', |
51 | 'created_date', | |
52 | 'modified_date', | |
be2fb01f | 53 | ]; |
546a1ecc | 54 | |
018d5e02 TO |
55 | /** |
56 | * Determine whether a schedule based on this mapping is sufficiently | |
57 | * complete. | |
58 | * | |
59 | * @param \CRM_Core_DAO_ActionSchedule $schedule | |
60 | * @return array | |
61 | * Array (string $code => string $message). | |
62 | * List of error messages. | |
63 | */ | |
64 | public function validateSchedule($schedule) { | |
be2fb01f | 65 | $errors = []; |
018d5e02 TO |
66 | if (CRM_Utils_System::isNull($schedule->entity_value) || $schedule->entity_value === '0') { |
67 | $errors['entity'] = ts('Please select a specific date field.'); | |
68 | } | |
69 | elseif (count(CRM_Utils_Array::explodePadded($schedule->entity_value)) > 1) { | |
70 | $errors['entity'] = ts('You may only select one contact field per reminder'); | |
71 | } | |
72 | elseif (CRM_Utils_System::isNull($schedule->entity_status) || $schedule->entity_status === '0') { | |
73 | $errors['entity'] = ts('Please select whether the reminder is sent each year.'); | |
74 | } | |
75 | ||
76 | return $errors; | |
77 | } | |
78 | ||
546a1ecc TO |
79 | /** |
80 | * Generate a query to locate recipients who match the given | |
81 | * schedule. | |
82 | * | |
83 | * @param \CRM_Core_DAO_ActionSchedule $schedule | |
84 | * The schedule as configured by the administrator. | |
85 | * @param string $phase | |
86 | * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST. | |
ad37ac8e | 87 | * @param array $defaultParams |
88 | * | |
546a1ecc | 89 | * @return \CRM_Utils_SQL_Select |
546a1ecc | 90 | * @throws \CRM_Core_Exception |
ad37ac8e | 91 | * @see RecipientBuilder |
546a1ecc | 92 | */ |
efc40454 | 93 | public function createQuery($schedule, $phase, $defaultParams) { |
546a1ecc TO |
94 | $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); |
95 | $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); | |
96 | ||
97 | // FIXME: This assumes that $values only has one field, but UI shows multiselect. | |
98 | // Properly supporting multiselect would require total rewrite of this function. | |
99 | if (count($selectedValues) != 1 || !isset($selectedValues[0])) { | |
100 | throw new \CRM_Core_Exception("Error: Scheduled reminders may only have one contact field."); | |
101 | } | |
102 | elseif (in_array($selectedValues[0], $this->contactDateFields)) { | |
103 | $dateDBField = $selectedValues[0]; | |
efc40454 | 104 | $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams); |
be2fb01f | 105 | $query->param([ |
546a1ecc TO |
106 | 'casAddlCheckFrom' => 'civicrm_contact e', |
107 | 'casContactIdField' => 'e.id', | |
108 | 'casEntityIdField' => 'e.id', | |
109 | 'casContactTableAlias' => 'e', | |
be2fb01f | 110 | ]); |
546a1ecc TO |
111 | $query->where('e.is_deleted = 0 AND e.is_deceased = 0'); |
112 | } | |
113 | else { | |
114 | //custom field | |
be2fb01f CW |
115 | $customFieldParams = ['id' => substr($selectedValues[0], 7)]; |
116 | $customGroup = $customField = []; | |
546a1ecc TO |
117 | \CRM_Core_BAO_CustomField::retrieve($customFieldParams, $customField); |
118 | $dateDBField = $customField['column_name']; | |
be2fb01f | 119 | $customGroupParams = ['id' => $customField['custom_group_id'], $customGroup]; |
546a1ecc | 120 | \CRM_Core_BAO_CustomGroup::retrieve($customGroupParams, $customGroup); |
efc40454 | 121 | $query = \CRM_Utils_SQL_Select::from("{$customGroup['table_name']} e")->param($defaultParams); |
be2fb01f | 122 | $query->param([ |
546a1ecc TO |
123 | 'casAddlCheckFrom' => "{$customGroup['table_name']} e", |
124 | 'casContactIdField' => 'e.entity_id', | |
125 | 'casEntityIdField' => 'e.id', | |
126 | 'casContactTableAlias' => NULL, | |
be2fb01f | 127 | ]); |
69078420 SL |
128 | // possible to have no "where" in this case |
129 | $query->where('1'); | |
546a1ecc TO |
130 | } |
131 | ||
132 | $query['casDateField'] = 'e.' . $dateDBField; | |
133 | ||
134 | if (in_array(2, $selectedStatuses)) { | |
135 | $query['casAnniversaryMode'] = 1; | |
efc40454 | 136 | $query['casDateField'] = 'DATE_ADD(' . $query['casDateField'] . ', INTERVAL ROUND(DATEDIFF(DATE(' . $query['casNow'] . '), ' . $query['casDateField'] . ') / 365) YEAR)'; |
546a1ecc TO |
137 | } |
138 | ||
139 | return $query; | |
140 | } | |
141 | ||
142 | } |