CRM-13812#COMMENT-54485
[civicrm-core.git] / CRM / Activity / BAO / ActivityTarget.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
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 */
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 /**
50 * funtion to add activity target
51 *
52 * @param array $activity_id (reference ) an assoc array of name/value pairs
53 * @param array $target_contact_id (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) {
91da6cd5 60 $target = new CRM_Activity_BAO_ActivityContact();
e7e657f0 61 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 62 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
f813f78e 63
6a488035 64 $target->copyValues($params);
a24b3694 65 $target->record_type_id = $targetID ;
6a488035
TO
66 return $target->save();
67 }
68
69 /**
70 * function to retrieve id of target contact by activity_id
71 *
72 * @param int $id ID of the activity
73 *
74 * @return mixed
75 *
76 * @access public
77 *
78 */
79 static function retrieveTargetIdsByActivityId($activity_id) {
80 $targetArray = array();
81 if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
82 return $targetArray;
83 }
84
e7e657f0 85 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 86 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
87
91da6cd5
DL
88 $sql = "
89SELECT contact_id
90FROM civicrm_activity_contact
91INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
92WHERE activity_id = %1
a24b3694 93AND record_type_id = $targetID
91da6cd5
DL
94AND civicrm_contact.is_deleted = 0
95";
6a488035
TO
96 $target = CRM_Core_DAO::executeQuery($sql, array(1 => array($activity_id, 'Integer')));
97 while ($target->fetch()) {
91da6cd5 98 $targetArray[] = $target->contact_id;
6a488035
TO
99 }
100 return $targetArray;
101 }
102
103 /**
104 * function to retrieve names of target contact by activity_id
105 *
106 * @param int $id ID of the activity
107 *
108 * @return array
109 *
110 * @access public
111 *
112 */
113 static function getTargetNames($activityID) {
114 $targetNames = array();
115
116 if (empty($activityID)) {
117 return $targetNames;
118 }
e7e657f0 119 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
a24b3694 120 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
6a488035 121
91da6cd5
DL
122 $query = "
123SELECT contact_a.id, contact_a.sort_name
124FROM civicrm_contact contact_a
125INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
126WHERE civicrm_activity_contact.activity_id = %1
a24b3694 127AND civicrm_activity_contact.record_type_id = $targetID
91da6cd5
DL
128AND contact_a.is_deleted = 0
129";
6a488035
TO
130 $queryParam = array(1 => array($activityID, 'Integer'));
131
132 $dao = CRM_Core_DAO::executeQuery($query, $queryParam);
133 while ($dao->fetch()) {
134 $targetNames[$dao->id] = $dao->sort_name;
135 }
136
137 return $targetNames;
138 }
139}
140