Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-11-17-13-40-23
[civicrm-core.git] / CRM / Activity / BAO / ActivityTarget.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
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 +--------------------------------------------------------------------+
26*/
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_ActivityTarget extends CRM_Activity_DAO_ActivityContact {
6a488035
TO
41
42 /**
43 * class constructor
44 */
45 function __construct() {
46 parent::__construct();
47 }
48
49 /**
fd31fa4c 50 * function to add activity target
6a488035 51 *
fd31fa4c
EM
52 * @param $params
53 *
54 * @internal param array $activity_id (reference ) an assoc array of name/value pairs
55 * @internal param array $target_contact_id (reference ) the array that holds all the db ids
6a488035
TO
56 *
57 * @return object activity type of object that is added
58 * @access public
6a488035
TO
59 */
60 public static function create(&$params) {
91da6cd5 61 $target = new CRM_Activity_BAO_ActivityContact();
e7e657f0 62 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 63 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
f813f78e 64
6a488035 65 $target->copyValues($params);
a24b3694 66 $target->record_type_id = $targetID ;
6a488035
TO
67 return $target->save();
68 }
69
70 /**
71 * function to retrieve id of target contact by activity_id
72 *
6c8f6e67
EM
73 * @param $activity_id
74 *
75 * @internal param int $id ID of the activity
6a488035
TO
76 *
77 * @return mixed
78 *
79 * @access public
6a488035
TO
80 */
81 static function retrieveTargetIdsByActivityId($activity_id) {
82 $targetArray = array();
83 if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
84 return $targetArray;
85 }
86
e7e657f0 87 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 88 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
89
91da6cd5
DL
90 $sql = "
91SELECT contact_id
92FROM civicrm_activity_contact
93INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
94WHERE activity_id = %1
a24b3694 95AND record_type_id = $targetID
91da6cd5
DL
96AND civicrm_contact.is_deleted = 0
97";
6a488035
TO
98 $target = CRM_Core_DAO::executeQuery($sql, array(1 => array($activity_id, 'Integer')));
99 while ($target->fetch()) {
91da6cd5 100 $targetArray[] = $target->contact_id;
6a488035
TO
101 }
102 return $targetArray;
103 }
104
105 /**
106 * function to retrieve names of target contact by activity_id
107 *
fd31fa4c
EM
108 * @param $activityID
109 *
110 * @internal param int $id ID of the activity
6a488035
TO
111 *
112 * @return array
113 *
114 * @access public
6a488035
TO
115 */
116 static function getTargetNames($activityID) {
117 $targetNames = array();
118
119 if (empty($activityID)) {
120 return $targetNames;
121 }
e7e657f0 122 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 123 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
6a488035 124
91da6cd5
DL
125 $query = "
126SELECT contact_a.id, contact_a.sort_name
127FROM civicrm_contact contact_a
128INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
129WHERE civicrm_activity_contact.activity_id = %1
a24b3694 130AND civicrm_activity_contact.record_type_id = $targetID
91da6cd5
DL
131AND contact_a.is_deleted = 0
132";
6a488035
TO
133 $queryParam = array(1 => array($activityID, 'Integer'));
134
135 $dao = CRM_Core_DAO::executeQuery($query, $queryParam);
136 while ($dao->fetch()) {
137 $targetNames[$dao->id] = $dao->sort_name;
138 }
139
140 return $targetNames;
141 }
142}
143