Merge pull request #16196 from eileenmcnaughton/mem_add
[civicrm-core.git] / CRM / Activity / ActionMapping.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * @package CRM
14 * @copyright CiviCRM LLC https://civicrm.org/licensing
15 */
16
17
18 /**
19 * Class CRM_Activity_ActionMapping
20 *
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
24 * the contact record.
25 */
26 class CRM_Activity_ActionMapping extends \Civi\ActionSchedule\Mapping {
27
28 /**
29 * The value for civicrm_action_schedule.mapping_id which identifies the
30 * "Activity" mapping.
31 *
32 * Note: This value is chosen to match legacy DB IDs.
33 */
34 const ACTIVITY_MAPPING_ID = 1;
35
36 /**
37 * Register Activity-related action mappings.
38 *
39 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
40 */
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',
51 ]));
52 }
53
54 /**
55 * Get a list of recipient types.
56 *
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.
59 *
60 * @return array
61 * array(string $value => string $label).
62 * Ex: array('assignee' => 'Activity Assignee').
63 */
64 public function getRecipientTypes() {
65 return \CRM_Core_OptionGroup::values('activity_contacts');
66 }
67
68 /**
69 * Generate a query to locate recipients who match the given
70 * schedule.
71 *
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.
76 *
77 * @param array $defaultParams
78 *
79 * @return \CRM_Utils_SQL_Select
80 * @see RecipientBuilder
81 */
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);
85
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';
92
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);
97 }
98 else {
99 $recipientTypeId = $schedule->recipient;
100 }
101 $query->join('r', "INNER JOIN civicrm_activity_contact r ON r.activity_id = e.id AND record_type_id = {$recipientTypeId}");
102 }
103 // build where clause
104 if (!empty($selectedValues)) {
105 $query->where("e.activity_type_id IN (#selectedValues)")
106 ->param('selectedValues', $selectedValues);
107 }
108 else {
109 $query->where("e.activity_type_id IS NULL");
110 }
111
112 if (!empty($selectedStatuses)) {
113 $query->where("e.status_id IN (#selectedStatuss)")
114 ->param('selectedStatuss', $selectedStatuses);
115 }
116 $query->where('e.is_current_revision = 1 AND e.is_deleted = 0');
117
118 return $query;
119 }
120
121 }