Merge pull request #4772 from jitendrapurohit/CRM-15750
[civicrm-core.git] / CRM / Core / Smarty / plugins / function.simpleActivityContacts.php
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 * @param $smarty
21 *
22 * @return empty
23 */
24 function smarty_function_simpleActivityContacts($params, &$smarty) {
25 if (empty($params['activity_id'])) {
26 $smarty->trigger_error('assign: missing \'activity_id\' parameter');
27 }
28 if (!isset($params['target_var'])) {
29 $params['target_var'] = 'target';
30 }
31 if (!isset($params['assignee_var'])) {
32 $params['assignee_var'] = 'assignee';
33 }
34 if (!isset($params['return'])) {
35 $params['return'] = 'contact_id,contact_type,display_name,sort_name,first_name,last_name';
36 }
37
38 require_once 'api/api.php';
39 require_once 'api/v3/utils.php';
40 $activity = civicrm_api('activity', 'getsingle', array(
41 'version' => 3,
42 'id' => $params['activity_id'],
43 'return.target_contact_id' => 1,
44 'return.assignee_contact_id' => 1,
45 ));
46
47 $baseContactParams = array('version' => 3);
48 foreach (explode(',', $params['return']) as $field) {
49 $baseContactParams['return.' . $field] = 1;
50 }
51
52 foreach (array(
53 'target', 'assignee') as $role) {
54 $contact = array();
55 if (!empty($activity[$role . '_contact_id'])) {
56 $contact_id = array_shift($activity[$role . '_contact_id']);
57 $contact = civicrm_api('contact', 'getsingle', $baseContactParams + array(
58 'contact_id' => $contact_id,
59 ));
60 }
61 $smarty->assign($params[$role . '_var'], $contact);
62 }
63
64 return '';
65 }
66