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