Merge pull request #11949 from magnolia61/Hide_waitinglist_for_past_and_registration_...
[civicrm-core.git] / CRM / Event / ActionMapping.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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
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 )));
65 $registrations->register(CRM_Event_ActionMapping::create(array(
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'),
73 )));
74 $registrations->register(CRM_Event_ActionMapping::create(array(
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'),
82 )));
83 }
84
85 /**
86 * Get a list of available date fields.
87 *
88 * @return array
89 * Array(string $fieldName => string $fieldLabel).
90 */
91 public function getDateFields() {
92 return array(
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'),
97 );
98 }
99
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:
133 return array();
134 }
135 }
136
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.
145 * @param array $defaultParams
146 *
147 * @return \CRM_Utils_SQL_Select
148 * @see RecipientBuilder
149 */
150 public function createQuery($schedule, $phase, $defaultParams) {
151 $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value);
152 $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status);
153
154 $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams);;
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);
160
161 $query->join('r', 'INNER JOIN civicrm_event r ON e.event_id = r.id');
162 if ($schedule->recipient_listing && $schedule->limit_to) {
163 switch ($schedule->recipient) {
164 case 'participant_role':
165 $query->where("e.role_id IN (#recipList)")
166 ->param('recipList', \CRM_Utils_Array::explodePadded($schedule->recipient_listing));
167 break;
168
169 default:
170 break;
171 }
172 }
173
174 // build where clause
175 if (!empty($selectedValues)) {
176 $valueField = ($this->id == \CRM_Event_ActionMapping::EVENT_TYPE_MAPPING_ID) ? 'event_type_id' : 'id';
177 $query->where("r.{$valueField} IN (@selectedValues)")
178 ->param('selectedValues', $selectedValues);
179 }
180 else {
181 $query->where(($this->id == \CRM_Event_ActionMapping::EVENT_TYPE_MAPPING_ID) ? "r.event_type_id IS NULL" : "r.id IS NULL");
182 }
183
184 $query->where('r.is_active = 1');
185 $query->where('r.is_template = 0');
186
187 // participant status criteria not to be implemented for additional recipients
188 // ... why not?
189 if (!empty($selectedStatuses)) {
190 switch ($phase) {
191 case RecipientBuilder::PHASE_RELATION_FIRST:
192 case RecipientBuilder::PHASE_RELATION_REPEAT:
193 $query->where("e.status_id IN (#selectedStatuses)")
194 ->param('selectedStatuses', $selectedStatuses);
195 break;
196
197 }
198 }
199 return $query;
200 }
201
202 }