Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class is for activity functions | |
38 | * | |
39 | */ | |
40 | class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { | |
41 | ||
42 | /** | |
43 | * static field for all the activity information that we can potentially export | |
44 | * | |
45 | * @var array | |
46 | * @static | |
47 | */ | |
48 | static $_exportableFields = NULL; | |
49 | ||
50 | /** | |
51 | * static field for all the activity information that we can potentially import | |
52 | * | |
53 | * @var array | |
54 | * @static | |
55 | */ | |
56 | static $_importableFields = NULL; | |
57 | ||
58 | /** | |
59 | * Check if there is absolute minimum of data to add the object | |
60 | * | |
61 | * @param array $params (reference ) an assoc array of name/value pairs | |
62 | * | |
63 | * @return boolean | |
64 | * @access public | |
65 | */ | |
66 | public static function dataExists(&$params) { | |
67 | if (CRM_Utils_Array::value('source_contact_id', $params) || | |
68 | CRM_Utils_Array::value('id', $params) | |
69 | ) { | |
70 | return TRUE; | |
71 | } | |
72 | return FALSE; | |
73 | } | |
74 | ||
75 | /** | |
76 | * Takes a bunch of params that are needed to match certain criteria and | |
77 | * retrieves the relevant objects. Typically the valid params are only | |
78 | * contact_id. We'll tweak this function to be more full featured over a period | |
79 | * of time. This is the inverse function of create. It also stores all the retrieved | |
80 | * values in the default array | |
81 | * | |
82 | * @param array $params (reference ) an assoc array of name/value pairs | |
83 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
84 | * @param string $activityType activity type | |
85 | * | |
86 | * @return object CRM_Core_BAO_Meeting object | |
87 | * @access public | |
88 | */ | |
89 | public static function retrieve(&$params, &$defaults) { | |
90 | $activity = new CRM_Activity_DAO_Activity(); | |
91 | $activity->copyValues($params); | |
92 | ||
93 | if ($activity->find(TRUE)) { | |
e7e657f0 | 94 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
034500d4 | 95 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
96 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); | |
97 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
98 | ||
6a488035 TO |
99 | // TODO: at some stage we'll have to deal |
100 | // TODO: with multiple values for assignees and targets, but | |
101 | // TODO: for now, let's just fetch first row | |
034500d4 | 102 | $defaults['assignee_contact'] = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $assigneeID); |
103 | $assignee_contact_names = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $assigneeID); | |
6a488035 | 104 | $defaults['assignee_contact_value'] = implode('; ', $assignee_contact_names); |
eb873b6e | 105 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); |
6a488035 | 106 | if ($activity->activity_type_id != CRM_Core_OptionGroup::getValue('activity_type', 'Bulk Email', 'name')) { |
034500d4 | 107 | $defaults['target_contact'] = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $targetID); |
108 | $target_contact_names = CRM_Activity_BAO_ActivityContact::getNames($activity->id, $targetID); | |
6a488035 TO |
109 | $defaults['target_contact_value'] = implode('; ', $target_contact_names); |
110 | } | |
111 | elseif (CRM_Core_Permission::check('access CiviMail') || | |
112 | (CRM_Mailing_Info::workflowEnabled() && | |
113 | CRM_Core_Permission::check('create mailings') | |
114 | ) | |
115 | ) { | |
116 | $defaults['mailingId'] = CRM_Utils_System::url('civicrm/mailing/report', | |
eb873b6e | 117 | "mid={$activity->source_record_id}&reset=1&atype={$activity->activity_type_id}&aid={$activity->id}&cid={$sourceContactId}&context=activity" |
6a488035 TO |
118 | ); |
119 | } | |
120 | else { | |
121 | $defaults['target_contact_value'] = ts('(recipients)'); | |
122 | } | |
b319d00a | 123 | |
65ebc887 | 124 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); |
ad674e50 | 125 | $defaults['source_contact_id'] = $sourceContactId; |
6a488035 | 126 | |
65ebc887 | 127 | if ($sourceContactId && |
6a488035 | 128 | !CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', |
65ebc887 | 129 | $sourceContactId, |
6a488035 TO |
130 | 'is_deleted' |
131 | ) | |
132 | ) { | |
133 | $defaults['source_contact'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', | |
65ebc887 | 134 | $sourceContactId, |
6a488035 TO |
135 | 'sort_name' |
136 | ); | |
137 | } | |
138 | ||
139 | //get case subject | |
140 | $defaults['case_subject'] = CRM_Case_BAO_Case::getCaseSubject($activity->id); | |
141 | ||
142 | CRM_Core_DAO::storeValues($activity, $defaults); | |
143 | ||
144 | return $activity; | |
145 | } | |
146 | return NULL; | |
147 | } | |
148 | ||
149 | /** | |
150 | * Function to delete the activity | |
151 | * | |
152 | * @param array $params associated array | |
153 | * | |
154 | * @return void | |
155 | * @access public | |
156 | * | |
157 | */ | |
158 | public static function deleteActivity(&$params, $moveToTrash = FALSE) { | |
159 | // CRM-9137 | |
160 | if (CRM_Utils_Array::value('id', $params) && !is_array($params['id'])) { | |
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 | |
e7e657f0 | 212 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
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 | |
6a488035 | 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 | } | |
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 | /** | |
257 | * Delete activity assignment record | |
258 | * | |
259 | * @param int $id activity id | |
260 | * | |
261 | * @return null | |
262 | * @access public | |
263 | */ | |
a24b3694 | 264 | public static function deleteActivityContact($activityId, $recordTypeID = NULL) { |
1d85d241 DL |
265 | $activityContact = new CRM_Activity_BAO_ActivityContact(); |
266 | $activityContact->activity_id = $activityId; | |
a24b3694 | 267 | if ($recordTypeID) { |
268 | $activityContact->record_type_id = $recordTypeID; | |
6a488035 | 269 | } |
1d85d241 | 270 | $activityContact->delete(); |
6a488035 TO |
271 | } |
272 | ||
273 | /** | |
274 | * Function to process the activities | |
275 | * | |
276 | * @param object $form form object | |
277 | * @param array $params associated array of the submitted values | |
278 | * @param array $ids array of ids | |
279 | * @param string $activityType activity Type | |
280 | * @param boolean $record true if it is Record Activity | |
281 | * @access public | |
282 | * | |
283 | * @return | |
284 | */ | |
285 | public static function create(&$params) { | |
286 | // check required params | |
287 | if (!self::dataExists($params)) { | |
288 | CRM_Core_Error::fatal('Not enough data to create activity object,'); | |
289 | } | |
290 | ||
291 | $activity = new CRM_Activity_DAO_Activity(); | |
292 | ||
293 | if (isset($params['id']) && empty($params['id'])) { | |
294 | unset($params['id']); | |
295 | } | |
296 | ||
297 | if (!CRM_Utils_Array::value('status_id', $params) && | |
298 | !CRM_Utils_Array::value('activity_status_id', $params) && | |
299 | !CRM_Utils_Array::value('id', $params) | |
300 | ) { | |
301 | if (isset($params['activity_date_time']) && | |
302 | strcmp($params['activity_date_time'], CRM_Utils_Date::processDate(date('Ymd')) == -1) | |
303 | ) { | |
304 | $params['status_id'] = 2; | |
305 | } | |
306 | else { | |
307 | $params['status_id'] = 1; | |
308 | } | |
309 | } | |
310 | ||
311 | //set priority to Normal for Auto-populated activities (for Cases) | |
312 | if (CRM_Utils_Array::value('priority_id', $params) === NULL && | |
313 | // if not set and not 0 | |
314 | !CRM_Utils_Array::value('id', $params) | |
315 | ) { | |
cbf48754 | 316 | $priority = CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'); |
6a488035 TO |
317 | $params['priority_id'] = array_search('Normal', $priority); |
318 | } | |
319 | ||
320 | if (!empty($params['target_contact_id']) && is_array($params['target_contact_id'])) { | |
321 | $params['target_contact_id'] = array_unique($params['target_contact_id']); | |
322 | } | |
323 | if (!empty($params['assignee_contact_id']) && is_array($params['assignee_contact_id'])) { | |
324 | $params['assignee_contact_id'] = array_unique($params['assignee_contact_id']); | |
325 | } | |
326 | ||
327 | // CRM-9137 | |
328 | if (CRM_Utils_Array::value('id', $params)) { | |
329 | CRM_Utils_Hook::pre('edit', 'Activity', $activity->id, $params); | |
330 | } | |
331 | else { | |
332 | CRM_Utils_Hook::pre('create', 'Activity', NULL, $params); | |
333 | } | |
334 | ||
335 | $activity->copyValues($params); | |
336 | if (isset($params['case_id'])) { | |
337 | // CRM-8708, preserve case ID even though it's not part of the SQL model | |
338 | $activity->case_id = $params['case_id']; | |
339 | } | |
340 | elseif (is_numeric($activity->id)) { | |
341 | // CRM-8708, preserve case ID even though it's not part of the SQL model | |
342 | $activity->case_id = CRM_Case_BAO_Case::getCaseIdByActivityId($activity->id); | |
343 | } | |
344 | ||
345 | // start transaction | |
346 | $transaction = new CRM_Core_Transaction(); | |
347 | ||
348 | $result = $activity->save(); | |
349 | ||
350 | if (is_a($result, 'CRM_Core_Error')) { | |
351 | $transaction->rollback(); | |
352 | return $result; | |
353 | } | |
354 | ||
355 | $activityId = $activity->id; | |
e7e657f0 | 356 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
a24b3694 | 357 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
358 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
359 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
6a488035 | 360 | |
1d85d241 DL |
361 | if (isset($params['source_contact_id'])) { |
362 | $acParams = array( | |
363 | 'activity_id' => $activityId, | |
364 | 'contact_id' => $params['source_contact_id'], | |
a24b3694 | 365 | 'record_type_id' => $sourceID |
1d85d241 | 366 | ); |
8ea7ce5d | 367 | self::deleteActivityContact($activityId, $sourceID); |
1d85d241 DL |
368 | CRM_Activity_BAO_ActivityContact::create($acParams); |
369 | } | |
370 | ||
6a488035 TO |
371 | // check and attach and files as needed |
372 | CRM_Core_BAO_File::processAttachment($params, 'civicrm_activity', $activityId); | |
373 | ||
374 | // attempt to save activity assignment | |
375 | $resultAssignment = NULL; | |
376 | if (CRM_Utils_Array::value('assignee_contact_id', $params)) { | |
377 | ||
378 | $assignmentParams = array('activity_id' => $activityId); | |
379 | ||
380 | if (is_array($params['assignee_contact_id'])) { | |
381 | if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) { | |
382 | // first delete existing assignments if any | |
a24b3694 | 383 | self::deleteActivityContact($activityId, $assigneeID); |
6a488035 TO |
384 | } |
385 | ||
386 | $values = array(); | |
387 | foreach ($params['assignee_contact_id'] as $acID) { | |
388 | if ($acID) { | |
a24b3694 | 389 | $values[] = "( $activityId, $acID, $assigneeID )"; |
6a488035 TO |
390 | } |
391 | } | |
392 | while (!empty($values)) { | |
393 | $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT); | |
394 | $str = implode(',', $input); | |
a24b3694 | 395 | $sql = "INSERT IGNORE INTO civicrm_activity_contact ( activity_id, contact_id, record_type_id ) VALUES $str;"; |
6a488035 TO |
396 | CRM_Core_DAO::executeQuery($sql); |
397 | } | |
398 | } | |
399 | else { | |
b319d00a | 400 | $assignmentParams['contact_id'] = $params['assignee_contact_id']; |
034500d4 | 401 | $assignmentParams['record_type_id'] = $assigneeID; |
6a488035 | 402 | if (CRM_Utils_Array::value('id', $params)) { |
cc1c86e9 | 403 | $assignment = new CRM_Activity_BAO_ActivityContact(); |
6a488035 | 404 | $assignment->activity_id = $activityId; |
034500d4 | 405 | $assignment->record_type_id = $assigneeID; |
6a488035 TO |
406 | $assignment->find(TRUE); |
407 | ||
b319d00a | 408 | if ($assignment->contact_id != $params['assignee_contact_id']) { |
6a488035 | 409 | $assignmentParams['id'] = $assignment->id; |
034500d4 | 410 | $resultAssignment = CRM_Activity_BAO_ActivityContact::create($assignmentParams); |
6a488035 TO |
411 | } |
412 | } | |
413 | else { | |
034500d4 | 414 | $resultAssignment = CRM_Activity_BAO_ActivityContact::create($assignmentParams); |
6a488035 TO |
415 | } |
416 | } | |
417 | } | |
418 | else { | |
419 | if (CRM_Utils_Array::value('deleteActivityAssignment', $params, TRUE)) { | |
a24b3694 | 420 | self::deleteActivityContact($activityId, $assigneeID); |
6a488035 TO |
421 | } |
422 | } | |
423 | ||
424 | if (is_a($resultAssignment, 'CRM_Core_Error')) { | |
425 | $transaction->rollback(); | |
426 | return $resultAssignment; | |
427 | } | |
428 | ||
429 | // attempt to save activity targets | |
430 | $resultTarget = NULL; | |
431 | if (CRM_Utils_Array::value('target_contact_id', $params)) { | |
432 | ||
433 | $targetParams = array('activity_id' => $activityId); | |
434 | $resultTarget = array(); | |
435 | if (is_array($params['target_contact_id'])) { | |
436 | if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) { | |
437 | // first delete existing targets if any | |
a24b3694 | 438 | self::deleteActivityContact($activityId, $targetID ); |
6a488035 TO |
439 | } |
440 | ||
441 | $values = array(); | |
442 | foreach ($params['target_contact_id'] as $tid) { | |
443 | if ($tid) { | |
a24b3694 | 444 | $values[] = "( $activityId, $tid, $targetID )"; |
6a488035 TO |
445 | } |
446 | } | |
447 | ||
448 | while (!empty($values)) { | |
449 | $input = array_splice($values, 0, CRM_Core_DAO::BULK_INSERT_COUNT); | |
450 | $str = implode(',', $input); | |
a24b3694 | 451 | $sql = "INSERT IGNORE INTO civicrm_activity_contact ( activity_id, contact_id, record_type_id ) VALUES $str;"; |
6a488035 TO |
452 | CRM_Core_DAO::executeQuery($sql); |
453 | } | |
454 | } | |
455 | else { | |
b319d00a | 456 | $targetParams['contact_id'] = $params['target_contact_id']; |
034500d4 | 457 | $targetParams['record_type_id'] = $targetID; |
6a488035 | 458 | if (CRM_Utils_Array::value('id', $params)) { |
034500d4 | 459 | $target = new CRM_Activity_BAO_ActivityContact(); |
6a488035 | 460 | $target->activity_id = $activityId; |
034500d4 | 461 | $target->record_type_id = $targetID; |
6a488035 TO |
462 | $target->find(TRUE); |
463 | ||
b319d00a | 464 | if ($target->contact_id != $params['target_contact_id']) { |
6a488035 | 465 | $targetParams['id'] = $target->id; |
034500d4 | 466 | $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams); |
6a488035 TO |
467 | } |
468 | } | |
469 | else { | |
034500d4 | 470 | $resultTarget = CRM_Activity_BAO_ActivityContact::create($targetParams); |
6a488035 TO |
471 | } |
472 | } | |
473 | } | |
474 | else { | |
475 | if (CRM_Utils_Array::value('deleteActivityTarget', $params, TRUE)) { | |
a24b3694 | 476 | self::deleteActivityContact($activityId, $targetID ); |
6a488035 TO |
477 | } |
478 | } | |
479 | ||
0a9f61c4 | 480 | // write to changelog before transaction is committed/rolled |
6a488035 TO |
481 | // back (and prepare status to display) |
482 | if (CRM_Utils_Array::value('id', $params)) { | |
483 | $logMsg = "Activity (id: {$result->id} ) updated with "; | |
484 | } | |
485 | else { | |
486 | $logMsg = "Activity created for "; | |
487 | } | |
488 | ||
489 | $msgs = array(); | |
490 | if (isset($params['source_contact_id'])) { | |
491 | $msgs[] = "source={$params['source_contact_id']}"; | |
492 | } | |
493 | ||
494 | if (CRM_Utils_Array::value('target_contact_id', $params)) { | |
495 | if (is_array($params['target_contact_id']) && !CRM_Utils_array::crmIsEmptyArray($params['target_contact_id'])) { | |
496 | $msgs[] = "target=" . implode(',', $params['target_contact_id']); | |
497 | // take only first target | |
498 | // will be used for recently viewed display | |
499 | $t = array_slice($params['target_contact_id'], 0, 1); | |
500 | $recentContactId = $t[0]; | |
501 | } | |
502 | elseif (isset($params['target_contact_id'])) { | |
503 | $msgs[] = "target={$params['target_contact_id']}"; | |
504 | // will be used for recently viewed display | |
505 | $recentContactId = $params['target_contact_id']; | |
506 | } | |
507 | } | |
508 | else { | |
509 | // at worst, take source for recently viewed display | |
510 | $recentContactId = CRM_Utils_Array::value('source_contact_id',$params); | |
511 | } | |
512 | ||
513 | if (isset($params['assignee_contact_id'])) { | |
514 | if (is_array($params['assignee_contact_id'])) { | |
515 | $msgs[] = "assignee=" . implode(',', $params['assignee_contact_id']); | |
516 | } | |
517 | else { | |
518 | $msgs[] = "assignee={$params['assignee_contact_id']}"; | |
519 | } | |
520 | } | |
521 | $logMsg .= implode(', ', $msgs); | |
522 | ||
523 | self::logActivityAction($result, $logMsg); | |
524 | ||
525 | if (CRM_Utils_Array::value('custom', $params) && | |
526 | is_array($params['custom']) | |
527 | ) { | |
528 | CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_activity', $result->id); | |
529 | } | |
530 | ||
531 | $transaction->commit(); | |
532 | if (!CRM_Utils_Array::value('skipRecentView', $params)) { | |
533 | $recentOther = array(); | |
534 | if (CRM_Utils_Array::value('case_id', $params)) { | |
535 | $caseContactID = CRM_Core_DAO::getFieldValue('CRM_Case_DAO_CaseContact', $params['case_id'], 'contact_id', 'case_id'); | |
536 | $url = CRM_Utils_System::url('civicrm/case/activity/view', | |
537 | "reset=1&aid={$activity->id}&cid={$caseContactID}&caseID={$params['case_id']}&context=home" | |
538 | ); | |
539 | } | |
540 | else { | |
eb873b6e | 541 | $q = "action=view&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$params['source_contact_id']}&context=home"; |
6a488035 TO |
542 | if ($activity->activity_type_id != CRM_Core_OptionGroup::getValue('activity_type', 'Email', 'name')) { |
543 | $url = CRM_Utils_System::url('civicrm/activity', $q); | |
544 | if ($activity->activity_type_id == CRM_Core_OptionGroup::getValue('activity_type', 'Print PDF Letter', 'name')) { | |
545 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/activity/pdf/add', | |
eb873b6e | 546 | "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$params['source_contact_id']}&context=home" |
6a488035 TO |
547 | ); |
548 | } | |
549 | else { | |
550 | $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/activity/add', | |
eb873b6e | 551 | "action=update&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$params['source_contact_id']}&context=home" |
6a488035 TO |
552 | ); |
553 | } | |
554 | ||
555 | if (CRM_Core_Permission::check("delete activities")) { | |
556 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', | |
eb873b6e | 557 | "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$params['source_contact_id']}&context=home" |
6a488035 TO |
558 | ); |
559 | } | |
560 | } | |
561 | else { | |
562 | $url = CRM_Utils_System::url('civicrm/activity/view', $q); | |
563 | if (CRM_Core_Permission::check('delete activities')) { | |
564 | $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/activity', | |
eb873b6e | 565 | "action=delete&reset=1&id={$activity->id}&atype={$activity->activity_type_id}&cid={$params['source_contact_id']}&context=home" |
6a488035 TO |
566 | ); |
567 | } | |
568 | } | |
569 | } | |
570 | ||
571 | if (!isset($activity->parent_id)) { | |
572 | $recentContactDisplay = CRM_Contact_BAO_Contact::displayName($recentContactId); | |
573 | // add the recently created Activity | |
574 | $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE); | |
575 | $activitySubject = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $activity->id, 'subject'); | |
576 | ||
577 | $title = ""; | |
578 | if (isset($activitySubject)) { | |
579 | $title = $activitySubject . ' - '; | |
580 | } | |
581 | ||
582 | $title = $title . $recentContactDisplay; | |
583 | if (CRM_Utils_Array::value($activity->activity_type_id, $activityTypes)) { | |
584 | $title .= ' (' . $activityTypes[$activity->activity_type_id] . ')'; | |
585 | } | |
586 | ||
587 | CRM_Utils_Recent::add($title, | |
588 | $url, | |
589 | $activity->id, | |
590 | 'Activity', | |
591 | $recentContactId, | |
592 | $recentContactDisplay, | |
593 | $recentOther | |
594 | ); | |
595 | } | |
596 | } | |
597 | ||
598 | // reset the group contact cache since smart groups might be affected due to this | |
599 | CRM_Contact_BAO_GroupContactCache::remove(); | |
600 | ||
601 | if (CRM_Utils_Array::value('id', $params)) { | |
602 | CRM_Utils_Hook::post('edit', 'Activity', $activity->id, $activity); | |
603 | } | |
604 | else { | |
605 | CRM_Utils_Hook::post('create', 'Activity', $activity->id, $activity); | |
606 | } | |
607 | ||
608 | // if the subject contains a ‘[case #…]’ string, file that activity on the related case (CRM-5916) | |
609 | $matches = array(); | |
610 | if (preg_match('/\[case #([0-9a-h]{7})\]/', CRM_Utils_Array::value('subject', $params), $matches)) { | |
611 | $key = CRM_Core_DAO::escapeString(CIVICRM_SITE_KEY); | |
612 | $hash = $matches[1]; | |
613 | $query = "SELECT id FROM civicrm_case WHERE SUBSTR(SHA1(CONCAT('$key', id)), 1, 7) = '$hash'"; | |
614 | $caseParams = array( | |
615 | 'activity_id' => $activity->id, | |
616 | 'case_id' => CRM_Core_DAO::singleValueQuery($query), | |
617 | ); | |
618 | if ($caseParams['case_id']) { | |
619 | CRM_Case_BAO_Case::processCaseActivity($caseParams); | |
620 | } | |
621 | else { | |
622 | self::logActivityAction($activity, "unknown case hash encountered: $hash"); | |
623 | } | |
624 | } | |
625 | ||
626 | return $result; | |
627 | } | |
628 | ||
629 | public static function logActivityAction($activity, $logMessage = NULL) { | |
630 | $session = CRM_Core_Session::singleton(); | |
631 | $id = $session->get('userID'); | |
632 | if (!$id) { | |
e7e657f0 | 633 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
8a6844b3 DL |
634 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
635 | $id = self::getActivityContact($activity->id. $sourceID); | |
6a488035 TO |
636 | } |
637 | $logParams = array( | |
638 | 'entity_table' => 'civicrm_activity', | |
639 | 'entity_id' => $activity->id, | |
640 | 'modified_id' => $id, | |
641 | 'modified_date' => date('YmdHis'), | |
642 | 'data' => $logMessage, | |
643 | ); | |
644 | CRM_Core_BAO_Log::add($logParams); | |
645 | return TRUE; | |
646 | } | |
647 | ||
648 | /** | |
0a9f61c4 | 649 | * function to get the list Activities |
6a488035 TO |
650 | * |
651 | * @param array $input array of parameters | |
652 | * Keys include | |
0a9f61c4 | 653 | * - contact_id int contact_id whose activities we want to retrieve |
6a488035 TO |
654 | * - offset int which row to start from ? |
655 | * - rowCount int how many rows to fetch | |
656 | * - sort object|array object or array describing sort order for sql query. | |
657 | * - admin boolean if contact is admin | |
658 | * - caseId int case ID | |
659 | * - context string page on which selector is build | |
660 | * - activity_type_id int|string the activitiy types we want to restrict by | |
661 | * | |
0a9f61c4 | 662 | * @return array (reference) $values the relevant data object values of open activities |
6a488035 TO |
663 | * |
664 | * @access public | |
665 | * @static | |
666 | */ | |
667 | static function &getActivities($input) { | |
668 | //step 1: Get the basic activity data | |
2517d079 DL |
669 | $bulkActivityTypeID = CRM_Core_OptionGroup::getValue( |
670 | 'activity_type', | |
6a488035 TO |
671 | 'Bulk Email', |
672 | 'name' | |
673 | ); | |
674 | ||
675 | $config = CRM_Core_Config::singleton(); | |
676 | ||
677 | $randomNum = md5(uniqid()); | |
678 | $activityTempTable = "civicrm_temp_activity_details_{$randomNum}"; | |
679 | ||
680 | $tableFields = array( | |
681 | 'activity_id' => 'int unsigned', | |
682 | 'activity_date_time' => 'datetime', | |
b8cab202 | 683 | 'source_record_id' => 'int unsigned', |
6a488035 TO |
684 | 'status_id' => 'int unsigned', |
685 | 'subject' => 'varchar(255)', | |
cec82bce | 686 | 'source_contact_name' => 'varchar(255)', |
6a488035 TO |
687 | 'activity_type_id' => 'int unsigned', |
688 | 'activity_type' => 'varchar(128)', | |
689 | 'case_id' => 'int unsigned', | |
690 | 'case_subject' => 'varchar(255)', | |
691 | 'campaign_id' => 'int unsigned', | |
692 | ); | |
693 | ||
694 | $sql = "CREATE TEMPORARY TABLE {$activityTempTable} ( "; | |
695 | $insertValueSQL = array(); | |
6da5bfce | 696 | // The activityTempTable contains the sorted rows |
697 | // so in order to maintain the sort order as-is we add an auto_increment | |
698 | // field; we can sort by this later to ensure the sort order stays correct. | |
699 | $sql .= " fixed_sort_order INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,"; | |
6a488035 TO |
700 | foreach ($tableFields as $name => $desc) { |
701 | $sql .= "$name $desc,\n"; | |
702 | $insertValueSQL[] = $name; | |
703 | } | |
704 | ||
6da5bfce | 705 | // add unique key on activity_id just to be sure |
706 | // this cannot be primary key because we need that for the auto_increment | |
707 | // fixed_sort_order field | |
6a488035 | 708 | $sql .= " |
c1d26519 | 709 | UNIQUE KEY ( activity_id ) |
6a488035 TO |
710 | ) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci |
711 | "; | |
712 | ||
713 | CRM_Core_DAO::executeQuery($sql); | |
714 | ||
715 | $insertSQL = "INSERT INTO {$activityTempTable} (" . implode(',', $insertValueSQL) . " ) "; | |
716 | ||
717 | $order = $limit = $groupBy = ''; | |
b8cab202 | 718 | $groupBy = " GROUP BY tbl.activity_id "; |
6a488035 TO |
719 | |
720 | if (!empty($input['sort'])) { | |
721 | if (is_a($input['sort'], 'CRM_Utils_Sort')) { | |
722 | $orderBy = $input['sort']->orderBy(); | |
723 | if (!empty($orderBy)) { | |
724 | $order = " ORDER BY $orderBy"; | |
725 | } | |
726 | } | |
727 | elseif (trim($input['sort'])) { | |
21d32567 DL |
728 | $sort = CRM_Utils_Type::escape($input['sort'], 'String'); |
729 | $order = " ORDER BY $sort "; | |
6a488035 TO |
730 | } |
731 | } | |
732 | ||
733 | if (empty($order)) { | |
6da5bfce | 734 | // context = 'activity' in Activities tab. |
6a488035 TO |
735 | $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 "; |
736 | } | |
737 | ||
738 | if (!empty($input['rowCount']) && | |
739 | $input['rowCount'] > 0 | |
740 | ) { | |
741 | $limit = " LIMIT {$input['offset']}, {$input['rowCount']} "; | |
742 | } | |
743 | ||
744 | $input['count'] = FALSE; | |
745 | list($sqlClause, $params) = self::getActivitySQLClause($input); | |
746 | ||
747 | $query = "{$insertSQL} | |
748 | SELECT DISTINCT tbl.* from ( {$sqlClause} ) | |
749 | as tbl "; | |
750 | ||
751 | //filter case activities - CRM-5761 | |
752 | $components = self::activityComponents(); | |
753 | if (!in_array('CiviCase', $components)) { | |
754 | $query .= " | |
755 | LEFT JOIN civicrm_case_activity ON ( civicrm_case_activity.activity_id = tbl.activity_id ) | |
756 | WHERE civicrm_case_activity.id IS NULL"; | |
757 | } | |
758 | ||
759 | $query = $query . $groupBy . $order . $limit; | |
760 | ||
761 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
762 | ||
6a488035 TO |
763 | // step 2: Get target and assignee contacts for above activities |
764 | // create temp table for target contacts | |
91da6cd5 DL |
765 | $activityContactTempTable = "civicrm_temp_activity_contact_{$randomNum}"; |
766 | $query = "CREATE TEMPORARY TABLE {$activityContactTempTable} ( | |
d8a22375 | 767 | activity_id int unsigned, contact_id int unsigned, record_type_id varchar(16), contact_name varchar(255), is_deleted int unsigned ) |
6a488035 TO |
768 | ENGINE=MYISAM DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci"; |
769 | ||
770 | CRM_Core_DAO::executeQuery($query); | |
771 | ||
772 | // note that we ignore bulk email for targets, since we don't show it in selector | |
91da6cd5 | 773 | $query = " |
d8a22375 | 774 | INSERT INTO {$activityContactTempTable} ( activity_id, contact_id, record_type_id, contact_name, is_deleted ) |
91da6cd5 DL |
775 | SELECT ac.activity_id, |
776 | ac.contact_id, | |
5b5da1ef | 777 | ac.record_type_id, |
d8a22375 BS |
778 | c.sort_name, |
779 | c.is_deleted | |
91da6cd5 | 780 | FROM civicrm_activity_contact ac |
ef41c3d4 | 781 | INNER JOIN {$activityTempTable} ON ( ac.activity_id = {$activityTempTable}.activity_id ) |
5b5da1ef | 782 | INNER JOIN civicrm_contact c ON c.id = ac.contact_id |
91da6cd5 | 783 | "; |
6a488035 TO |
784 | CRM_Core_DAO::executeQuery($query); |
785 | ||
6a488035 | 786 | // step 3: Combine all temp tables to get final query for activity selector |
6da5bfce | 787 | // sort by the original sort order, stored in fixed_sort_order |
6a488035 | 788 | $query = " |
91da6cd5 DL |
789 | SELECT {$activityTempTable}.*, |
790 | {$activityContactTempTable}.contact_id, | |
a24b3694 | 791 | {$activityContactTempTable}.record_type_id, |
d8a22375 BS |
792 | {$activityContactTempTable}.contact_name, |
793 | {$activityContactTempTable}.is_deleted | |
91da6cd5 DL |
794 | FROM {$activityTempTable} |
795 | INNER JOIN {$activityContactTempTable} on {$activityTempTable}.activity_id = {$activityContactTempTable}.activity_id | |
6da5bfce | 796 | ORDER BY fixed_sort_order |
6a488035 TO |
797 | "; |
798 | ||
799 | ||
800 | $dao = CRM_Core_DAO::executeQuery($query); | |
801 | ||
802 | //CRM-3553, need to check user has access to target groups. | |
803 | $mailingIDs = CRM_Mailing_BAO_Mailing::mailingACLIDs(); | |
804 | $accessCiviMail = ( | |
805 | (CRM_Core_Permission::check('access CiviMail')) || | |
806 | (CRM_Mailing_Info::workflowEnabled() && | |
807 | CRM_Core_Permission::check('create mailings')) | |
808 | ); | |
809 | ||
810 | //get all campaigns. | |
811 | $allCampaigns = CRM_Campaign_BAO_Campaign::getCampaigns(NULL, NULL, FALSE, FALSE, FALSE, TRUE); | |
812 | ||
813 | $values = array(); | |
e7e657f0 | 814 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
a24b3694 | 815 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
816 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
817 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
b319d00a | 818 | |
a24b3694 | 819 | |
6a488035 TO |
820 | while ($dao->fetch()) { |
821 | $activityID = $dao->activity_id; | |
822 | $values[$activityID]['activity_id'] = $dao->activity_id; | |
823 | $values[$activityID]['source_record_id'] = $dao->source_record_id; | |
824 | $values[$activityID]['activity_type_id'] = $dao->activity_type_id; | |
825 | $values[$activityID]['activity_type'] = $dao->activity_type; | |
826 | $values[$activityID]['activity_date_time'] = $dao->activity_date_time; | |
827 | $values[$activityID]['status_id'] = $dao->status_id; | |
828 | $values[$activityID]['subject'] = $dao->subject; | |
6a488035 TO |
829 | $values[$activityID]['campaign_id'] = $dao->campaign_id; |
830 | ||
831 | if ($dao->campaign_id) { | |
832 | $values[$activityID]['campaign'] = $allCampaigns[$dao->campaign_id]; | |
833 | } | |
834 | ||
835 | if (!CRM_Utils_Array::value('assignee_contact_name', $values[$activityID])) { | |
836 | $values[$activityID]['assignee_contact_name'] = array(); | |
837 | } | |
838 | ||
839 | if (!CRM_Utils_Array::value('target_contact_name', $values[$activityID])) { | |
840 | $values[$activityID]['target_contact_name'] = array(); | |
841 | } | |
842 | ||
d8a22375 BS |
843 | // if deleted, wrap in <del> |
844 | if ( $dao->is_deleted ) { | |
845 | $dao->contact_name = "<del>{$dao->contact_name}</dao>"; | |
846 | } | |
847 | ||
a24b3694 | 848 | if ($dao->record_type_id == $sourceID && $dao->contact_id) { |
91da6cd5 DL |
849 | $values[$activityID]['source_contact_id'] = $dao->contact_id; |
850 | $values[$activityID]['source_contact_name'] = $dao->contact_name; | |
851 | } | |
852 | ||
6a488035 TO |
853 | if (!$bulkActivityTypeID || ($bulkActivityTypeID != $dao->activity_type_id)) { |
854 | // build array of target / assignee names | |
a24b3694 | 855 | if ($dao->record_type_id == $targetID && $dao->contact_id) { |
91da6cd5 | 856 | $values[$activityID]['target_contact_name'][$dao->contact_id] = $dao->contact_name; |
6a488035 | 857 | } |
a24b3694 | 858 | if ($dao->record_type_id == $assigneeID && $dao->contact_id) { |
91da6cd5 | 859 | $values[$activityID]['assignee_contact_name'][$dao->contact_id] = $dao->contact_name; |
6a488035 TO |
860 | } |
861 | ||
862 | // case related fields | |
863 | $values[$activityID]['case_id'] = $dao->case_id; | |
864 | $values[$activityID]['case_subject'] = $dao->case_subject; | |
865 | } | |
866 | else { | |
867 | $values[$activityID]['recipients'] = ts('(recipients)'); | |
2678caac | 868 | $values[$activityID]['mailingId'] = false; |
6a488035 TO |
869 | if ( |
870 | $accessCiviMail && | |
871 | ($mailingIDs === TRUE || in_array($dao->source_record_id, $mailingIDs)) | |
872 | ) { | |
2678caac | 873 | $values[$activityID]['mailingId'] = true; |
6a488035 TO |
874 | } |
875 | } | |
876 | } | |
877 | ||
6a488035 TO |
878 | return $values; |
879 | } | |
880 | ||
881 | /** | |
882 | * Get the component id and name those are enabled and logged in | |
883 | * user has permission. To decide whether we are going to include | |
884 | * component related activities w/ core activity retrieve process. | |
885 | * | |
886 | * return an array of component id and name. | |
887 | * @static | |
888 | **/ | |
889 | static function activityComponents() { | |
890 | $components = array(); | |
891 | $compInfo = CRM_Core_Component::getEnabledComponents(); | |
892 | foreach ($compInfo as $compObj) { | |
893 | if (CRM_Utils_Array::value('showActivitiesInCore', $compObj->info)) { | |
894 | if ($compObj->info['name'] == 'CiviCampaign') { | |
895 | $componentPermission = "administer {$compObj->name}"; | |
896 | } | |
897 | else { | |
898 | $componentPermission = "access {$compObj->name}"; | |
899 | } | |
900 | if ($compObj->info['name'] == 'CiviCase') { | |
901 | if (CRM_Case_BAO_Case::accessCiviCase()) { | |
902 | $components[$compObj->componentID] = $compObj->info['name']; | |
903 | } | |
904 | } | |
905 | elseif (CRM_Core_Permission::check($componentPermission)) { | |
906 | $components[$compObj->componentID] = $compObj->info['name']; | |
907 | } | |
908 | } | |
909 | } | |
910 | ||
911 | return $components; | |
912 | } | |
913 | ||
914 | /** | |
915 | * function to get the activity Count | |
916 | * | |
917 | * @param array $input array of parameters | |
918 | * Keys include | |
0a9f61c4 | 919 | * - contact_id int contact_id whose activities we want to retrieve |
6a488035 TO |
920 | * - admin boolean if contact is admin |
921 | * - caseId int case ID | |
922 | * - context string page on which selector is build | |
0a9f61c4 | 923 | * - activity_type_id int|string the activity types we want to restrict by |
6a488035 TO |
924 | * |
925 | * @return int count of activities | |
926 | * | |
927 | * @access public | |
928 | * @static | |
929 | */ | |
930 | static function &getActivitiesCount($input) { | |
931 | $input['count'] = TRUE; | |
932 | list($sqlClause, $params) = self::getActivitySQLClause($input); | |
933 | ||
934 | //filter case activities - CRM-5761 | |
935 | $components = self::activityComponents(); | |
936 | if (!in_array('CiviCase', $components)) { | |
937 | $query = " | |
938 | SELECT COUNT(DISTINCT(tbl.activity_id)) as count | |
939 | FROM ( {$sqlClause} ) as tbl | |
940 | LEFT JOIN civicrm_case_activity ON ( civicrm_case_activity.activity_id = tbl.activity_id ) | |
941 | WHERE civicrm_case_activity.id IS NULL"; | |
942 | } | |
943 | else { | |
944 | $query = "SELECT COUNT(DISTINCT(activity_id)) as count from ( {$sqlClause} ) as tbl"; | |
945 | } | |
946 | ||
947 | return CRM_Core_DAO::singleValueQuery($query, $params); | |
948 | } | |
949 | ||
950 | /** | |
951 | * function to get the activity sql clause to pick activities | |
952 | * | |
953 | * @param array $input array of parameters | |
954 | * Keys include | |
0a9f61c4 | 955 | * - contact_id int contact_id whose activities we want to retrieve |
6a488035 TO |
956 | * - admin boolean if contact is admin |
957 | * - caseId int case ID | |
958 | * - context string page on which selector is build | |
959 | * - count boolean are we interested in the count clause only? | |
0a9f61c4 | 960 | * - activity_type_id int|string the activity types we want to restrict by |
6a488035 TO |
961 | * |
962 | * @return int count of activities | |
963 | * | |
964 | * @access public | |
965 | * @static | |
966 | */ | |
967 | static function getActivitySQLClause($input) { | |
968 | $params = array(); | |
969 | $sourceWhere = $targetWhere = $assigneeWhere = $caseWhere = 1; | |
970 | ||
971 | $config = CRM_Core_Config::singleton(); | |
972 | if (!CRM_Utils_Array::value('admin', $input, FALSE)) { | |
91da6cd5 | 973 | $sourceWhere = ' ac.contact_id = %1 '; |
6a488035 TO |
974 | $caseWhere = ' civicrm_case_contact.contact_id = %1 '; |
975 | ||
976 | $params = array(1 => array($input['contact_id'], 'Integer')); | |
977 | } | |
978 | ||
979 | $commonClauses = array( | |
980 | "civicrm_option_group.name = 'activity_type'", | |
981 | "civicrm_activity.is_deleted = 0", | |
982 | "civicrm_activity.is_current_revision = 1", | |
2517d079 | 983 | "civicrm_activity.is_test= 0", |
6a488035 TO |
984 | ); |
985 | ||
986 | if ($input['context'] != 'activity') { | |
987 | $commonClauses[] = "civicrm_activity.status_id = 1"; | |
988 | } | |
989 | ||
990 | //Filter on component IDs. | |
991 | $components = self::activityComponents(); | |
992 | if (!empty($components)) { | |
993 | $componentsIn = implode(',', array_keys($components)); | |
994 | $commonClauses[] = "( civicrm_option_value.component_id IS NULL OR civicrm_option_value.component_id IN ( $componentsIn ) )"; | |
995 | } | |
996 | else { | |
997 | $commonClauses[] = "civicrm_option_value.component_id IS NULL"; | |
998 | } | |
999 | ||
1000 | // activity type ID clause | |
1001 | if (!empty($input['activity_type_id'])) { | |
1002 | if (is_array($input['activity_type_id'])) { | |
1003 | foreach ($input['activity_type_id'] as $idx => $value) { | |
1004 | $input['activity_type_id'][$idx] = CRM_Utils_Type::escape($value, 'Positive'); | |
1005 | } | |
1006 | $commonClauses[] = "civicrm_activity.activity_type_id IN ( " . implode(",", $input['activity_type_id']) . " ) "; | |
1007 | } | |
1008 | else { | |
1009 | $activityTypeID = CRM_Utils_Type::escape($input['activity_type_id'], 'Positive'); | |
1010 | $commonClauses[] = "civicrm_activity.activity_type_id = $activityTypeID"; | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | // exclude by activity type clause | |
1015 | if (!empty($input['activity_type_exclude_id'])) { | |
1016 | if (is_array($input['activity_type_exclude_id'])) { | |
1017 | foreach ($input['activity_type_exclude_id'] as $idx => $value) { | |
1018 | $input['activity_type_exclude_id'][$idx] = CRM_Utils_Type::escape($value, 'Positive'); | |
1019 | } | |
1020 | $commonClauses[] = "civicrm_activity.activity_type_id NOT IN ( " . implode(",", $input['activity_type_exclude_id']) . " ) "; | |
1021 | } | |
1022 | else { | |
1023 | $activityTypeID = CRM_Utils_Type::escape($input['activity_type_exclude_id'], 'Positive'); | |
1024 | $commonClauses[] = "civicrm_activity.activity_type_id != $activityTypeID"; | |
1025 | } | |
1026 | } | |
1027 | ||
1028 | $commonClause = implode(' AND ', $commonClauses); | |
1029 | ||
1030 | $includeCaseActivities = FALSE; | |
1031 | if (in_array('CiviCase', $components)) { | |
1032 | $includeCaseActivities = TRUE; | |
1033 | } | |
1034 | ||
1035 | ||
1036 | // build main activity table select clause | |
1037 | $sourceSelect = ''; | |
cec82bce | 1038 | |
1039 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); | |
1040 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); | |
a3696ce8 | 1041 | $sourceJoin = " |
b8cab202 | 1042 | INNER JOIN civicrm_activity_contact ac ON ac.activity_id = civicrm_activity.id |
a3696ce8 DL |
1043 | INNER JOIN civicrm_contact contact ON ac.contact_id = contact.id |
1044 | "; | |
6a488035 TO |
1045 | |
1046 | if (!$input['count']) { | |
1047 | $sourceSelect = ', | |
1048 | civicrm_activity.activity_date_time, | |
b8cab202 | 1049 | civicrm_activity.source_record_id, |
6a488035 TO |
1050 | civicrm_activity.status_id, |
1051 | civicrm_activity.subject, | |
cec82bce | 1052 | contact.sort_name as source_contact_name, |
6a488035 TO |
1053 | civicrm_option_value.value as activity_type_id, |
1054 | civicrm_option_value.label as activity_type, | |
1055 | null as case_id, null as case_subject, | |
5b5da1ef | 1056 | civicrm_activity.campaign_id as campaign_id |
6a488035 TO |
1057 | '; |
1058 | ||
b8cab202 PJ |
1059 | $sourceJoin .= " |
1060 | 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) | |
1061 | "; | |
6a488035 TO |
1062 | } |
1063 | ||
1064 | $sourceClause = " | |
1065 | SELECT civicrm_activity.id as activity_id | |
1066 | {$sourceSelect} | |
1067 | from civicrm_activity | |
1068 | left join civicrm_option_value on | |
1069 | civicrm_activity.activity_type_id = civicrm_option_value.value | |
1070 | left join civicrm_option_group on | |
1071 | civicrm_option_group.id = civicrm_option_value.option_group_id | |
1072 | {$sourceJoin} | |
1073 | where | |
1074 | {$sourceWhere} | |
1075 | AND $commonClause | |
1076 | "; | |
1077 | ||
6a488035 TO |
1078 | // Build case clause |
1079 | // or else exclude Inbound Emails that have been filed on a case. | |
1080 | $caseClause = ''; | |
1081 | ||
1082 | if ($includeCaseActivities) { | |
1083 | $caseSelect = ''; | |
1084 | if (!$input['count']) { | |
1085 | $caseSelect = ', | |
1086 | civicrm_activity.activity_date_time, | |
b8cab202 | 1087 | civicrm_activity.source_record_id, |
6a488035 TO |
1088 | civicrm_activity.status_id, |
1089 | civicrm_activity.subject, | |
cec82bce | 1090 | contact.sort_name as source_contact_name, |
6a488035 TO |
1091 | civicrm_option_value.value as activity_type_id, |
1092 | civicrm_option_value.label as activity_type, | |
1093 | null as case_id, null as case_subject, | |
1094 | civicrm_activity.campaign_id as campaign_id'; | |
1095 | } | |
1096 | ||
1097 | $caseClause = " | |
1098 | union all | |
1099 | ||
1100 | SELECT civicrm_activity.id as activity_id | |
1101 | {$caseSelect} | |
1102 | from civicrm_activity | |
1103 | inner join civicrm_case_activity on | |
1104 | civicrm_case_activity.activity_id = civicrm_activity.id | |
1105 | inner join civicrm_case on | |
1106 | civicrm_case_activity.case_id = civicrm_case.id | |
1107 | inner join civicrm_case_contact on | |
1108 | civicrm_case_contact.case_id = civicrm_case.id and {$caseWhere} | |
1109 | left join civicrm_option_value on | |
1110 | civicrm_activity.activity_type_id = civicrm_option_value.value | |
1111 | left join civicrm_option_group on | |
1112 | civicrm_option_group.id = civicrm_option_value.option_group_id | |
1113 | {$sourceJoin} | |
1114 | where | |
1115 | {$caseWhere} | |
1116 | AND $commonClause | |
1117 | and ( ( civicrm_case_activity.case_id IS NULL ) OR | |
1118 | ( civicrm_option_value.name <> 'Inbound Email' AND | |
1119 | civicrm_option_value.name <> 'Email' AND civicrm_case_activity.case_id | |
1120 | IS NOT NULL ) | |
1121 | ) | |
1122 | "; | |
1123 | } | |
1124 | ||
a3696ce8 | 1125 | $returnClause = " {$sourceClause} {$caseClause} "; |
6a488035 TO |
1126 | |
1127 | return array($returnClause, $params); | |
1128 | } | |
1129 | ||
1130 | /** | |
1131 | * send the message to all the contacts and also insert a | |
1132 | * contact activity in each contacts record | |
1133 | * | |
1134 | * @param array $contactDetails the array of contact details to send the email | |
1135 | * @param string $subject the subject of the message | |
1136 | * @param string $message the message contents | |
1137 | * @param string $emailAddress use this 'to' email address instead of the default Primary address | |
1138 | * @param int $userID use this userID if set | |
1139 | * @param string $from | |
1140 | * @param array $attachments the array of attachments if any | |
0a9f61c4 | 1141 | * @param string $cc cc recipient |
1142 | * @param string $bcc bcc recipient | |
6a488035 | 1143 | * @param array $contactIds contact ids |
c1d26519 | 1144 | * @param string $additionalDetails the additional information of CC and BCC appended to the activity Details |
6a488035 TO |
1145 | * |
1146 | * @return array ( sent, activityId) if any email is sent and activityId | |
1147 | * @access public | |
1148 | * @static | |
1149 | */ | |
1150 | static function sendEmail( | |
1151 | &$contactDetails, | |
1152 | &$subject, | |
1153 | &$text, | |
1154 | &$html, | |
1155 | $emailAddress, | |
1156 | $userID = NULL, | |
1157 | $from = NULL, | |
1158 | $attachments = NULL, | |
1159 | $cc = NULL, | |
1160 | $bcc = NULL, | |
c1d26519 | 1161 | $contactIds, // FIXME a param with no default shouldn't be last |
1162 | $additionalDetails = NULL | |
6a488035 TO |
1163 | ) { |
1164 | // get the contact details of logged in contact, which we set as from email | |
1165 | if ($userID == NULL) { | |
1166 | $session = CRM_Core_Session::singleton(); | |
1167 | $userID = $session->get('userID'); | |
1168 | } | |
1169 | ||
1170 | list($fromDisplayName, $fromEmail, $fromDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($userID); | |
1171 | if (!$fromEmail) { | |
1172 | return array(count($contactDetails), 0, count($contactDetails)); | |
1173 | } | |
1174 | if (!trim($fromDisplayName)) { | |
1175 | $fromDisplayName = $fromEmail; | |
1176 | } | |
1177 | ||
1178 | // CRM-4575 | |
1179 | // token replacement of addressee/email/postal greetings | |
1180 | // get the tokens added in subject and message | |
1181 | $subjectToken = CRM_Utils_Token::getTokens($subject); | |
1182 | $messageToken = CRM_Utils_Token::getTokens($text); | |
1183 | $messageToken = array_merge($messageToken, CRM_Utils_Token::getTokens($html)); | |
1184 | ||
1185 | if (!$from) { | |
1186 | $from = "$fromDisplayName <$fromEmail>"; | |
1187 | } | |
1188 | ||
1189 | //create the meta level record first ( email activity ) | |
1190 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', | |
1191 | 'Email', | |
1192 | 'name' | |
1193 | ); | |
1194 | ||
1195 | // CRM-6265: save both text and HTML parts in details (if present) | |
1196 | if ($html and $text) { | |
c1d26519 | 1197 | $details = "-ALTERNATIVE ITEM 0-\n$html$additionalDetails\n-ALTERNATIVE ITEM 1-\n$text$additionalDetails\n-ALTERNATIVE END-\n"; |
6a488035 TO |
1198 | } |
1199 | else { | |
1200 | $details = $html ? $html : $text; | |
c1d26519 | 1201 | $details .= $additionalDetails; |
6a488035 TO |
1202 | } |
1203 | ||
1204 | $activityParams = array( | |
1205 | 'source_contact_id' => $userID, | |
1206 | 'activity_type_id' => $activityTypeID, | |
1207 | 'activity_date_time' => date('YmdHis'), | |
1208 | 'subject' => $subject, | |
1209 | 'details' => $details, | |
1210 | // FIXME: check for name Completed and get ID from that lookup | |
1211 | 'status_id' => 2, | |
1212 | ); | |
1213 | ||
1214 | // CRM-5916: strip [case #…] before saving the activity (if present in subject) | |
1215 | $activityParams['subject'] = preg_replace('/\[case #([0-9a-h]{7})\] /', '', $activityParams['subject']); | |
1216 | ||
1217 | // add the attachments to activity params here | |
1218 | if ($attachments) { | |
1219 | // first process them | |
1220 | $activityParams = array_merge($activityParams, | |
1221 | $attachments | |
1222 | ); | |
1223 | } | |
1224 | ||
1225 | $activity = self::create($activityParams); | |
1226 | ||
1227 | // get the set of attachments from where they are stored | |
1228 | $attachments = CRM_Core_BAO_File::getEntityFile('civicrm_activity', | |
1229 | $activity->id | |
1230 | ); | |
1231 | $returnProperties = array(); | |
1232 | if (isset($messageToken['contact'])) { | |
1233 | foreach ($messageToken['contact'] as $key => $value) { | |
1234 | $returnProperties[$value] = 1; | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | if (isset($subjectToken['contact'])) { | |
1239 | foreach ($subjectToken['contact'] as $key => $value) { | |
1240 | if (!isset($returnProperties[$value])) { | |
1241 | $returnProperties[$value] = 1; | |
1242 | } | |
1243 | } | |
1244 | } | |
1245 | ||
1246 | ||
1247 | // get token details for contacts, call only if tokens are used | |
1248 | $details = array(); | |
1249 | if (!empty($returnProperties)) { | |
1250 | list($details) = CRM_Utils_Token::getTokenDetails( | |
1251 | $contactIds, | |
1252 | $returnProperties, | |
1253 | NULL, NULL, FALSE, | |
1254 | $messageToken, | |
1255 | 'CRM_Activity_BAO_Activity' | |
1256 | ); | |
1257 | } | |
1258 | ||
1259 | // call token hook | |
1260 | $tokens = array(); | |
1261 | CRM_Utils_Hook::tokens($tokens); | |
1262 | $categories = array_keys($tokens); | |
1263 | ||
1264 | $escapeSmarty = FALSE; | |
1265 | if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { | |
1266 | $smarty = CRM_Core_Smarty::singleton(); | |
1267 | $escapeSmarty = TRUE; | |
1268 | } | |
1269 | ||
1270 | $sent = $notSent = array(); | |
1271 | foreach ($contactDetails as $values) { | |
1272 | $contactId = $values['contact_id']; | |
1273 | $emailAddress = $values['email']; | |
1274 | ||
1275 | if (!empty($details) && is_array($details["{$contactId}"])) { | |
1276 | // unset email from details since it always returns primary email address | |
1277 | unset($details["{$contactId}"]['email']); | |
1278 | unset($details["{$contactId}"]['email_id']); | |
1279 | $values = array_merge($values, $details["{$contactId}"]); | |
1280 | } | |
1281 | ||
1282 | $tokenSubject = CRM_Utils_Token::replaceContactTokens($subject, $values, FALSE, $subjectToken, FALSE, $escapeSmarty); | |
1283 | $tokenSubject = CRM_Utils_Token::replaceHookTokens($tokenSubject, $values, $categories, FALSE, $escapeSmarty); | |
1284 | ||
1285 | //CRM-4539 | |
1286 | if ($values['preferred_mail_format'] == 'Text' || $values['preferred_mail_format'] == 'Both') { | |
1287 | $tokenText = CRM_Utils_Token::replaceContactTokens($text, $values, FALSE, $messageToken, FALSE, $escapeSmarty); | |
1288 | $tokenText = CRM_Utils_Token::replaceHookTokens($tokenText, $values, $categories, FALSE, $escapeSmarty); | |
1289 | } | |
1290 | else { | |
1291 | $tokenText = NULL; | |
1292 | } | |
1293 | ||
1294 | if ($values['preferred_mail_format'] == 'HTML' || $values['preferred_mail_format'] == 'Both') { | |
1295 | $tokenHtml = CRM_Utils_Token::replaceContactTokens($html, $values, TRUE, $messageToken, FALSE, $escapeSmarty); | |
1296 | $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $values, $categories, TRUE, $escapeSmarty); | |
1297 | } | |
1298 | else { | |
1299 | $tokenHtml = NULL; | |
1300 | } | |
1301 | ||
1302 | if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { | |
1303 | // also add the contact tokens to the template | |
1304 | $smarty->assign_by_ref('contact', $values); | |
1305 | ||
1306 | $tokenSubject = $smarty->fetch("string:$tokenSubject"); | |
1307 | $tokenText = $smarty->fetch("string:$tokenText"); | |
1308 | $tokenHtml = $smarty->fetch("string:$tokenHtml"); | |
1309 | } | |
1310 | ||
1311 | $sent = FALSE; | |
1312 | if (self::sendMessage( | |
1313 | $from, | |
1314 | $userID, | |
1315 | $contactId, | |
1316 | $tokenSubject, | |
1317 | $tokenText, | |
1318 | $tokenHtml, | |
1319 | $emailAddress, | |
1320 | $activity->id, | |
1321 | $attachments, | |
1322 | $cc, | |
1323 | $bcc | |
1324 | )) { | |
1325 | $sent = TRUE; | |
1326 | } | |
1327 | } | |
1328 | ||
1329 | return array($sent, $activity->id); | |
1330 | } | |
1331 | ||
1332 | static function sendSMS(&$contactDetails, | |
1333 | &$activityParams, | |
1334 | &$smsParams = array(), | |
1335 | &$contactIds, | |
1336 | $userID = NULL | |
1337 | ) { | |
1338 | if ($userID == NULL) { | |
1339 | $session = CRM_Core_Session::singleton(); | |
1340 | $userID = $session->get('userID'); | |
1341 | } | |
1342 | ||
1343 | $text = &$activityParams['text_message']; | |
1344 | $html = &$activityParams['html_message']; | |
1345 | ||
1346 | // CRM-4575 | |
1347 | // token replacement of addressee/email/postal greetings | |
1348 | // get the tokens added in subject and message | |
1349 | $messageToken = CRM_Utils_Token::getTokens($text); | |
1350 | $messageToken = array_merge($messageToken, | |
1351 | CRM_Utils_Token::getTokens($html) | |
1352 | ); | |
1353 | ||
1354 | //create the meta level record first ( sms activity ) | |
1355 | $activityTypeID = CRM_Core_OptionGroup::getValue('activity_type', | |
1356 | 'SMS', | |
1357 | 'name' | |
1358 | ); | |
1359 | ||
1360 | // CRM-6265: save both text and HTML parts in details (if present) | |
1361 | if ($html and $text) { | |
1362 | $details = "-ALTERNATIVE ITEM 0-\n$html\n-ALTERNATIVE ITEM 1-\n$text\n-ALTERNATIVE END-\n"; | |
1363 | } | |
1364 | else { | |
1365 | $details = $html ? $html : $text; | |
1366 | } | |
1367 | ||
1368 | $activitySubject = $activityParams['activity_subject']; | |
1369 | $activityParams = array( | |
1370 | 'source_contact_id' => $userID, | |
1371 | 'activity_type_id' => $activityTypeID, | |
1372 | 'activity_date_time' => date('YmdHis'), | |
1373 | 'subject' => $activitySubject, | |
1374 | 'details' => $details, | |
1375 | // FIXME: check for name Completed and get ID from that lookup | |
1376 | 'status_id' => 2, | |
1377 | ); | |
1378 | ||
1379 | $activity = self::create($activityParams); | |
1380 | $activityID = $activity->id; | |
1381 | ||
1382 | $returnProperties = array(); | |
1383 | ||
1384 | if (isset($messageToken['contact'])) { | |
1385 | foreach ($messageToken['contact'] as $key => $value) { | |
1386 | $returnProperties[$value] = 1; | |
1387 | } | |
1388 | } | |
1389 | ||
1390 | // call token hook | |
1391 | $tokens = array(); | |
1392 | CRM_Utils_Hook::tokens($tokens); | |
1393 | $categories = array_keys($tokens); | |
1394 | ||
1395 | // get token details for contacts, call only if tokens are used | |
1396 | $details = array(); | |
1397 | if (!empty($returnProperties) || !empty($tokens)) { | |
1398 | list($details) = CRM_Utils_Token::getTokenDetails($contactIds, | |
1399 | $returnProperties, | |
1400 | NULL, NULL, FALSE, | |
1401 | $messageToken, | |
1402 | 'CRM_Activity_BAO_Activity' | |
1403 | ); | |
1404 | } | |
1405 | ||
f53ea1ce | 1406 | $success = 0; |
c5a6413b DS |
1407 | $escapeSmarty = FALSE; |
1408 | $errMsgs = array(); | |
6a488035 TO |
1409 | foreach ($contactDetails as $values) { |
1410 | $contactId = $values['contact_id']; | |
1411 | ||
1412 | if (!empty($details) && is_array($details["{$contactId}"])) { | |
1413 | // unset email from details since it always returns primary email address | |
1414 | unset($details["{$contactId}"]['email']); | |
1415 | unset($details["{$contactId}"]['email_id']); | |
1416 | $values = array_merge($values, $details["{$contactId}"]); | |
1417 | } | |
1418 | ||
1419 | $tokenText = CRM_Utils_Token::replaceContactTokens($text, $values, FALSE, $messageToken, FALSE, $escapeSmarty); | |
1420 | $tokenText = CRM_Utils_Token::replaceHookTokens($tokenText, $values, $categories, FALSE, $escapeSmarty); | |
1421 | ||
1422 | $tokenHtml = CRM_Utils_Token::replaceContactTokens($html, $values, TRUE, $messageToken, FALSE, $escapeSmarty); | |
1423 | $tokenHtml = CRM_Utils_Token::replaceHookTokens($tokenHtml, $values, $categories, TRUE, $escapeSmarty); | |
1424 | ||
d65e1a68 | 1425 | // Only send if the phone is of type mobile |
9357a775 DS |
1426 | $phoneTypes = CRM_Core_OptionGroup::values('phone_type', TRUE, FALSE, FALSE, NULL, 'name'); |
1427 | if ($values['phone_type_id'] == CRM_Utils_Array::value('Mobile', $phoneTypes)) { | |
d65e1a68 | 1428 | $smsParams['To'] = $values['phone']; |
01aca362 DL |
1429 | } |
1430 | else { | |
d65e1a68 TW |
1431 | $smsParams['To'] = ''; |
1432 | } | |
6a488035 | 1433 | |
c5a6413b DS |
1434 | $sendResult = self::sendSMSMessage( |
1435 | $contactId, | |
1436 | $tokenText, | |
1437 | $tokenHtml, | |
1438 | $smsParams, | |
e8cb3963 DS |
1439 | $activityID, |
1440 | $userID | |
c5a6413b DS |
1441 | ); |
1442 | ||
1443 | if (PEAR::isError($sendResult)) { | |
1444 | // Collect all of the PEAR_Error objects | |
1445 | $errMsgs[] = $sendResult; | |
1446 | } else { | |
f53ea1ce | 1447 | $success++; |
6a488035 TO |
1448 | } |
1449 | } | |
1450 | ||
c5a6413b DS |
1451 | // If at least one message was sent and no errors |
1452 | // were generated then return a boolean value of TRUE. | |
1453 | // Otherwise, return FALSE (no messages sent) or | |
1454 | // and array of 1 or more PEAR_Error objects. | |
1455 | $sent = FALSE; | |
1456 | if ($success > 0 && count($errMsgs) == 0) { | |
1457 | $sent = TRUE; | |
1458 | } elseif (count($errMsgs) > 0) { | |
1459 | $sent = $errMsgs; | |
1460 | } | |
1461 | ||
f53ea1ce | 1462 | return array($sent, $activity->id, $success); |
6a488035 TO |
1463 | } |
1464 | ||
1465 | /** | |
1466 | * send the sms message to a specific contact | |
1467 | * | |
1468 | * @param int $toID the contact id of the recipient | |
1469 | * @param int $activityID the activity ID that tracks the message | |
1470 | * @param array $smsParams the params used for sending sms | |
1471 | * | |
c5a6413b | 1472 | * @return mixed true on success or PEAR_Error object |
6a488035 TO |
1473 | * @access public |
1474 | * @static | |
1475 | */ | |
1476 | static function sendSMSMessage($toID, | |
1477 | &$tokenText, | |
1478 | &$tokenHtml, | |
1479 | $smsParams = array(), | |
e8cb3963 DS |
1480 | $activityID, |
1481 | $userID = null | |
6a488035 TO |
1482 | ) { |
1483 | $toDoNotSms = ""; | |
1484 | $toPhoneNumber = ""; | |
1485 | ||
1486 | if ($smsParams['To']) { | |
1487 | $toPhoneNumber = trim($smsParams['To']); | |
1488 | } | |
1489 | elseif ($toID) { | |
1490 | $filters = array('is_deceased' => 0, 'is_deleted' => 0, 'do_not_sms' => 0); | |
1491 | $toPhoneNumbers = CRM_Core_BAO_Phone::allPhones($toID, FALSE, 'Mobile', $filters); | |
1492 | //to get primary mobile ph,if not get a first mobile ph | |
1493 | if (!empty($toPhoneNumbers)) { | |
1494 | $toPhoneNumerDetails = reset($toPhoneNumbers); | |
1495 | $toPhoneNumber = CRM_Utils_Array::value('phone', $toPhoneNumerDetails); | |
1496 | //contact allows to send sms | |
1497 | $toDoNotSms = 0; | |
1498 | } | |
1499 | } | |
1500 | ||
1501 | // make sure both phone are valid | |
1502 | // and that the recipient wants to receive sms | |
1503 | if (empty($toPhoneNumber) or $toDoNotSms) { | |
c5a6413b DS |
1504 | return PEAR::raiseError( |
1505 | 'Recipient phone number is invalid or recipient does not want to receive SMS', | |
1506 | null, | |
1507 | PEAR_ERROR_RETURN | |
1508 | ); | |
6a488035 TO |
1509 | } |
1510 | ||
1511 | $message = $tokenHtml ? $tokenHtml : $tokenText; | |
1512 | $recipient = $smsParams['To']; | |
1513 | $smsParams['contact_id'] = $toID; | |
1514 | $smsParams['parent_activity_id'] = $activityID; | |
1515 | ||
1516 | $providerObj = CRM_SMS_Provider::singleton(array('provider_id' => $smsParams['provider_id'])); | |
e8cb3963 | 1517 | $sendResult = $providerObj->send($recipient, $smsParams, $message, NULL, $userID); |
c5a6413b DS |
1518 | if (PEAR::isError($sendResult)) { |
1519 | return $sendResult; | |
6a488035 TO |
1520 | } |
1521 | ||
e7e657f0 | 1522 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
a24b3694 | 1523 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); |
b319d00a | 1524 | |
a24b3694 | 1525 | |
6a488035 TO |
1526 | // add activity target record for every sms that is send |
1527 | $activityTargetParams = array( | |
1528 | 'activity_id' => $activityID, | |
1d85d241 | 1529 | 'contact_id' => $toID, |
a24b3694 | 1530 | 'record_type_id' => $targetID |
6a488035 | 1531 | ); |
1d85d241 | 1532 | CRM_Activity_BAO_ActivityContact::create($activityTargetParams); |
6a488035 TO |
1533 | |
1534 | return TRUE; | |
1535 | } | |
1536 | ||
1537 | /** | |
1538 | * send the message to a specific contact | |
1539 | * | |
1540 | * @param string $from the name and email of the sender | |
1541 | * @param int $toID the contact id of the recipient | |
1542 | * @param string $subject the subject of the message | |
1543 | * @param string $message the message contents | |
1544 | * @param string $emailAddress use this 'to' email address instead of the default Primary address | |
1545 | * @param int $activityID the activity ID that tracks the message | |
1546 | * | |
1547 | * @return boolean true if successfull else false. | |
1548 | * @access public | |
1549 | * @static | |
1550 | */ | |
1551 | static function sendMessage($from, | |
1552 | $fromID, | |
1553 | $toID, | |
1554 | &$subject, | |
1555 | &$text_message, | |
1556 | &$html_message, | |
1557 | $emailAddress, | |
1558 | $activityID, | |
1559 | $attachments = NULL, | |
1560 | $cc = NULL, | |
1561 | $bcc = NULL | |
1562 | ) { | |
1563 | list($toDisplayName, $toEmail, $toDoNotEmail) = CRM_Contact_BAO_Contact::getContactDetails($toID); | |
1564 | if ($emailAddress) { | |
1565 | $toEmail = trim($emailAddress); | |
1566 | } | |
1567 | ||
1568 | // make sure both email addresses are valid | |
1569 | // and that the recipient wants to receive email | |
1570 | if (empty($toEmail) or $toDoNotEmail) { | |
1571 | return FALSE; | |
1572 | } | |
1573 | if (!trim($toDisplayName)) { | |
1574 | $toDisplayName = $toEmail; | |
1575 | } | |
1576 | ||
e7e657f0 | 1577 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
a24b3694 | 1578 | //$sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
1579 | //$assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
1580 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
1581 | ||
6a488035 TO |
1582 | // create the params array |
1583 | $mailParams = array( | |
1584 | 'groupName' => 'Activity Email Sender', | |
1585 | 'from' => $from, | |
1586 | 'toName' => $toDisplayName, | |
1587 | 'toEmail' => $toEmail, | |
1588 | 'subject' => $subject, | |
1589 | 'cc' => $cc, | |
1590 | 'bcc' => $bcc, | |
1591 | 'text' => $text_message, | |
1592 | 'html' => $html_message, | |
1593 | 'attachments' => $attachments, | |
1594 | ); | |
1595 | ||
1596 | if (!CRM_Utils_Mail::send($mailParams)) { | |
1597 | return FALSE; | |
1598 | } | |
1599 | ||
1600 | // add activity target record for every mail that is send | |
1601 | $activityTargetParams = array( | |
1602 | 'activity_id' => $activityID, | |
1d85d241 | 1603 | 'contact_id' => $toID, |
b319d00a | 1604 | 'record_type_id' => $targetID |
6a488035 | 1605 | ); |
1d85d241 | 1606 | CRM_Activity_BAO_ActivityContact::create($activityTargetParams); |
6a488035 TO |
1607 | return TRUE; |
1608 | } | |
1609 | ||
1610 | /** | |
1611 | * combine all the importable fields from the lower levels object | |
1612 | * | |
1613 | * The ordering is important, since currently we do not have a weight | |
1614 | * scheme. Adding weight is super important and should be done in the | |
1615 | * next week or so, before this can be called complete. | |
1616 | * | |
1617 | * @param NULL | |
1618 | * | |
1619 | * @return array array of importable Fields | |
1620 | * @access public | |
1621 | * @static | |
1622 | */ | |
1623 | static function &importableFields($status = FALSE) { | |
1624 | if (!self::$_importableFields) { | |
1625 | if (!self::$_importableFields) { | |
1626 | self::$_importableFields = array(); | |
1627 | } | |
1628 | if (!$status) { | |
1629 | $fields = array('' => array('title' => ts('- do not import -'))); | |
1630 | } | |
1631 | else { | |
1632 | $fields = array('' => array('title' => ts('- Activity Fields -'))); | |
1633 | } | |
1634 | ||
1635 | $tmpFields = CRM_Activity_DAO_Activity::import(); | |
1636 | $contactFields = CRM_Contact_BAO_Contact::importableFields('Individual', NULL); | |
1637 | ||
1638 | // Using new Dedupe rule. | |
1639 | $ruleParams = array( | |
1640 | 'contact_type' => 'Individual', | |
1641 | 'used' => 'Unsupervised', | |
1642 | ); | |
1643 | $fieldsArray = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); | |
1644 | ||
1645 | $tmpConatctField = array(); | |
1646 | if (is_array($fieldsArray)) { | |
1647 | foreach ($fieldsArray as $value) { | |
1648 | $customFieldId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', | |
1649 | $value, | |
1650 | 'id', | |
1651 | 'column_name' | |
1652 | ); | |
1653 | $value = $customFieldId ? 'custom_' . $customFieldId : $value; | |
1654 | $tmpConatctField[trim($value)] = $contactFields[trim($value)]; | |
1655 | $tmpConatctField[trim($value)]['title'] = $tmpConatctField[trim($value)]['title'] . " (match to contact)"; | |
1656 | } | |
1657 | } | |
1658 | $tmpConatctField['external_identifier'] = $contactFields['external_identifier']; | |
1659 | $tmpConatctField['external_identifier']['title'] = $contactFields['external_identifier']['title'] . " (match to contact)"; | |
1660 | $fields = array_merge($fields, $tmpConatctField); | |
1661 | $fields = array_merge($fields, $tmpFields); | |
1662 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Activity')); | |
1663 | self::$_importableFields = $fields; | |
1664 | } | |
1665 | return self::$_importableFields; | |
1666 | } | |
1667 | ||
1668 | /** | |
1669 | * To get the Activities of a target contact | |
1670 | * | |
1671 | * @param $contactId Integer ContactId of the contact whose activities | |
1672 | * need to find | |
1673 | * | |
1674 | * @return array array of activity fields | |
1675 | * @access public | |
1676 | */ | |
1677 | static function getContactActivity($contactId) { | |
1678 | $activities = array(); | |
e7e657f0 | 1679 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
a24b3694 | 1680 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
1681 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
1682 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
b319d00a | 1683 | |
6a488035 TO |
1684 | |
1685 | // First look for activities where contactId is one of the targets | |
91da6cd5 | 1686 | $query = " |
a24b3694 | 1687 | SELECT activity_id, record_type_id |
91da6cd5 DL |
1688 | FROM civicrm_activity_contact |
1689 | WHERE contact_id = $contactId | |
1690 | "; | |
1691 | $dao = CRM_Core_DAO::executeQuery($query); | |
6a488035 | 1692 | while ($dao->fetch()) { |
a24b3694 | 1693 | if ($dao->record_type_id == $targetID ) { |
91da6cd5 DL |
1694 | $activities[$dao->activity_id]['targets'][$contactId] = $contactId; |
1695 | } | |
a24b3694 | 1696 | else if ($dao->record_type_id == $assigneeID) { |
91da6cd5 DL |
1697 | $activities[$dao->activity_id]['asignees'][$contactId] = $contactId; |
1698 | } | |
1699 | else { | |
1700 | // do source stuff here | |
42d30b83 | 1701 | $activities[$dao->activity_id]['source_contact_id'] = $contactId; |
91da6cd5 | 1702 | } |
6a488035 TO |
1703 | } |
1704 | ||
91da6cd5 | 1705 | $activityIds = array_keys($activities); |
6a488035 TO |
1706 | if (count($activityIds) < 1) { |
1707 | return array(); | |
1708 | } | |
91da6cd5 | 1709 | |
6a488035 | 1710 | $activityIds = implode(',', $activityIds); |
91da6cd5 DL |
1711 | $query = " |
1712 | SELECT activity.id as activity_id, | |
1713 | activity_type_id, | |
1714 | subject, location, activity_date_time, details, status_id | |
1715 | FROM civicrm_activity activity | |
1716 | WHERE activity.id IN ($activityIds)"; | |
6a488035 | 1717 | |
91da6cd5 | 1718 | $dao = CRM_Core_DAO::executeQuery($query); |
6a488035 TO |
1719 | |
1720 | $activityTypes = CRM_Core_OptionGroup::values('activity_type'); | |
1721 | $activityStatuses = CRM_Core_OptionGroup::values('activity_status'); | |
1722 | ||
1723 | while ($dao->fetch()) { | |
6a488035 | 1724 | $activities[$dao->activity_id]['id'] = $dao->activity_id; |
6a488035 TO |
1725 | $activities[$dao->activity_id]['activity_type_id'] = $dao->activity_type_id; |
1726 | $activities[$dao->activity_id]['subject'] = $dao->subject; | |
1727 | $activities[$dao->activity_id]['location'] = $dao->location; | |
1728 | $activities[$dao->activity_id]['activity_date_time'] = $dao->activity_date_time; | |
1729 | $activities[$dao->activity_id]['details'] = $dao->details; | |
1730 | $activities[$dao->activity_id]['status_id'] = $dao->status_id; | |
1731 | $activities[$dao->activity_id]['activity_name'] = $activityTypes[$dao->activity_type_id]; | |
1732 | $activities[$dao->activity_id]['status'] = $activityStatuses[$dao->status_id]; | |
42d30b83 DL |
1733 | |
1734 | // set to null if not set | |
1735 | if (!isset($activities[$dao->activity_id]['source_contact_id'])) { | |
1736 | $activities[$dao->activity_id]['source_contact_id'] = NULL; | |
1737 | } | |
6a488035 TO |
1738 | } |
1739 | return $activities; | |
1740 | } | |
1741 | ||
1742 | /** | |
1743 | * Function to add activity for Membership/Event/Contribution | |
1744 | * | |
0a9f61c4 | 1745 | * @param object $activity (reference) particular component object |
6a488035 TO |
1746 | * @param string $activityType for Membership Signup or Renewal |
1747 | * | |
1748 | * | |
1749 | * @static | |
1750 | * @access public | |
1751 | */ | |
1752 | static function addActivity(&$activity, | |
1753 | $activityType = 'Membership Signup', | |
1754 | $targetContactID = NULL | |
1755 | ) { | |
1756 | if ($activity->__table == 'civicrm_membership') { | |
1757 | $membershipType = CRM_Member_PseudoConstant::membershipType($activity->membership_type_id); | |
1758 | ||
1759 | if (!$membershipType) { | |
1760 | $membershipType = ts('Membership'); | |
1761 | } | |
1762 | ||
1763 | $subject = "{$membershipType}"; | |
1764 | ||
1765 | if (!empty($activity->source) && $activity->source != 'null') { | |
1766 | $subject .= " - {$activity->source}"; | |
1767 | } | |
1768 | ||
1769 | if ($activity->owner_membership_id) { | |
1770 | $query = " | |
1771 | SELECT display_name | |
1772 | FROM civicrm_contact, civicrm_membership | |
1773 | WHERE civicrm_contact.id = civicrm_membership.contact_id | |
1774 | AND civicrm_membership.id = $activity->owner_membership_id | |
1775 | "; | |
1776 | $displayName = CRM_Core_DAO::singleValueQuery($query); | |
1777 | $subject .= " (by {$displayName})"; | |
1778 | } | |
1779 | ||
1780 | $subject .= " - Status: " . CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', $activity->status_id); | |
1781 | // CRM-72097 changed from start date to today | |
1782 | $date = date('YmdHis'); | |
1783 | $component = 'Membership'; | |
1784 | } | |
1785 | elseif ($activity->__table == 'civicrm_participant') { | |
c2be40dc | 1786 | $event = CRM_Event_BAO_Event::getEvents(1, $activity->event_id, TRUE, FALSE); |
6a488035 TO |
1787 | |
1788 | $roles = CRM_Event_PseudoConstant::participantRole(); | |
1789 | $status = CRM_Event_PseudoConstant::participantStatus(); | |
1790 | ||
1791 | $subject = $event[$activity->event_id]; | |
1792 | if (CRM_Utils_Array::value($activity->role_id, $roles)) { | |
1793 | $subject .= ' - ' . $roles[$activity->role_id]; | |
1794 | } | |
1795 | if (CRM_Utils_Array::value($activity->status_id, $status)) { | |
1796 | $subject .= ' - ' . $status[$activity->status_id]; | |
1797 | } | |
1798 | $date = date('YmdHis'); | |
1799 | if ($activityType != 'Email') { | |
1800 | $activityType = 'Event Registration'; | |
1801 | } | |
1802 | $component = 'Event'; | |
1803 | } | |
1804 | elseif ($activity->__table == 'civicrm_contribution') { | |
1805 | //create activity record only for Completed Contributions | |
1806 | if ($activity->contribution_status_id != 1) { | |
1807 | return; | |
1808 | } | |
1809 | ||
1810 | $subject = NULL; | |
1811 | ||
1812 | $subject .= CRM_Utils_Money::format($activity->total_amount, $activity->currency); | |
1813 | if (!empty($activity->source) && $activity->source != 'null') { | |
1814 | $subject .= " - {$activity->source}"; | |
1815 | } | |
1816 | $date = CRM_Utils_Date::isoToMysql($activity->receive_date); | |
1817 | $activityType = $component = 'Contribution'; | |
1818 | } | |
1819 | $activityParams = array( | |
1820 | 'source_contact_id' => $activity->contact_id, | |
1821 | 'source_record_id' => $activity->id, | |
1822 | 'activity_type_id' => CRM_Core_OptionGroup::getValue('activity_type', | |
1823 | $activityType, | |
1824 | 'name' | |
1825 | ), | |
1826 | 'subject' => $subject, | |
1827 | 'activity_date_time' => $date, | |
1828 | 'is_test' => $activity->is_test, | |
1829 | 'status_id' => CRM_Core_OptionGroup::getValue('activity_status', | |
1830 | 'Completed', | |
1831 | 'name' | |
1832 | ), | |
1833 | 'skipRecentView' => TRUE, | |
1834 | 'campaign_id' => $activity->campaign_id, | |
1835 | ); | |
1836 | ||
1837 | // create activity with target contacts | |
1838 | $session = CRM_Core_Session::singleton(); | |
1839 | $id = $session->get('userID'); | |
1840 | if ($id) { | |
1841 | $activityParams['source_contact_id'] = $id; | |
1842 | $activityParams['target_contact_id'][] = $activity->contact_id; | |
1843 | } | |
1844 | ||
1845 | //CRM-4027 | |
1846 | if ($targetContactID) { | |
1847 | $activityParams['target_contact_id'][] = $targetContactID; | |
1848 | } | |
1849 | if (is_a(self::create($activityParams), 'CRM_Core_Error')) { | |
1850 | CRM_Core_Error::fatal("Failed creating Activity for $component of id {$activity->id}"); | |
1851 | return FALSE; | |
1852 | } | |
1853 | } | |
1854 | ||
1855 | /** | |
0a9f61c4 | 1856 | * Function to get Parent activity for currently viewed activity |
6a488035 TO |
1857 | * |
1858 | * @param int $activityId current activity id | |
1859 | * | |
0a9f61c4 | 1860 | * @return int $parentId Id of parent activity otherwise false. |
6a488035 TO |
1861 | * @access public |
1862 | */ | |
1863 | static function getParentActivity($activityId) { | |
1864 | static $parentActivities = array(); | |
1865 | ||
1866 | $activityId = CRM_Utils_Type::escape($activityId, 'Integer'); | |
1867 | ||
1868 | if (!array_key_exists($activityId, $parentActivities)) { | |
1869 | $parentActivities[$activityId] = array(); | |
1870 | ||
1871 | $parentId = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', | |
1872 | $activityId, | |
1873 | 'parent_id' | |
1874 | ); | |
1875 | ||
1876 | $parentActivities[$activityId] = $parentId ? $parentId : FALSE; | |
1877 | } | |
1878 | ||
1879 | return $parentActivities[$activityId]; | |
1880 | } | |
1881 | ||
1882 | /** | |
1883 | * Function to get total count of prior revision of currently viewd activity | |
1884 | * | |
1885 | * @param int $activityId current activity id | |
1886 | * | |
0a9f61c4 | 1887 | * @return int $params count of prior activities otherwise false. |
6a488035 TO |
1888 | * @access public |
1889 | */ | |
1890 | static function getPriorCount($activityID) { | |
1891 | static $priorCounts = array(); | |
1892 | ||
1893 | $activityID = CRM_Utils_Type::escape($activityID, 'Integer'); | |
1894 | ||
1895 | if (!array_key_exists($activityID, $priorCounts)) { | |
1896 | $priorCounts[$activityID] = array(); | |
1897 | $originalID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', | |
1898 | $activityID, | |
1899 | 'original_id' | |
1900 | ); | |
1901 | $count = 0; | |
1902 | if ($originalID) { | |
1903 | $query = " | |
1904 | SELECT count( id ) AS cnt | |
1905 | FROM civicrm_activity | |
1906 | WHERE ( id = {$originalID} OR original_id = {$originalID} ) | |
1907 | AND is_current_revision = 0 | |
1908 | AND id < {$activityID} | |
1909 | "; | |
1910 | $params = array(1 => array($originalID, 'Integer')); | |
1911 | $count = CRM_Core_DAO::singleValueQuery($query, $params); | |
1912 | } | |
1913 | $priorCounts[$activityID] = $count ? $count : 0; | |
1914 | } | |
1915 | ||
1916 | return $priorCounts[$activityID]; | |
1917 | } | |
1918 | ||
1919 | /** | |
0a9f61c4 | 1920 | * Function to get all prior activities of currently viewe |
1921 | * d activity | |
6a488035 TO |
1922 | * |
1923 | * @param int $activityId current activity id | |
1924 | * | |
0a9f61c4 | 1925 | * @return array $result prior activities info. |
6a488035 TO |
1926 | * @access public |
1927 | */ | |
1928 | static function getPriorAcitivities($activityID, $onlyPriorRevisions = FALSE) { | |
1929 | static $priorActivities = array(); | |
1930 | ||
1931 | $activityID = CRM_Utils_Type::escape($activityID, 'Integer'); | |
1932 | $index = $activityID . '_' . (int) $onlyPriorRevisions; | |
1933 | ||
1934 | if (!array_key_exists($index, $priorActivities)) { | |
1935 | $priorActivities[$index] = array(); | |
1936 | ||
1937 | $originalID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', | |
1938 | $activityID, | |
1939 | 'original_id' | |
1940 | ); | |
1941 | if ($originalID) { | |
1942 | $query = " | |
1943 | SELECT c.display_name as name, cl.modified_date as date, ca.id as activityID | |
1944 | FROM civicrm_log cl, civicrm_contact c, civicrm_activity ca | |
1945 | WHERE (ca.id = %1 OR ca.original_id = %1) | |
1946 | AND cl.entity_table = 'civicrm_activity' | |
1947 | AND cl.entity_id = ca.id | |
1948 | AND cl.modified_id = c.id | |
1949 | "; | |
1950 | if ($onlyPriorRevisions) { | |
1951 | $query .= " AND ca.id < {$activityID}"; | |
1952 | } | |
1953 | $query .= " ORDER BY ca.id DESC"; | |
1954 | ||
1955 | $params = array(1 => array($originalID, 'Integer')); | |
1956 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
1957 | ||
1958 | while ($dao->fetch()) { | |
1959 | $priorActivities[$index][$dao->activityID]['id'] = $dao->activityID; | |
1960 | $priorActivities[$index][$dao->activityID]['name'] = $dao->name; | |
1961 | $priorActivities[$index][$dao->activityID]['date'] = $dao->date; | |
1962 | $priorActivities[$index][$dao->activityID]['link'] = 'javascript:viewActivity( $dao->activityID );'; | |
1963 | } | |
1964 | $dao->free(); | |
1965 | } | |
1966 | } | |
1967 | return $priorActivities[$index]; | |
1968 | } | |
1969 | ||
1970 | /** | |
1971 | * Function to find the latest revision of a given activity | |
1972 | * | |
1973 | * @param int $activityId prior activity id | |
1974 | * | |
1975 | * @return int $params current activity id. | |
1976 | * @access public | |
1977 | */ | |
1978 | static function getLatestActivityId($activityID) { | |
1979 | static $latestActivityIds = array(); | |
1980 | ||
1981 | $activityID = CRM_Utils_Type::escape($activityID, 'Integer'); | |
1982 | ||
1983 | if (!array_key_exists($activityID, $latestActivityIds)) { | |
1984 | $latestActivityIds[$activityID] = array(); | |
1985 | ||
1986 | $originalID = CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', | |
1987 | $activityID, | |
1988 | 'original_id' | |
1989 | ); | |
1990 | if ($originalID) { | |
1991 | $activityID = $originalID; | |
1992 | } | |
1993 | $params = array(1 => array($activityID, 'Integer')); | |
1994 | $query = "SELECT id from civicrm_activity where original_id = %1 and is_current_revision = 1"; | |
1995 | ||
1996 | $latestActivityIds[$activityID] = CRM_Core_DAO::singleValueQuery($query, $params); | |
1997 | } | |
1998 | ||
1999 | return $latestActivityIds[$activityID]; | |
2000 | } | |
2001 | ||
2002 | /** | |
2003 | * Function to create a follow up a given activity | |
2004 | * | |
2005 | * @activityId int activity id of parent activity | |
2006 | * | |
2007 | * @param array $activity details | |
2008 | * | |
2009 | * @access public | |
2010 | */ | |
2011 | static function createFollowupActivity($activityId, $params) { | |
2012 | if (!$activityId) { | |
2013 | return; | |
2014 | } | |
2015 | ||
2016 | $session = CRM_Core_Session::singleton(); | |
2017 | ||
2018 | $followupParams = array(); | |
2019 | $followupParams['parent_id'] = $activityId; | |
2020 | $followupParams['source_contact_id'] = $session->get('userID'); | |
2021 | $followupParams['status_id'] = CRM_Core_OptionGroup::getValue('activity_status', 'Scheduled', 'name'); | |
2022 | ||
2023 | $followupParams['activity_type_id'] = $params['followup_activity_type_id']; | |
2024 | // Get Subject of Follow-up Activiity, CRM-4491 | |
2025 | $followupParams['subject'] = CRM_Utils_Array::value('followup_activity_subject', $params); | |
2026 | ||
2027 | //create target contact for followup | |
2028 | if (CRM_Utils_Array::value('target_contact_id', $params)) { | |
2029 | $followupParams['target_contact_id'] = $params['target_contact_id']; | |
2030 | } | |
2031 | ||
2032 | $followupParams['activity_date_time'] = CRM_Utils_Date::processDate($params['followup_date'], | |
2033 | $params['followup_date_time'] | |
2034 | ); | |
2035 | $followupActivity = self::create($followupParams); | |
2036 | ||
2037 | return $followupActivity; | |
2038 | } | |
2039 | ||
2040 | /** | |
2041 | * Function to get Activity specific File according activity type Id. | |
2042 | * | |
2043 | * @param int $activityTypeId activity id | |
2044 | * | |
2045 | * @return if file exists returns $activityTypeFile activity filename otherwise false. | |
2046 | * | |
2047 | * @static | |
2048 | */ | |
2049 | static function getFileForActivityTypeId($activityTypeId, $crmDir = 'Activity') { | |
2050 | $activityTypes = CRM_Case_PseudoConstant::caseActivityType(FALSE, TRUE); | |
2051 | ||
2052 | if ($activityTypes[$activityTypeId]['name']) { | |
2053 | $activityTypeFile = CRM_Utils_String::munge(ucwords($activityTypes[$activityTypeId]['name']), '', 0); | |
2054 | } | |
2055 | else { | |
2056 | return FALSE; | |
2057 | } | |
2058 | ||
2059 | global $civicrm_root; | |
2060 | $config = CRM_Core_Config::singleton(); | |
2061 | if (!file_exists(rtrim($civicrm_root, '/') . "/CRM/{$crmDir}/Form/Activity/{$activityTypeFile}.php")) { | |
2062 | if (empty($config->customPHPPathDir)) { | |
2063 | return FALSE; | |
2064 | } | |
2065 | elseif (!file_exists(rtrim($config->customPHPPathDir, '/') . "/CRM/{$crmDir}/Form/Activity/{$activityTypeFile}.php")) { | |
2066 | return FALSE; | |
2067 | } | |
2068 | } | |
2069 | ||
2070 | return $activityTypeFile; | |
2071 | } | |
2072 | ||
2073 | /** | |
2074 | * Function to restore the activity | |
2075 | * | |
2076 | * @param array $params associated array | |
2077 | * | |
2078 | * @return void | |
2079 | * @access public | |
2080 | * | |
2081 | */ | |
2082 | public static function restoreActivity(&$params) { | |
2083 | $activity = new CRM_Activity_DAO_Activity(); | |
2084 | $activity->copyValues($params); | |
2085 | ||
2086 | $activity->is_deleted = 0; | |
2087 | $result = $activity->save(); | |
2088 | ||
2089 | return $result; | |
2090 | } | |
2091 | ||
2092 | /** | |
2093 | * Get the exportable fields for Activities | |
2094 | * | |
2095 | * @param string $name if it is called by case $name = Case else $name = Activity | |
2096 | * | |
2097 | * @return array array of exportable Fields | |
2098 | * @access public | |
2099 | * @static | |
2100 | */ | |
2101 | static function &exportableFields($name = 'Activity') { | |
2102 | if (!isset(self::$_exportableFields[$name])) { | |
2103 | self::$_exportableFields[$name] = array(); | |
2104 | ||
2105 | // TO DO, ideally we should retrieve all fields from xml, in this case since activity processing is done | |
2106 | // my case hence we have defined fields as case_* | |
2107 | if ($name == 'Activity') { | |
2108 | $exportableFields = CRM_Activity_DAO_Activity::export(); | |
6a488035 TO |
2109 | $exportableFields['source_contact_id']['title'] = ts('Source Contact ID'); |
2110 | $exportableFields['source_contact'] = array( | |
2111 | 'title' => ts('Source Contact'), | |
2112 | 'type' => CRM_Utils_Type::T_STRING, | |
2113 | ); | |
2114 | ||
2115 | ||
2116 | $Activityfields = array( | |
2117 | 'activity_type' => array('title' => ts('Activity Type'), 'type' => CRM_Utils_Type::T_STRING), | |
2118 | 'activity_status' => array('title' => ts('Activity Status'), 'type' => CRM_Utils_Type::T_STRING), | |
2119 | ); | |
2120 | $fields = array_merge($Activityfields, $exportableFields); | |
2121 | } | |
2122 | else { | |
2123 | //set title to activity fields | |
2124 | $fields = array( | |
2125 | 'case_activity_subject' => array('title' => ts('Activity Subject'), 'type' => CRM_Utils_Type::T_STRING), | |
2126 | 'case_source_contact_id' => array('title' => ts('Activity Reporter'), 'type' => CRM_Utils_Type::T_STRING), | |
2127 | 'case_recent_activity_date' => array('title' => ts('Activity Actual Date'), 'type' => CRM_Utils_Type::T_DATE), | |
2128 | 'case_scheduled_activity_date' => array('title' => ts('Activity Scheduled Date'), 'type' => CRM_Utils_Type::T_DATE), | |
2129 | 'case_recent_activity_type' => array('title' => ts('Activity Type'), 'type' => CRM_Utils_Type::T_STRING), | |
2130 | 'case_activity_status' => array('title' => ts('Activity Status'), 'type' => CRM_Utils_Type::T_STRING), | |
2131 | 'case_activity_duration' => array('title' => ts('Activity Duration'), 'type' => CRM_Utils_Type::T_INT), | |
2132 | 'case_activity_medium_id' => array('title' => ts('Activity Medium'), 'type' => CRM_Utils_Type::T_INT), | |
2133 | 'case_activity_details' => array('title' => ts('Activity Details'), 'type' => CRM_Utils_Type::T_TEXT), | |
2134 | 'case_activity_is_auto' => array('title' => ts('Activity Auto-generated?'), 'type' => CRM_Utils_Type::T_BOOLEAN), | |
2135 | ); | |
2136 | ||
2137 | // add custom data for cases | |
2138 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Case')); | |
2139 | } | |
2140 | ||
2141 | // add custom data for case activities | |
2142 | $fields = array_merge($fields, CRM_Core_BAO_CustomField::getFieldsForImport('Activity')); | |
2143 | ||
2144 | self::$_exportableFields[$name] = $fields; | |
2145 | } | |
2146 | return self::$_exportableFields[$name]; | |
2147 | } | |
2148 | ||
2149 | /** | |
2150 | * Get the allowed profile fields for Activities | |
2151 | * | |
2152 | * @return array array of activity profile Fields | |
2153 | * @access public | |
2154 | */ | |
2155 | static function getProfileFields() { | |
2156 | $exportableFields = self::exportableFields('Activity'); | |
4f79a2f5 | 2157 | $skipFields = array( |
2158 | 'activity_id', | |
2159 | 'activity_type', | |
2160 | 'source_contact_id', | |
2161 | 'source_contact', | |
2162 | 'activity_campaign', | |
2163 | 'activity_is_test', | |
2164 | 'is_current_revision', | |
2165 | 'activity_is_deleted', | |
2166 | ); | |
6a488035 TO |
2167 | $config = CRM_Core_Config::singleton(); |
2168 | if (!in_array('CiviCampaign', $config->enableComponents)) { | |
2169 | $skipFields[] = 'activity_engagement_level'; | |
2170 | } | |
2171 | ||
2172 | foreach ($skipFields as $field) { | |
2173 | if (isset($exportableFields[$field])) { | |
2174 | unset($exportableFields[$field]); | |
2175 | } | |
2176 | } | |
2177 | ||
2178 | // hack to use 'activity_type_id' instead of 'activity_type' | |
2179 | $exportableFields['activity_status_id'] = $exportableFields['activity_status']; | |
2180 | unset($exportableFields['activity_status']); | |
2181 | ||
2182 | return $exportableFields; | |
2183 | } | |
2184 | ||
2185 | /** | |
f1504541 | 2186 | * This function deletes the activity record related to contact record, |
6a488035 TO |
2187 | * when there are no target and assignee record w/ other contact. |
2188 | * | |
2189 | * @param int $contactId contactId | |
2190 | * | |
2191 | * @return true/null | |
2192 | * @access public | |
2193 | */ | |
2194 | public static function cleanupActivity($contactId) { | |
2195 | $result = NULL; | |
2196 | if (!$contactId) { | |
2197 | return $result; | |
2198 | } | |
e7e657f0 | 2199 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
2bf96211 | 2200 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
6a488035 TO |
2201 | |
2202 | $transaction = new CRM_Core_Transaction(); | |
2203 | ||
f1504541 DL |
2204 | // delete activity if there is no record in civicrm_activity_contact |
2205 | // pointing to any other contact record | |
2bf96211 | 2206 | $activityContact = new CRM_Activity_DAO_ActivityContact(); |
2207 | $activityContact->contact_id = $contactId; | |
2208 | $activityContact->record_type_id = $sourceID; | |
2209 | $activityContact->find(); | |
6a488035 | 2210 | |
2bf96211 | 2211 | while ($activityContact->fetch()) { |
f1504541 | 2212 | // delete activity_contact record for the deleted contact |
32ecf7bb BS |
2213 | $activityContact->delete(); |
2214 | ||
2215 | $activityContactOther = new CRM_Activity_DAO_ActivityContact(); | |
2216 | $activityContactOther->activity_id = $activityContact->activity_id; | |
32ecf7bb | 2217 | |
83e0a89c | 2218 | // delete activity only if no other contacts connected |
f1504541 | 2219 | if ( ! $activityContactOther->find(TRUE) ) { |
32ecf7bb BS |
2220 | $activityParams = array('id' => $activityContact->activity_id); |
2221 | $result = self::deleteActivity($activityParams); | |
2222 | } | |
2223 | ||
2224 | $activityContactOther->free(); | |
6a488035 | 2225 | } |
6a488035 | 2226 | |
2bf96211 | 2227 | $activityContact->free(); |
6a488035 TO |
2228 | $transaction->commit(); |
2229 | ||
2230 | return $result; | |
2231 | } | |
2232 | ||
2233 | /** | |
2234 | * Does user has sufficient permission for view/edit activity record. | |
2235 | * | |
2236 | * @param int $activityId activity record id. | |
2237 | * @param int $action edit/view | |
2238 | * | |
2239 | * @return boolean $allow true/false | |
2240 | * @access public | |
2241 | */ | |
2242 | public static function checkPermission($activityId, $action) { | |
2243 | $allow = FALSE; | |
2244 | if (!$activityId || | |
2245 | !in_array($action, array(CRM_Core_Action::UPDATE, CRM_Core_Action::VIEW)) | |
2246 | ) { | |
2247 | return $allow; | |
2248 | } | |
2249 | ||
2250 | $activity = new CRM_Activity_DAO_Activity(); | |
2251 | $activity->id = $activityId; | |
2252 | if (!$activity->find(TRUE)) { | |
2253 | return $allow; | |
2254 | } | |
2255 | ||
2256 | //component related permissions. | |
2257 | $compPermissions = array( | |
2258 | 'CiviCase' => array('administer CiviCase', | |
2259 | 'access my cases and activities', | |
2260 | 'access all cases and activities', | |
2261 | ), | |
2262 | 'CiviMail' => array('access CiviMail'), | |
2263 | 'CiviEvent' => array('access CiviEvent'), | |
2264 | 'CiviGrant' => array('access CiviGrant'), | |
2265 | 'CiviPledge' => array('access CiviPledge'), | |
2266 | 'CiviMember' => array('access CiviMember'), | |
2267 | 'CiviReport' => array('access CiviReport'), | |
2268 | 'CiviContribute' => array('access CiviContribute'), | |
2269 | 'CiviCampaign' => array('administer CiviCampaign'), | |
2270 | ); | |
2271 | ||
2272 | //return early when it is case activity. | |
2273 | $isCaseActivity = CRM_Case_BAO_Case::isCaseActivity($activityId); | |
2274 | //check for civicase related permission. | |
2275 | if ($isCaseActivity) { | |
2276 | $allow = FALSE; | |
2277 | foreach ($compPermissions['CiviCase'] as $per) { | |
2278 | if (CRM_Core_Permission::check($per)) { | |
2279 | $allow = TRUE; | |
2280 | break; | |
2281 | } | |
2282 | } | |
2283 | ||
2284 | //check for case specific permissions. | |
2285 | if ($allow) { | |
2286 | $oper = 'view'; | |
2287 | if ($action == CRM_Core_Action::UPDATE) { | |
2288 | $oper = 'edit'; | |
2289 | } | |
2290 | $allow = CRM_Case_BAO_Case::checkPermission($activityId, | |
2291 | $oper, | |
2292 | $activity->activity_type_id | |
2293 | ); | |
2294 | } | |
2295 | ||
2296 | return $allow; | |
2297 | } | |
2298 | ||
6a488035 TO |
2299 | //first check the component permission. |
2300 | $sql = " | |
2301 | SELECT component_id | |
2302 | FROM civicrm_option_value val | |
2303 | INNER JOIN civicrm_option_group grp ON ( grp.id = val.option_group_id AND grp.name = %1 ) | |
2304 | WHERE val.value = %2"; | |
2305 | $params = array(1 => array('activity_type', 'String'), | |
2306 | 2 => array($activity->activity_type_id, 'Integer'), | |
2307 | ); | |
2308 | $componentId = CRM_Core_DAO::singleValueQuery($sql, $params); | |
2309 | ||
2310 | if ($componentId) { | |
2311 | $componentName = CRM_Core_Component::getComponentName($componentId); | |
2312 | $compPermission = CRM_Utils_Array::value($componentName, $compPermissions); | |
2313 | ||
2314 | //here we are interesting in any single permission. | |
2315 | if (is_array($compPermission)) { | |
2316 | foreach ($compPermission as $per) { | |
2317 | if (CRM_Core_Permission::check($per)) { | |
2318 | $allow = TRUE; | |
2319 | break; | |
2320 | } | |
2321 | } | |
2322 | } | |
2323 | } | |
2324 | ||
2325 | //check for this permission related to contact. | |
2326 | $permission = CRM_Core_Permission::VIEW; | |
2327 | if ($action == CRM_Core_Action::UPDATE) { | |
2328 | $permission = CRM_Core_Permission::EDIT; | |
2329 | } | |
2330 | ||
e7e657f0 | 2331 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
034500d4 | 2332 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
2333 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
2334 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
2335 | ||
6a488035 TO |
2336 | //check for source contact. |
2337 | if (!$componentId || $allow) { | |
65ebc887 | 2338 | $sourceContactId = self::getActivityContact($activity->id, $sourceID); |
32ecf7bb BS |
2339 | //account for possibility of activity not having a source contact (as it may have been deleted) |
2340 | if ( $sourceContactId ) { | |
2341 | $allow = CRM_Contact_BAO_Contact_Permission::allow($sourceContactId, $permission); | |
2342 | } | |
6a488035 TO |
2343 | } |
2344 | ||
2345 | //check for target and assignee contacts. | |
2346 | if ($allow) { | |
2347 | //first check for supper permission. | |
2348 | $supPermission = 'view all contacts'; | |
2349 | if ($action == CRM_Core_Action::UPDATE) { | |
2350 | $supPermission = 'edit all contacts'; | |
2351 | } | |
2352 | $allow = CRM_Core_Permission::check($supPermission); | |
2353 | ||
2354 | //user might have sufficient permission, through acls. | |
2355 | if (!$allow) { | |
2356 | $allow = TRUE; | |
2357 | //get the target contacts. | |
034500d4 | 2358 | $targetContacts = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $targetID); |
6a488035 TO |
2359 | foreach ($targetContacts as $cnt => $contactId) { |
2360 | if (!CRM_Contact_BAO_Contact_Permission::allow($contactId, $permission)) { | |
2361 | $allow = FALSE; | |
2362 | break; | |
2363 | } | |
2364 | } | |
2365 | ||
2366 | //get the assignee contacts. | |
2367 | if ($allow) { | |
d7f083ac | 2368 | $assigneeContacts = CRM_Activity_BAO_ActivityContact::retrieveContactIdsByActivityId($activity->id, $assigneeID); |
6a488035 TO |
2369 | foreach ($assigneeContacts as $cnt => $contactId) { |
2370 | if (!CRM_Contact_BAO_Contact_Permission::allow($contactId, $permission)) { | |
2371 | $allow = FALSE; | |
2372 | break; | |
2373 | } | |
2374 | } | |
2375 | } | |
2376 | } | |
2377 | } | |
2378 | ||
2379 | return $allow; | |
2380 | } | |
2381 | ||
2382 | /** | |
2383 | * This function is a wrapper for ajax activity selector | |
2384 | * | |
2385 | * @param array $params associated array for params record id. | |
2386 | * | |
2387 | * @return array $contactActivities associated array of contact activities | |
2388 | * @access public | |
2389 | */ | |
2390 | public static function getContactActivitySelector(&$params) { | |
2391 | // format the params | |
2392 | $params['offset'] = ($params['page'] - 1) * $params['rp']; | |
2393 | $params['rowCount'] = $params['rp']; | |
2394 | $params['sort'] = CRM_Utils_Array::value('sortBy', $params); | |
2395 | $params['caseId'] = NULL; | |
2396 | $context = CRM_Utils_Array::value('context', $params); | |
2397 | ||
2398 | // get contact activities | |
2399 | $activities = CRM_Activity_BAO_Activity::getActivities($params); | |
2400 | ||
2401 | // add total | |
2402 | $params['total'] = CRM_Activity_BAO_Activity::getActivitiesCount($params); | |
2403 | ||
2404 | // format params and add links | |
2405 | $contactActivities = array(); | |
2406 | ||
2407 | if (!empty($activities)) { | |
2408 | $activityStatus = CRM_Core_PseudoConstant::activityStatus(); | |
2409 | ||
2410 | // check logged in user for permission | |
2411 | $page = new CRM_Core_Page(); | |
2412 | CRM_Contact_Page_View::checkUserPermission($page, $params['contact_id']); | |
2413 | $permissions = array($page->_permission); | |
2414 | if (CRM_Core_Permission::check('delete activities')) { | |
2415 | $permissions[] = CRM_Core_Permission::DELETE; | |
2416 | } | |
2417 | ||
2418 | $mask = CRM_Core_Action::mask($permissions); | |
2419 | ||
2420 | foreach ($activities as $activityId => $values) { | |
2421 | $contactActivities[$activityId]['activity_type'] = $values['activity_type']; | |
2422 | $contactActivities[$activityId]['subject'] = $values['subject']; | |
2423 | if ($params['contact_id'] == $values['source_contact_id']) { | |
2424 | $contactActivities[$activityId]['source_contact'] = $values['source_contact_name']; | |
2425 | } | |
2426 | elseif ($values['source_contact_id']) { | |
5a99d240 KJ |
2427 | $contactActivities[$activityId]['source_contact'] = CRM_Utils_System::href($values['source_contact_name'], |
2428 | 'civicrm/contact/view', "reset=1&cid={$values['source_contact_id']}"); | |
6a488035 TO |
2429 | } |
2430 | else { | |
2431 | $contactActivities[$activityId]['source_contact'] = '<em>n/a</em>'; | |
2432 | } | |
2433 | ||
2434 | if (isset($values['mailingId']) && !empty($values['mailingId'])) { | |
5a99d240 KJ |
2435 | $contactActivities[$activityId]['target_contact'] = CRM_Utils_System::href($values['recipients'], |
2436 | 'civicrm/mailing/report/event', | |
2437 | "mid={$values['source_record_id']}&reset=1&event=queue&cid={$params['contact_id']}&context=activitySelector"); | |
6a488035 TO |
2438 | } |
2439 | elseif (CRM_Utils_Array::value('recipients', $values)) { | |
2440 | $contactActivities[$activityId]['target_contact'] = $values['recipients']; | |
2441 | } | |
2442 | elseif (!$values['target_contact_name']) { | |
2443 | $contactActivities[$activityId]['target_contact'] = '<em>n/a</em>'; | |
2444 | } | |
2445 | elseif (!empty($values['target_contact_name'])) { | |
2446 | $count = 0; | |
2447 | $contactActivities[$activityId]['target_contact'] = ''; | |
2448 | foreach ($values['target_contact_name'] as $tcID => $tcName) { | |
2449 | if ($tcID && $count < 5) { | |
5a99d240 KJ |
2450 | $contactActivities[$activityId]['target_contact'] .= CRM_Utils_System::href($tcName, |
2451 | 'civicrm/contact/view', "reset=1&cid={$tcID}"); | |
6a488035 TO |
2452 | $count++; |
2453 | if ($count) { | |
2454 | $contactActivities[$activityId]['target_contact'] .= "; "; | |
2455 | } | |
2456 | ||
2457 | if ($count == 4) { | |
2458 | $contactActivities[$activityId]['target_contact'] .= "(" . ts('more') . ")"; | |
2459 | break; | |
2460 | } | |
2461 | } | |
2462 | } | |
2463 | } | |
2464 | ||
2465 | if (empty($values['assignee_contact_name'])) { | |
2466 | $contactActivities[$activityId]['assignee_contact'] = '<em>n/a</em>'; | |
2467 | } | |
2468 | elseif (!empty($values['assignee_contact_name'])) { | |
2469 | $count = 0; | |
2470 | $contactActivities[$activityId]['assignee_contact'] = ''; | |
2471 | foreach ($values['assignee_contact_name'] as $acID => $acName) { | |
2472 | if ($acID && $count < 5) { | |
2473 | $contactActivities[$activityId]['assignee_contact'] .= CRM_Utils_System::href($acName, 'civicrm/contact/view', "reset=1&cid={$acID}"); | |
2474 | $count++; | |
2475 | if ($count) { | |
2476 | $contactActivities[$activityId]['assignee_contact'] .= "; "; | |
2477 | } | |
2478 | ||
2479 | if ($count == 4) { | |
2480 | $contactActivities[$activityId]['assignee_contact'] .= "(" . ts('more') . ")"; | |
2481 | break; | |
2482 | } | |
2483 | } | |
2484 | } | |
2485 | } | |
6a488035 TO |
2486 | |
2487 | $contactActivities[$activityId]['activity_date'] = CRM_Utils_Date::customFormat($values['activity_date_time']); | |
2488 | $contactActivities[$activityId]['status'] = $activityStatus[$values['status_id']]; | |
2489 | ||
2490 | // add class to this row if overdue | |
2491 | $contactActivities[$activityId]['class'] = ''; | |
2492 | if (CRM_Utils_Date::overdue(CRM_Utils_Array::value('activity_date_time', $values)) | |
2493 | && CRM_Utils_Array::value('status_id', $values) == 1 | |
2494 | ) { | |
2495 | $contactActivities[$activityId]['class'] = 'status-overdue'; | |
2496 | } | |
2497 | else { | |
2498 | $contactActivities[$activityId]['class'] = 'status-ontime'; | |
2499 | } | |
2500 | ||
2501 | // build links | |
2502 | $contactActivities[$activityId]['links'] = ''; | |
2503 | $accessMailingReport = FALSE; | |
2504 | if (CRM_Utils_Array::value('mailingId', $values)) { | |
2505 | $accessMailingReport = TRUE; | |
2506 | } | |
2507 | ||
2508 | $actionLinks = CRM_Activity_Selector_Activity::actionLinks( | |
2509 | CRM_Utils_Array::value('activity_type_id', $values), | |
2510 | CRM_Utils_Array::value('source_record_id', $values), | |
2511 | $accessMailingReport, | |
2512 | CRM_Utils_Array::value('activity_id', $values) | |
2513 | ); | |
2514 | ||
2515 | $actionMask = array_sum(array_keys($actionLinks)) & $mask; | |
2516 | ||
2517 | $contactActivities[$activityId]['links'] = CRM_Core_Action::formLink($actionLinks, | |
2518 | $actionMask, | |
2519 | array( | |
2520 | 'id' => $values['activity_id'], | |
2521 | 'cid' => $params['contact_id'], | |
2522 | 'cxt' => $context, | |
2523 | 'caseid' => CRM_Utils_Array::value('case_id', $values), | |
87dab4a4 AH |
2524 | ), |
2525 | ts('more'), | |
2526 | FALSE, | |
2527 | 'activity.tab.row', | |
2528 | 'Activity', | |
2529 | $values['activity_id'] | |
6a488035 TO |
2530 | ); |
2531 | } | |
2532 | } | |
2533 | ||
2534 | return $contactActivities; | |
2535 | } | |
2536 | ||
2537 | /* | |
2538 | * Used to copy custom fields and attachments from an existing activity to another. | |
2539 | * see CRM_Case_Page_AJAX::_convertToCaseActivity() for example | |
2540 | */ | |
2541 | static function copyExtendedActivityData($params) { | |
2542 | // attach custom data to the new activity | |
2543 | $customParams = $htmlType = array(); | |
2544 | $customValues = CRM_Core_BAO_CustomValueTable::getEntityValues($params['activityID'], 'Activity'); | |
2545 | ||
2546 | if (!empty($customValues)) { | |
2547 | $fieldIds = implode(', ', array_keys($customValues)); | |
2548 | $sql = "SELECT id FROM civicrm_custom_field WHERE html_type = 'File' AND id IN ( {$fieldIds} )"; | |
2549 | $result = CRM_Core_DAO::executeQuery($sql); | |
2550 | ||
2551 | while ($result->fetch()) { | |
2552 | $htmlType[] = $result->id; | |
2553 | } | |
2554 | ||
2555 | foreach ($customValues as $key => $value) { | |
2556 | if ($value !== NULL) { // CRM-10542 | |
2557 | if (in_array($key, $htmlType)) { | |
2558 | $fileValues = CRM_Core_BAO_File::path($value, $params['activityID']); | |
2559 | $customParams["custom_{$key}_-1"] = array( | |
2560 | 'name' => $fileValues[0], | |
2561 | 'path' => $fileValues[1], | |
2562 | ); | |
2563 | } | |
2564 | else { | |
2565 | $customParams["custom_{$key}_-1"] = $value; | |
2566 | } | |
2567 | } | |
2568 | } | |
2569 | CRM_Core_BAO_CustomValueTable::postProcess($customParams, CRM_Core_DAO::$_nullArray, 'civicrm_activity', | |
2570 | $params['mainActivityId'], 'Activity' | |
2571 | ); | |
2572 | } | |
2573 | ||
2574 | // copy activity attachments ( if any ) | |
2575 | CRM_Core_BAO_File::copyEntityFile('civicrm_activity', $params['activityID'], 'civicrm_activity', $params['mainActivityId']); | |
2576 | } | |
65ebc887 | 2577 | |
2578 | public static function getActivityContact($activityId, $recordTypeID = NULL, $column = 'contact_id') { | |
2579 | $activityContact = new CRM_Activity_BAO_ActivityContact(); | |
2580 | $activityContact->activity_id = $activityId; | |
2581 | if ($recordTypeID) { | |
2582 | $activityContact->record_type_id = $recordTypeID; | |
2583 | } | |
2584 | if ($activityContact->find(TRUE)) { | |
b319d00a | 2585 | return $activityContact->$column; |
65ebc887 | 2586 | } |
42d30b83 DL |
2587 | return NULL; |
2588 | } | |
2589 | ||
2590 | public static function getSourceContactID($activityId) { | |
2591 | static $sourceID = NULL; | |
2592 | if (!$sourceID) { | |
e7e657f0 | 2593 | $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); |
42d30b83 DL |
2594 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
2595 | } | |
2596 | ||
2597 | return self::getActivityContact($activityId, $sourceID); | |
65ebc887 | 2598 | } |
42d30b83 | 2599 | |
6a488035 TO |
2600 | } |
2601 |