3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class CRM_Activity_ActionMapping
21 * This defines the scheduled-reminder functionality for contact
22 * entities. It is useful for, e.g., sending a reminder based on
23 * birth date, modification date, or other custom dates on
26 class CRM_Activity_ActionMapping
extends \Civi\ActionSchedule\Mapping
{
29 * The value for civicrm_action_schedule.mapping_id which identifies the
32 * Note: This value is chosen to match legacy DB IDs.
34 const ACTIVITY_MAPPING_ID
= 1;
37 * Register Activity-related action mappings.
39 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
41 public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent
$registrations) {
42 $registrations->register(CRM_Activity_ActionMapping
::create([
43 'id' => CRM_Activity_ActionMapping
::ACTIVITY_MAPPING_ID
,
44 'entity' => 'civicrm_activity',
45 'entity_label' => ts('Activity'),
46 'entity_value' => 'activity_type',
47 'entity_value_label' => ts('Activity Type'),
48 'entity_status' => 'activity_status',
49 'entity_status_label' => ts('Activity Status'),
50 'entity_date_start' => 'activity_date_time',
55 * Get a list of recipient types.
57 * Note: A single schedule may filter on *zero* or *one* recipient types.
58 * When an admin chooses a value, it's stored in $schedule->recipient.
61 * array(string $value => string $label).
62 * Ex: array('assignee' => 'Activity Assignee').
64 public function getRecipientTypes() {
65 return \CRM_Core_OptionGroup
::values('activity_contacts');
69 * Generate a query to locate recipients who match the given
72 * @param \CRM_Core_DAO_ActionSchedule $schedule
73 * The schedule as configured by the administrator.
74 * @param string $phase
75 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
77 * @param array $defaultParams
79 * @return \CRM_Utils_SQL_Select
80 * @see RecipientBuilder
82 public function createQuery($schedule, $phase, $defaultParams) {
83 $selectedValues = (array) \CRM_Utils_Array
::explodePadded($schedule->entity_value
);
84 $selectedStatuses = (array) \CRM_Utils_Array
::explodePadded($schedule->entity_status
);
86 $query = \CRM_Utils_SQL_Select
::from("{$this->entity} e")->param($defaultParams);
87 $query['casAddlCheckFrom'] = 'civicrm_activity e';
88 $query['casContactIdField'] = 'r.contact_id';
89 $query['casEntityIdField'] = 'e.id';
90 $query['casContactTableAlias'] = NULL;
91 $query['casDateField'] = 'e.activity_date_time';
93 if (!is_null($schedule->limit_to
)) {
94 $activityContacts = \CRM_Activity_BAO_ActivityContact
::buildOptions('record_type_id', 'validate');
95 if ($schedule->limit_to
== 0 ||
!isset($activityContacts[$schedule->recipient
])) {
96 $recipientTypeId = \CRM_Utils_Array
::key('Activity Targets', $activityContacts);
99 $recipientTypeId = $schedule->recipient
;
101 $query->join('r', "INNER JOIN civicrm_activity_contact r ON r.activity_id = e.id AND record_type_id = {$recipientTypeId}");
103 // build where clause
104 if (!empty($selectedValues)) {
105 $query->where("e.activity_type_id IN (#selectedValues)")
106 ->param('selectedValues', $selectedValues);
109 $query->where("e.activity_type_id IS NULL");
112 if (!empty($selectedStatuses)) {
113 $query->where("e.status_id IN (#selectedStatuss)")
114 ->param('selectedStatuss', $selectedStatuses);
116 $query->where('e.is_current_revision = 1 AND e.is_deleted = 0');
122 * Determine whether a schedule based on this mapping should
123 * send to additional contacts.
125 public function sendToAdditional($entityId): bool {