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