Added the current uncommited changes to production code, and rebased to 4.6.8
[civicrm-core.git] / CRM / Activity / BAO / ActivityAssignment.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
3819f101 35 * This class is for activity assignment functions.
6a488035 36 */
91da6cd5 37class CRM_Activity_BAO_ActivityAssignment extends CRM_Activity_DAO_ActivityContact {
6a488035
TO
38
39 /**
fe482240 40 * Class constructor.
6a488035 41 */
00be9182 42 public function __construct() {
6a488035
TO
43 parent::__construct();
44 }
45
46 /**
47 * Add activity assignment.
48 *
041ab3d1
TO
49 * @param array $params
50 * (reference ) an assoc array of name/value pairs.
fd31fa4c 51 *
a6c01b45
CW
52 * @return object
53 * activity type of object that is added
6a488035
TO
54 */
55 public static function create(&$params) {
cc1c86e9 56 $assignment = new CRM_Activity_BAO_ActivityContact();
44f817d4 57 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
a24b3694 58 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
6a488035
TO
59
60 $assignment->copyValues($params);
a24b3694 61 $assignment->record_type_id = $assigneeID;
cc1c86e9 62
6a488035
TO
63 return $assignment->save();
64 }
65
66 /**
fe482240 67 * Retrieve assignee_id by activity_id.
6a488035 68 *
c490a46a 69 * @param int $activity_id
6a488035 70 *
91d49cae 71 * @return array
6a488035 72 */
00be9182 73 public static function retrieveAssigneeIdsByActivityId($activity_id) {
96f94695 74 $assigneeArray = [];
6a488035
TO
75 if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
76 return $assigneeArray;
77 }
78
44f817d4 79 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
a24b3694 80 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
81
91da6cd5 82 $sql = "
1d85d241
DL
83SELECT contact_id
84FROM civicrm_activity_contact
91da6cd5
DL
85INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
86WHERE activity_id = %1
a24b3694 87AND record_type_id = $assigneeID
91da6cd5
DL
88AND civicrm_contact.is_deleted = 0
89";
96f94695 90 $assignment = CRM_Core_DAO::executeQuery($sql, [
91 1 => [
92 $activity_id,
93 'Integer',
94 ],
95 ]);
6a488035 96 while ($assignment->fetch()) {
91da6cd5 97 $assigneeArray[] = $assignment->contact_id;
6a488035
TO
98 }
99
100 return $assigneeArray;
101 }
102
103 /**
fe482240 104 * Retrieve assignee names by activity_id.
6a488035 105 *
041ab3d1
TO
106 * @param array $activityIDs
107 * IDs of the activities.
108 * @param bool $isDisplayName
109 * If set returns display names of assignees.
110 * @param bool $skipDetails
111 * If false returns all details of assignee contact.
6a488035
TO
112 *
113 * @return array
6a488035 114 */
00be9182 115 public static function getAssigneeNames($activityIDs, $isDisplayName = FALSE, $skipDetails = TRUE) {
96f94695 116 $assigneeNames = [];
90b05581 117 if (empty($activityIDs)) {
6a488035
TO
118 return $assigneeNames;
119 }
44f817d4 120 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
a24b3694 121 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
6a488035
TO
122
123 $whereClause = "";
124 if (!$skipDetails) {
125 $whereClause = " AND ce.is_primary= 1";
126 }
90b05581 127 $inClause = implode(",", $activityIDs);
6a488035 128
91da6cd5 129 $query = "
90b05581
DG
130SELECT contact_a.id, contact_a.sort_name, contact_a.display_name, ce.email,
131 civicrm_activity_contact.activity_id
91da6cd5 132FROM civicrm_contact contact_a
1d85d241 133INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
91da6cd5 134LEFT JOIN civicrm_email ce ON ce.contact_id = contact_a.id
90b05581 135WHERE civicrm_activity_contact.activity_id IN ( $inClause )
91da6cd5 136AND contact_a.is_deleted = 0
f813f78e 137AND civicrm_activity_contact.record_type_id = $assigneeID
91da6cd5
DL
138 {$whereClause}
139";
6a488035 140
90b05581 141 $dao = CRM_Core_DAO::executeQuery($query);
6a488035
TO
142 while ($dao->fetch()) {
143 if (!$isDisplayName) {
144 $assigneeNames[$dao->id] = $dao->sort_name;
145 }
146 else {
147 if ($skipDetails) {
148 $assigneeNames[$dao->id] = $dao->display_name;
149 }
150 else {
151 $assigneeNames[$dao->id]['contact_id'] = $dao->id;
152 $assigneeNames[$dao->id]['display_name'] = $dao->display_name;
153 $assigneeNames[$dao->id]['sort_name'] = $dao->sort_name;
154 $assigneeNames[$dao->id]['email'] = $dao->email;
155 $assigneeNames[$dao->id]['role'] = ts('Activity Assignee');
90b05581 156 $assigneeNames[$dao->id]['activity_id'] = $dao->activity_id;
6a488035
TO
157 }
158 }
159 }
160 return $assigneeNames;
161 }
96025800 162
6a488035 163}