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