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