Commit | Line | Data |
---|---|---|
546a1ecc TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
3435af9a | 4 | | CiviCRM version 4.7 | |
546a1ecc TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2015 | | |
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) { | |
56 | $registrations->register(CRM_Event_ActionMapping::create(array( | |
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'), | |
64 | 'entity_date_start' => 'event_start_date', | |
65 | 'entity_date_end' => 'event_end_date', | |
46f5566c TO |
66 | ))); |
67 | $registrations->register(CRM_Event_ActionMapping::create(array( | |
68 | 'id' => CRM_Event_ActionMapping::EVENT_NAME_MAPPING_ID, | |
69 | 'entity' => 'civicrm_participant', | |
70 | 'entity_label' => ts('Event Name'), | |
71 | 'entity_value' => 'civicrm_event', | |
72 | 'entity_value_label' => ts('Event Name'), | |
73 | 'entity_status' => 'civicrm_participant_status_type', | |
74 | 'entity_status_label' => ts('Participant Status'), | |
75 | 'entity_date_start' => 'event_start_date', | |
76 | 'entity_date_end' => 'event_end_date', | |
46f5566c TO |
77 | ))); |
78 | $registrations->register(CRM_Event_ActionMapping::create(array( | |
79 | 'id' => CRM_Event_ActionMapping::EVENT_TPL_MAPPING_ID, | |
80 | 'entity' => 'civicrm_participant', | |
81 | 'entity_label' => ts('Event Template'), | |
82 | 'entity_value' => 'event_template', | |
83 | 'entity_value_label' => ts('Event Template'), | |
84 | 'entity_status' => 'civicrm_participant_status_type', | |
85 | 'entity_status_label' => ts('Participant Status'), | |
86 | 'entity_date_start' => 'event_start_date', | |
87 | 'entity_date_end' => 'event_end_date', | |
46f5566c TO |
88 | ))); |
89 | } | |
90 | ||
0effed37 TO |
91 | /** |
92 | * Get a list of recipient types. | |
93 | * | |
94 | * Note: A single schedule may filter on *zero* or *one* recipient types. | |
95 | * When an admin chooses a value, it's stored in $schedule->recipient. | |
96 | * | |
97 | * @return array | |
98 | * array(string $value => string $label). | |
99 | * Ex: array('assignee' => 'Activity Assignee'). | |
100 | */ | |
101 | public function getRecipientTypes() { | |
102 | return \CRM_Core_OptionGroup::values('event_contacts', FALSE, FALSE, FALSE, NULL, 'label', TRUE, FALSE, 'name'); | |
103 | } | |
104 | ||
105 | /** | |
106 | * Get a list of recipients which match the given type. | |
107 | * | |
108 | * Note: A single schedule may filter on *multiple* recipients. | |
109 | * When an admin chooses value(s), it's stored in $schedule->recipient_listing. | |
110 | * | |
111 | * @param string $recipientType | |
112 | * Ex: 'participant_role'. | |
113 | * @return array | |
114 | * Array(mixed $name => string $label). | |
115 | * Ex: array(1 => 'Attendee', 2 => 'Volunteer'). | |
116 | * @see getRecipientTypes | |
117 | */ | |
118 | public function getRecipientListing($recipientType) { | |
119 | switch ($recipientType) { | |
120 | case 'participant_role': | |
121 | return \CRM_Event_PseudoConstant::participantRole(); | |
122 | ||
123 | default: | |
124 | return array(); | |
125 | } | |
126 | } | |
127 | ||
546a1ecc TO |
128 | /** |
129 | * Generate a query to locate recipients who match the given | |
130 | * schedule. | |
131 | * | |
132 | * @param \CRM_Core_DAO_ActionSchedule $schedule | |
133 | * The schedule as configured by the administrator. | |
134 | * @param string $phase | |
135 | * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST. | |
136 | * @return \CRM_Utils_SQL_Select | |
137 | * @see RecipientBuilder | |
138 | */ | |
efc40454 | 139 | public function createQuery($schedule, $phase, $defaultParams) { |
546a1ecc TO |
140 | $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); |
141 | $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); | |
142 | ||
efc40454 | 143 | $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams);; |
546a1ecc TO |
144 | $query['casAddlCheckFrom'] = 'civicrm_event r'; |
145 | $query['casContactIdField'] = 'e.contact_id'; | |
146 | $query['casEntityIdField'] = 'e.id'; | |
147 | $query['casContactTableAlias'] = NULL; | |
148 | $query['casDateField'] = str_replace('event_', 'r.', $schedule->start_action_date); | |
149 | ||
150 | $query->join('r', 'INNER JOIN civicrm_event r ON e.event_id = r.id'); | |
151 | if ($schedule->recipient_listing && $schedule->limit_to) { | |
673c1c81 | 152 | switch ($schedule->recipient) { |
546a1ecc TO |
153 | case 'participant_role': |
154 | $query->where("e.role_id IN (#recipList)") | |
155 | ->param('recipList', \CRM_Utils_Array::explodePadded($schedule->recipient_listing)); | |
156 | break; | |
157 | ||
158 | default: | |
159 | break; | |
160 | } | |
161 | } | |
162 | ||
163 | // build where clause | |
164 | if (!empty($selectedValues)) { | |
46f5566c | 165 | $valueField = ($this->id == \CRM_Event_ActionMapping::EVENT_TYPE_MAPPING_ID) ? 'event_type_id' : 'id'; |
546a1ecc TO |
166 | $query->where("r.{$valueField} IN (@selectedValues)") |
167 | ->param('selectedValues', $selectedValues); | |
168 | } | |
169 | else { | |
46f5566c | 170 | $query->where(($this->id == \CRM_Event_ActionMapping::EVENT_TYPE_MAPPING_ID) ? "r.event_type_id IS NULL" : "r.id IS NULL"); |
546a1ecc TO |
171 | } |
172 | ||
173 | $query->where('r.is_active = 1'); | |
174 | $query->where('r.is_template = 0'); | |
175 | ||
176 | // participant status criteria not to be implemented for additional recipients | |
673c1c81 | 177 | // ... why not? |
546a1ecc TO |
178 | if (!empty($selectedStatuses)) { |
179 | switch ($phase) { | |
180 | case RecipientBuilder::PHASE_RELATION_FIRST: | |
181 | case RecipientBuilder::PHASE_RELATION_REPEAT: | |
182 | $query->where("e.status_id IN (#selectedStatuses)") | |
183 | ->param('selectedStatuses', $selectedStatuses); | |
184 | break; | |
185 | ||
186 | } | |
187 | } | |
188 | return $query; | |
189 | } | |
190 | ||
546a1ecc | 191 | } |