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