Merge pull request #15821 from seamuslee001/dev_core_183_custom_group
[civicrm-core.git] / CRM / Activity / BAO / ActivityContact.php
CommitLineData
adc7f09e
DL
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
adc7f09e 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
adc7f09e 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
adc7f09e
DL
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
adc7f09e
DL
16 */
17
18/**
3819f101 19 * This class is for activity assignment functions.
adc7f09e
DL
20 */
21class CRM_Activity_BAO_ActivityContact extends CRM_Activity_DAO_ActivityContact {
22
23 /**
fe482240 24 * Class constructor.
adc7f09e 25 */
00be9182 26 public function __construct() {
adc7f09e
DL
27 parent::__construct();
28 }
29
30 /**
2e2605fe 31 * Function to add activity contact.
adc7f09e 32 *
041ab3d1
TO
33 * @param array $params
34 * The values for this table: activity id, contact id, record type.
adc7f09e 35 *
a6c01b45
CW
36 * @return object
37 * activity_contact object
adc7f09e
DL
38 */
39 public static function create(&$params) {
40 $activityContact = new CRM_Activity_DAO_ActivityContact();
41
42 $activityContact->copyValues($params);
2517d079
DL
43 if (!$activityContact->find(TRUE)) {
44 return $activityContact->save();
45 }
46 return $activityContact;
adc7f09e 47 }
f04255e4
DL
48
49 /**
fe482240 50 * Retrieve names of contact by activity_id.
f04255e4 51 *
c490a46a
CW
52 * @param int $activityID
53 * @param int $recordTypeID
6c8f6e67
EM
54 * @param bool $alsoIDs
55 *
f04255e4 56 * @return array
f04255e4 57 */
00be9182 58 public static function getNames($activityID, $recordTypeID, $alsoIDs = FALSE) {
96f94695 59 $names = [];
60 $ids = [];
f04255e4
DL
61
62 if (empty($activityID)) {
96f94695 63 return $alsoIDs ? [$names, $ids] : $names;
f04255e4
DL
64 }
65
66 $query = "
67SELECT contact_a.id, contact_a.sort_name
68FROM civicrm_contact contact_a
69INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
70WHERE civicrm_activity_contact.activity_id = %1
a24b3694 71AND civicrm_activity_contact.record_type_id = %2
f04255e4
DL
72AND contact_a.is_deleted = 0
73";
96f94695 74 $params = [
75 1 => [$activityID, 'Integer'],
76 2 => [$recordTypeID, 'Integer'],
77 ];
f04255e4
DL
78
79 $dao = CRM_Core_DAO::executeQuery($query, $params);
80 while ($dao->fetch()) {
81 $names[$dao->id] = $dao->sort_name;
82 $ids[] = $dao->id;
83 }
84
96f94695 85 return $alsoIDs ? [$names, $ids] : $names;
f04255e4
DL
86 }
87
034500d4 88 /**
fe482240 89 * Retrieve id of target contact by activity_id.
034500d4 90 *
100fef9d
CW
91 * @param int $activityID
92 * @param int $recordTypeID
77b97be7 93 *
034500d4 94 * @return mixed
034500d4 95 */
00be9182 96 public static function retrieveContactIdsByActivityId($activityID, $recordTypeID) {
96f94695 97 $activityContact = [];
034500d4 98 if (!CRM_Utils_Rule::positiveInteger($activityID) ||
353ffa53
TO
99 !CRM_Utils_Rule::positiveInteger($recordTypeID)
100 ) {
034500d4 101 return $activityContact;
102 }
103
104 $sql = " SELECT contact_id
105FROM civicrm_activity_contact
106INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
107WHERE activity_id = %1
2517d079
DL
108AND record_type_id = %2
109AND civicrm_contact.is_deleted = 0
034500d4 110";
96f94695 111 $params = [
112 1 => [$activityID, 'Integer'],
113 2 => [$recordTypeID, 'Integer'],
114 ];
034500d4 115
116 $dao = CRM_Core_DAO::executeQuery($sql, $params);
117 while ($dao->fetch()) {
118 $activityContact[] = $dao->contact_id;
119 }
120 return $activityContact;
121 }
122
ffd93213
EM
123 /**
124 * Get the links associate array as defined by the links.ini file.
125 *
ffd93213
EM
126 * Experimental... -
127 * Should look a bit like
128 * [local_col_name] => "related_tablename:related_col_name"
129 *
7808aae6
SB
130 * @see DB_DataObject::getLinks()
131 * @see DB_DataObject::getLink()
ffd93213 132 *
2b37475d 133 * @return array|null
62d3ee27
SL
134 * array if there are links defined for this table.
135 * empty array - if there is a links.ini file, but no links on this table
136 * null - if no links.ini exists for this database (hence try auto_links).
ffd93213 137 */
00be9182 138 public function links() {
96f94695 139 $link = ['activity_id' => 'civicrm_activity:id'];
6e1bb60c
N
140 return $link;
141 }
96025800 142
adc7f09e 143}