Merge pull request #4511 from giant-rabbit/ajax-fatal-log-fix
[civicrm-core.git] / CRM / Activity / BAO / ActivityContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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_ActivityContact extends CRM_Activity_DAO_ActivityContact {
41
42 /**
43 * Class constructor
44 */
45 function __construct() {
46 parent::__construct();
47 }
48
49 /**
50 * Funtion to add activity contact
51 *
52 * @param array $params the values for this table: activity id, contact id, record type
53 *
54 * @return object activity_contact object
55 * @access public
56 *
57 */
58 public static function create(&$params) {
59 $activityContact = new CRM_Activity_DAO_ActivityContact();
60
61 $activityContact->copyValues($params);
62 if (!$activityContact->find(TRUE)) {
63 return $activityContact->save();
64 }
65 return $activityContact;
66 }
67
68 /**
69 * Retrieve names of contact by activity_id
70 *
71 * @param int $activityID
72 * @param int $recordTypeID
73 * @param bool $alsoIDs
74 *
75 * @return array
76 *
77 * @access public
78 */
79 static function getNames($activityID, $recordTypeID, $alsoIDs = FALSE) {
80 $names = array();
81 $ids = array();
82
83 if (empty($activityID)) {
84 return $alsoIDs ? array($names, $ids) : $names;
85 }
86
87 $query = "
88 SELECT contact_a.id, contact_a.sort_name
89 FROM civicrm_contact contact_a
90 INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
91 WHERE civicrm_activity_contact.activity_id = %1
92 AND civicrm_activity_contact.record_type_id = %2
93 AND contact_a.is_deleted = 0
94 ";
95 $params = array(
96 1 => array($activityID, 'Integer'),
97 2 => array($recordTypeID, 'Integer')
98 );
99
100 $dao = CRM_Core_DAO::executeQuery($query, $params);
101 while ($dao->fetch()) {
102 $names[$dao->id] = $dao->sort_name;
103 $ids[] = $dao->id;
104 }
105
106 return $alsoIDs ? array($names, $ids) : $names;
107 }
108
109 /**
110 * Retrieve id of target contact by activity_id
111 *
112 * @param int $activityID
113 * @param int $recordTypeID
114 *
115 * @return mixed
116 *
117 * @access public
118 */
119 static function retrieveContactIdsByActivityId($activityID, $recordTypeID) {
120 $activityContact = array();
121 if (!CRM_Utils_Rule::positiveInteger($activityID) ||
122 !CRM_Utils_Rule::positiveInteger($recordTypeID)) {
123 return $activityContact;
124 }
125
126 $sql = " SELECT contact_id
127 FROM civicrm_activity_contact
128 INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
129 WHERE activity_id = %1
130 AND record_type_id = %2
131 AND civicrm_contact.is_deleted = 0
132 ";
133 $params = array(
134 1 => array($activityID, 'Integer'),
135 2 => array($recordTypeID, 'Integer')
136 );
137
138 $dao = CRM_Core_DAO::executeQuery($sql, $params);
139 while ($dao->fetch()) {
140 $activityContact[] = $dao->contact_id;
141 }
142 return $activityContact;
143 }
144
145 /**
146 * Get the links associate array as defined by the links.ini file.
147 *
148 *
149 * Experimental... -
150 * Should look a bit like
151 * [local_col_name] => "related_tablename:related_col_name"
152 *
153 *
154 * @return array|null
155 * array = if there are links defined for this table.
156 * empty array - if there is a links.ini file, but no links on this table
157 * null - if no links.ini exists for this database (hence try auto_links).
158 * @access public
159 * @see DB_DataObject::getLinks(), DB_DataObject::getLink()
160 */
161 /**
162 * @return array|null
163 */
164 function links() {
165 $link = array('activity_id' => 'civicrm_activity:id');
166 return $link;
167 }
168 }