3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
14 * Class CRM_Contact_ActionMapping
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
21 class CRM_Contact_ActionMapping
extends \Civi\ActionSchedule\Mapping
{
24 * The value for civicrm_action_schedule.mapping_id which identifies the
27 * Note: This value is chosen to match legacy DB IDs.
29 const CONTACT_MAPPING_ID
= 6;
32 * Register Contact-related action mappings.
34 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
36 public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent
$registrations) {
37 $registrations->register(CRM_Contact_ActionMapping
::create([
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',
49 private $contactDateFields = [
56 * Determine whether a schedule based on this mapping is sufficiently
59 * @param \CRM_Core_DAO_ActionSchedule $schedule
61 * Array (string $code => string $message).
62 * List of error messages.
64 public function validateSchedule($schedule) {
66 if (CRM_Utils_System
::isNull($schedule->entity_value
) ||
$schedule->entity_value
=== '0') {
67 $errors['entity'] = ts('Please select a specific date field.');
69 elseif (count(CRM_Utils_Array
::explodePadded($schedule->entity_value
)) > 1) {
70 $errors['entity'] = ts('You may only select one contact field per reminder');
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.');
80 * Generate a query to locate recipients who match the given
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.
87 * @param array $defaultParams
89 * @return \CRM_Utils_SQL_Select
90 * @throws \CRM_Core_Exception
91 * @see RecipientBuilder
93 public function createQuery($schedule, $phase, $defaultParams) {
94 $selectedValues = (array) \CRM_Utils_Array
::explodePadded($schedule->entity_value
);
95 $selectedStatuses = (array) \CRM_Utils_Array
::explodePadded($schedule->entity_status
);
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.");
102 elseif (in_array($selectedValues[0], $this->contactDateFields
)) {
103 $dateDBField = $selectedValues[0];
104 $query = \CRM_Utils_SQL_Select
::from("{$this->entity} e")->param($defaultParams);
106 'casAddlCheckFrom' => 'civicrm_contact e',
107 'casContactIdField' => 'e.id',
108 'casEntityIdField' => 'e.id',
109 'casContactTableAlias' => 'e',
111 $query->where('e.is_deleted = 0 AND e.is_deceased = 0');
115 $customFieldParams = ['id' => substr($selectedValues[0], 7)];
116 $customGroup = $customField = [];
117 \CRM_Core_BAO_CustomField
::retrieve($customFieldParams, $customField);
118 $dateDBField = $customField['column_name'];
119 $customGroupParams = ['id' => $customField['custom_group_id'], $customGroup];
120 \CRM_Core_BAO_CustomGroup
::retrieve($customGroupParams, $customGroup);
121 $query = \CRM_Utils_SQL_Select
::from("{$customGroup['table_name']} e")->param($defaultParams);
123 'casAddlCheckFrom' => "{$customGroup['table_name']} e",
124 'casContactIdField' => 'e.entity_id',
125 'casEntityIdField' => 'e.id',
126 'casContactTableAlias' => NULL,
128 // possible to have no "where" in this case
132 $query['casDateField'] = 'e.' . $dateDBField;
134 if (in_array(2, $selectedStatuses)) {
135 $query['casAnniversaryMode'] = 1;
136 $query['casDateField'] = 'DATE_ADD(' . $query['casDateField'] . ', INTERVAL ROUND(DATEDIFF(DATE(' . $query['casNow'] . '), ' . $query['casDateField'] . ') / 365) YEAR)';