| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.7 | |
| 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 | |
| 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', |
| 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', |
| 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', |
| 88 | ))); |
| 89 | } |
| 90 | |
| 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 | |
| 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 | * @param array $defaultParams |
| 137 | * |
| 138 | * @return \CRM_Utils_SQL_Select |
| 139 | * @see RecipientBuilder |
| 140 | */ |
| 141 | public function createQuery($schedule, $phase, $defaultParams) { |
| 142 | $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value); |
| 143 | $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status); |
| 144 | |
| 145 | $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams);; |
| 146 | $query['casAddlCheckFrom'] = 'civicrm_event r'; |
| 147 | $query['casContactIdField'] = 'e.contact_id'; |
| 148 | $query['casEntityIdField'] = 'e.id'; |
| 149 | $query['casContactTableAlias'] = NULL; |
| 150 | $query['casDateField'] = str_replace('event_', 'r.', $schedule->start_action_date); |
| 151 | |
| 152 | $query->join('r', 'INNER JOIN civicrm_event r ON e.event_id = r.id'); |
| 153 | if ($schedule->recipient_listing && $schedule->limit_to) { |
| 154 | switch ($schedule->recipient) { |
| 155 | case 'participant_role': |
| 156 | $query->where("e.role_id IN (#recipList)") |
| 157 | ->param('recipList', \CRM_Utils_Array::explodePadded($schedule->recipient_listing)); |
| 158 | break; |
| 159 | |
| 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | // build where clause |
| 166 | if (!empty($selectedValues)) { |
| 167 | $valueField = ($this->id == \CRM_Event_ActionMapping::EVENT_TYPE_MAPPING_ID) ? 'event_type_id' : 'id'; |
| 168 | $query->where("r.{$valueField} IN (@selectedValues)") |
| 169 | ->param('selectedValues', $selectedValues); |
| 170 | } |
| 171 | else { |
| 172 | $query->where(($this->id == \CRM_Event_ActionMapping::EVENT_TYPE_MAPPING_ID) ? "r.event_type_id IS NULL" : "r.id IS NULL"); |
| 173 | } |
| 174 | |
| 175 | $query->where('r.is_active = 1'); |
| 176 | $query->where('r.is_template = 0'); |
| 177 | |
| 178 | // participant status criteria not to be implemented for additional recipients |
| 179 | // ... why not? |
| 180 | if (!empty($selectedStatuses)) { |
| 181 | switch ($phase) { |
| 182 | case RecipientBuilder::PHASE_RELATION_FIRST: |
| 183 | case RecipientBuilder::PHASE_RELATION_REPEAT: |
| 184 | $query->where("e.status_id IN (#selectedStatuses)") |
| 185 | ->param('selectedStatuses', $selectedStatuses); |
| 186 | break; |
| 187 | |
| 188 | } |
| 189 | } |
| 190 | return $query; |
| 191 | } |
| 192 | |
| 193 | } |