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