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