Merge pull request #15818 from colemanw/fields
[civicrm-core.git] / CRM / Core / Smarty / plugins / function.simpleActivityContacts.php
CommitLineData
6a488035
TO
1<?php
2
3/**
4 * Get details for the target and assignee contact of an activity.
5 *
6 * This is "simple" in that it is only appropriate for activities in which the business-process
7 * guarantees that there is only one target and one assignee. If the business-process permits
8 * multiple targets or multiple assignees, then consider the more versatile (but less sugary)
9 * function "crmAPI".
10 *
11 * Note: This will perform like a dog, but who cares -- at most, we deal with O(100) iterations
12 * as part of a background task.
13 *
16b10e64
CW
14 * @param array $params
15 * , Array with keys:
16 * - activity_id: int, required
17 * - target_var: string, optional; name of a variable which will store the first/only target contact; default "target"
18 * - assignee_var: string, optional; name of a variable which will store the first/only assignee contact; default "assignee"
19 * - return: string, optional; comma-separated list of fields to return for each contact
6a488035 20 *
16b10e64 21 * @param CRM_Core_Smarty $smarty
77b97be7 22 *
16b10e64 23 * @return string
6a488035
TO
24 */
25function smarty_function_simpleActivityContacts($params, &$smarty) {
26 if (empty($params['activity_id'])) {
27 $smarty->trigger_error('assign: missing \'activity_id\' parameter');
28 }
29 if (!isset($params['target_var'])) {
30 $params['target_var'] = 'target';
31 }
32 if (!isset($params['assignee_var'])) {
33 $params['assignee_var'] = 'assignee';
34 }
35 if (!isset($params['return'])) {
36 $params['return'] = 'contact_id,contact_type,display_name,sort_name,first_name,last_name';
37 }
38
39 require_once 'api/api.php';
40 require_once 'api/v3/utils.php';
be2fb01f 41 $activity = civicrm_api('activity', 'getsingle', [
353ffa53
TO
42 'version' => 3,
43 'id' => $params['activity_id'],
44 'return.target_contact_id' => 1,
45 'return.assignee_contact_id' => 1,
be2fb01f 46 ]);
6a488035 47
be2fb01f 48 $baseContactParams = ['version' => 3];
6a488035
TO
49 foreach (explode(',', $params['return']) as $field) {
50 $baseContactParams['return.' . $field] = 1;
51 }
52
be2fb01f 53 foreach ([
518fa0ee
SL
54 'target',
55 'assignee',
56 ] as $role) {
be2fb01f 57 $contact = [];
6a488035
TO
58 if (!empty($activity[$role . '_contact_id'])) {
59 $contact_id = array_shift($activity[$role . '_contact_id']);
be2fb01f 60 $contact = civicrm_api('contact', 'getsingle', $baseContactParams + [
518fa0ee
SL
61 'contact_id' => $contact_id,
62 ]);
6a488035
TO
63 }
64 $smarty->assign($params[$role . '_var'], $contact);
65 }
66
67 return '';
68}