Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
a30c801b | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
a30c801b TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
244bbdd8 CW |
13 | * This api exposes CiviCRM EntityTag records. |
14 | * | |
15 | * Use this api to add/remove tags from a contact/activity/etc. | |
16 | * To create/update/delete the tags themselves, use the Tag api. | |
6a488035 TO |
17 | * |
18 | * @package CiviCRM_APIv3 | |
6a488035 TO |
19 | */ |
20 | ||
6a488035 | 21 | /** |
0965e988 | 22 | * Get entity tags. |
6a488035 TO |
23 | * |
24 | * @param array $params | |
25 | * | |
26 | * @return array | |
27 | */ | |
28 | function civicrm_api3_entity_tag_get($params) { | |
c6835264 | 29 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
6a488035 | 30 | } |
11e09c59 TO |
31 | |
32 | /** | |
0965e988 EM |
33 | * Adjust Metadata for Get action. |
34 | * | |
35 | * The metadata is used for setting defaults, documentation & validation. | |
6a488035 | 36 | * |
cf470720 | 37 | * @param array $params |
b081365f | 38 | * Array of parameters determined by getfields. |
6a488035 TO |
39 | */ |
40 | function _civicrm_api3_entity_tag_get_spec(&$params) { | |
cf8f0fff | 41 | $params['entity_id']['api.aliases'] = ['contact_id']; |
6a488035 TO |
42 | $params['entity_table']['api.default'] = 'civicrm_contact'; |
43 | } | |
44 | ||
6a488035 | 45 | /** |
0965e988 | 46 | * Create an entity tag. |
6a488035 TO |
47 | * |
48 | * @param array $params | |
49 | * | |
50 | * @return array | |
6a488035 TO |
51 | */ |
52 | function civicrm_api3_entity_tag_create($params) { | |
6a488035 TO |
53 | return _civicrm_api3_entity_tag_common($params, 'add'); |
54 | } | |
55 | ||
56 | /** | |
0965e988 | 57 | * Mark entity tag as removed. |
6a488035 TO |
58 | * |
59 | * @param array $params | |
60 | * | |
61 | * @return array | |
62 | */ | |
63 | function civicrm_api3_entity_tag_delete($params) { | |
64 | ||
65 | return _civicrm_api3_entity_tag_common($params, 'remove'); | |
66 | } | |
11e09c59 TO |
67 | |
68 | /** | |
0965e988 EM |
69 | * Modify metadata. |
70 | * | |
d0997921 | 71 | * @param array $params |
6a488035 TO |
72 | */ |
73 | function _civicrm_api3_entity_tag_delete_spec(&$params) { | |
74 | // set as not required as tag_id also acceptable & no either/or std yet | |
75 | $params['id']['api.required'] = 0; | |
76 | } | |
77 | ||
78 | /** | |
0965e988 | 79 | * Helper function for formatting tags (part of api v2 legacy). |
6a488035 | 80 | * |
72b3a70c | 81 | * @param array $params |
77b97be7 | 82 | * @param string $op |
6a488035 | 83 | * |
a6c01b45 | 84 | * @return array |
6a488035 TO |
85 | */ |
86 | function _civicrm_api3_entity_tag_common($params, $op = 'add') { | |
87 | ||
cf8f0fff | 88 | $entityIDs = $tagIDs = []; |
6a488035 TO |
89 | $entityTable = 'civicrm_contact'; |
90 | if (is_array($params)) { | |
91 | foreach ($params as $n => $v) { | |
92 | if ((substr($n, 0, 10) == 'contact_id') || (substr($n, 0, 9) == 'entity_id')) { | |
93 | $entityIDs[] = $v; | |
94 | } | |
95 | elseif (substr($n, 0, 6) == 'tag_id') { | |
d9bbb948 CW |
96 | if (is_array($v)) { |
97 | $tagIDs = array_merge($tagIDs, $v); | |
98 | } | |
99 | else { | |
100 | $tagIDs[] = $v; | |
101 | } | |
6a488035 TO |
102 | } |
103 | elseif (substr($n, 0, 12) == 'entity_table') { | |
104 | $entityTable = $v; | |
105 | } | |
106 | } | |
107 | } | |
424616b8 | 108 | |
6a488035 TO |
109 | if (empty($entityIDs)) { |
110 | return civicrm_api3_create_error('contact_id is a required field'); | |
111 | } | |
112 | ||
113 | if (empty($tagIDs)) { | |
bff5783c WDC |
114 | if ($op == 'remove') { |
115 | $tagIDs = array_keys(CRM_Core_BAO_EntityTag::getContactTags($entityIDs[0])); | |
116 | } | |
117 | else { | |
118 | return civicrm_api3_create_error('tag_id is a required field'); | |
119 | } | |
6a488035 TO |
120 | } |
121 | ||
cf8f0fff | 122 | $values = ['is_error' => 0]; |
6a488035 TO |
123 | if ($op == 'add') { |
124 | $values['total_count'] = $values['added'] = $values['not_added'] = 0; | |
125 | foreach ($tagIDs as $tagID) { | |
424616b8 | 126 | list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable, |
127 | CRM_Utils_Array::value('check_permissions', $params)); | |
6a488035 TO |
128 | $values['total_count'] += $te; |
129 | $values['added'] += $a; | |
130 | $values['not_added'] += $na; | |
131 | } | |
132 | } | |
133 | else { | |
134 | $values['total_count'] = $values['removed'] = $values['not_removed'] = 0; | |
135 | foreach ($tagIDs as $tagID) { | |
424616b8 | 136 | list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable, CRM_Utils_Array::value('check_permissions', $params)); |
6a488035 TO |
137 | $values['total_count'] += $te; |
138 | $values['removed'] += $r; | |
139 | $values['not_removed'] += $nr; | |
140 | } | |
141 | } | |
c6835264 CW |
142 | if (empty($values['added']) && empty($values['removed'])) { |
143 | $values['is_error'] = 1; | |
144 | $values['error_message'] = "Unable to $op tags"; | |
145 | } | |
6a488035 TO |
146 | return $values; |
147 | } | |
d9bbb948 CW |
148 | |
149 | /** | |
150 | * Replace tags for an entity | |
151 | */ | |
152 | function civicrm_api3_entity_tag_replace($params) { | |
153 | $transaction = new CRM_Core_Transaction(); | |
154 | try { | |
d9bbb948 CW |
155 | |
156 | $baseParams = _civicrm_api3_generic_replace_base_params($params); | |
157 | unset($baseParams['tag_id']); | |
158 | ||
159 | // Lookup pre-existing records | |
160 | $preexisting = civicrm_api3('entity_tag', 'get', $baseParams); | |
161 | $preexisting = array_column($preexisting['values'], 'tag_id'); | |
162 | $toAdd = isset($params['tag_id']) ? $params['tag_id'] : array_column($params['values'], 'tag_id'); | |
163 | $toRemove = array_diff($preexisting, $toAdd); | |
164 | ||
165 | $result = []; | |
166 | if ($toAdd) { | |
167 | $result = _civicrm_api3_entity_tag_common(array_merge($baseParams, ['tag_id' => $toAdd]), 'add'); | |
168 | } | |
169 | if ($toRemove) { | |
170 | $result += _civicrm_api3_entity_tag_common(array_merge($baseParams, ['tag_id' => $toRemove]), 'remove'); | |
171 | } | |
172 | // Not really errors | |
173 | unset($result['is_error'], $result['error_message']); | |
174 | ||
175 | return civicrm_api3_create_success($result, $params, 'EntityTag', 'replace'); | |
176 | } | |
7c31ae57 | 177 | catch (Exception $e) { |
d9bbb948 CW |
178 | $transaction->rollback(); |
179 | return civicrm_api3_create_error($e->getMessage()); | |
180 | } | |
181 | } |