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