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