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