INFRA-132 - CRM/ - Fix misc oddball syntax
[civicrm-core.git] / CRM / Activity / BAO / ActivityTarget.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class is for activity assignment functions
38 *
39 */
40 class CRM_Activity_BAO_ActivityTarget extends CRM_Activity_DAO_ActivityContact {
41
42 /**
43 * Class constructor
44 */
45 public function __construct() {
46 parent::__construct();
47 }
48
49 /**
50 * Add activity target
51 *
52 * @param array $params
53 *
54 * @return object activity type of object that is added
55 */
56 public static function create(&$params) {
57 $target = new CRM_Activity_BAO_ActivityContact();
58 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
59 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
60
61 $target->copyValues($params);
62 $target->record_type_id = $targetID;
63 return $target->save();
64 }
65
66 /**
67 * Retrieve id of target contact by activity_id
68 *
69 * @param int $activity_id
70 *
71 * @return mixed
72 *
73 */
74 public static function retrieveTargetIdsByActivityId($activity_id) {
75 $targetArray = array();
76 if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
77 return $targetArray;
78 }
79
80 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
81 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
82
83 $sql = "
84 SELECT contact_id
85 FROM civicrm_activity_contact
86 INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
87 WHERE activity_id = %1
88 AND record_type_id = $targetID
89 AND civicrm_contact.is_deleted = 0
90 ";
91 $target = CRM_Core_DAO::executeQuery($sql, array(1 => array($activity_id, 'Integer')));
92 while ($target->fetch()) {
93 $targetArray[] = $target->contact_id;
94 }
95 return $targetArray;
96 }
97
98 /**
99 * Retrieve names of target contact by activity_id
100 *
101 * @param int $activityID
102 *
103 * @return array
104 *
105 */
106 public static function getTargetNames($activityID) {
107 $targetNames = array();
108
109 if (empty($activityID)) {
110 return $targetNames;
111 }
112 $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name');
113 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
114
115 $query = "
116 SELECT contact_a.id, contact_a.sort_name
117 FROM civicrm_contact contact_a
118 INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
119 WHERE civicrm_activity_contact.activity_id = %1
120 AND civicrm_activity_contact.record_type_id = $targetID
121 AND contact_a.is_deleted = 0
122 ";
123 $queryParam = array(1 => array($activityID, 'Integer'));
124
125 $dao = CRM_Core_DAO::executeQuery($query, $queryParam);
126 while ($dao->fetch()) {
127 $targetNames[$dao->id] = $dao->sort_name;
128 }
129
130 return $targetNames;
131 }
132 }