Commit | Line | Data |
---|---|---|
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 | * | |
14 | * @param $params, array with keys: | |
15 | * - activity_id: int, required | |
16 | * - target_var: string, optional; name of a variable which will store the first/only target contact; default "target" | |
17 | * - assignee_var: string, optional; name of a variable which will store the first/only assignee contact; default "assignee" | |
18 | * - return: string, optional; comma-separated list of fields to return for each contact | |
19 | * | |
20 | * @return empty | |
21 | */ | |
22 | function smarty_function_simpleActivityContacts($params, &$smarty) { | |
23 | if (empty($params['activity_id'])) { | |
24 | $smarty->trigger_error('assign: missing \'activity_id\' parameter'); | |
25 | } | |
26 | if (!isset($params['target_var'])) { | |
27 | $params['target_var'] = 'target'; | |
28 | } | |
29 | if (!isset($params['assignee_var'])) { | |
30 | $params['assignee_var'] = 'assignee'; | |
31 | } | |
32 | if (!isset($params['return'])) { | |
33 | $params['return'] = 'contact_id,contact_type,display_name,sort_name,first_name,last_name'; | |
34 | } | |
35 | ||
36 | require_once 'api/api.php'; | |
37 | require_once 'api/v3/utils.php'; | |
38 | $activity = civicrm_api('activity', 'getsingle', array( | |
39 | 'version' => 3, | |
40 | 'id' => $params['activity_id'], | |
41 | 'return.target_contact_id' => 1, | |
42 | 'return.assignee_contact_id' => 1, | |
43 | )); | |
44 | ||
45 | $baseContactParams = array('version' => 3); | |
46 | foreach (explode(',', $params['return']) as $field) { | |
47 | $baseContactParams['return.' . $field] = 1; | |
48 | } | |
49 | ||
50 | foreach (array( | |
51 | 'target', 'assignee') as $role) { | |
52 | $contact = array(); | |
53 | if (!empty($activity[$role . '_contact_id'])) { | |
54 | $contact_id = array_shift($activity[$role . '_contact_id']); | |
55 | $contact = civicrm_api('contact', 'getsingle', $baseContactParams + array( | |
56 | 'contact_id' => $contact_id, | |
57 | )); | |
58 | } | |
59 | $smarty->assign($params[$role . '_var'], $contact); | |
60 | } | |
61 | ||
62 | return ''; | |
63 | } | |
64 |