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 | ||
28 | use Civi\ActionSchedule\RecipientBuilder; | |
29 | ||
30 | /** | |
31 | * Class CRM_Event_ActionMapping | |
32 | * | |
33 | * This defines the scheduled-reminder functionality for CiviEvent | |
34 | * participants. It allows one to target messages on the | |
35 | * event's start-date/end-date, with additional filtering by | |
36 | * event-type, event-template, or event-id. | |
37 | */ | |
38 | class CRM_Event_ActionMapping extends \Civi\ActionSchedule\Mapping { | |
39 | ||
46f5566c TO |
40 | /** |
41 | * The value for civicrm_action_schedule.mapping_id which identifies the | |
42 | * "Event Type" mapping. | |
43 | * | |
44 | * Note: This value is chosen to match legacy DB IDs. | |
45 | */ | |
46 | const EVENT_TYPE_MAPPING_ID = 2; | |
47 | const EVENT_NAME_MAPPING_ID = 3; | |
48 | const EVENT_TPL_MAPPING_ID = 5; | |
49 | ||
50 | /** | |
51 | * Register CiviEvent-related action mappings. | |
52 | * | |
53 | * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations | |
54 | */ | |
55 | public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent $registrations) { | |
be2fb01f | 56 | $registrations->register(CRM_Event_ActionMapping::create([ |
46f5566c TO |
57 | 'id' => CRM_Event_ActionMapping::EVENT_TYPE_MAPPING_ID, |
58 | 'entity' => 'civicrm_participant', | |
59 | 'entity_label' => ts('Event Type'), | |
60 | 'entity_value' => 'event_type', | |
61 | 'entity_value_label' => ts('Event Type'), | |
62 | 'entity_status' => 'civicrm_participant_status_type', | |
63 | 'entity_status_label' => ts('Participant Status'), | |
be2fb01f CW |
64 | ])); |
65 | $registrations->register(CRM_Event_ActionMapping::create([ | |
46f5566c TO |
66 | 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID, |
67 | 'entity' => 'civicrm_participant', | |
68 | 'entity_label' => ts('Event Name'), | |
69 | 'entity_value' => 'civicrm_event', | |
70 | 'entity_value_label' => ts('Event Name'), | |
71 | 'entity_status' => 'civicrm_participant_status_type', | |
72 | 'entity_status_label' => ts('Participant Status'), | |
be2fb01f CW |
73 | ])); |
74 | $registrations->register(CRM_Event_ActionMapping::create([ | |
46f5566c TO |
75 | 'id' => CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID, |
76 | 'entity' => 'civicrm_participant', | |
77 | 'entity_label' => ts('Event Template'), | |
78 | 'entity_value' => 'event_template', | |
79 | 'entity_value_label' => ts('Event Template'), | |
80 | 'entity_status' => 'civicrm_participant_status_type', | |
81 | 'entity_status_label' => ts('Participant Status'), | |
be2fb01f | 82 | ])); |
46f5566c TO |
83 | } |
84 | ||
9f1cadce JP |
85 | /** |
86 | * Get a list of available date fields. | |
87 | * | |
88 | * @return array | |
89 | * Array(string $fieldName => string $fieldLabel). | |
90 | */ | |
91 | public function getDateFields() { | |
be2fb01f | 92 | return [ |
9f1cadce JP |
93 | 'start_date' => ts('Event Start Date'), |
94 | 'end_date' => ts('Event End Date'), | |
95 | 'registration_start_date' => ts('Registration Start Date'), | |
96 | 'registration_end_date' => ts('Registration End Date'), | |
be2fb01f | 97 | ]; |
9f1cadce JP |
98 | } |
99 | ||
0effed37 TO |
100 | /** |
101 | * Get a list of recipient types. | |
102 | * | |
103 | * Note: A single schedule may filter on *zero* or *one* recipient types. | |
104 | * When an admin chooses a value, it's stored in $schedule->recipient. | |
105 | * | |
106 | * @return array | |
107 | * array(string $value => string $label). | |
108 | * Ex: array('assignee' => 'Activity Assignee'). | |
109 | */ | |
110 | public function getRecipientTypes() { | |
111 | return \CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name'); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Get a list of recipients which match the given type. | |
116 | * | |
117 | * Note: A single schedule may filter on *multiple* recipients. | |
118 | * When an admin chooses value(s), it's stored in $schedule->recipient_listing. | |
119 | * | |
120 | * @param string $recipientType | |
121 | * Ex: 'participant_role'. | |
122 | * @return array | |
123 | * Array(mixed $name => string $label). | |
124 | * Ex: array(1 => 'Attendee', 2 => 'Volunteer'). | |
125 | * @see getRecipientTypes | |
126 | */ | |
127 | public function getRecipientListing($recipientType) { | |
128 | switch ($recipientType) { | |
129 | case 'participant_role': | |
130 | return \CRM_Event_PseudoConstant::participantRole(); | |
131 | ||
132 | default: | |
be2fb01f | 133 | return []; |
0effed37 TO |
134 | } |
135 | } | |
136 | ||
546a1ecc TO |
137 | /** |
138 | * Generate a query to locate recipients who match the given | |
139 | * schedule. | |
140 | * | |
141 | * @param \CRM_Core_DAO_ActionSchedule $schedule | |
142 | * The schedule as configured by the administrator. | |
143 | * @param string $phase | |
144 | * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST. | |
ad37ac8e | 145 | * @param array $defaultParams |
146 | * | |
546a1ecc TO |
147 | * @return \CRM_Utils_SQL_Select |
148 | * @see RecipientBuilder | |
149 | */ | |
efc40454 | 150 | public function createQuery($schedule, $phase, $defaultParams) { |
546a1ecc TO |
151 | $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); |
152 | $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); | |
153 | ||
efc40454 | 154 | $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams);; |
546a1ecc TO |
155 | $query['casAddlCheckFrom'] = 'civicrm_event r'; |
156 | $query['casContactIdField'] = 'e.contact_id'; | |
157 | $query['casEntityIdField'] = 'e.id'; | |
158 | $query['casContactTableAlias'] = NULL; | |
159 | $query['casDateField'] = str_replace('event_', 'r.', $schedule->start_action_date); | |
ba6673f6 | 160 | if (empty($query['casDateField']) && $schedule->absolute_date) { |
e08fae02 | 161 | $query['casDateField'] = "'" . CRM_Utils_Type::escape($schedule->absolute_date, 'String') . "'"; |
ba6673f6 | 162 | } |
546a1ecc TO |
163 | |
164 | $query->join('r', 'INNER JOIN civicrm_event r ON e.event_id = r.id'); | |
165 | if ($schedule->recipient_listing && $schedule->limit_to) { | |
673c1c81 | 166 | switch ($schedule->recipient) { |
546a1ecc TO |
167 | case 'participant_role': |
168 | $query->where("e.role_id IN (#recipList)") | |
169 | ->param('recipList', \CRM_Utils_Array::explodePadded($schedule->recipient_listing)); | |
170 | break; | |
171 | ||
172 | default: | |
173 | break; | |
174 | } | |
175 | } | |
176 | ||
177 | // build where clause | |
178 | if (!empty($selectedValues)) { | |
46f5566c | 179 | $valueField = ($this->id == \CRM_Event_ActionMapping::EVENT_TYPE_MAPPING_ID) ? 'event_type_id' : 'id'; |
546a1ecc TO |
180 | $query->where("r.{$valueField} IN (@selectedValues)") |
181 | ->param('selectedValues', $selectedValues); | |
182 | } | |
183 | else { | |
46f5566c | 184 | $query->where(($this->id == \CRM_Event_ActionMapping::EVENT_TYPE_MAPPING_ID) ? "r.event_type_id IS NULL" : "r.id IS NULL"); |
546a1ecc TO |
185 | } |
186 | ||
187 | $query->where('r.is_active = 1'); | |
188 | $query->where('r.is_template = 0'); | |
189 | ||
190 | // participant status criteria not to be implemented for additional recipients | |
673c1c81 | 191 | // ... why not? |
546a1ecc TO |
192 | if (!empty($selectedStatuses)) { |
193 | switch ($phase) { | |
194 | case RecipientBuilder::PHASE_RELATION_FIRST: | |
195 | case RecipientBuilder::PHASE_RELATION_REPEAT: | |
196 | $query->where("e.status_id IN (#selectedStatuses)") | |
197 | ->param('selectedStatuses', $selectedStatuses); | |
198 | break; | |
199 | ||
200 | } | |
201 | } | |
202 | return $query; | |
203 | } | |
204 | ||
546a1ecc | 205 | } |