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