Merge pull request #15841 from mattwire/participant_cleanup_removeparticipantfrominput
[civicrm-core.git] / CRM / Activity / BAO / ActivityContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * This class is for activity assignment functions.
20 */
21 class CRM_Activity_BAO_ActivityContact extends CRM_Activity_DAO_ActivityContact {
22
23 /**
24 * Class constructor.
25 */
26 public function __construct() {
27 parent::__construct();
28 }
29
30 /**
31 * Function to add activity contact.
32 *
33 * @param array $params
34 * The values for this table: activity id, contact id, record type.
35 *
36 * @return object
37 * activity_contact object
38 */
39 public static function create(&$params) {
40 $activityContact = new CRM_Activity_DAO_ActivityContact();
41
42 $activityContact->copyValues($params);
43 if (!$activityContact->find(TRUE)) {
44 return $activityContact->save();
45 }
46 return $activityContact;
47 }
48
49 /**
50 * Retrieve names of contact by activity_id.
51 *
52 * @param int $activityID
53 * @param int $recordTypeID
54 * @param bool $alsoIDs
55 *
56 * @return array
57 */
58 public static function getNames($activityID, $recordTypeID, $alsoIDs = FALSE) {
59 $names = [];
60 $ids = [];
61
62 if (empty($activityID)) {
63 return $alsoIDs ? [$names, $ids] : $names;
64 }
65
66 $query = "
67 SELECT contact_a.id, contact_a.sort_name
68 FROM civicrm_contact contact_a
69 INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
70 WHERE civicrm_activity_contact.activity_id = %1
71 AND civicrm_activity_contact.record_type_id = %2
72 AND contact_a.is_deleted = 0
73 ";
74 $params = [
75 1 => [$activityID, 'Integer'],
76 2 => [$recordTypeID, 'Integer'],
77 ];
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
85 return $alsoIDs ? [$names, $ids] : $names;
86 }
87
88 /**
89 * Retrieve id of target contact by activity_id.
90 *
91 * @param int $activityID
92 * @param int $recordTypeID
93 *
94 * @return mixed
95 */
96 public static function retrieveContactIdsByActivityId($activityID, $recordTypeID) {
97 $activityContact = [];
98 if (!CRM_Utils_Rule::positiveInteger($activityID) ||
99 !CRM_Utils_Rule::positiveInteger($recordTypeID)
100 ) {
101 return $activityContact;
102 }
103
104 $sql = " SELECT contact_id
105 FROM civicrm_activity_contact
106 INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
107 WHERE activity_id = %1
108 AND record_type_id = %2
109 AND civicrm_contact.is_deleted = 0
110 ";
111 $params = [
112 1 => [$activityID, 'Integer'],
113 2 => [$recordTypeID, 'Integer'],
114 ];
115
116 $dao = CRM_Core_DAO::executeQuery($sql, $params);
117 while ($dao->fetch()) {
118 $activityContact[] = $dao->contact_id;
119 }
120 return $activityContact;
121 }
122
123 /**
124 * Get the links associate array as defined by the links.ini file.
125 *
126 * Experimental... -
127 * Should look a bit like
128 * [local_col_name] => "related_tablename:related_col_name"
129 *
130 * @see DB_DataObject::getLinks()
131 * @see DB_DataObject::getLink()
132 *
133 * @return array|null
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).
137 */
138 public function links() {
139 $link = ['activity_id' => 'civicrm_activity:id'];
140 return $link;
141 }
142
143 }