Merge pull request #15707 from eileenmcnaughton/setter
[civicrm-core.git] / CRM / Activity / BAO / ActivityTarget.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
3819f101 35 * This class is for activity assignment functions.
6a488035 36 */
91da6cd5 37class CRM_Activity_BAO_ActivityTarget extends CRM_Activity_DAO_ActivityContact {
6a488035
TO
38
39 /**
fe482240 40 * Class constructor.
6a488035 41 */
00be9182 42 public function __construct() {
6a488035
TO
43 parent::__construct();
44 }
45
46 /**
fe482240 47 * Add activity target.
6a488035 48 *
c490a46a 49 * @param array $params
6a488035 50 *
a6c01b45
CW
51 * @return object
52 * activity type of object that is added
6a488035
TO
53 */
54 public static function create(&$params) {
91da6cd5 55 $target = new CRM_Activity_BAO_ActivityContact();
44f817d4 56 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
a24b3694 57 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
f813f78e 58
6a488035 59 $target->copyValues($params);
33421d01 60 $target->record_type_id = $targetID;
6a488035
TO
61 return $target->save();
62 }
63
64 /**
fe482240 65 * Retrieve id of target contact by activity_id.
6c8f6e67 66 *
c490a46a 67 * @param int $activity_id
6a488035
TO
68 *
69 * @return mixed
6a488035 70 */
00be9182 71 public static function retrieveTargetIdsByActivityId($activity_id) {
96f94695 72 $targetArray = [];
6a488035
TO
73 if (!CRM_Utils_Rule::positiveInteger($activity_id)) {
74 return $targetArray;
75 }
76
44f817d4 77 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
a24b3694 78 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
79
91da6cd5
DL
80 $sql = "
81SELECT contact_id
82FROM civicrm_activity_contact
83INNER JOIN civicrm_contact ON contact_id = civicrm_contact.id
84WHERE activity_id = %1
a24b3694 85AND record_type_id = $targetID
91da6cd5
DL
86AND civicrm_contact.is_deleted = 0
87";
96f94695 88 $target = CRM_Core_DAO::executeQuery($sql, [
89 1 => [
90 $activity_id,
91 'Integer',
92 ],
93 ]);
6a488035 94 while ($target->fetch()) {
91da6cd5 95 $targetArray[] = $target->contact_id;
6a488035
TO
96 }
97 return $targetArray;
98 }
99
100 /**
fe482240 101 * Retrieve names of target contact by activity_id.
fd31fa4c 102 *
c490a46a 103 * @param int $activityID
6a488035
TO
104 *
105 * @return array
6a488035 106 */
00be9182 107 public static function getTargetNames($activityID) {
96f94695 108 $targetNames = [];
6a488035
TO
109
110 if (empty($activityID)) {
111 return $targetNames;
112 }
44f817d4 113 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
a24b3694 114 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
6a488035 115
91da6cd5
DL
116 $query = "
117SELECT contact_a.id, contact_a.sort_name
118FROM civicrm_contact contact_a
119INNER JOIN civicrm_activity_contact ON civicrm_activity_contact.contact_id = contact_a.id
120WHERE civicrm_activity_contact.activity_id = %1
a24b3694 121AND civicrm_activity_contact.record_type_id = $targetID
91da6cd5
DL
122AND contact_a.is_deleted = 0
123";
96f94695 124 $queryParam = [1 => [$activityID, 'Integer']];
6a488035
TO
125
126 $dao = CRM_Core_DAO::executeQuery($query, $queryParam);
127 while ($dao->fetch()) {
128 $targetNames[$dao->id] = $dao->sort_name;
129 }
130
131 return $targetNames;
132 }
96025800 133
6a488035 134}