Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-04-23-15-17-29
[civicrm-core.git] / CRM / Activity / BAO / ActivityAssignment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2013
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is for activity assignment functions
38 *
39 */
40 class CRM_Activity_BAO_ActivityAssignment extends CRM_Activity_DAO_ActivityContact {
41
42 /**
43 * class constructor
44 */
45 function __construct() {
46 parent::__construct();
47 }
48
49 /**
50 * Add activity assignment.
51 *
52 * @param array $params (reference ) an assoc array of name/value pairs
53 * @param array $ids (reference ) the array that holds all the db ids
54 *
55 * @return object activity type of object that is added
56 * @access public
57 *
58 */
59 public static function create(&$params) {
60 $assignment = new CRM_Activity_BAO_ActivityAssignment();
61 $activityContacts = CRM_Core_PseudoConstant::activityContacts('name');
62 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
63
64 $assignment->copyValues($params);
65 $assignment->record_type_id = $assigneeID;
66 if (isset($assignment->assignee_contact_id)) {
67 $assignment->contact_id = $assignment->assignee_contact_id;
68 }
69 return $assignment->save();
70 }
71
72 /**
73 * Retrieve assignee_id by activity_id
74 *
75 * @param int $id ID of the activity
76 *
77 * @return void
78 *
79 * @access public
80 *
81 */
82 static function retrieveAssigneeIdsByActivityId($activity_id) {
83 $assigneeArray = array();
84 if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
85 return $assigneeArray;
86 }
87
88 $activityContacts = CRM_Core_PseudoConstant::activityContacts('name');
89 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
90
91 $sql = "
92 SELECT contact_id
93 FROM civicrm_activity_contact
94 INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
95 WHERE activity_id = %1
96 AND record_type_id = $assigneeID
97 AND civicrm_contact.is_deleted = 0
98 ";
99 $assignment = CRM_Core_DAO::executeQuery($sql, array(1 => array($activity_id, 'Integer')));
100 while ($assignment->fetch()) {
101 $assigneeArray[] = $assignment->contact_id;
102 }
103
104 return $assigneeArray;
105 }
106
107 /**
108 * Retrieve assignee names by activity_id
109 *
110 * @param int $id ID of the activity
111 * @param boolean $isDisplayName if set returns display names of assignees
112 * @param boolean $skipDetails if false returns all details of assignee contact.
113 *
114 * @return array
115 *
116 * @access public
117 *
118 */
119 static function getAssigneeNames($activityID, $isDisplayName = FALSE, $skipDetails = TRUE) {
120 $assigneeNames = array();
121 if (empty($activityID)) {
122 return $assigneeNames;
123 }
124 $activityContacts = CRM_Core_PseudoConstant::activityContacts('name');
125 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
126
127 $whereClause = "";
128 if (!$skipDetails) {
129 $whereClause = " AND ce.is_primary= 1";
130 }
131
132 $query = "
133 SELECT contact_a.id, contact_a.sort_name, contact_a.display_name, ce.email
134 FROM civicrm_contact contact_a
135 INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
136 LEFT JOIN civicrm_email ce ON ce.contact_id = contact_a.id
137 WHERE civicrm_activity_contact.activity_id = %1
138 AND contact_a.is_deleted = 0
139 AND civicrm_activity_contact.record_type_id = $assigneeID
140 {$whereClause}
141 ";
142
143 $queryParam = array(1 => array($activityID, 'Integer'));
144 $dao = CRM_Core_DAO::executeQuery($query, $queryParam);
145 while ($dao->fetch()) {
146 if (!$isDisplayName) {
147 $assigneeNames[$dao->id] = $dao->sort_name;
148 }
149 else {
150 if ($skipDetails) {
151 $assigneeNames[$dao->id] = $dao->display_name;
152 }
153 else {
154 $assigneeNames[$dao->id]['contact_id'] = $dao->id;
155 $assigneeNames[$dao->id]['display_name'] = $dao->display_name;
156 $assigneeNames[$dao->id]['sort_name'] = $dao->sort_name;
157 $assigneeNames[$dao->id]['email'] = $dao->email;
158 $assigneeNames[$dao->id]['role'] = ts('Activity Assignee');
159 }
160 }
161 }
162 return $assigneeNames;
163 }
164 }
165