Priceset 2nd half
[civicrm-core.git] / CRM / Activity / ActionMapping.php
CommitLineData
546a1ecc
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
546a1ecc 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
546a1ecc
TO
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
7808aae6
SB
28/**
29 * @package CRM
6b83d5bd 30 * @copyright CiviCRM LLC (c) 2004-2019
7808aae6
SB
31 */
32
546a1ecc 33
46f5566c
TO
34/**
35 * Class CRM_Activity_ActionMapping
36 *
37 * This defines the scheduled-reminder functionality for contact
38 * entities. It is useful for, e.g., sending a reminder based on
39 * birth date, modification date, or other custom dates on
40 * the contact record.
41 */
546a1ecc
TO
42class CRM_Activity_ActionMapping extends \Civi\ActionSchedule\Mapping {
43
46f5566c
TO
44 /**
45 * The value for civicrm_action_schedule.mapping_id which identifies the
46 * "Activity" mapping.
47 *
48 * Note: This value is chosen to match legacy DB IDs.
49 */
50 const ACTIVITY_MAPPING_ID = 1;
51
52 /**
53 * Register Activity-related action mappings.
54 *
55 * @param \Civi\ActionSchedule\Event\MappingRegisterEvent $registrations
56 */
57 public static function onRegisterActionMappings(\Civi\ActionSchedule\Event\MappingRegisterEvent $registrations) {
96f94695 58 $registrations->register(CRM_Activity_ActionMapping::create([
46f5566c
TO
59 'id' => CRM_Activity_ActionMapping::ACTIVITY_MAPPING_ID,
60 'entity' => 'civicrm_activity',
61 'entity_label' => ts('Activity'),
62 'entity_value' => 'activity_type',
63 'entity_value_label' => ts('Activity Type'),
64 'entity_status' => 'activity_status',
65 'entity_status_label' => ts('Activity Status'),
66 'entity_date_start' => 'activity_date_time',
96f94695 67 ]));
46f5566c
TO
68 }
69
0effed37
TO
70 /**
71 * Get a list of recipient types.
72 *
73 * Note: A single schedule may filter on *zero* or *one* recipient types.
74 * When an admin chooses a value, it's stored in $schedule->recipient.
75 *
76 * @return array
77 * array(string $value => string $label).
78 * Ex: array('assignee' => 'Activity Assignee').
79 */
80 public function getRecipientTypes() {
81 return \CRM_Core_OptionGroup::values('activity_contacts');
82 }
83
546a1ecc
TO
84 /**
85 * Generate a query to locate recipients who match the given
86 * schedule.
87 *
88 * @param \CRM_Core_DAO_ActionSchedule $schedule
89 * The schedule as configured by the administrator.
90 * @param string $phase
91 * See, e.g., RecipientBuilder::PHASE_RELATION_FIRST.
ad37ac8e 92 *
93 * @param array $defaultParams
94 *
546a1ecc
TO
95 * @return \CRM_Utils_SQL_Select
96 * @see RecipientBuilder
546a1ecc 97 */
efc40454 98 public function createQuery($schedule, $phase, $defaultParams) {
546a1ecc
TO
99 $selectedValues = (array) \CRM_Utils_Array::explodePadded($schedule->entity_value);
100 $selectedStatuses = (array) \CRM_Utils_Array::explodePadded($schedule->entity_status);
101
efc40454 102 $query = \CRM_Utils_SQL_Select::from("{$this->entity} e")->param($defaultParams);
546a1ecc
TO
103 $query['casAddlCheckFrom'] = 'civicrm_activity e';
104 $query['casContactIdField'] = 'r.contact_id';
105 $query['casEntityIdField'] = 'e.id';
106 $query['casContactTableAlias'] = NULL;
107 $query['casDateField'] = 'e.activity_date_time';
108
109 if (!is_null($schedule->limit_to)) {
44f817d4 110 $activityContacts = \CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
546a1ecc
TO
111 if ($schedule->limit_to == 0 || !isset($activityContacts[$schedule->recipient])) {
112 $recipientTypeId = \CRM_Utils_Array::key('Activity Targets', $activityContacts);
113 }
114 else {
115 $recipientTypeId = $schedule->recipient;
116 }
117 $query->join('r', "INNER JOIN civicrm_activity_contact r ON r.activity_id = e.id AND record_type_id = {$recipientTypeId}");
118 }
119 // build where clause
120 if (!empty($selectedValues)) {
121 $query->where("e.activity_type_id IN (#selectedValues)")
122 ->param('selectedValues', $selectedValues);
123 }
124 else {
125 $query->where("e.activity_type_id IS NULL");
126 }
127
128 if (!empty($selectedStatuses)) {
129 $query->where("e.status_id IN (#selectedStatuss)")
130 ->param('selectedStatuss', $selectedStatuses);
131 }
132 $query->where('e.is_current_revision = 1 AND e.is_deleted = 0');
133
134 return $query;
135 }
136
137}