Commit | Line | Data |
---|---|---|
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 | /** | |
7808aae6 | 35 | * This class is for activity functions. |
6a488035 TO |
36 | */ |
37 | class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { | |
38 | ||
ce9d78e1 CW |
39 | /** |
40 | * Activity status types | |
41 | */ | |
42 | const | |
43 | INCOMPLETE = 0, | |
44 | COMPLETED = 1, | |
45 | CANCELLED = 2; | |
46 | ||
6a488035 | 47 | /** |
fe482240 | 48 | * Static field for all the activity information that we can potentially export. |
6a488035 TO |
49 | * |
50 | * @var array | |
6a488035 TO |
51 | */ |
52 | static $_exportableFields = NULL; | |
53 | ||
54 | /** | |
fe482240 | 55 | * Static field for all the activity information that we can potentially import. |
6a488035 TO |
56 | * |
57 | * @var array | |
6a488035 TO |
58 | */ |
59 | static $_importableFields = NULL; | |
60 | ||
61 | /** | |
db7de9c1 | 62 | * Check if there is absolute minimum of data to add the object. |
6a488035 | 63 | * |
041ab3d1 TO |
64 | * @param array $params |
65 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 66 | * |
59f4c9ee | 67 | * @return bool |
6a488035 TO |
68 | */ |
69 | public static function dataExists(&$params) { | |
8cc574cf | 70 | if (!empty($params['source_contact_id']) || !empty($params['id'])) { |
6a488035 TO |
71 | return TRUE; |
72 | } | |
73 | return FALSE; | |
74 | } | |
75 | ||
76 | /** | |
d66c61b6 | 77 | * @deprecated |
78 | * | |
d8689418 | 79 | * Fetch object based on array of properties. |
6a488035 | 80 | * |
041ab3d1 TO |
81 | * @param array $params |
82 | * (reference ) an assoc array of name/value pairs. | |
83 | * @param array $defaults | |
84 | * (reference ) an assoc array to hold the flattened values. | |
1cfa04c4 | 85 | * |
16b10e64 | 86 | * @return CRM_Activity_DAO_Activity |
6a488035 TO |
87 | */ |
88 | public static function retrieve(&$params, &$defaults) { | |
d66c61b6 | 89 | // this will bypass acls - use the api instead. |
90 | // @todo add deprecation logging to this function. | |
6a488035 TO |
91 | $activity = new CRM_Activity_DAO_Activity(); |
92 | $activity->copyValues($params); | |
93 | ||
94 | if ($activity->find(TRUE)) { | |
44f817d4 | 95 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
034500d4 | 96 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
97 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); | |
98 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
99 | ||
6a488035 | 100 | // TODO: at some stage we'll have to deal |
7808aae6 SB |
101 | // with multiple values for assignees and targets, but |
102 | // for now, let's just fetch first row. | |
034500d4 | 103 | $defaults['assignee_contact'] = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $assigneeID); |
104 | $assignee_contact_names = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $assigneeID); | |
6a488035 | 105 | $defaults['assignee_contact_value'] = implode('; ', $assignee_contact_names); |
eb873b6e | 106 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); |
d66c61b6 | 107 | if ($activity->activity_type_id != CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email')) { |
034500d4 | 108 | $defaults['target_contact'] = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $targetID); |
109 | $target_contact_names = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $targetID); | |
6a488035 TO |
110 | $defaults['target_contact_value'] = implode('; ', $target_contact_names); |
111 | } | |
112 | elseif (CRM_Core_Permission::check('access CiviMail') || | |
113 | (CRM_Mailing_Info::workflowEnabled() && | |
114 | CRM_Core_Permission::check('create mailings') | |
115 | ) | |
116 | ) { | |
117 | $defaults['mailingId'] = CRM_Utils_System::url('civicrm/mailing/report', | |
eb873b6e | 118 | "mid={$activity->source_record_id}&reset=1&atype={$activity->activity_type_id}&aid={$activity->id}&cid={$sourceContactId}&context=activity" |
6a488035 TO |
119 | ); |
120 | } | |
121 | else { | |
122 | $defaults['target_contact_value'] = ts('(recipients)'); | |
123 | } | |
b319d00a | 124 | |
65ebc887 | 125 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); |
ad674e50 | 126 | $defaults['source_contact_id'] = $sourceContactId; |
6a488035 | 127 | |
65ebc887 | 128 | if ($sourceContactId && |
6a488035 | 129 | !CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
65ebc887 | 130 | $sourceContactId, |
6a488035 TO |
131 | 'is_deleted' |
132 | ) | |
133 | ) { | |
134 | $defaults['source_contact'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
65ebc887 | 135 | $sourceContactId, |
6a488035 TO |
136 | 'sort_name' |
137 | ); | |
138 | } | |
139 | ||
7808aae6 | 140 | // Get case subject. |
6a488035 TO |
141 | $defaults['case_subject'] = CRM_Case_BAO_Case::getCaseSubject($activity->id); |
142 | ||
143 | CRM_Core_DAO::storeValues($activity, $defaults); | |
144 | ||
145 | return $activity; | |
146 | } | |
147 | return NULL; | |
148 | } | |
149 | ||
150 | /** | |
d8689418 | 151 | * Delete the activity. |
6a488035 | 152 | * |
041ab3d1 | 153 | * @param array $params |
e63aff1c | 154 | * @param bool $moveToTrash |
6a488035 | 155 | * |
d8689418 | 156 | * @return mixed |
6a488035 TO |
157 | */ |
158 | public static function deleteActivity(&$params, $moveToTrash = FALSE) { | |
159 | // CRM-9137 | |
a7488080 | 160 | if (!empty($params['id']) && !is_array($params['id'])) { |
6a488035 TO |
161 | CRM_Utils_Hook::pre('delete', 'Activity', $params['id'], $params); |
162 | } | |
163 | else { | |
164 | CRM_Utils_Hook::pre('delete', 'Activity', NULL, $params); | |
165 | } | |
166 | ||
167 | $transaction = new CRM_Core_Transaction(); | |
168 | if (is_array(CRM_Utils_Array::value('source_record_id', $params))) { | |
169 | $sourceRecordIds = implode(',', $params['source_record_id']); | |
170 | } | |
171 | else { | |
172 | $sourceRecordIds = CRM_Utils_Array::value('source_record_id', $params); | |
173 | } | |
174 | ||
175 | $result = NULL; | |
176 | if (!$moveToTrash) { | |
177 | if (!isset($params['id'])) { | |
178 | if (is_array($params['activity_type_id'])) { | |
179 | $activityTypes = implode(',', $params['activity_type_id']); | |
180 | } | |
181 | else { | |
182 | $activityTypes = $params['activity_type_id']; | |
183 | } | |
184 | ||
185 | $query = "DELETE FROM civicrm_activity WHERE source_record_id IN ({$sourceRecordIds}) AND activity_type_id IN ( {$activityTypes} )"; | |
186 | $dao = CRM_Core_DAO::executeQuery($query); | |
187 | } | |
188 | else { | |
189 | $activity = new CRM_Activity_DAO_Activity(); | |
190 | $activity->copyValues($params); | |
191 | $result = $activity->delete(); | |
192 | ||
193 | // CRM-8708 | |
194 | $activity->case_id = CRM_Case_BAO_Case::getCaseIdByActivityId($activity->id); | |
93bcc9e8 BS |
195 | |
196 | // CRM-13994 delete activity entity_tag | |
197 | $query = "DELETE FROM civicrm_entity_tag WHERE entity_table = 'civicrm_activity' AND entity_id = {$activity->id}"; | |
198 | $dao = CRM_Core_DAO::executeQuery($query); | |
6a488035 TO |
199 | } |
200 | } | |
201 | else { | |
202 | $activity = new CRM_Activity_DAO_Activity(); | |
203 | $activity->copyValues($params); | |
204 | ||
205 | $activity->is_deleted = 1; | |
206 | $result = $activity->save(); | |
207 | ||
93bcc9e8 BS |
208 | // CRM-4525 log activity delete |
209 | $logMsg = 'Case Activity deleted for'; | |
96f94695 | 210 | $msgs = []; |
034500d4 | 211 | |
44f817d4 | 212 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
034500d4 | 213 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
214 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
215 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
216 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); | |
6a488035 TO |
217 | if ($sourceContactId) { |
218 | $msgs[] = " source={$sourceContactId}"; | |
219 | } | |
034500d4 | 220 | |
7808aae6 | 221 | // get target contacts. |
034500d4 | 222 | $targetContactIds = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $targetID); |
6a488035 TO |
223 | if (!empty($targetContactIds)) { |
224 | $msgs[] = " target =" . implode(',', array_keys($targetContactIds)); | |
225 | } | |
7808aae6 | 226 | // get assignee contacts. |
034500d4 | 227 | $assigneeContactIds = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $assigneeID); |
6a488035 TO |
228 | if (!empty($assigneeContactIds)) { |
229 | $msgs[] = " assignee =" . implode(',', array_keys($assigneeContactIds)); | |
230 | } | |
231 | ||
232 | $logMsg .= implode(', ', $msgs); | |
233 | ||
234 | self::logActivityAction($activity, $logMsg); | |
235 | } | |
236 | ||
237 | // delete the recently created Activity | |
238 | if ($result) { | |
96f94695 | 239 | $activityRecent = [ |
6a488035 TO |
240 | 'id' => $activity->id, |
241 | 'type' => 'Activity', | |
96f94695 | 242 | ]; |
6a488035 TO |
243 | CRM_Utils_Recent::del($activityRecent); |
244 | } | |
245 | ||
246 | $transaction->commit(); | |
247 | if (isset($activity)) { | |
248 | // CRM-8708 | |
249 | $activity->case_id = CRM_Case_BAO_Case::getCaseIdByActivityId($activity->id); | |
250 | CRM_Utils_Hook::post('delete', 'Activity', $activity->id, $activity); | |
251 | } | |
252 | ||
253 | return $result; | |
254 | } | |
255 | ||
256 | /** | |
d8689418 | 257 | * Delete activity assignment record. |
6a488035 | 258 | * |
c490a46a | 259 | * @param int $activityId |
100fef9d | 260 | * @param int $recordTypeID |
6a488035 | 261 | */ |
a24b3694 | 262 | public static function deleteActivityContact($activityId, $recordTypeID = NULL) { |
1d85d241 DL |
263 | $activityContact = new CRM_Activity_BAO_ActivityContact(); |
264 | $activityContact->activity_id = $activityId; | |
a24b3694 | 265 | if ($recordTypeID) { |
266 | $activityContact->record_type_id = $recordTypeID; | |
6a488035 | 267 | } |
1ad799fa | 268 | |
7808aae6 SB |
269 | // Let's check if activity contact record exits and then delete. |
270 | // Looks like delete leads to deadlock when multiple simultaneous | |
271 | // requests are done. CRM-15470 | |
1ad799fa | 272 | if ($activityContact->find()) { |
273 | $activityContact->delete(); | |
274 | } | |
6a488035 TO |
275 | } |
276 | ||
277 | /** | |
d8689418 | 278 | * Process the activities. |
6a488035 | 279 | * |
041ab3d1 TO |
280 | * @param array $params |
281 | * Associated array of the submitted values. | |
1cfa04c4 | 282 | * |
e63aff1c | 283 | * @throws CRM_Core_Exception |
6a488035 | 284 | * |
59f4c9ee | 285 | * @return CRM_Activity_BAO_Activity|null|object |
6a488035 TO |
286 | */ |
287 | public static function create(&$params) { | |
ea6a17a9 TO |
288 | // CRM-20958 - These fields are managed by MySQL triggers. Watch out for clients resaving stale timestamps. |
289 | unset($params['created_date']); | |
290 | unset($params['modified_date']); | |
291 | ||
6a488035 TO |
292 | // check required params |
293 | if (!self::dataExists($params)) { | |
9af2925b | 294 | throw new CRM_Core_Exception('Not enough data to create activity object'); |
6a488035 TO |
295 | } |
296 | ||
297 | $activity = new CRM_Activity_DAO_Activity(); | |
298 | ||
299 | if (isset($params['id']) && empty($params['id'])) { | |
300 | unset($params['id']); | |
301 | } | |
302 | ||
8cc574cf | 303 | if (empty($params['status_id']) && empty($params['activity_status_id']) && empty($params['id'])) { |
6a488035 TO |
304 | if (isset($params['activity_date_time']) && |
305 | strcmp($params['activity_date_time'], CRM_Utils_Date::processDate(date('Ymd')) == -1) | |
306 | ) { | |
fc0c4d20 | 307 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'); |
6a488035 TO |
308 | } |
309 | else { | |
fc0c4d20 | 310 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Scheduled'); |
6a488035 TO |
311 | } |
312 | } | |
313 | ||
7808aae6 | 314 | // Set priority to Normal for Auto-populated activities (for Cases) |
6a488035 TO |
315 | if (CRM_Utils_Array::value('priority_id', $params) === NULL && |
316 | // if not set and not 0 | |
317 | !CRM_Utils_Array::value('id', $params) | |
318 | ) { | |
cbf48754 | 319 | $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'); |
6a488035 TO |
320 | $params['priority_id'] = array_search('Normal', $priority); |
321 | } | |
322 | ||
323 | if (!empty($params['target_contact_id']) && is_array($params['target_contact_id'])) { | |
324 | $params['target_contact_id'] = array_unique($params['target_contact_id']); | |
325 | } | |
326 | if (!empty($params['assignee_contact_id']) && is_array($params['assignee_contact_id'])) { | |
327 | $params['assignee_contact_id'] = array_unique($params['assignee_contact_id']); | |
328 | } | |
329 | ||
330 | // CRM-9137 | |
a7488080 | 331 | if (!empty($params['id'])) { |
6a488035 TO |
332 | CRM_Utils_Hook::pre('edit', 'Activity', $activity->id, $params); |
333 | } | |
334 | else { | |
335 | CRM_Utils_Hook::pre('create', 'Activity', NULL, $params); | |
336 | } | |
337 | ||
338 | $activity->copyValues($params); | |
339 | if (isset($params['case_id'])) { | |
340 | // CRM-8708, preserve case ID even though it's not part of the SQL model | |
341 | $activity->case_id = $params['case_id']; | |
342 | } | |
343 | elseif (is_numeric($activity->id)) { | |
344 | // CRM-8708, preserve case ID even though it's not part of the SQL model | |
345 | $activity->case_id = CRM_Case_BAO_Case::getCaseIdByActivityId($activity->id); | |
346 | } | |
347 | ||
348 | // start transaction | |
349 | $transaction = new CRM_Core_Transaction(); | |
350 | ||
351 | $result = $activity->save(); | |
352 | ||
353 | if (is_a($result, 'CRM_Core_Error')) { | |
354 | $transaction->rollback(); | |
355 | return $result; | |
356 | } | |
357 | ||
358 | $activityId = $activity->id; | |
fc0c4d20 MW |
359 | $sourceID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Source'); |
360 | $assigneeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Assignees'); | |
361 | $targetID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets'); | |
6a488035 | 362 | |
1d85d241 | 363 | if (isset($params['source_contact_id'])) { |
96f94695 | 364 | $acParams = [ |
1d85d241 | 365 | 'activity_id' => $activityId, |
9d5494f7 | 366 | 'contact_id' => $params['source_contact_id'], |
21dfd5f5 | 367 | 'record_type_id' => $sourceID, |
96f94695 | 368 | ]; |
8ea7ce5d | 369 | self::deleteActivityContact($activityId, $sourceID); |
1d85d241 DL |
370 | CRM_Activity_BAO_ActivityContact::create($acParams); |
371 | } | |
372 | ||
6a488035 TO |
373 | // check and attach and files as needed |
374 | CRM_Core_BAO_File::processAttachment($params, 'civicrm_activity', $activityId); | |
375 | ||
376 | // attempt to save activity assignment | |
377 | $resultAssignment = NULL; | |
a7488080 | 378 | if (!empty($params['assignee_contact_id'])) { |
6a488035 | 379 | |
96f94695 | 380 | $assignmentParams = ['activity_id' => $activityId]; |
6a488035 TO |
381 | |
382 | if (is_array($params['assignee_contact_id'])) { | |
383 | if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) { | |
384 | // first delete existing assignments if any | |
a24b3694 | 385 | self::deleteActivityContact($activityId, $assigneeID); |
6a488035 TO |
386 | } |
387 | ||
6a488035 TO |
388 | foreach ($params['assignee_contact_id'] as $acID) { |
389 | if ($acID) { | |
96f94695 | 390 | $assigneeParams = [ |
a8578678 FG |
391 | 'activity_id' => $activityId, |
392 | 'contact_id' => $acID, | |
393 | 'record_type_id' => $assigneeID, | |
96f94695 | 394 | ]; |
a8578678 | 395 | CRM_Activity_BAO_ActivityContact::create($assigneeParams); |
6a488035 TO |
396 | } |
397 | } | |
6a488035 TO |
398 | } |
399 | else { | |
b319d00a | 400 | $assignmentParams['contact_id'] = $params['assignee_contact_id']; |
034500d4 | 401 | $assignmentParams['record_type_id'] = $assigneeID; |
a7488080 | 402 | if (!empty($params['id'])) { |
cc1c86e9 | 403 | $assignment = new CRM_Activity_BAO_ActivityContact(); |
6a488035 | 404 | $assignment->activity_id = $activityId; |
034500d4 | 405 | $assignment->record_type_id = $assigneeID; |
6a488035 TO |
406 | $assignment->find(TRUE); |
407 | ||
b319d00a | 408 | if ($assignment->contact_id != $params['assignee_contact_id']) { |
6a488035 | 409 | $assignmentParams['id'] = $assignment->id; |
034500d4 | 410 | $resultAssignment = CRM_Activity_BAO_ActivityContact::create($assignmentParams); |
6a488035 TO |
411 | } |
412 | } | |
413 | else { | |
034500d4 | 414 | $resultAssignment = CRM_Activity_BAO_ActivityContact::create($assignmentParams); |
6a488035 TO |
415 | } |
416 | } | |
417 | } | |
418 | else { | |
419 | if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) { | |
a24b3694 | 420 | self::deleteActivityContact($activityId, $assigneeID); |
6a488035 TO |
421 | } |
422 | } | |
423 | ||
424 | if (is_a($resultAssignment, 'CRM_Core_Error')) { | |
425 | $transaction->rollback(); | |
426 | return $resultAssignment; | |
427 | } | |
428 | ||
429 | // attempt to save activity targets | |
430 | $resultTarget = NULL; | |
a7488080 | 431 | if (!empty($params['target_contact_id'])) { |
6a488035 | 432 | |
96f94695 | 433 | $targetParams = ['activity_id' => $activityId]; |
434 | $resultTarget = []; | |
6a488035 TO |
435 | if (is_array($params['target_contact_id'])) { |
436 | if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) { | |
437 | // first delete existing targets if any | |
9d5494f7 | 438 | self::deleteActivityContact($activityId, $targetID); |
6a488035 TO |
439 | } |
440 | ||
6a488035 TO |
441 | foreach ($params['target_contact_id'] as $tid) { |
442 | if ($tid) { | |
96f94695 | 443 | $targetContactParams = [ |
a8578678 FG |
444 | 'activity_id' => $activityId, |
445 | 'contact_id' => $tid, | |
446 | 'record_type_id' => $targetID, | |
96f94695 | 447 | ]; |
a8578678 | 448 | CRM_Activity_BAO_ActivityContact::create($targetContactParams); |
6a488035 TO |
449 | } |
450 | } | |
6a488035 TO |
451 | } |
452 | else { | |
b319d00a | 453 | $targetParams['contact_id'] = $params['target_contact_id']; |
034500d4 | 454 | $targetParams['record_type_id'] = $targetID; |
a7488080 | 455 | if (!empty($params['id'])) { |
034500d4 | 456 | $target = new CRM_Activity_BAO_ActivityContact(); |
6a488035 | 457 | $target->activity_id = $activityId; |
034500d4 | 458 | $target->record_type_id = $targetID; |
6a488035 TO |
459 | $target->find(TRUE); |
460 | ||
b319d00a | 461 | if ($target->contact_id != $params['target_contact_id']) { |
6a488035 | 462 | $targetParams['id'] = $target->id; |
034500d4 | 463 | $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams); |
6a488035 TO |
464 | } |
465 | } | |
466 | else { | |
034500d4 | 467 | $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams); |
6a488035 TO |
468 | } |
469 | } | |
470 | } | |
471 | else { | |
472 | if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) { | |
9d5494f7 | 473 | self::deleteActivityContact($activityId, $targetID); |
6a488035 TO |
474 | } |
475 | } | |
476 | ||
0a9f61c4 | 477 | // write to changelog before transaction is committed/rolled |
6a488035 | 478 | // back (and prepare status to display) |
a7488080 | 479 | if (!empty($params['id'])) { |
6a488035 TO |
480 | $logMsg = "Activity (id: {$result->id} ) updated with "; |
481 | } | |
482 | else { | |
483 | $logMsg = "Activity created for "; | |
484 | } | |
485 | ||
96f94695 | 486 | $msgs = []; |
6a488035 TO |
487 | if (isset($params['source_contact_id'])) { |
488 | $msgs[] = "source={$params['source_contact_id']}"; | |
489 | } | |
490 | ||
a7488080 | 491 | if (!empty($params['target_contact_id'])) { |
35522279 | 492 | if (is_array($params['target_contact_id']) && !CRM_Utils_Array::crmIsEmptyArray($params['target_contact_id'])) { |
6a488035 TO |
493 | $msgs[] = "target=" . implode(',', $params['target_contact_id']); |
494 | // take only first target | |
495 | // will be used for recently viewed display | |
496 | $t = array_slice($params['target_contact_id'], 0, 1); | |
497 | $recentContactId = $t[0]; | |
498 | } | |
7808aae6 | 499 | // Is array check fixes warning without degrading functionality but it seems this bit of code may no longer work |
9af2925b EM |
500 | // as it may always be an array |
501 | elseif (isset($params['target_contact_id']) && !is_array($params['target_contact_id'])) { | |
6a488035 TO |
502 | $msgs[] = "target={$params['target_contact_id']}"; |
503 | // will be used for recently viewed display | |
504 | $recentContactId = $params['target_contact_id']; | |
505 | } | |
506 | } | |
507 | else { | |
508 | // at worst, take source for recently viewed display | |
9d5494f7 | 509 | $recentContactId = CRM_Utils_Array::value('source_contact_id', $params); |
6a488035 TO |
510 | } |
511 | ||
512 | if (isset($params['assignee_contact_id'])) { | |
513 | if (is_array($params['assignee_contact_id'])) { | |
514 | $msgs[] = "assignee=" . implode(',', $params['assignee_contact_id']); | |
515 | } | |
516 | else { | |
517 | $msgs[] = "assignee={$params['assignee_contact_id']}"; | |
518 | } | |
519 | } | |
520 | $logMsg .= implode(', ', $msgs); | |
521 | ||
522 | self::logActivityAction($result, $logMsg); | |
523 | ||
a7488080 | 524 | if (!empty($params['custom']) && |
6a488035 TO |
525 | is_array($params['custom']) |
526 | ) { | |
527 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_activity', $result->id); | |
528 | } | |
529 | ||
530 | $transaction->commit(); | |
a7488080 | 531 | if (empty($params['skipRecentView'])) { |
96f94695 | 532 | $recentOther = []; |
a7488080 | 533 | if (!empty($params['case_id'])) { |
6a488035 TO |
534 | $caseContactID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $params['case_id'], 'contact_id', 'case_id'); |
535 | $url = CRM_Utils_System::url('civicrm/case/activity/view', | |
536 | "reset=1&aid={$activity->id}&cid={$caseContactID}&caseID={$params['case_id']}&context=home" | |
537 | ); | |
538 | } | |
539 | else { | |
7ad77be0 | 540 | $q = "action=view&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home"; |
d66c61b6 | 541 | if ($activity->activity_type_id != CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Email')) { |
6a488035 | 542 | $url = CRM_Utils_System::url('civicrm/activity', $q); |
d66c61b6 | 543 | if ($activity->activity_type_id == CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Print PDF Letter')) { |
6a488035 | 544 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/activity/pdf/add', |
eb873b6e | 545 | "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$params['source_contact_id']}&context=home" |
6a488035 TO |
546 | ); |
547 | } | |
548 | else { | |
549 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/activity/add', | |
e63aff1c | 550 | "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home" |
6a488035 TO |
551 | ); |
552 | } | |
553 | ||
554 | if (CRM_Core_Permission::check("delete activities")) { | |
555 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', | |
e63aff1c | 556 | "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home" |
6a488035 TO |
557 | ); |
558 | } | |
559 | } | |
560 | else { | |
561 | $url = CRM_Utils_System::url('civicrm/activity/view', $q); | |
562 | if (CRM_Core_Permission::check('delete activities')) { | |
563 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', | |
7ad77be0 | 564 | "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid=" . CRM_Utils_Array::value('source_contact_id', $params) . "&context=home" |
6a488035 TO |
565 | ); |
566 | } | |
567 | } | |
568 | } | |
569 | ||
570 | if (!isset($activity->parent_id)) { | |
571 | $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId); | |
572 | // add the recently created Activity | |
fc0c4d20 | 573 | $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id'); |
6a488035 TO |
574 | $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activity->id, 'subject'); |
575 | ||
576 | $title = ""; | |
577 | if (isset($activitySubject)) { | |
578 | $title = $activitySubject . ' - '; | |
579 | } | |
580 | ||
581 | $title = $title . $recentContactDisplay; | |
a7488080 | 582 | if (!empty($activityTypes[$activity->activity_type_id])) { |
6a488035 TO |
583 | $title .= ' (' . $activityTypes[$activity->activity_type_id] . ')'; |
584 | } | |
585 | ||
586 | CRM_Utils_Recent::add($title, | |
587 | $url, | |
588 | $activity->id, | |
589 | 'Activity', | |
590 | $recentContactId, | |
591 | $recentContactDisplay, | |
592 | $recentOther | |
593 | ); | |
594 | } | |
595 | } | |
596 | ||
2b68a50c | 597 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
6a488035 | 598 | |
6a488035 | 599 | // if the subject contains a ‘[case #…]’ string, file that activity on the related case (CRM-5916) |
96f94695 | 600 | $matches = []; |
2019dea3 JP |
601 | $subjectToMatch = CRM_Utils_Array::value('subject', $params); |
602 | if (preg_match('/\[case #([0-9a-h]{7})\]/', $subjectToMatch, $matches)) { | |
9d5494f7 TO |
603 | $key = CRM_Core_DAO::escapeString(CIVICRM_SITE_KEY); |
604 | $hash = $matches[1]; | |
2019dea3 JP |
605 | $query = "SELECT id FROM civicrm_case WHERE SUBSTR(SHA1(CONCAT('$key', id)), 1, 7) = '" . CRM_Core_DAO::escapeString($hash) . "'"; |
606 | } | |
607 | elseif (preg_match('/\[case #(\d+)\]/', $subjectToMatch, $matches)) { | |
608 | $query = "SELECT id FROM civicrm_case WHERE id = '" . CRM_Core_DAO::escapeString($matches[1]) . "'"; | |
609 | } | |
610 | if (!empty($matches)) { | |
96f94695 | 611 | $caseParams = [ |
6a488035 TO |
612 | 'activity_id' => $activity->id, |
613 | 'case_id' => CRM_Core_DAO::singleValueQuery($query), | |
96f94695 | 614 | ]; |
6a488035 TO |
615 | if ($caseParams['case_id']) { |
616 | CRM_Case_BAO_Case::processCaseActivity($caseParams); | |
617 | } | |
618 | else { | |
2019dea3 | 619 | self::logActivityAction($activity, "Case details for {$matches[1]} not found while recording an activity on case."); |
6a488035 TO |
620 | } |
621 | } | |
aec75ba0 JP |
622 | if (!empty($params['id'])) { |
623 | CRM_Utils_Hook::post('edit', 'Activity', $activity->id, $activity); | |
624 | } | |
625 | else { | |
626 | CRM_Utils_Hook::post('create', 'Activity', $activity->id, $activity); | |
627 | } | |
6a488035 TO |
628 | |
629 | return $result; | |
630 | } | |
631 | ||
ffd93213 | 632 | /** |
0965e988 EM |
633 | * Create an activity. |
634 | * | |
635 | * @todo elaborate on what this does. | |
636 | * | |
637 | * @param CRM_Core_DAO_Activity $activity | |
638 | * @param string $logMessage | |
ffd93213 EM |
639 | * |
640 | * @return bool | |
641 | */ | |
6a488035 | 642 | public static function logActivityAction($activity, $logMessage = NULL) { |
3bdcd4ec | 643 | $id = CRM_Core_Session::getLoggedInContactID(); |
6a488035 | 644 | if (!$id) { |
44f817d4 | 645 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
8a6844b3 | 646 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
be89435d | 647 | $id = self::getActivityContact($activity->id, $sourceID); |
6a488035 | 648 | } |
96f94695 | 649 | $logParams = [ |
6a488035 TO |
650 | 'entity_table' => 'civicrm_activity', |
651 | 'entity_id' => $activity->id, | |
652 | 'modified_id' => $id, | |
653 | 'modified_date' => date('YmdHis'), | |
654 | 'data' => $logMessage, | |
96f94695 | 655 | ]; |
6a488035 TO |
656 | CRM_Core_BAO_Log::add($logParams); |
657 | return TRUE; | |
658 | } | |
659 | ||
660 | /** | |
0965e988 | 661 | * Get the list Activities. |
6a488035 | 662 | * |
466e3a53 | 663 | * @param array $params |
041ab3d1 | 664 | * Array of parameters. |
6a488035 | 665 | * Keys include |
0a9f61c4 | 666 | * - contact_id int contact_id whose activities we want to retrieve |
6a488035 TO |
667 | * - offset int which row to start from ? |
668 | * - rowCount int how many rows to fetch | |
669 | * - sort object|array object or array describing sort order for sql query. | |
670 | * - admin boolean if contact is admin | |
671 | * - caseId int case ID | |
672 | * - context string page on which selector is build | |
673 | * - activity_type_id int|string the activitiy types we want to restrict by | |
674 | * | |
84be264e | 675 | * @return array |
0965e988 | 676 | * Relevant data object values of open activities |
5b22d1b8 | 677 | * @throws \CiviCRM_API3_Exception |
6a488035 | 678 | */ |
84be264e | 679 | public static function getActivities($params) { |
96f94695 | 680 | $activities = []; |
5161bb0c | 681 | |
466e3a53 | 682 | // Activity.Get API params |
84be264e | 683 | $activityParams = self::getActivityParamsForDashboardFunctions($params); |
6a488035 | 684 | |
466e3a53 | 685 | if (!empty($params['rowCount']) && |
686 | $params['rowCount'] > 0 | |
6a488035 | 687 | ) { |
466e3a53 | 688 | $activityParams['options']['limit'] = $params['rowCount']; |
6a488035 TO |
689 | } |
690 | ||
466e3a53 | 691 | if (!empty($params['sort'])) { |
692 | if (is_a($params['sort'], 'CRM_Utils_Sort')) { | |
693 | $order = $params['sort']->orderBy(); | |
694 | } | |
695 | elseif (trim($params['sort'])) { | |
696 | $order = CRM_Utils_Type::escape($params['sort'], 'String'); | |
697 | } | |
6a488035 TO |
698 | } |
699 | ||
885d25ac | 700 | $activityParams['options']['sort'] = empty($order) ? "activity_date_time DESC" : str_replace('activity_type ', 'activity_type_id.label ', $order); |
9d13e312 | 701 | |
84be264e | 702 | $activityParams['return'] = [ |
703 | 'activity_date_time', | |
704 | 'source_record_id', | |
705 | 'source_contact_id', | |
706 | 'source_contact_name', | |
707 | 'assignee_contact_id', | |
708 | 'target_contact_id', | |
84be264e | 709 | 'assignee_contact_name', |
710 | 'status_id', | |
711 | 'subject', | |
712 | 'activity_type_id', | |
713 | 'activity_type', | |
714 | 'case_id', | |
715 | 'campaign_id', | |
716 | ]; | |
717 | foreach (['case_id' => 'CiviCase', 'campaign_id' => 'CiviCampaign'] as $attr => $component) { | |
718 | if (in_array($component, self::activityComponents())) { | |
719 | $activityParams['return'][] = $attr; | |
720 | } | |
721 | } | |
466e3a53 | 722 | $result = civicrm_api3('Activity', 'Get', $activityParams); |
6a488035 | 723 | |
5b22d1b8 | 724 | $bulkActivityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Bulk Email'); |
466e3a53 | 725 | $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); |
6a488035 | 726 | |
7808aae6 | 727 | // CRM-3553, need to check user has access to target groups. |
6a488035 | 728 | $mailingIDs = CRM_Mailing_BAO_Mailing::mailingACLIDs(); |
466e3a53 | 729 | $accessCiviMail = ((CRM_Core_Permission::check('access CiviMail')) || |
730 | (CRM_Mailing_Info::workflowEnabled() && CRM_Core_Permission::check('create mailings')) | |
6a488035 TO |
731 | ); |
732 | ||
96f94695 | 733 | $mappingParams = [ |
466e3a53 | 734 | 'id' => 'activity_id', |
735 | 'source_record_id' => 'source_record_id', | |
736 | 'activity_type_id' => 'activity_type_id', | |
737 | 'activity_date_time' => 'activity_date_time', | |
738 | 'status_id' => 'status_id', | |
739 | 'subject' => 'subject', | |
740 | 'campaign_id' => 'campaign_id', | |
741 | 'assignee_contact_name' => 'assignee_contact_name', | |
466e3a53 | 742 | 'source_contact_id' => 'source_contact_id', |
743 | 'source_contact_name' => 'source_contact_name', | |
744 | 'case_id' => 'case_id', | |
96f94695 | 745 | ]; |
6a488035 | 746 | |
466e3a53 | 747 | foreach ($result['values'] as $id => $activity) { |
6a488035 | 748 | |
96f94695 | 749 | $activities[$id] = []; |
6a488035 | 750 | |
a84a8555 | 751 | $isBulkActivity = (!$bulkActivityTypeID || ($bulkActivityTypeID === $activity['activity_type_id'])); |
734f2683 | 752 | $activities[$id]['target_contact_counter'] = count($activity['target_contact_id']); |
753 | if ($activities[$id]['target_contact_counter']) { | |
754 | try { | |
755 | $activities[$id]['target_contact_name'][$activity['target_contact_id'][0]] = civicrm_api3('Contact', 'getvalue', ['id' => $activity['target_contact_id'][0], 'return' => 'sort_name']); | |
756 | } | |
757 | catch (CiviCRM_API3_Exception $e) { | |
758 | // Really they should have names but a fatal here feels wrong. | |
759 | $activities[$id]['target_contact_name'] = ''; | |
760 | } | |
761 | } | |
466e3a53 | 762 | foreach ($mappingParams as $apiKey => $expectedName) { |
96f94695 | 763 | if (in_array($apiKey, [ |
764 | 'assignee_contact_name', | |
765 | 'target_contact_name', | |
766 | ])) { | |
767 | $activities[$id][$expectedName] = CRM_Utils_Array::value($apiKey, $activity, []); | |
91da6cd5 | 768 | |
466e3a53 | 769 | if ($isBulkActivity) { |
96f94695 | 770 | $activities[$id]['recipients'] = ts('(%1 recipients)', [1 => count($activity['target_contact_name'])]); |
466e3a53 | 771 | $activities[$id]['mailingId'] = FALSE; |
772 | if ($accessCiviMail && | |
773 | ($mailingIDs === TRUE || in_array($activity['source_record_id'], $mailingIDs)) | |
774 | ) { | |
775 | $activities[$id]['mailingId'] = TRUE; | |
776 | } | |
777 | } | |
6a488035 | 778 | } |
6a488035 | 779 | // case related fields |
5161bb0c | 780 | elseif ($apiKey == 'case_id' && !$isBulkActivity) { |
466e3a53 | 781 | $activities[$id][$expectedName] = CRM_Utils_Array::value($apiKey, $activity); |
5161bb0c | 782 | |
783 | // fetch case subject for case ID found | |
784 | if (!empty($activity['case_id'])) { | |
785 | $activities[$id]['case_subject'] = CRM_Core_DAO::executeQuery('CRM_Case_DAO_Case', $activity['case_id'], 'subject'); | |
786 | } | |
466e3a53 | 787 | } |
788 | else { | |
789 | $activities[$id][$expectedName] = CRM_Utils_Array::value($apiKey, $activity); | |
790 | if ($apiKey == 'activity_type_id') { | |
5b22d1b8 | 791 | $activities[$id]['activity_type'] = CRM_Core_PseudoConstant::getName('CRM_Activity_BAO_Activity', 'activity_type_id', $activities[$id][$expectedName]); |
466e3a53 | 792 | } |
793 | elseif ($apiKey == 'campaign_id') { | |
794 | $activities[$id]['campaign'] = CRM_Utils_Array::value($activities[$id][$expectedName], $allCampaigns); | |
795 | } | |
6a488035 TO |
796 | } |
797 | } | |
5161bb0c | 798 | // if deleted, wrap in <del> |
799 | if (!empty($activity['source_contact_id']) && | |
800 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $activity['source_contact_id'], 'is_deleted') | |
801 | ) { | |
802 | $activities[$id]['source_contact_name'] = sprintf("<del>%s<del>", $activity['source_contact_name']); | |
803 | } | |
804 | $activities[$id]['is_recurring_activity'] = CRM_Core_BAO_RecurringEntity::getParentFor($id, 'civicrm_activity'); | |
6a488035 TO |
805 | } |
806 | ||
84be264e | 807 | return $activities; |
6a488035 TO |
808 | } |
809 | ||
5b22d1b8 MW |
810 | /** |
811 | * Filter the activity types to only return the ones we actually asked for | |
812 | * Uses params['activity_type_id'] and params['activity_type_exclude_id'] | |
813 | * | |
814 | * @param $params | |
815 | * @return array|null (Use in Activity.get API activity_type_id) | |
816 | */ | |
817 | public static function filterActivityTypes($params) { | |
96f94695 | 818 | $activityTypes = []; |
5b22d1b8 MW |
819 | |
820 | // If no activity types are specified, get all the active ones | |
821 | if (empty($params['activity_type_id'])) { | |
822 | $activityTypes = CRM_Activity_BAO_Activity::buildOptions('activity_type_id', 'get'); | |
823 | } | |
824 | ||
825 | // If no activity types are specified or excluded, return the list of all active ones | |
826 | if (empty($params['activity_type_id']) && empty($params['activity_type_exclude_id'])) { | |
827 | if (!empty($activityTypes)) { | |
96f94695 | 828 | return ['IN' => array_keys($activityTypes)]; |
5b22d1b8 MW |
829 | } |
830 | return NULL; | |
831 | } | |
832 | ||
833 | // If we have specified activity types, build a list to return, excluding the ones we don't want. | |
834 | if (!empty($params['activity_type_id'])) { | |
835 | if (!is_array($params['activity_type_id'])) { | |
836 | // Turn it into array if only one specified, so we don't duplicate processing below | |
96f94695 | 837 | $params['activity_type_id'] = [$params['activity_type_id'] => $params['activity_type_id']]; |
5b22d1b8 MW |
838 | } |
839 | foreach ($params['activity_type_id'] as $value) { | |
840 | // Add each activity type that was specified to list | |
841 | $value = CRM_Utils_Type::escape($value, 'Positive'); | |
842 | $activityTypes[$value] = $value; | |
843 | } | |
844 | } | |
845 | ||
846 | // Build the list of activity types to exclude (from $params['activity_type_exclude_id']) | |
847 | if (!empty($params['activity_type_exclude_id'])) { | |
848 | if (!is_array($params['activity_type_exclude_id'])) { | |
849 | // Turn it into array if only one specified, so we don't duplicate processing below | |
96f94695 | 850 | $params['activity_type_exclude_id'] = [$params['activity_type_exclude_id'] => $params['activity_type_exclude_id']]; |
5b22d1b8 MW |
851 | } |
852 | foreach ($params['activity_type_exclude_id'] as $value) { | |
853 | // Remove each activity type from list if it should be excluded | |
854 | $value = CRM_Utils_Type::escape($value, 'Positive'); | |
855 | if (array_key_exists($value, $activityTypes)) { | |
856 | unset($activityTypes[$value]); | |
857 | } | |
858 | } | |
859 | } | |
860 | ||
96f94695 | 861 | return ['IN' => array_keys($activityTypes)]; |
5b22d1b8 MW |
862 | } |
863 | ||
ff2a3553 | 864 | /** |
865 | * @inheritDoc | |
866 | */ | |
867 | public function addSelectWhereClause() { | |
cdacd6ab | 868 | $clauses = []; |
869 | $permittedActivityTypeIDs = self::getPermittedActivityTypes(); | |
870 | if (empty($permittedActivityTypeIDs)) { | |
871 | // This just prevents a mysql fail if they have no access - should be extremely edge case. | |
872 | $permittedActivityTypeIDs = [0]; | |
873 | } | |
874 | $clauses['activity_type_id'] = ('IN (' . implode(', ', $permittedActivityTypeIDs) . ')'); | |
875 | ||
876 | $contactClause = CRM_Utils_SQL::mergeSubquery('Contact'); | |
877 | if ($contactClause) { | |
878 | $contactClause = implode(' AND contact_id ', $contactClause); | |
879 | $clauses['id'][] = "IN (SELECT activity_id FROM civicrm_activity_contact WHERE contact_id $contactClause)"; | |
ff2a3553 | 880 | } |
cdacd6ab | 881 | CRM_Utils_Hook::selectWhereClause($this, $clauses); |
ff2a3553 | 882 | return $clauses; |
883 | } | |
884 | ||
6a488035 | 885 | /** |
c4937fe9 | 886 | * Get an array of components that are accessible by the currenct user. |
0965e988 | 887 | * |
c4937fe9 | 888 | * This means checking if they are enabled and if the user has appropriate permission. |
6a488035 | 889 | * |
c4937fe9 | 890 | * For most components the permission is access component (e.g 'access CiviContribute'). |
891 | * Exceptions as CiviCampaign (administer CiviCampaign) and CiviCase | |
892 | * (accesses a case function which enforces edit all cases or edit my cases. Case | |
893 | * permissions are also handled on a per activity basis). | |
894 | * | |
895 | * Checks whether logged in user has permission to the component. | |
896 | * | |
897 | * @param bool $excludeComponentHandledActivities | |
898 | * Should we exclude components whose display is handled in the components. | |
899 | * In practice this means should we include CiviCase in the results. Presumbaly | |
900 | * at the time it was decided case activities should be shown in the case framework and | |
901 | * that this concept might be extended later. In practice most places that | |
902 | * call this then re-add CiviCase in some way so it's all a bit... odd. | |
903 | * | |
904 | * @return array Array of component id and name. | |
16b10e64 | 905 | * Array of component id and name. |
59f4c9ee | 906 | */ |
c4937fe9 | 907 | public static function activityComponents($excludeComponentHandledActivities = TRUE) { |
96f94695 | 908 | $components = []; |
6a488035 TO |
909 | $compInfo = CRM_Core_Component::getEnabledComponents(); |
910 | foreach ($compInfo as $compObj) { | |
c4937fe9 | 911 | $includeComponent = !$excludeComponentHandledActivities || !empty($compObj->info['showActivitiesInCore']); |
912 | if ($includeComponent) { | |
6a488035 TO |
913 | if ($compObj->info['name'] == 'CiviCampaign') { |
914 | $componentPermission = "administer {$compObj->name}"; | |
915 | } | |
916 | else { | |
917 | $componentPermission = "access {$compObj->name}"; | |
918 | } | |
919 | if ($compObj->info['name'] == 'CiviCase') { | |
920 | if (CRM_Case_BAO_Case::accessCiviCase()) { | |
921 | $components[$compObj->componentID] = $compObj->info['name']; | |
922 | } | |
923 | } | |
924 | elseif (CRM_Core_Permission::check($componentPermission)) { | |
925 | $components[$compObj->componentID] = $compObj->info['name']; | |
926 | } | |
927 | } | |
928 | } | |
929 | ||
930 | return $components; | |
931 | } | |
932 | ||
6ab43e1b | 933 | /** |
934 | * Get the activity Count. | |
935 | * | |
936 | * @param array $input | |
937 | * Array of parameters. | |
938 | * Keys include | |
939 | * - contact_id int contact_id whose activities we want to retrieve | |
940 | * - admin boolean if contact is admin | |
941 | * - caseId int case ID | |
942 | * - context string page on which selector is build | |
943 | * - activity_type_id int|string the activity types we want to restrict by | |
944 | * | |
945 | * @return int | |
946 | * count of activities | |
947 | */ | |
948 | public static function getActivitiesCount($input) { | |
84be264e | 949 | $activityParams = self::getActivityParamsForDashboardFunctions($input); |
950 | return civicrm_api3('Activity', 'getcount', $activityParams); | |
6ab43e1b | 951 | } |
952 | ||
6a488035 | 953 | /** |
0965e988 EM |
954 | * Send the message to all the contacts. |
955 | * | |
956 | * Also insert a contact activity in each contacts record. | |
6a488035 | 957 | * |
041ab3d1 TO |
958 | * @param array $contactDetails |
959 | * The array of contact details to send the email. | |
960 | * @param string $subject | |
961 | * The subject of the message. | |
fd31fa4c EM |
962 | * @param $text |
963 | * @param $html | |
041ab3d1 TO |
964 | * @param string $emailAddress |
965 | * Use this 'to' email address instead of the default Primary address. | |
966 | * @param int $userID | |
967 | * Use this userID if set. | |
6a488035 | 968 | * @param string $from |
041ab3d1 TO |
969 | * @param array $attachments |
970 | * The array of attachments if any. | |
971 | * @param string $cc | |
972 | * Cc recipient. | |
973 | * @param string $bcc | |
974 | * Bcc recipient. | |
975 | * @param array $contactIds | |
976 | * Contact ids. | |
977 | * @param string $additionalDetails | |
978 | * The additional information of CC and BCC appended to the activity Details. | |
cb5d08cd JP |
979 | * @param array $contributionIds |
980 | * @param int $campaignId | |
6a488035 | 981 | * |
a6c01b45 CW |
982 | * @return array |
983 | * ( sent, activityId) if any email is sent and activityId | |
6a488035 | 984 | */ |
59f4c9ee | 985 | public static function sendEmail( |
6a488035 TO |
986 | &$contactDetails, |
987 | &$subject, | |
988 | &$text, | |
989 | &$html, | |
990 | $emailAddress, | |
9d5494f7 TO |
991 | $userID = NULL, |
992 | $from = NULL, | |
6a488035 | 993 | $attachments = NULL, |
9d5494f7 TO |
994 | $cc = NULL, |
995 | $bcc = NULL, | |
6c552737 | 996 | $contactIds = NULL, |
7e2ec997 | 997 | $additionalDetails = NULL, |
824989b9 | 998 | $contributionIds = NULL, |
cb5d08cd | 999 | $campaignId = NULL |
6a488035 TO |
1000 | ) { |
1001 | // get the contact details of logged in contact, which we set as from email | |
1002 | if ($userID == NULL) { | |
3bdcd4ec | 1003 | $userID = CRM_Core_Session::getLoggedInContactID(); |
6a488035 TO |
1004 | } |
1005 | ||
1006 | list($fromDisplayName, $fromEmail, $fromDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($userID); | |
1007 | if (!$fromEmail) { | |
96f94695 | 1008 | return [count($contactDetails), 0, count($contactDetails)]; |
6a488035 TO |
1009 | } |
1010 | if (!trim($fromDisplayName)) { | |
1011 | $fromDisplayName = $fromEmail; | |
1012 | } | |
1013 | ||
1014 | // CRM-4575 | |
1015 | // token replacement of addressee/email/postal greetings | |
1016 | // get the tokens added in subject and message | |
1017 | $subjectToken = CRM_Utils_Token::getTokens($subject); | |
1018 | $messageToken = CRM_Utils_Token::getTokens($text); | |
1019 | $messageToken = array_merge($messageToken, CRM_Utils_Token::getTokens($html)); | |
c7436e9c | 1020 | $allTokens = array_merge($messageToken, $subjectToken); |
6a488035 TO |
1021 | |
1022 | if (!$from) { | |
1023 | $from = "$fromDisplayName <$fromEmail>"; | |
1024 | } | |
1025 | ||
1026 | //create the meta level record first ( email activity ) | |
5b22d1b8 | 1027 | $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Email'); |
6a488035 TO |
1028 | |
1029 | // CRM-6265: save both text and HTML parts in details (if present) | |
1030 | if ($html and $text) { | |
c1d26519 | 1031 | $details = "-ALTERNATIVE ITEM 0-\n$html$additionalDetails\n-ALTERNATIVE ITEM 1-\n$text$additionalDetails\n-ALTERNATIVE END-\n"; |
6a488035 TO |
1032 | } |
1033 | else { | |
1034 | $details = $html ? $html : $text; | |
c1d26519 | 1035 | $details .= $additionalDetails; |
6a488035 TO |
1036 | } |
1037 | ||
96f94695 | 1038 | $activityParams = [ |
6a488035 TO |
1039 | 'source_contact_id' => $userID, |
1040 | 'activity_type_id' => $activityTypeID, | |
1041 | 'activity_date_time' => date('YmdHis'), | |
1042 | 'subject' => $subject, | |
1043 | 'details' => $details, | |
1044 | // FIXME: check for name Completed and get ID from that lookup | |
5b22d1b8 | 1045 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'), |
cb5d08cd | 1046 | 'campaign_id' => $campaignId, |
96f94695 | 1047 | ]; |
6a488035 TO |
1048 | |
1049 | // CRM-5916: strip [case #…] before saving the activity (if present in subject) | |
1050 | $activityParams['subject'] = preg_replace('/\[case #([0-9a-h]{7})\] /', '', $activityParams['subject']); | |
1051 | ||
1052 | // add the attachments to activity params here | |
1053 | if ($attachments) { | |
1054 | // first process them | |
1055 | $activityParams = array_merge($activityParams, | |
1056 | $attachments | |
1057 | ); | |
1058 | } | |
1059 | ||
1060 | $activity = self::create($activityParams); | |
1061 | ||
1062 | // get the set of attachments from where they are stored | |
1063 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', | |
1064 | $activity->id | |
1065 | ); | |
96f94695 | 1066 | $returnProperties = []; |
6a488035 TO |
1067 | if (isset($messageToken['contact'])) { |
1068 | foreach ($messageToken['contact'] as $key => $value) { | |
1069 | $returnProperties[$value] = 1; | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | if (isset($subjectToken['contact'])) { | |
1074 | foreach ($subjectToken['contact'] as $key => $value) { | |
1075 | if (!isset($returnProperties[$value])) { | |
1076 | $returnProperties[$value] = 1; | |
1077 | } | |
1078 | } | |
1079 | } | |
1080 | ||
6a488035 | 1081 | // get token details for contacts, call only if tokens are used |
96f94695 | 1082 | $details = []; |
db969160 | 1083 | if (!empty($returnProperties) || !empty($tokens) || !empty($allTokens)) { |
6a488035 TO |
1084 | list($details) = CRM_Utils_Token::getTokenDetails( |
1085 | $contactIds, | |
1086 | $returnProperties, | |
1087 | NULL, NULL, FALSE, | |
c7436e9c | 1088 | $allTokens, |
6a488035 TO |
1089 | 'CRM_Activity_BAO_Activity' |
1090 | ); | |
1091 | } | |
1092 | ||
1093 | // call token hook | |
96f94695 | 1094 | $tokens = []; |
6a488035 TO |
1095 | CRM_Utils_Hook::tokens($tokens); |
1096 | $categories = array_keys($tokens); | |
1097 | ||
1098 | $escapeSmarty = FALSE; | |
1099 | if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { | |
1100 | $smarty = CRM_Core_Smarty::singleton(); | |
1101 | $escapeSmarty = TRUE; | |
1102 | } | |
1103 | ||
96f94695 | 1104 | $contributionDetails = []; |
7e2ec997 E |
1105 | if (!empty($contributionIds)) { |
1106 | $contributionDetails = CRM_Contribute_BAO_Contribution::replaceContributionTokens( | |
1107 | $contributionIds, | |
1108 | $subject, | |
1109 | $subjectToken, | |
1110 | $text, | |
1111 | $html, | |
1112 | $messageToken, | |
1113 | $escapeSmarty | |
1114 | ); | |
1115 | } | |
1116 | ||
96f94695 | 1117 | $sent = $notSent = []; |
6a488035 TO |
1118 | foreach ($contactDetails as $values) { |
1119 | $contactId = $values['contact_id']; | |
1120 | $emailAddress = $values['email']; | |
1121 | ||
7e2ec997 E |
1122 | if (!empty($contributionDetails)) { |
1123 | $subject = $contributionDetails[$contactId]['subject']; | |
1124 | $text = $contributionDetails[$contactId]['text']; | |
1125 | $html = $contributionDetails[$contactId]['html']; | |
1126 | } | |
1127 | ||
6a488035 TO |
1128 | if (!empty($details) && is_array($details["{$contactId}"])) { |
1129 | // unset email from details since it always returns primary email address | |
1130 | unset($details["{$contactId}"]['email']); | |
1131 | unset($details["{$contactId}"]['email_id']); | |
1132 | $values = array_merge($values, $details["{$contactId}"]); | |
1133 | } | |
1134 | ||
1135 | $tokenSubject = CRM_Utils_Token::replaceContactTokens($subject, $values, FALSE, $subjectToken, FALSE, $escapeSmarty); | |
1136 | $tokenSubject = CRM_Utils_Token::replaceHookTokens($tokenSubject, $values, $categories, FALSE, $escapeSmarty); | |
1137 | ||
7808aae6 | 1138 | // CRM-4539 |
6a488035 TO |
1139 | if ($values['preferred_mail_format'] == 'Text' || $values['preferred_mail_format'] == 'Both') { |
1140 | $tokenText = CRM_Utils_Token::replaceContactTokens($text, $values, FALSE, $messageToken, FALSE, $escapeSmarty); | |
1141 | $tokenText = CRM_Utils_Token::replaceHookTokens($tokenText, $values, $categories, FALSE, $escapeSmarty); | |
1142 | } | |
1143 | else { | |
1144 | $tokenText = NULL; | |
1145 | } | |
1146 | ||
1147 | if ($values['preferred_mail_format'] == 'HTML' || $values['preferred_mail_format'] == 'Both') { | |
1148 | $tokenHtml = CRM_Utils_Token::replaceContactTokens($html, $values, TRUE, $messageToken, FALSE, $escapeSmarty); | |
1149 | $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $values, $categories, TRUE, $escapeSmarty); | |
1150 | } | |
1151 | else { | |
1152 | $tokenHtml = NULL; | |
1153 | } | |
1154 | ||
1155 | if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { | |
1156 | // also add the contact tokens to the template | |
1157 | $smarty->assign_by_ref('contact', $values); | |
1158 | ||
1159 | $tokenSubject = $smarty->fetch("string:$tokenSubject"); | |
9d5494f7 TO |
1160 | $tokenText = $smarty->fetch("string:$tokenText"); |
1161 | $tokenHtml = $smarty->fetch("string:$tokenHtml"); | |
6a488035 TO |
1162 | } |
1163 | ||
1164 | $sent = FALSE; | |
1165 | if (self::sendMessage( | |
9d5494f7 TO |
1166 | $from, |
1167 | $userID, | |
1168 | $contactId, | |
1169 | $tokenSubject, | |
1170 | $tokenText, | |
1171 | $tokenHtml, | |
1172 | $emailAddress, | |
1173 | $activity->id, | |
1174 | $attachments, | |
1175 | $cc, | |
1176 | $bcc | |
1177 | ) | |
1178 | ) { | |
6a488035 TO |
1179 | $sent = TRUE; |
1180 | } | |
1181 | } | |
1182 | ||
96f94695 | 1183 | return [$sent, $activity->id]; |
6a488035 TO |
1184 | } |
1185 | ||
ffd93213 | 1186 | /** |
36f5faa3 | 1187 | * Send SMS. Returns: bool $sent, int $activityId, int $success (number of sent SMS) |
0965e988 | 1188 | * |
100fef9d CW |
1189 | * @param array $contactDetails |
1190 | * @param array $activityParams | |
36f5faa3 MW |
1191 | * @param array $smsProviderParams |
1192 | * @param array $contactIds | |
1193 | * @param int $sourceContactId This is the source contact Id | |
ffd93213 | 1194 | * |
36f5faa3 | 1195 | * @return array(bool $sent, int $activityId, int $success) |
ffd93213 EM |
1196 | * @throws CRM_Core_Exception |
1197 | */ | |
59f4c9ee | 1198 | public static function sendSMS( |
36f5faa3 | 1199 | &$contactDetails = NULL, |
6a488035 | 1200 | &$activityParams, |
96f94695 | 1201 | &$smsProviderParams = [], |
36f5faa3 MW |
1202 | &$contactIds = NULL, |
1203 | $sourceContactId = NULL | |
6a488035 | 1204 | ) { |
63483feb MM |
1205 | if (!CRM_Core_Permission::check('send SMS')) { |
1206 | throw new CRM_Core_Exception("You do not have the 'send SMS' permission"); | |
1207 | } | |
6a488035 | 1208 | |
36f5faa3 MW |
1209 | if (!isset($contactDetails) && !isset($contactIds)) { |
1210 | Throw new CRM_Core_Exception('You must specify either $contactDetails or $contactIds'); | |
1211 | } | |
1212 | // Populate $contactDetails and $contactIds if only one is set | |
1213 | if (is_array($contactIds) && !empty($contactIds) && empty($contactDetails)) { | |
1214 | foreach ($contactIds as $id) { | |
1215 | try { | |
96f94695 | 1216 | $contactDetails[] = civicrm_api3('Contact', 'getsingle', ['contact_id' => $id]); |
36f5faa3 MW |
1217 | } |
1218 | catch (Exception $e) { | |
1219 | // Contact Id doesn't exist | |
1220 | } | |
1221 | } | |
6a488035 | 1222 | } |
36f5faa3 MW |
1223 | elseif (is_array($contactDetails) && !empty($contactDetails) && empty($contactIds)) { |
1224 | foreach ($contactDetails as $contact) { | |
1225 | $contactIds[] = $contact['contact_id']; | |
1226 | } | |
1227 | } | |
6a488035 | 1228 | |
36f5faa3 MW |
1229 | // Get logged in User Id |
1230 | if (empty($sourceContactId)) { | |
1231 | $sourceContactId = CRM_Core_Session::getLoggedInContactID(); | |
1232 | } | |
6a488035 | 1233 | |
36f5faa3 | 1234 | $text = &$activityParams['sms_text_message']; |
6a488035 | 1235 | |
36f5faa3 | 1236 | // Create the meta level record first ( sms activity ) |
96f94695 | 1237 | $activityParams = [ |
36f5faa3 MW |
1238 | 'source_contact_id' => $sourceContactId, |
1239 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'SMS'), | |
6a488035 | 1240 | 'activity_date_time' => date('YmdHis'), |
36f5faa3 MW |
1241 | 'subject' => $activityParams['activity_subject'], |
1242 | 'details' => $text, | |
fc0c4d20 | 1243 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'), |
96f94695 | 1244 | ]; |
6a488035 TO |
1245 | $activity = self::create($activityParams); |
1246 | $activityID = $activity->id; | |
1247 | ||
36f5faa3 MW |
1248 | // Process Tokens |
1249 | // token replacement of addressee/email/postal greetings | |
1250 | // get the tokens added in subject and message | |
1251 | $messageToken = CRM_Utils_Token::getTokens($text); | |
96f94695 | 1252 | $returnProperties = []; |
6a488035 TO |
1253 | if (isset($messageToken['contact'])) { |
1254 | foreach ($messageToken['contact'] as $key => $value) { | |
1255 | $returnProperties[$value] = 1; | |
1256 | } | |
1257 | } | |
36f5faa3 | 1258 | // Call tokens hook |
96f94695 | 1259 | $tokens = []; |
6a488035 TO |
1260 | CRM_Utils_Hook::tokens($tokens); |
1261 | $categories = array_keys($tokens); | |
6a488035 | 1262 | // get token details for contacts, call only if tokens are used |
96f94695 | 1263 | $tokenDetails = []; |
6a488035 | 1264 | if (!empty($returnProperties) || !empty($tokens)) { |
36f5faa3 | 1265 | list($tokenDetails) = CRM_Utils_Token::getTokenDetails($contactIds, |
6a488035 TO |
1266 | $returnProperties, |
1267 | NULL, NULL, FALSE, | |
1268 | $messageToken, | |
1269 | 'CRM_Activity_BAO_Activity' | |
1270 | ); | |
1271 | } | |
1272 | ||
f53ea1ce | 1273 | $success = 0; |
96f94695 | 1274 | $errMsgs = []; |
36f5faa3 MW |
1275 | foreach ($contactDetails as $contact) { |
1276 | $contactId = $contact['contact_id']; | |
6a488035 | 1277 | |
36f5faa3 MW |
1278 | // Replace tokens |
1279 | if (!empty($tokenDetails) && is_array($tokenDetails["{$contactId}"])) { | |
c965c606 | 1280 | // unset phone from details since it always returns primary number |
36f5faa3 MW |
1281 | unset($tokenDetails["{$contactId}"]['phone']); |
1282 | unset($tokenDetails["{$contactId}"]['phone_type_id']); | |
1283 | $contact = array_merge($contact, $tokenDetails["{$contactId}"]); | |
6a488035 | 1284 | } |
36f5faa3 MW |
1285 | $tokenText = CRM_Utils_Token::replaceContactTokens($text, $contact, FALSE, $messageToken, FALSE, FALSE); |
1286 | $tokenText = CRM_Utils_Token::replaceHookTokens($tokenText, $contact, $categories, FALSE, FALSE); | |
6a488035 | 1287 | |
d65e1a68 | 1288 | // Only send if the phone is of type mobile |
36f5faa3 MW |
1289 | if ($contact['phone_type_id'] == CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', 'Mobile')) { |
1290 | $smsProviderParams['To'] = $contact['phone']; | |
01aca362 DL |
1291 | } |
1292 | else { | |
36f5faa3 | 1293 | $smsProviderParams['To'] = ''; |
d65e1a68 | 1294 | } |
6a488035 | 1295 | |
a9b7ee41 | 1296 | $doNotSms = CRM_Utils_Array::value('do_not_sms', $contact, 0); |
c5a6413b | 1297 | |
a9b7ee41 SL |
1298 | if ($doNotSms) { |
1299 | $errMsgs[] = PEAR::raiseError('Contact Does not accept SMS', NULL, PEAR_ERROR_RETURN); | |
9d5494f7 TO |
1300 | } |
1301 | else { | |
a9b7ee41 SL |
1302 | $sendResult = self::sendSMSMessage( |
1303 | $contactId, | |
1304 | $tokenText, | |
1305 | $smsProviderParams, | |
1306 | $activityID, | |
1307 | $sourceContactId | |
1308 | ); | |
1309 | ||
1310 | if (PEAR::isError($sendResult)) { | |
1311 | // Collect all of the PEAR_Error objects | |
1312 | $errMsgs[] = $sendResult; | |
1313 | } | |
1314 | else { | |
1315 | $success++; | |
1316 | } | |
6a488035 TO |
1317 | } |
1318 | } | |
1319 | ||
c5a6413b DS |
1320 | // If at least one message was sent and no errors |
1321 | // were generated then return a boolean value of TRUE. | |
1322 | // Otherwise, return FALSE (no messages sent) or | |
1323 | // and array of 1 or more PEAR_Error objects. | |
1324 | $sent = FALSE; | |
1325 | if ($success > 0 && count($errMsgs) == 0) { | |
1326 | $sent = TRUE; | |
9d5494f7 TO |
1327 | } |
1328 | elseif (count($errMsgs) > 0) { | |
c5a6413b DS |
1329 | $sent = $errMsgs; |
1330 | } | |
1331 | ||
96f94695 | 1332 | return [$sent, $activity->id, $success]; |
6a488035 TO |
1333 | } |
1334 | ||
1335 | /** | |
5c9ff055 | 1336 | * Send the sms message to a specific contact. |
6a488035 | 1337 | * |
041ab3d1 TO |
1338 | * @param int $toID |
1339 | * The contact id of the recipient. | |
77b97be7 | 1340 | * @param $tokenText |
36f5faa3 | 1341 | * @param array $smsProviderParams |
041ab3d1 TO |
1342 | * The params used for sending sms. |
1343 | * @param int $activityID | |
1344 | * The activity ID that tracks the message. | |
36f5faa3 | 1345 | * @param int $sourceContactID |
6a488035 | 1346 | * |
72b3a70c CW |
1347 | * @return bool|PEAR_Error |
1348 | * true on success or PEAR_Error object | |
6a488035 | 1349 | */ |
59f4c9ee | 1350 | public static function sendSMSMessage( |
9d5494f7 | 1351 | $toID, |
6a488035 | 1352 | &$tokenText, |
96f94695 | 1353 | $smsProviderParams = [], |
e8cb3963 | 1354 | $activityID, |
36f5faa3 | 1355 | $sourceContactID = NULL |
6a488035 | 1356 | ) { |
36f5faa3 | 1357 | $toPhoneNumber = NULL; |
36f5faa3 MW |
1358 | if ($smsProviderParams['To']) { |
1359 | // If phone number is specified use it | |
1360 | $toPhoneNumber = trim($smsProviderParams['To']); | |
6a488035 TO |
1361 | } |
1362 | elseif ($toID) { | |
36f5faa3 | 1363 | // No phone number specified, so find a suitable one for the contact |
96f94695 | 1364 | $filters = ['is_deceased' => 0, 'is_deleted' => 0, 'do_not_sms' => 0]; |
6a488035 | 1365 | $toPhoneNumbers = CRM_Core_BAO_Phone::allPhones($toID, FALSE, 'Mobile', $filters); |
36f5faa3 | 1366 | // To get primary mobile phonenumber, if not get the first mobile phonenumber |
6a488035 | 1367 | if (!empty($toPhoneNumbers)) { |
36f5faa3 MW |
1368 | $toPhoneNumberDetails = reset($toPhoneNumbers); |
1369 | $toPhoneNumber = CRM_Utils_Array::value('phone', $toPhoneNumberDetails); | |
7808aae6 | 1370 | // Contact allows to send sms |
6a488035 TO |
1371 | } |
1372 | } | |
1373 | ||
1374 | // make sure both phone are valid | |
1375 | // and that the recipient wants to receive sms | |
a9b7ee41 | 1376 | if (empty($toPhoneNumber)) { |
c5a6413b DS |
1377 | return PEAR::raiseError( |
1378 | 'Recipient phone number is invalid or recipient does not want to receive SMS', | |
9d5494f7 | 1379 | NULL, |
c5a6413b DS |
1380 | PEAR_ERROR_RETURN |
1381 | ); | |
6a488035 TO |
1382 | } |
1383 | ||
a9b7ee41 | 1384 | $recipient = $toPhoneNumber; |
36f5faa3 MW |
1385 | $smsProviderParams['contact_id'] = $toID; |
1386 | $smsProviderParams['parent_activity_id'] = $activityID; | |
6a488035 | 1387 | |
96f94695 | 1388 | $providerObj = CRM_SMS_Provider::singleton(['provider_id' => $smsProviderParams['provider_id']]); |
36f5faa3 | 1389 | $sendResult = $providerObj->send($recipient, $smsProviderParams, $tokenText, NULL, $sourceContactID); |
c5a6413b DS |
1390 | if (PEAR::isError($sendResult)) { |
1391 | return $sendResult; | |
6a488035 TO |
1392 | } |
1393 | ||
36f5faa3 MW |
1394 | // add activity target record for every sms that is sent |
1395 | $targetID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_ActivityContact', 'record_type_id', 'Activity Targets'); | |
96f94695 | 1396 | $activityTargetParams = [ |
6a488035 | 1397 | 'activity_id' => $activityID, |
9d5494f7 | 1398 | 'contact_id' => $toID, |
21dfd5f5 | 1399 | 'record_type_id' => $targetID, |
96f94695 | 1400 | ]; |
1d85d241 | 1401 | CRM_Activity_BAO_ActivityContact::create($activityTargetParams); |
6a488035 TO |
1402 | |
1403 | return TRUE; | |
1404 | } | |
1405 | ||
1406 | /** | |
5c9ff055 | 1407 | * Send the message to a specific contact. |
6a488035 | 1408 | * |
041ab3d1 TO |
1409 | * @param string $from |
1410 | * The name and email of the sender. | |
100fef9d | 1411 | * @param int $fromID |
041ab3d1 TO |
1412 | * @param int $toID |
1413 | * The contact id of the recipient. | |
1414 | * @param string $subject | |
1415 | * The subject of the message. | |
77b97be7 EM |
1416 | * @param $text_message |
1417 | * @param $html_message | |
041ab3d1 TO |
1418 | * @param string $emailAddress |
1419 | * Use this 'to' email address instead of the default Primary address. | |
1420 | * @param int $activityID | |
1421 | * The activity ID that tracks the message. | |
77b97be7 EM |
1422 | * @param null $attachments |
1423 | * @param null $cc | |
1424 | * @param null $bcc | |
6a488035 | 1425 | * |
59f4c9ee TO |
1426 | * @return bool |
1427 | * TRUE if successful else FALSE. | |
6a488035 | 1428 | */ |
59f4c9ee | 1429 | public static function sendMessage( |
9d5494f7 | 1430 | $from, |
6a488035 TO |
1431 | $fromID, |
1432 | $toID, | |
1433 | &$subject, | |
1434 | &$text_message, | |
1435 | &$html_message, | |
1436 | $emailAddress, | |
1437 | $activityID, | |
1438 | $attachments = NULL, | |
9d5494f7 TO |
1439 | $cc = NULL, |
1440 | $bcc = NULL | |
6a488035 TO |
1441 | ) { |
1442 | list($toDisplayName, $toEmail, $toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($toID); | |
1443 | if ($emailAddress) { | |
1444 | $toEmail = trim($emailAddress); | |
1445 | } | |
1446 | ||
1447 | // make sure both email addresses are valid | |
1448 | // and that the recipient wants to receive email | |
1449 | if (empty($toEmail) or $toDoNotEmail) { | |
1450 | return FALSE; | |
1451 | } | |
1452 | if (!trim($toDisplayName)) { | |
1453 | $toDisplayName = $toEmail; | |
1454 | } | |
1455 | ||
44f817d4 | 1456 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
a24b3694 | 1457 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
1458 | ||
6a488035 | 1459 | // create the params array |
96f94695 | 1460 | $mailParams = [ |
6a488035 TO |
1461 | 'groupName' => 'Activity Email Sender', |
1462 | 'from' => $from, | |
1463 | 'toName' => $toDisplayName, | |
1464 | 'toEmail' => $toEmail, | |
1465 | 'subject' => $subject, | |
1466 | 'cc' => $cc, | |
1467 | 'bcc' => $bcc, | |
1468 | 'text' => $text_message, | |
1469 | 'html' => $html_message, | |
1470 | 'attachments' => $attachments, | |
96f94695 | 1471 | ]; |
6a488035 TO |
1472 | |
1473 | if (!CRM_Utils_Mail::send($mailParams)) { | |
1474 | return FALSE; | |
1475 | } | |
1476 | ||
1477 | // add activity target record for every mail that is send | |
96f94695 | 1478 | $activityTargetParams = [ |
6a488035 | 1479 | 'activity_id' => $activityID, |
1d85d241 | 1480 | 'contact_id' => $toID, |
21dfd5f5 | 1481 | 'record_type_id' => $targetID, |
96f94695 | 1482 | ]; |
1d85d241 | 1483 | CRM_Activity_BAO_ActivityContact::create($activityTargetParams); |
6a488035 TO |
1484 | return TRUE; |
1485 | } | |
1486 | ||
1487 | /** | |
db7de9c1 | 1488 | * Combine all the importable fields from the lower levels object. |
6a488035 TO |
1489 | * |
1490 | * The ordering is important, since currently we do not have a weight | |
1491 | * scheme. Adding weight is super important and should be done in the | |
1492 | * next week or so, before this can be called complete. | |
1493 | * | |
dd244018 EM |
1494 | * @param bool $status |
1495 | * | |
a6c01b45 CW |
1496 | * @return array |
1497 | * array of importable Fields | |
6a488035 | 1498 | */ |
00be9182 | 1499 | public static function &importableFields($status = FALSE) { |
6a488035 TO |
1500 | if (!self::$_importableFields) { |
1501 | if (!self::$_importableFields) { | |
96f94695 | 1502 | self::$_importableFields = []; |
6a488035 TO |
1503 | } |
1504 | if (!$status) { | |
96f94695 | 1505 | $fields = ['' => ['title' => ts('- do not import -')]]; |
6a488035 TO |
1506 | } |
1507 | else { | |
96f94695 | 1508 | $fields = ['' => ['title' => ts('- Activity Fields -')]]; |
6a488035 TO |
1509 | } |
1510 | ||
1511 | $tmpFields = CRM_Activity_DAO_Activity::import(); | |
1512 | $contactFields = CRM_Contact_BAO_Contact::importableFields('Individual', NULL); | |
1513 | ||
1514 | // Using new Dedupe rule. | |
96f94695 | 1515 | $ruleParams = [ |
6a488035 | 1516 | 'contact_type' => 'Individual', |
9d5494f7 | 1517 | 'used' => 'Unsupervised', |
96f94695 | 1518 | ]; |
6a488035 TO |
1519 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); |
1520 | ||
96f94695 | 1521 | $tmpConatctField = []; |
6a488035 TO |
1522 | if (is_array($fieldsArray)) { |
1523 | foreach ($fieldsArray as $value) { | |
1524 | $customFieldId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', | |
1525 | $value, | |
1526 | 'id', | |
1527 | 'column_name' | |
1528 | ); | |
1529 | $value = $customFieldId ? 'custom_' . $customFieldId : $value; | |
1530 | $tmpConatctField[trim($value)] = $contactFields[trim($value)]; | |
1531 | $tmpConatctField[trim($value)]['title'] = $tmpConatctField[trim($value)]['title'] . " (match to contact)"; | |
1532 | } | |
1533 | } | |
1534 | $tmpConatctField['external_identifier'] = $contactFields['external_identifier']; | |
1535 | $tmpConatctField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . " (match to contact)"; | |
1536 | $fields = array_merge($fields, $tmpConatctField); | |
1537 | $fields = array_merge($fields, $tmpFields); | |
1538 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Activity')); | |
1539 | self::$_importableFields = $fields; | |
1540 | } | |
1541 | return self::$_importableFields; | |
1542 | } | |
1543 | ||
1544 | /** | |
a59cecb1 | 1545 | * @deprecated - use the api instead. |
1546 | * | |
57507ae6 | 1547 | * Get the Activities of a target contact. |
6a488035 | 1548 | * |
041ab3d1 TO |
1549 | * @param int $contactId |
1550 | * Id of the contact whose activities need to find. | |
6a488035 | 1551 | * |
a6c01b45 CW |
1552 | * @return array |
1553 | * array of activity fields | |
6a488035 | 1554 | */ |
00be9182 | 1555 | public static function getContactActivity($contactId) { |
a59cecb1 | 1556 | // @todo remove this function entirely. |
96f94695 | 1557 | $activities = []; |
44f817d4 | 1558 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
a24b3694 | 1559 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
1560 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
1561 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
b319d00a | 1562 | |
6a488035 | 1563 | // First look for activities where contactId is one of the targets |
91da6cd5 | 1564 | $query = " |
a24b3694 | 1565 | SELECT activity_id, record_type_id |
91da6cd5 DL |
1566 | FROM civicrm_activity_contact |
1567 | WHERE contact_id = $contactId | |
1568 | "; | |
1569 | $dao = CRM_Core_DAO::executeQuery($query); | |
6a488035 | 1570 | while ($dao->fetch()) { |
9d5494f7 | 1571 | if ($dao->record_type_id == $targetID) { |
91da6cd5 DL |
1572 | $activities[$dao->activity_id]['targets'][$contactId] = $contactId; |
1573 | } | |
4c9b6178 | 1574 | elseif ($dao->record_type_id == $assigneeID) { |
91da6cd5 DL |
1575 | $activities[$dao->activity_id]['asignees'][$contactId] = $contactId; |
1576 | } | |
1577 | else { | |
1578 | // do source stuff here | |
42d30b83 | 1579 | $activities[$dao->activity_id]['source_contact_id'] = $contactId; |
91da6cd5 | 1580 | } |
6a488035 TO |
1581 | } |
1582 | ||
91da6cd5 | 1583 | $activityIds = array_keys($activities); |
6a488035 | 1584 | if (count($activityIds) < 1) { |
96f94695 | 1585 | return []; |
6a488035 | 1586 | } |
91da6cd5 | 1587 | |
6a488035 | 1588 | $activityIds = implode(',', $activityIds); |
91da6cd5 DL |
1589 | $query = " |
1590 | SELECT activity.id as activity_id, | |
1591 | activity_type_id, | |
1592 | subject, location, activity_date_time, details, status_id | |
1593 | FROM civicrm_activity activity | |
1594 | WHERE activity.id IN ($activityIds)"; | |
6a488035 | 1595 | |
91da6cd5 | 1596 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 | 1597 | |
6a488035 | 1598 | while ($dao->fetch()) { |
6a488035 | 1599 | $activities[$dao->activity_id]['id'] = $dao->activity_id; |
6a488035 TO |
1600 | $activities[$dao->activity_id]['activity_type_id'] = $dao->activity_type_id; |
1601 | $activities[$dao->activity_id]['subject'] = $dao->subject; | |
1602 | $activities[$dao->activity_id]['location'] = $dao->location; | |
1603 | $activities[$dao->activity_id]['activity_date_time'] = $dao->activity_date_time; | |
1604 | $activities[$dao->activity_id]['details'] = $dao->details; | |
1605 | $activities[$dao->activity_id]['status_id'] = $dao->status_id; | |
f9aa1e86 MW |
1606 | $activities[$dao->activity_id]['activity_name'] = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $dao->activity_type_id); |
1607 | $activities[$dao->activity_id]['status'] = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_status_id', $dao->status_id); | |
42d30b83 DL |
1608 | |
1609 | // set to null if not set | |
1610 | if (!isset($activities[$dao->activity_id]['source_contact_id'])) { | |
1611 | $activities[$dao->activity_id]['source_contact_id'] = NULL; | |
1612 | } | |
6a488035 TO |
1613 | } |
1614 | return $activities; | |
1615 | } | |
1616 | ||
1617 | /** | |
57507ae6 | 1618 | * Add activity for Membership/Event/Contribution. |
6a488035 | 1619 | * |
041ab3d1 TO |
1620 | * @param object $activity |
1621 | * (reference) particular component object. | |
1622 | * @param string $activityType | |
1623 | * For Membership Signup or Renewal. | |
c490a46a | 1624 | * @param int $targetContactID |
d2460a89 | 1625 | * @param array $params |
66a1e31f | 1626 | * Activity params to override. |
6a488035 | 1627 | * |
59f4c9ee | 1628 | * @return bool|NULL |
6a488035 | 1629 | */ |
59f4c9ee | 1630 | public static function addActivity( |
9d5494f7 | 1631 | &$activity, |
6a488035 | 1632 | $activityType = 'Membership Signup', |
d2460a89 | 1633 | $targetContactID = NULL, |
96f94695 | 1634 | $params = [] |
6a488035 | 1635 | ) { |
d2460a89 | 1636 | $date = date('YmdHis'); |
6a488035 | 1637 | if ($activity->__table == 'civicrm_membership') { |
6a488035 TO |
1638 | $component = 'Membership'; |
1639 | } | |
1640 | elseif ($activity->__table == 'civicrm_participant') { | |
6a488035 TO |
1641 | if ($activityType != 'Email') { |
1642 | $activityType = 'Event Registration'; | |
1643 | } | |
1644 | $component = 'Event'; | |
1645 | } | |
1646 | elseif ($activity->__table == 'civicrm_contribution') { | |
7808aae6 | 1647 | // create activity record only for Completed Contributions |
5b22d1b8 MW |
1648 | $contributionCompletedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'); |
1649 | if ($activity->contribution_status_id != $contributionCompletedStatusId) { | |
59f4c9ee | 1650 | return NULL; |
6a488035 | 1651 | } |
98f0683a | 1652 | $activityType = $component = 'Contribution'; |
6a488035 | 1653 | |
b6d493f3 MD |
1654 | // retrieve existing activity based on source_record_id and activity_type |
1655 | if (empty($params['id'])) { | |
96f94695 | 1656 | $params['id'] = CRM_Utils_Array::value('id', civicrm_api3('Activity', 'Get', [ |
b6d493f3 MD |
1657 | 'source_record_id' => $activity->id, |
1658 | 'activity_type_id' => $activityType, | |
96f94695 | 1659 | ])); |
b6d493f3 | 1660 | } |
6150b2a0 MD |
1661 | if (!empty($params['id'])) { |
1662 | // CRM-13237 : if activity record found, update it with campaign id of contribution | |
1663 | $params['campaign_id'] = $activity->campaign_id; | |
1664 | } | |
b6d493f3 | 1665 | |
6a488035 | 1666 | $date = CRM_Utils_Date::isoToMysql($activity->receive_date); |
6a488035 | 1667 | } |
d2460a89 | 1668 | |
96f94695 | 1669 | $activityParams = [ |
6a488035 TO |
1670 | 'source_contact_id' => $activity->contact_id, |
1671 | 'source_record_id' => $activity->id, | |
d66c61b6 | 1672 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', $activityType), |
6a488035 TO |
1673 | 'activity_date_time' => $date, |
1674 | 'is_test' => $activity->is_test, | |
d66c61b6 | 1675 | 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Completed'), |
6a488035 TO |
1676 | 'skipRecentView' => TRUE, |
1677 | 'campaign_id' => $activity->campaign_id, | |
96f94695 | 1678 | ]; |
d2460a89 MD |
1679 | $activityParams = array_merge($activityParams, $params); |
1680 | ||
1681 | if (empty($activityParams['subject'])) { | |
1682 | $activityParams['subject'] = self::getActivitySubject($activity); | |
1683 | } | |
6a488035 | 1684 | |
6e143f06 WA |
1685 | if (!empty($activity->activity_id)) { |
1686 | $activityParams['id'] = $activity->activity_id; | |
1687 | } | |
6a488035 | 1688 | // create activity with target contacts |
6150b2a0 MD |
1689 | $id = CRM_Core_Session::getLoggedInContactID(); |
1690 | if ($id) { | |
1691 | $activityParams['source_contact_id'] = $id; | |
71acd4bf | 1692 | $activityParams['target_contact_id'][] = $activity->contact_id; |
6a488035 TO |
1693 | } |
1694 | ||
b870f878 | 1695 | // CRM-14945 |
1696 | if (property_exists($activity, 'details')) { | |
1697 | $activityParams['details'] = $activity->details; | |
1698 | } | |
6a488035 TO |
1699 | //CRM-4027 |
1700 | if ($targetContactID) { | |
71acd4bf | 1701 | $activityParams['target_contact_id'][] = $targetContactID; |
6a488035 | 1702 | } |
d66c61b6 | 1703 | // @todo - use api - remove lots of wrangling above. Remove deprecated fatal & let form layer |
1704 | // deal with any exceptions. | |
6a488035 TO |
1705 | if (is_a(self::create($activityParams), 'CRM_Core_Error')) { |
1706 | CRM_Core_Error::fatal("Failed creating Activity for $component of id {$activity->id}"); | |
1707 | return FALSE; | |
1708 | } | |
1709 | } | |
1710 | ||
d2460a89 | 1711 | /** |
66a1e31f | 1712 | * Get activity subject on basis of component object. |
d2460a89 MD |
1713 | * |
1714 | * @param object $entityObj | |
66a1e31f | 1715 | * particular component object. |
d2460a89 MD |
1716 | * |
1717 | * @return string | |
1718 | */ | |
1719 | public static function getActivitySubject($entityObj) { | |
1720 | switch ($entityObj->__table) { | |
1721 | case 'civicrm_membership': | |
1722 | $membershipType = CRM_Member_PseudoConstant::membershipType($entityObj->membership_type_id); | |
1723 | $subject = $membershipType ? $membershipType : ts('Membership'); | |
1724 | ||
5ab57aa2 SL |
1725 | if (is_array($subject)) { |
1726 | $subject = implode(", ", $subject); | |
1727 | } | |
1728 | ||
d2460a89 MD |
1729 | if (!CRM_Utils_System::isNull($entityObj->source)) { |
1730 | $subject .= " - {$entityObj->source}"; | |
1731 | } | |
1732 | ||
1733 | if ($entityObj->owner_membership_id) { | |
1734 | list($displayName) = CRM_Contact_BAO_Contact::getDisplayAndImage(CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $entityObj->owner_membership_id, 'contact_id')); | |
1735 | $subject .= sprintf(' (by %s)', $displayName); | |
1736 | } | |
1737 | ||
1738 | $subject .= " - Status: " . CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $entityObj->status_id, 'label'); | |
1739 | return $subject; | |
1740 | ||
1741 | case 'civicrm_participant': | |
1742 | $event = CRM_Event_BAO_Event::getEvents(1, $entityObj->event_id, TRUE, FALSE); | |
1743 | $roles = CRM_Event_PseudoConstant::participantRole(); | |
1744 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
1745 | $subject = $event[$entityObj->event_id]; | |
1746 | ||
1747 | if (!empty($roles[$entityObj->role_id])) { | |
1748 | $subject .= ' - ' . $roles[$entityObj->role_id]; | |
1749 | } | |
1750 | if (!empty($status[$entityObj->status_id])) { | |
1751 | $subject .= ' - ' . $status[$entityObj->status_id]; | |
1752 | } | |
1753 | ||
1754 | return $subject; | |
1755 | ||
1756 | case 'civicrm_contribution': | |
1757 | $subject = CRM_Utils_Money::format($entityObj->total_amount, $entityObj->currency); | |
1758 | if (!CRM_Utils_System::isNull($entityObj->source)) { | |
1759 | $subject .= " - {$entityObj->source}"; | |
1760 | } | |
1761 | ||
1ea22892 | 1762 | // Amount and source could exceed max length of subject column. |
1763 | return CRM_Utils_String::ellipsify($subject, 255); | |
d2460a89 MD |
1764 | } |
1765 | } | |
1766 | ||
6a488035 | 1767 | /** |
57507ae6 | 1768 | * Get Parent activity for currently viewed activity. |
6a488035 | 1769 | * |
041ab3d1 TO |
1770 | * @param int $activityId |
1771 | * Current activity id. | |
6a488035 | 1772 | * |
a6c01b45 | 1773 | * @return int |
57507ae6 | 1774 | * Id of parent activity otherwise false. |
6a488035 | 1775 | */ |
00be9182 | 1776 | public static function getParentActivity($activityId) { |
96f94695 | 1777 | static $parentActivities = []; |
6a488035 TO |
1778 | |
1779 | $activityId = CRM_Utils_Type::escape($activityId, 'Integer'); | |
1780 | ||
1781 | if (!array_key_exists($activityId, $parentActivities)) { | |
96f94695 | 1782 | $parentActivities[$activityId] = []; |
6a488035 TO |
1783 | |
1784 | $parentId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', | |
1785 | $activityId, | |
1786 | 'parent_id' | |
1787 | ); | |
1788 | ||
1789 | $parentActivities[$activityId] = $parentId ? $parentId : FALSE; | |
1790 | } | |
1791 | ||
1792 | return $parentActivities[$activityId]; | |
1793 | } | |
1794 | ||
1795 | /** | |
57507ae6 | 1796 | * Get total count of prior revision of currently viewed activity. |
77b97be7 | 1797 | * |
041ab3d1 TO |
1798 | * @param $activityID |
1799 | * Current activity id. | |
6a488035 | 1800 | * |
a6c01b45 CW |
1801 | * @return int |
1802 | * $params count of prior activities otherwise false. | |
6a488035 | 1803 | */ |
00be9182 | 1804 | public static function getPriorCount($activityID) { |
96f94695 | 1805 | static $priorCounts = []; |
6a488035 TO |
1806 | |
1807 | $activityID = CRM_Utils_Type::escape($activityID, 'Integer'); | |
1808 | ||
1809 | if (!array_key_exists($activityID, $priorCounts)) { | |
96f94695 | 1810 | $priorCounts[$activityID] = []; |
6a488035 TO |
1811 | $originalID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', |
1812 | $activityID, | |
1813 | 'original_id' | |
1814 | ); | |
1815 | $count = 0; | |
1816 | if ($originalID) { | |
1817 | $query = " | |
1818 | SELECT count( id ) AS cnt | |
1819 | FROM civicrm_activity | |
1820 | WHERE ( id = {$originalID} OR original_id = {$originalID} ) | |
1821 | AND is_current_revision = 0 | |
1822 | AND id < {$activityID} | |
1823 | "; | |
96f94695 | 1824 | $params = [1 => [$originalID, 'Integer']]; |
6a488035 TO |
1825 | $count = CRM_Core_DAO::singleValueQuery($query, $params); |
1826 | } | |
1827 | $priorCounts[$activityID] = $count ? $count : 0; | |
1828 | } | |
1829 | ||
1830 | return $priorCounts[$activityID]; | |
1831 | } | |
1832 | ||
1833 | /** | |
db7de9c1 | 1834 | * Get all prior activities of currently viewed activity. |
6a488035 | 1835 | * |
041ab3d1 TO |
1836 | * @param $activityID |
1837 | * Current activity id. | |
77b97be7 EM |
1838 | * @param bool $onlyPriorRevisions |
1839 | * | |
a6c01b45 CW |
1840 | * @return array |
1841 | * prior activities info. | |
6a488035 | 1842 | */ |
00be9182 | 1843 | public static function getPriorAcitivities($activityID, $onlyPriorRevisions = FALSE) { |
96f94695 | 1844 | static $priorActivities = []; |
6a488035 TO |
1845 | |
1846 | $activityID = CRM_Utils_Type::escape($activityID, 'Integer'); | |
1847 | $index = $activityID . '_' . (int) $onlyPriorRevisions; | |
1848 | ||
1849 | if (!array_key_exists($index, $priorActivities)) { | |
96f94695 | 1850 | $priorActivities[$index] = []; |
6a488035 TO |
1851 | |
1852 | $originalID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', | |
1853 | $activityID, | |
1854 | 'original_id' | |
1855 | ); | |
6ea979d9 CW |
1856 | if (!$originalID) { |
1857 | $originalID = $activityID; | |
1858 | } | |
6a488035 TO |
1859 | if ($originalID) { |
1860 | $query = " | |
1861 | SELECT c.display_name as name, cl.modified_date as date, ca.id as activityID | |
1862 | FROM civicrm_log cl, civicrm_contact c, civicrm_activity ca | |
1863 | WHERE (ca.id = %1 OR ca.original_id = %1) | |
1864 | AND cl.entity_table = 'civicrm_activity' | |
1865 | AND cl.entity_id = ca.id | |
1866 | AND cl.modified_id = c.id | |
1867 | "; | |
1868 | if ($onlyPriorRevisions) { | |
1869 | $query .= " AND ca.id < {$activityID}"; | |
1870 | } | |
1871 | $query .= " ORDER BY ca.id DESC"; | |
1872 | ||
96f94695 | 1873 | $params = [1 => [$originalID, 'Integer']]; |
6a488035 TO |
1874 | $dao = CRM_Core_DAO::executeQuery($query, $params); |
1875 | ||
1876 | while ($dao->fetch()) { | |
1877 | $priorActivities[$index][$dao->activityID]['id'] = $dao->activityID; | |
1878 | $priorActivities[$index][$dao->activityID]['name'] = $dao->name; | |
1879 | $priorActivities[$index][$dao->activityID]['date'] = $dao->date; | |
6a488035 | 1880 | } |
6a488035 TO |
1881 | } |
1882 | } | |
1883 | return $priorActivities[$index]; | |
1884 | } | |
1885 | ||
1886 | /** | |
db7de9c1 | 1887 | * Find the latest revision of a given activity. |
6a488035 | 1888 | * |
041ab3d1 TO |
1889 | * @param int $activityID |
1890 | * Prior activity id. | |
6a488035 | 1891 | * |
a6c01b45 CW |
1892 | * @return int |
1893 | * current activity id. | |
6a488035 | 1894 | */ |
00be9182 | 1895 | public static function getLatestActivityId($activityID) { |
96f94695 | 1896 | static $latestActivityIds = []; |
6a488035 TO |
1897 | |
1898 | $activityID = CRM_Utils_Type::escape($activityID, 'Integer'); | |
1899 | ||
1900 | if (!array_key_exists($activityID, $latestActivityIds)) { | |
96f94695 | 1901 | $latestActivityIds[$activityID] = []; |
6a488035 TO |
1902 | |
1903 | $originalID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', | |
1904 | $activityID, | |
1905 | 'original_id' | |
1906 | ); | |
1907 | if ($originalID) { | |
1908 | $activityID = $originalID; | |
1909 | } | |
96f94695 | 1910 | $params = [1 => [$activityID, 'Integer']]; |
6a488035 TO |
1911 | $query = "SELECT id from civicrm_activity where original_id = %1 and is_current_revision = 1"; |
1912 | ||
1913 | $latestActivityIds[$activityID] = CRM_Core_DAO::singleValueQuery($query, $params); | |
1914 | } | |
1915 | ||
1916 | return $latestActivityIds[$activityID]; | |
1917 | } | |
1918 | ||
1919 | /** | |
db7de9c1 | 1920 | * Create a follow up a given activity. |
6a488035 | 1921 | * |
5a4f6742 CW |
1922 | * @param int $activityId |
1923 | * activity id of parent activity. | |
c490a46a | 1924 | * @param array $params |
77b97be7 | 1925 | * |
59f4c9ee | 1926 | * @return CRM_Activity_BAO_Activity|null|object |
6a488035 | 1927 | */ |
00be9182 | 1928 | public static function createFollowupActivity($activityId, $params) { |
6a488035 | 1929 | if (!$activityId) { |
59f4c9ee | 1930 | return NULL; |
6a488035 TO |
1931 | } |
1932 | ||
96f94695 | 1933 | $followupParams = []; |
6a488035 | 1934 | $followupParams['parent_id'] = $activityId; |
3bdcd4ec | 1935 | $followupParams['source_contact_id'] = CRM_Core_Session::getLoggedInContactID(); |
5b22d1b8 | 1936 | $followupParams['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_status_id', 'Scheduled'); |
6a488035 TO |
1937 | |
1938 | $followupParams['activity_type_id'] = $params['followup_activity_type_id']; | |
1939 | // Get Subject of Follow-up Activiity, CRM-4491 | |
1940 | $followupParams['subject'] = CRM_Utils_Array::value('followup_activity_subject', $params); | |
90b05581 | 1941 | $followupParams['assignee_contact_id'] = CRM_Utils_Array::value('followup_assignee_contact_id', $params); |
6a488035 | 1942 | |
7808aae6 | 1943 | // Create target contact for followup. |
a7488080 | 1944 | if (!empty($params['target_contact_id'])) { |
6a488035 TO |
1945 | $followupParams['target_contact_id'] = $params['target_contact_id']; |
1946 | } | |
1947 | ||
d7c5e6c3 | 1948 | $followupParams['activity_date_time'] = $params['followup_date']; |
6a488035 TO |
1949 | $followupActivity = self::create($followupParams); |
1950 | ||
1951 | return $followupActivity; | |
1952 | } | |
1953 | ||
1954 | /** | |
100fef9d | 1955 | * Get Activity specific File according activity type Id. |
6a488035 | 1956 | * |
041ab3d1 TO |
1957 | * @param int $activityTypeId |
1958 | * Activity id. | |
77b97be7 | 1959 | * @param string $crmDir |
6a488035 | 1960 | * |
72b3a70c CW |
1961 | * @return string|bool |
1962 | * if file exists returns $activityTypeFile activity filename otherwise false. | |
6a488035 | 1963 | */ |
00be9182 | 1964 | public static function getFileForActivityTypeId($activityTypeId, $crmDir = 'Activity') { |
6a488035 TO |
1965 | $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE); |
1966 | ||
1967 | if ($activityTypes[$activityTypeId]['name']) { | |
1968 | $activityTypeFile = CRM_Utils_String::munge(ucwords($activityTypes[$activityTypeId]['name']), '', 0); | |
1969 | } | |
1970 | else { | |
1971 | return FALSE; | |
1972 | } | |
1973 | ||
1974 | global $civicrm_root; | |
1975 | $config = CRM_Core_Config::singleton(); | |
1976 | if (!file_exists(rtrim($civicrm_root, '/') . "/CRM/{$crmDir}/Form/Activity/{$activityTypeFile}.php")) { | |
1977 | if (empty($config->customPHPPathDir)) { | |
1978 | return FALSE; | |
1979 | } | |
1980 | elseif (!file_exists(rtrim($config->customPHPPathDir, '/') . "/CRM/{$crmDir}/Form/Activity/{$activityTypeFile}.php")) { | |
1981 | return FALSE; | |
1982 | } | |
1983 | } | |
1984 | ||
1985 | return $activityTypeFile; | |
1986 | } | |
1987 | ||
1988 | /** | |
ee0ce2ef | 1989 | * Restore the activity. |
6a488035 | 1990 | * |
041ab3d1 | 1991 | * @param array $params |
6a488035 | 1992 | * |
ee0ce2ef | 1993 | * @return CRM_Activity_DAO_Activity |
6a488035 TO |
1994 | */ |
1995 | public static function restoreActivity(&$params) { | |
1996 | $activity = new CRM_Activity_DAO_Activity(); | |
1997 | $activity->copyValues($params); | |
1998 | ||
1999 | $activity->is_deleted = 0; | |
2000 | $result = $activity->save(); | |
2001 | ||
2002 | return $result; | |
2003 | } | |
2004 | ||
760ac501 | 2005 | /** |
ce9d78e1 CW |
2006 | * Return list of activity statuses of a given type. |
2007 | * | |
2008 | * Note: activity status options use the "grouping" field to distinguish status types. | |
2009 | * Types are defined in class constants INCOMPLETE, COMPLETED, CANCELLED | |
760ac501 | 2010 | * |
ce9d78e1 | 2011 | * @param int $type |
760ac501 CW |
2012 | * |
2013 | * @return array | |
2014 | */ | |
ce9d78e1 | 2015 | public static function getStatusesByType($type) { |
760ac501 | 2016 | if (!isset(Civi::$statics[__CLASS__][__FUNCTION__])) { |
96f94695 | 2017 | $statuses = civicrm_api3('OptionValue', 'get', [ |
d544ffcd | 2018 | 'option_group_id' => 'activity_status', |
96f94695 | 2019 | 'return' => ['value', 'name', 'filter'], |
2020 | 'options' => ['limit' => 0], | |
2021 | ]); | |
ce9d78e1 | 2022 | Civi::$statics[__CLASS__][__FUNCTION__] = $statuses['values']; |
760ac501 | 2023 | } |
96f94695 | 2024 | $ret = []; |
ce9d78e1 CW |
2025 | foreach (Civi::$statics[__CLASS__][__FUNCTION__] as $status) { |
2026 | if ($status['filter'] == $type) { | |
2027 | $ret[$status['value']] = $status['name']; | |
2028 | } | |
2029 | } | |
2030 | return $ret; | |
760ac501 CW |
2031 | } |
2032 | ||
2033 | /** | |
2034 | * Check if activity is overdue. | |
2035 | * | |
2036 | * @param array $activity | |
2037 | * | |
2038 | * @return bool | |
2039 | */ | |
2040 | public static function isOverdue($activity) { | |
ce9d78e1 | 2041 | return array_key_exists($activity['status_id'], self::getStatusesByType(self::INCOMPLETE)) && CRM_Utils_Date::overdue($activity['activity_date_time']); |
760ac501 CW |
2042 | } |
2043 | ||
6a488035 | 2044 | /** |
db7de9c1 | 2045 | * Get the exportable fields for Activities. |
6a488035 | 2046 | * |
041ab3d1 TO |
2047 | * @param string $name |
2048 | * If it is called by case $name = Case else $name = Activity. | |
6a488035 | 2049 | * |
a6c01b45 CW |
2050 | * @return array |
2051 | * array of exportable Fields | |
6a488035 | 2052 | */ |
dcc79888 | 2053 | public static function exportableFields($name = 'Activity') { |
96f94695 | 2054 | self::$_exportableFields[$name] = []; |
dcc79888 | 2055 | |
2056 | // TODO: ideally we should retrieve all fields from xml, in this case since activity processing is done | |
2057 | // my case hence we have defined fields as case_* | |
2058 | if ($name == 'Activity') { | |
2059 | $exportableFields = CRM_Activity_DAO_Activity::export(); | |
2060 | $exportableFields['source_contact_id'] = [ | |
2061 | 'title' => ts('Source Contact ID'), | |
2062 | 'type' => CRM_Utils_Type::T_INT, | |
2063 | ]; | |
96f94695 | 2064 | $exportableFields['source_contact'] = [ |
dcc79888 | 2065 | 'title' => ts('Source Contact'), |
2066 | 'type' => CRM_Utils_Type::T_STRING, | |
96f94695 | 2067 | ]; |
6a488035 | 2068 | |
96f94695 | 2069 | $Activityfields = [ |
2070 | 'activity_type' => [ | |
dcc79888 | 2071 | 'title' => ts('Activity Type'), |
2072 | 'name' => 'activity_type', | |
2073 | 'type' => CRM_Utils_Type::T_STRING, | |
2074 | 'searchByLabel' => TRUE, | |
96f94695 | 2075 | ], |
2076 | 'activity_status' => [ | |
dcc79888 | 2077 | 'title' => ts('Activity Status'), |
2078 | 'name' => 'activity_status', | |
2079 | 'type' => CRM_Utils_Type::T_STRING, | |
2080 | 'searchByLabel' => TRUE, | |
96f94695 | 2081 | ], |
2082 | 'activity_priority' => [ | |
dcc79888 | 2083 | 'title' => ts('Activity Priority'), |
2084 | 'name' => 'activity_priority', | |
2085 | 'type' => CRM_Utils_Type::T_STRING, | |
2086 | 'searchByLabel' => TRUE, | |
96f94695 | 2087 | ], |
2088 | ]; | |
dcc79888 | 2089 | $fields = array_merge($Activityfields, $exportableFields); |
2090 | } | |
2091 | else { | |
2092 | // Set title to activity fields. | |
96f94695 | 2093 | $fields = [ |
2094 | 'case_activity_subject' => [ | |
2095 | 'title' => ts('Activity Subject'), | |
2096 | 'type' => CRM_Utils_Type::T_STRING, | |
2097 | ], | |
2098 | 'case_source_contact_id' => [ | |
2099 | 'title' => ts('Activity Reporter'), | |
2100 | 'type' => CRM_Utils_Type::T_STRING, | |
2101 | ], | |
2102 | 'case_recent_activity_date' => [ | |
2103 | 'title' => ts('Activity Actual Date'), | |
2104 | 'type' => CRM_Utils_Type::T_DATE, | |
2105 | ], | |
2106 | 'case_scheduled_activity_date' => [ | |
dcc79888 | 2107 | 'title' => ts('Activity Scheduled Date'), |
2108 | 'type' => CRM_Utils_Type::T_DATE, | |
96f94695 | 2109 | ], |
2110 | 'case_recent_activity_type' => [ | |
2111 | 'title' => ts('Activity Type'), | |
2112 | 'type' => CRM_Utils_Type::T_STRING, | |
2113 | ], | |
2114 | 'case_activity_status' => [ | |
2115 | 'title' => ts('Activity Status'), | |
2116 | 'type' => CRM_Utils_Type::T_STRING, | |
2117 | ], | |
2118 | 'case_activity_duration' => [ | |
2119 | 'title' => ts('Activity Duration'), | |
2120 | 'type' => CRM_Utils_Type::T_INT, | |
2121 | ], | |
2122 | 'case_activity_medium_id' => [ | |
2123 | 'title' => ts('Activity Medium'), | |
2124 | 'type' => CRM_Utils_Type::T_INT, | |
2125 | ], | |
2126 | 'case_activity_details' => [ | |
2127 | 'title' => ts('Activity Details'), | |
2128 | 'type' => CRM_Utils_Type::T_TEXT, | |
2129 | ], | |
2130 | 'case_activity_is_auto' => [ | |
dcc79888 | 2131 | 'title' => ts('Activity Auto-generated?'), |
2132 | 'type' => CRM_Utils_Type::T_BOOLEAN, | |
96f94695 | 2133 | ], |
2134 | ]; | |
dcc79888 | 2135 | } |
6a488035 | 2136 | |
dcc79888 | 2137 | // add custom data for case activities |
2138 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Activity')); | |
6a488035 | 2139 | |
dcc79888 | 2140 | self::$_exportableFields[$name] = $fields; |
6a488035 TO |
2141 | return self::$_exportableFields[$name]; |
2142 | } | |
2143 | ||
2144 | /** | |
63e9c3fd | 2145 | * Get the allowed profile fields for Activities. |
6a488035 | 2146 | * |
a6c01b45 CW |
2147 | * @return array |
2148 | * array of activity profile Fields | |
6a488035 | 2149 | */ |
00be9182 | 2150 | public static function getProfileFields() { |
6a488035 | 2151 | $exportableFields = self::exportableFields('Activity'); |
96f94695 | 2152 | $skipFields = [ |
4f79a2f5 | 2153 | 'activity_id', |
2154 | 'activity_type', | |
2155 | 'source_contact_id', | |
2156 | 'source_contact', | |
2157 | 'activity_campaign', | |
2158 | 'activity_is_test', | |
2159 | 'is_current_revision', | |
2160 | 'activity_is_deleted', | |
96f94695 | 2161 | ]; |
6a488035 TO |
2162 | $config = CRM_Core_Config::singleton(); |
2163 | if (!in_array('CiviCampaign', $config->enableComponents)) { | |
2164 | $skipFields[] = 'activity_engagement_level'; | |
2165 | } | |
2166 | ||
2167 | foreach ($skipFields as $field) { | |
2168 | if (isset($exportableFields[$field])) { | |
2169 | unset($exportableFields[$field]); | |
2170 | } | |
2171 | } | |
2172 | ||
2173 | // hack to use 'activity_type_id' instead of 'activity_type' | |
2174 | $exportableFields['activity_status_id'] = $exportableFields['activity_status']; | |
2175 | unset($exportableFields['activity_status']); | |
2176 | ||
2177 | return $exportableFields; | |
2178 | } | |
2179 | ||
2180 | /** | |
63e9c3fd EM |
2181 | * This function deletes the activity record related to contact record. |
2182 | * | |
2183 | * This is conditional on there being no target and assignee record | |
2184 | * with other contacts. | |
6a488035 | 2185 | * |
041ab3d1 TO |
2186 | * @param int $contactId |
2187 | * ContactId. | |
6a488035 TO |
2188 | * |
2189 | * @return true/null | |
6a488035 TO |
2190 | */ |
2191 | public static function cleanupActivity($contactId) { | |
2192 | $result = NULL; | |
2193 | if (!$contactId) { | |
2194 | return $result; | |
2195 | } | |
44f817d4 | 2196 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
2bf96211 | 2197 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
6a488035 TO |
2198 | |
2199 | $transaction = new CRM_Core_Transaction(); | |
2200 | ||
f1504541 DL |
2201 | // delete activity if there is no record in civicrm_activity_contact |
2202 | // pointing to any other contact record | |
2bf96211 | 2203 | $activityContact = new CRM_Activity_DAO_ActivityContact(); |
2204 | $activityContact->contact_id = $contactId; | |
2205 | $activityContact->record_type_id = $sourceID; | |
2206 | $activityContact->find(); | |
6a488035 | 2207 | |
2bf96211 | 2208 | while ($activityContact->fetch()) { |
f1504541 | 2209 | // delete activity_contact record for the deleted contact |
32ecf7bb BS |
2210 | $activityContact->delete(); |
2211 | ||
2212 | $activityContactOther = new CRM_Activity_DAO_ActivityContact(); | |
2213 | $activityContactOther->activity_id = $activityContact->activity_id; | |
32ecf7bb | 2214 | |
83e0a89c | 2215 | // delete activity only if no other contacts connected |
9d5494f7 | 2216 | if (!$activityContactOther->find(TRUE)) { |
96f94695 | 2217 | $activityParams = ['id' => $activityContact->activity_id]; |
32ecf7bb BS |
2218 | $result = self::deleteActivity($activityParams); |
2219 | } | |
2220 | ||
6a488035 | 2221 | } |
6a488035 TO |
2222 | |
2223 | $transaction->commit(); | |
2224 | ||
2225 | return $result; | |
2226 | } | |
2227 | ||
2228 | /** | |
567b2076 | 2229 | * Does user has sufficient permission for view/edit activity record. |
6a488035 | 2230 | * |
041ab3d1 TO |
2231 | * @param int $activityId |
2232 | * Activity record id. | |
2233 | * @param int $action | |
2234 | * Edit/view. | |
6a488035 | 2235 | * |
59f4c9ee | 2236 | * @return bool |
6a488035 TO |
2237 | */ |
2238 | public static function checkPermission($activityId, $action) { | |
3af8de9f | 2239 | |
6a488035 | 2240 | if (!$activityId || |
96f94695 | 2241 | !in_array($action, [CRM_Core_Action::UPDATE, CRM_Core_Action::VIEW]) |
6a488035 | 2242 | ) { |
3af8de9f | 2243 | return FALSE; |
6a488035 TO |
2244 | } |
2245 | ||
2246 | $activity = new CRM_Activity_DAO_Activity(); | |
2247 | $activity->id = $activityId; | |
2248 | if (!$activity->find(TRUE)) { | |
3af8de9f | 2249 | return FALSE; |
6a488035 | 2250 | } |
ac4b9bc3 | 2251 | |
11b18d9d | 2252 | if (!self::hasPermissionForActivityType($activity->activity_type_id)) { |
ff2a3553 | 2253 | // this check is redundant for api access / anything that calls the selectWhereClause |
2254 | // to determine ACLs. | |
11b18d9d | 2255 | return FALSE; |
2256 | } | |
ac4b9bc3 | 2257 | // Return early when it is case activity. |
2258 | // Check for CiviCase related permission. | |
2259 | if (CRM_Case_BAO_Case::isCaseActivity($activityId)) { | |
2260 | return self::isContactPermittedAccessToCaseActivity($activityId, $action, $activity->activity_type_id); | |
2261 | } | |
2262 | ||
7808aae6 | 2263 | // Check for this permission related to contact. |
6a488035 TO |
2264 | $permission = CRM_Core_Permission::VIEW; |
2265 | if ($action == CRM_Core_Action::UPDATE) { | |
2266 | $permission = CRM_Core_Permission::EDIT; | |
2267 | } | |
2268 | ||
44f817d4 | 2269 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
034500d4 | 2270 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
2271 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
2272 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
2273 | ||
7808aae6 | 2274 | // Check for source contact. |
f3b59360 | 2275 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); |
2276 | // Account for possibility of activity not having a source contact (as it may have been deleted). | |
2277 | $allow = $sourceContactId ? CRM_Contact_BAO_Contact_Permission::allow($sourceContactId, $permission) : TRUE; | |
2278 | if (!$allow) { | |
2279 | return FALSE; | |
6a488035 TO |
2280 | } |
2281 | ||
7808aae6 | 2282 | // Check for target and assignee contacts. |
f3b59360 | 2283 | // First check for supper permission. |
2284 | $supPermission = 'view all contacts'; | |
2285 | if ($action == CRM_Core_Action::UPDATE) { | |
2286 | $supPermission = 'edit all contacts'; | |
2287 | } | |
2288 | $allow = CRM_Core_Permission::check($supPermission); | |
2289 | ||
2290 | // User might have sufficient permission, through acls. | |
2291 | if (!$allow) { | |
2292 | $allow = TRUE; | |
2293 | // Get the target contacts. | |
2294 | $targetContacts = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $targetID); | |
2295 | foreach ($targetContacts as $cnt => $contactId) { | |
2296 | if (!CRM_Contact_BAO_Contact_Permission::allow($contactId, $permission)) { | |
2297 | $allow = FALSE; | |
2298 | break; | |
2299 | } | |
2300 | } | |
2301 | ||
2302 | // Get the assignee contacts. | |
2303 | if ($allow) { | |
2304 | $assigneeContacts = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $assigneeID); | |
2305 | foreach ($assigneeContacts as $cnt => $contactId) { | |
6a488035 TO |
2306 | if (!CRM_Contact_BAO_Contact_Permission::allow($contactId, $permission)) { |
2307 | $allow = FALSE; | |
2308 | break; | |
2309 | } | |
2310 | } | |
6a488035 TO |
2311 | } |
2312 | } | |
2313 | ||
2314 | return $allow; | |
2315 | } | |
2316 | ||
ac4b9bc3 | 2317 | /** |
2318 | * Check if the logged in user has permission for the given case activity. | |
2319 | * | |
2320 | * @param int $activityId | |
2321 | * @param int $action | |
2322 | * @param int $activityTypeID | |
2323 | * | |
2324 | * @return bool | |
2325 | */ | |
2326 | protected static function isContactPermittedAccessToCaseActivity($activityId, $action, $activityTypeID) { | |
11b18d9d | 2327 | $oper = 'view'; |
2328 | if ($action == CRM_Core_Action::UPDATE) { | |
2329 | $oper = 'edit'; | |
ac4b9bc3 | 2330 | } |
11b18d9d | 2331 | $allow = CRM_Case_BAO_Case::checkPermission($activityId, |
2332 | $oper, | |
2333 | $activityTypeID | |
2334 | ); | |
ac4b9bc3 | 2335 | |
2336 | return $allow; | |
2337 | } | |
2338 | ||
15e11313 | 2339 | /** |
a2d210b2 | 2340 | * Check if the logged in user has permission to access the given activity type. |
2341 | * | |
15e11313 | 2342 | * @param int $activityTypeID |
a2d210b2 | 2343 | * |
15e11313 | 2344 | * @return bool |
2345 | */ | |
2346 | protected static function hasPermissionForActivityType($activityTypeID) { | |
a2d210b2 | 2347 | $permittedActivityTypes = self::getPermittedActivityTypes(); |
2348 | return isset($permittedActivityTypes[$activityTypeID]); | |
2349 | } | |
15e11313 | 2350 | |
a2d210b2 | 2351 | /** |
2352 | * Get the activity types the user is permitted to access. | |
2353 | * | |
2354 | * The types are filtered by the components they have access to. ie. a user | |
2355 | * with access CiviContribute but not CiviMember will see contribution related | |
2356 | * activities and activities with no component (e.g meetings) but not member related ones. | |
2357 | * | |
2358 | * @return array | |
2359 | */ | |
ff2a3553 | 2360 | protected static function getPermittedActivityTypes() { |
a2d210b2 | 2361 | $userID = (int) CRM_Core_Session::getLoggedInContactID(); |
2362 | if (!isset(Civi::$statics[__CLASS__]['permitted_activity_types'][$userID])) { | |
2363 | $permittedActivityTypes = []; | |
2364 | $components = self::activityComponents(FALSE); | |
2365 | $componentClause = empty($components) ? '' : (' OR component_id IN (' . implode(', ', array_keys($components)) . ')'); | |
2366 | ||
2367 | $types = CRM_Core_DAO::executeQuery( | |
2368 | " | |
2369 | SELECT option_value.value activity_type_id | |
2370 | FROM civicrm_option_value option_value | |
2371 | INNER JOIN civicrm_option_group grp ON (grp.id = option_group_id AND grp.name = 'activity_type') | |
2372 | WHERE component_id IS NULL $componentClause")->fetchAll(); | |
2373 | foreach ($types as $type) { | |
ff2a3553 | 2374 | $permittedActivityTypes[$type['activity_type_id']] = (int) $type['activity_type_id']; |
a2d210b2 | 2375 | } |
2376 | Civi::$statics[__CLASS__]['permitted_activity_types'][$userID] = $permittedActivityTypes; | |
2377 | } | |
2378 | return Civi::$statics[__CLASS__]['permitted_activity_types'][$userID]; | |
15e11313 | 2379 | } |
2380 | ||
84be264e | 2381 | /** |
2382 | * @param $params | |
2383 | * @return array | |
2384 | */ | |
2385 | protected static function getActivityParamsForDashboardFunctions($params) { | |
2386 | $activityParams = [ | |
2387 | 'is_deleted' => 0, | |
2388 | 'is_current_revision' => 1, | |
2389 | 'is_test' => 0, | |
2390 | 'contact_id' => CRM_Utils_Array::value('contact_id', $params), | |
6e793248 | 2391 | 'activity_date_time' => CRM_Utils_Array::value('activity_date_time', $params), |
84be264e | 2392 | 'check_permissions' => 1, |
2393 | 'options' => [ | |
2394 | 'offset' => CRM_Utils_Array::value('offset', $params, 0), | |
2395 | ], | |
2396 | ]; | |
2397 | ||
2398 | if (!empty($params['activity_status_id'])) { | |
2399 | $activityParams['activity_status_id'] = ['IN' => explode(',', $params['activity_status_id'])]; | |
2400 | } | |
2401 | ||
2402 | $activityParams['activity_type_id'] = self::filterActivityTypes($params); | |
2403 | $enabledComponents = self::activityComponents(); | |
2404 | // @todo - should we move this to activity get api. | |
2405 | foreach ([ | |
2406 | 'case_id' => 'CiviCase', | |
96f94695 | 2407 | 'campaign_id' => 'CiviCampaign', |
84be264e | 2408 | ] as $attr => $component) { |
2409 | if (!in_array($component, $enabledComponents)) { | |
2410 | $activityParams[$attr] = ['IS NULL' => 1]; | |
2411 | } | |
2412 | } | |
2413 | return $activityParams; | |
2414 | } | |
2415 | ||
55806731 CR |
2416 | /** |
2417 | * Checks if user has permissions to edit inbound e-mails, either bsic info | |
2418 | * or both basic information and content. | |
2419 | * | |
2420 | * @return bool | |
2421 | */ | |
2a7eaaa8 | 2422 | public static function checkEditInboundEmailsPermissions() { |
55806731 CR |
2423 | if (CRM_Core_Permission::check('edit inbound email basic information') |
2424 | || CRM_Core_Permission::check('edit inbound email basic information and content') | |
2425 | ) { | |
2426 | return TRUE; | |
2427 | } | |
2428 | ||
2429 | return FALSE; | |
2430 | } | |
2431 | ||
6a488035 | 2432 | /** |
db7de9c1 | 2433 | * Wrapper for ajax activity selector. |
6a488035 | 2434 | * |
041ab3d1 TO |
2435 | * @param array $params |
2436 | * Associated array for params record id. | |
6a488035 | 2437 | * |
a6c01b45 | 2438 | * @return array |
db7de9c1 | 2439 | * Associated array of contact activities |
6a488035 TO |
2440 | */ |
2441 | public static function getContactActivitySelector(&$params) { | |
7808aae6 | 2442 | // Format the params. |
9d5494f7 | 2443 | $params['offset'] = ($params['page'] - 1) * $params['rp']; |
6a488035 | 2444 | $params['rowCount'] = $params['rp']; |
9d5494f7 TO |
2445 | $params['sort'] = CRM_Utils_Array::value('sortBy', $params); |
2446 | $params['caseId'] = NULL; | |
2447 | $context = CRM_Utils_Array::value('context', $params); | |
e5dcfebc | 2448 | $showContactOverlay = !CRM_Utils_String::startsWith($context, "dashlet"); |
96f94695 | 2449 | $activityTypeInfo = civicrm_api3('OptionValue', 'get', [ |
8c99c0bb | 2450 | 'option_group_id' => "activity_type", |
96f94695 | 2451 | 'options' => ['limit' => 0], |
2452 | ]); | |
2453 | $activityIcons = []; | |
8c99c0bb CW |
2454 | foreach ($activityTypeInfo['values'] as $type) { |
2455 | if (!empty($type['icon'])) { | |
2456 | $activityIcons[$type['value']] = $type['icon']; | |
2457 | } | |
2458 | } | |
c43665cc | 2459 | CRM_Utils_Date::convertFormDateToApiFormat($params, 'activity_date_time'); |
6a488035 | 2460 | |
7808aae6 | 2461 | // Get contact activities. |
6e793248 | 2462 | $activities = CRM_Activity_BAO_Activity::getActivities($params); |
6a488035 | 2463 | |
7808aae6 | 2464 | // Add total. |
6e793248 | 2465 | $params['total'] = CRM_Activity_BAO_Activity::getActivitiesCount($params); |
6a488035 | 2466 | |
7808aae6 | 2467 | // Format params and add links. |
96f94695 | 2468 | $contactActivities = []; |
6a488035 TO |
2469 | |
2470 | if (!empty($activities)) { | |
2471 | $activityStatus = CRM_Core_PseudoConstant::activityStatus(); | |
2472 | ||
7808aae6 | 2473 | // Check logged in user for permission. |
6a488035 TO |
2474 | $page = new CRM_Core_Page(); |
2475 | CRM_Contact_Page_View::checkUserPermission($page, $params['contact_id']); | |
96f94695 | 2476 | $permissions = [$page->_permission]; |
6a488035 TO |
2477 | if (CRM_Core_Permission::check('delete activities')) { |
2478 | $permissions[] = CRM_Core_Permission::DELETE; | |
2479 | } | |
2480 | ||
2481 | $mask = CRM_Core_Action::mask($permissions); | |
2482 | ||
2483 | foreach ($activities as $activityId => $values) { | |
6e793248 | 2484 | $activity = ['source_contact_name' => '', 'target_contact_name' => '']; |
febb6506 | 2485 | $activity['DT_RowId'] = $activityId; |
7808aae6 | 2486 | // Add class to this row if overdue. |
b62580ac | 2487 | $activity['DT_RowClass'] = "crm-entity status-id-{$values['status_id']}"; |
760ac501 | 2488 | if (self::isOverdue($values)) { |
7d12de7f JL |
2489 | $activity['DT_RowClass'] .= ' status-overdue'; |
2490 | } | |
2491 | else { | |
2492 | $activity['DT_RowClass'] .= ' status-ontime'; | |
2493 | } | |
2494 | ||
96f94695 | 2495 | $activity['DT_RowAttr'] = []; |
febb6506 JL |
2496 | $activity['DT_RowAttr']['data-entity'] = 'activity'; |
2497 | $activity['DT_RowAttr']['data-id'] = $activityId; | |
7d12de7f | 2498 | |
8c99c0bb | 2499 | $activity['activity_type'] = (!empty($activityIcons[$values['activity_type_id']]) ? '<span class="crm-i ' . $activityIcons[$values['activity_type_id']] . '"></span> ' : '') . $values['activity_type']; |
7d12de7f | 2500 | $activity['subject'] = $values['subject']; |
ad280fb6 | 2501 | |
6a488035 | 2502 | if ($params['contact_id'] == $values['source_contact_id']) { |
7d12de7f | 2503 | $activity['source_contact_name'] = $values['source_contact_name']; |
6a488035 TO |
2504 | } |
2505 | elseif ($values['source_contact_id']) { | |
e846bd8d | 2506 | $srcTypeImage = ""; |
2507 | if ($showContactOverlay) { | |
2508 | $srcTypeImage = CRM_Contact_BAO_Contact_Utils::getImage( | |
2509 | CRM_Contact_BAO_Contact::getContactType($values['source_contact_id']), | |
2510 | FALSE, | |
2511 | $values['source_contact_id']); | |
2512 | } | |
160db45f | 2513 | $activity['source_contact_name'] = $srcTypeImage . CRM_Utils_System::href($values['source_contact_name'], |
96f94695 | 2514 | 'civicrm/contact/view', "reset=1&cid={$values['source_contact_id']}"); |
6a488035 TO |
2515 | } |
2516 | else { | |
7d12de7f | 2517 | $activity['source_contact_name'] = '<em>n/a</em>'; |
6a488035 TO |
2518 | } |
2519 | ||
2520 | if (isset($values['mailingId']) && !empty($values['mailingId'])) { | |
7d12de7f | 2521 | $activity['target_contact'] = CRM_Utils_System::href($values['recipients'], |
5a99d240 KJ |
2522 | 'civicrm/mailing/report/event', |
2523 | "mid={$values['source_record_id']}&reset=1&event=queue&cid={$params['contact_id']}&context=activitySelector"); | |
6a488035 | 2524 | } |
a7488080 | 2525 | elseif (!empty($values['recipients'])) { |
7d12de7f | 2526 | $activity['target_contact_name'] = $values['recipients']; |
6a488035 | 2527 | } |
9254ec4e | 2528 | elseif (isset($values['target_contact_counter']) && $values['target_contact_counter']) { |
7d12de7f | 2529 | $activity['target_contact_name'] = ''; |
a84a8555 | 2530 | $firstTargetName = reset($values['target_contact_name']); |
2531 | $firstTargetContactID = key($values['target_contact_name']); | |
2532 | ||
2533 | $targetLink = CRM_Utils_System::href($firstTargetName, 'civicrm/contact/view', "reset=1&cid={$firstTargetContactID}"); | |
2534 | if ($showContactOverlay) { | |
2535 | $targetTypeImage = CRM_Contact_BAO_Contact_Utils::getImage( | |
2536 | CRM_Contact_BAO_Contact::getContactType($firstTargetContactID), | |
2537 | FALSE, | |
2538 | $firstTargetContactID); | |
2539 | $activity['target_contact_name'] .= "<div>$targetTypeImage $targetLink"; | |
2540 | } | |
2541 | else { | |
2542 | $activity['target_contact_name'] .= $targetLink; | |
9254ec4e | 2543 | } |
6a488035 | 2544 | |
9254ec4e | 2545 | if ($extraCount = $values['target_contact_counter'] - 1) { |
96f94695 | 2546 | $activity['target_contact_name'] .= ";<br />" . "(" . ts('%1 more', [1 => $extraCount]) . ")"; |
ceb21ebb | 2547 | } |
2548 | if ($showContactOverlay) { | |
2549 | $activity['target_contact_name'] .= "</div> "; | |
6a488035 TO |
2550 | } |
2551 | } | |
9254ec4e | 2552 | elseif (!$values['target_contact_name']) { |
7d12de7f | 2553 | $activity['target_contact_name'] = '<em>n/a</em>'; |
9254ec4e | 2554 | } |
6a488035 | 2555 | |
ad280fb6 | 2556 | $activity['assignee_contact_name'] = ''; |
6a488035 | 2557 | if (empty($values['assignee_contact_name'])) { |
7d12de7f | 2558 | $activity['assignee_contact_name'] = '<em>n/a</em>'; |
6a488035 TO |
2559 | } |
2560 | elseif (!empty($values['assignee_contact_name'])) { | |
2561 | $count = 0; | |
7d12de7f | 2562 | $activity['assignee_contact_name'] = ''; |
6a488035 TO |
2563 | foreach ($values['assignee_contact_name'] as $acID => $acName) { |
2564 | if ($acID && $count < 5) { | |
e846bd8d | 2565 | $assigneeTypeImage = ""; |
ceb21ebb | 2566 | $assigneeLink = CRM_Utils_System::href($acName, 'civicrm/contact/view', "reset=1&cid={$acID}"); |
e846bd8d | 2567 | if ($showContactOverlay) { |
2568 | $assigneeTypeImage = CRM_Contact_BAO_Contact_Utils::getImage( | |
2569 | CRM_Contact_BAO_Contact::getContactType($acID), | |
2570 | FALSE, | |
2571 | $acID); | |
ceb21ebb | 2572 | $activity['assignee_contact_name'] .= "<div>$assigneeTypeImage $assigneeLink"; |
e846bd8d | 2573 | } |
ceb21ebb | 2574 | else { |
2575 | $activity['assignee_contact_name'] .= $assigneeLink; | |
2576 | } | |
2577 | ||
6a488035 TO |
2578 | $count++; |
2579 | if ($count) { | |
ceb21ebb | 2580 | $activity['assignee_contact_name'] .= "; "; |
2581 | } | |
2582 | if ($showContactOverlay) { | |
2583 | $activity['assignee_contact_name'] .= "</div> "; | |
6a488035 TO |
2584 | } |
2585 | ||
2586 | if ($count == 4) { | |
7d12de7f | 2587 | $activity['assignee_contact_name'] .= "(" . ts('more') . ")"; |
6a488035 TO |
2588 | break; |
2589 | } | |
2590 | } | |
2591 | } | |
2592 | } | |
6a488035 | 2593 | |
7d12de7f JL |
2594 | $activity['activity_date_time'] = CRM_Utils_Date::customFormat($values['activity_date_time']); |
2595 | $activity['status_id'] = $activityStatus[$values['status_id']]; | |
6a488035 TO |
2596 | |
2597 | // build links | |
7d12de7f | 2598 | $activity['links'] = ''; |
6a488035 | 2599 | $accessMailingReport = FALSE; |
a7488080 | 2600 | if (!empty($values['mailingId'])) { |
6a488035 TO |
2601 | $accessMailingReport = TRUE; |
2602 | } | |
2603 | ||
2604 | $actionLinks = CRM_Activity_Selector_Activity::actionLinks( | |
2605 | CRM_Utils_Array::value('activity_type_id', $values), | |
2606 | CRM_Utils_Array::value('source_record_id', $values), | |
2607 | $accessMailingReport, | |
2608 | CRM_Utils_Array::value('activity_id', $values) | |
2609 | ); | |
2610 | ||
2611 | $actionMask = array_sum(array_keys($actionLinks)) & $mask; | |
2612 | ||
7d12de7f | 2613 | $activity['links'] = CRM_Core_Action::formLink($actionLinks, |
6a488035 | 2614 | $actionMask, |
96f94695 | 2615 | [ |
6a488035 TO |
2616 | 'id' => $values['activity_id'], |
2617 | 'cid' => $params['contact_id'], | |
2618 | 'cxt' => $context, | |
2619 | 'caseid' => CRM_Utils_Array::value('case_id', $values), | |
96f94695 | 2620 | ], |
87dab4a4 AH |
2621 | ts('more'), |
2622 | FALSE, | |
2623 | 'activity.tab.row', | |
2624 | 'Activity', | |
2625 | $values['activity_id'] | |
6a488035 | 2626 | ); |
97c7504f | 2627 | |
04374d9d | 2628 | if ($values['is_recurring_activity']) { |
053eb755 | 2629 | $activity['is_recurring_activity'] = CRM_Core_BAO_RecurringEntity::getPositionAndCount($values['activity_id'], 'civicrm_activity'); |
04374d9d | 2630 | } |
7d12de7f JL |
2631 | |
2632 | array_push($contactActivities, $activity); | |
6a488035 TO |
2633 | } |
2634 | } | |
2635 | ||
96f94695 | 2636 | $activitiesDT = []; |
7d12de7f JL |
2637 | $activitiesDT['data'] = $contactActivities; |
2638 | $activitiesDT['recordsTotal'] = $params['total']; | |
2639 | $activitiesDT['recordsFiltered'] = $params['total']; | |
2640 | ||
2641 | return $activitiesDT; | |
6a488035 TO |
2642 | } |
2643 | ||
ffd93213 | 2644 | /** |
63e9c3fd EM |
2645 | * Copy custom fields and attachments from an existing activity to another. |
2646 | * | |
d3e86119 | 2647 | * @see CRM_Case_Page_AJAX::_convertToCaseActivity() |
c490a46a CW |
2648 | * |
2649 | * @param array $params | |
ffd93213 | 2650 | */ |
00be9182 | 2651 | public static function copyExtendedActivityData($params) { |
6a488035 | 2652 | // attach custom data to the new activity |
96f94695 | 2653 | $customParams = $htmlType = []; |
6a488035 TO |
2654 | $customValues = CRM_Core_BAO_CustomValueTable::getEntityValues($params['activityID'], 'Activity'); |
2655 | ||
2656 | if (!empty($customValues)) { | |
2657 | $fieldIds = implode(', ', array_keys($customValues)); | |
9d5494f7 TO |
2658 | $sql = "SELECT id FROM civicrm_custom_field WHERE html_type = 'File' AND id IN ( {$fieldIds} )"; |
2659 | $result = CRM_Core_DAO::executeQuery($sql); | |
6a488035 TO |
2660 | |
2661 | while ($result->fetch()) { | |
2662 | $htmlType[] = $result->id; | |
2663 | } | |
2664 | ||
2665 | foreach ($customValues as $key => $value) { | |
59f4c9ee TO |
2666 | if ($value !== NULL) { |
2667 | // CRM-10542 | |
6a488035 TO |
2668 | if (in_array($key, $htmlType)) { |
2669 | $fileValues = CRM_Core_BAO_File::path($value, $params['activityID']); | |
96f94695 | 2670 | $customParams["custom_{$key}_-1"] = [ |
6a488035 | 2671 | 'name' => $fileValues[0], |
ee59be7f | 2672 | 'type' => $fileValues[1], |
96f94695 | 2673 | ]; |
6a488035 TO |
2674 | } |
2675 | else { | |
2676 | $customParams["custom_{$key}_-1"] = $value; | |
2677 | } | |
2678 | } | |
2679 | } | |
5fc3ea24 | 2680 | CRM_Core_BAO_CustomValueTable::postProcess($customParams, 'civicrm_activity', |
6a488035 TO |
2681 | $params['mainActivityId'], 'Activity' |
2682 | ); | |
2683 | } | |
2684 | ||
2685 | // copy activity attachments ( if any ) | |
2686 | CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $params['activityID'], 'civicrm_activity', $params['mainActivityId']); | |
2687 | } | |
65ebc887 | 2688 | |
ffd93213 | 2689 | /** |
63e9c3fd EM |
2690 | * Get activity contact. |
2691 | * | |
100fef9d CW |
2692 | * @param int $activityId |
2693 | * @param int $recordTypeID | |
ffd93213 EM |
2694 | * @param string $column |
2695 | * | |
2696 | * @return null | |
2697 | */ | |
65ebc887 | 2698 | public static function getActivityContact($activityId, $recordTypeID = NULL, $column = 'contact_id') { |
2699 | $activityContact = new CRM_Activity_BAO_ActivityContact(); | |
2700 | $activityContact->activity_id = $activityId; | |
2701 | if ($recordTypeID) { | |
2702 | $activityContact->record_type_id = $recordTypeID; | |
2703 | } | |
2704 | if ($activityContact->find(TRUE)) { | |
b319d00a | 2705 | return $activityContact->$column; |
65ebc887 | 2706 | } |
42d30b83 DL |
2707 | return NULL; |
2708 | } | |
2709 | ||
ffd93213 | 2710 | /** |
567b2076 EM |
2711 | * Get source contact id. |
2712 | * | |
100fef9d | 2713 | * @param int $activityId |
ffd93213 EM |
2714 | * |
2715 | * @return null | |
2716 | */ | |
42d30b83 DL |
2717 | public static function getSourceContactID($activityId) { |
2718 | static $sourceID = NULL; | |
2719 | if (!$sourceID) { | |
44f817d4 | 2720 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
42d30b83 DL |
2721 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
2722 | } | |
2723 | ||
2724 | return self::getActivityContact($activityId, $sourceID); | |
65ebc887 | 2725 | } |
42d30b83 | 2726 | |
ffd93213 | 2727 | /** |
63e9c3fd EM |
2728 | * Set api filter. |
2729 | * | |
2730 | * @todo Document what this is for. | |
2731 | * | |
c490a46a | 2732 | * @param array $params |
ffd93213 | 2733 | */ |
00be9182 | 2734 | public function setApiFilter(&$params) { |
b53cbfbc | 2735 | if (!empty($params['target_contact_id'])) { |
6e1bb60c | 2736 | $this->selectAdd(); |
44f817d4 | 2737 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
6e1bb60c N |
2738 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
2739 | $obj = new CRM_Activity_BAO_ActivityContact(); | |
2740 | $params['return.target_contact_id'] = 1; | |
2741 | $this->joinAdd($obj, 'LEFT'); | |
2742 | $this->selectAdd('civicrm_activity.*'); | |
2743 | $this->whereAdd(" civicrm_activity_contact.contact_id = {$params['target_contact_id']} AND civicrm_activity_contact.record_type_id = {$targetID}"); | |
2744 | } | |
2745 | } | |
2746 | ||
2bbb4a91 | 2747 | /** |
7808aae6 | 2748 | * Send activity as attachment. |
2bbb4a91 | 2749 | * |
2750 | * @param object $activity | |
2751 | * @param array $mailToContacts | |
fc110b68 | 2752 | * @param array $params |
2bbb4a91 | 2753 | * |
bc883279 | 2754 | * @return bool |
2bbb4a91 | 2755 | */ |
96f94695 | 2756 | public static function sendToAssignee($activity, $mailToContacts, $params = []) { |
fc110b68 | 2757 | if (!CRM_Utils_Array::crmIsEmptyArray($mailToContacts)) { |
9f661777 | 2758 | $clientID = CRM_Utils_Array::value('client_id', $params); |
fc110b68 | 2759 | $caseID = CRM_Utils_Array::value('case_id', $params); |
2760 | ||
2bbb4a91 | 2761 | $ics = new CRM_Activity_BAO_ICalendar($activity); |
2762 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', $activity->id); | |
2763 | $ics->addAttachment($attachments, $mailToContacts); | |
2764 | ||
fc110b68 | 2765 | $result = CRM_Case_BAO_Case::sendActivityCopy($clientID, $activity->id, $mailToContacts, $attachments, $caseID); |
2bbb4a91 | 2766 | $ics->cleanup(); |
fc110b68 | 2767 | return $result; |
2bbb4a91 | 2768 | } |
2769 | return FALSE; | |
2770 | } | |
bc883279 | 2771 | |
1d6f94ab CW |
2772 | /** |
2773 | * @return array | |
2774 | */ | |
2775 | public static function getEntityRefFilters() { | |
2776 | return [ | |
2777 | ['key' => 'activity_type_id', 'value' => ts('Activity Type')], | |
2778 | ['key' => 'status_id', 'value' => ts('Activity Status')], | |
2779 | ]; | |
2780 | } | |
2781 | ||
6a488035 | 2782 | } |