CRM-16173 - WhitelistSubscriber - Filtering for API requests (entity/action/params)
[civicrm-core.git] / api / v3 / EntityTag.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 */
44function civicrm_api3_entity_tag_get($params) {
45
22e263ad 46 if (empty($params['entity_id'])) {
283c85e9 47 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
48 }
92e4c2a5 49 else {
283c85e9 50 //do legacy non-standard behaviour
51 $values = CRM_Core_BAO_EntityTag::getTag($params['entity_id'], $params['entity_table']);
52 $result = array();
53 foreach ($values as $v) {
54 $result[$v] = array('tag_id' => $v);
55 }
244bbdd8 56 return civicrm_api3_create_success($result, $params, 'EntityTag');
6a488035 57 }
6a488035 58}
11e09c59
TO
59
60/**
0965e988
EM
61 * Adjust Metadata for Get action.
62 *
63 * The metadata is used for setting defaults, documentation & validation.
6a488035 64 *
cf470720 65 * @param array $params
b081365f 66 * Array of parameters determined by getfields.
6a488035
TO
67 */
68function _civicrm_api3_entity_tag_get_spec(&$params) {
6a488035
TO
69 $params['entity_id']['api.aliases'] = array('contact_id');
70 $params['entity_table']['api.default'] = 'civicrm_contact';
71}
72
6a488035 73/**
0965e988 74 * Create an entity tag.
6a488035
TO
75 *
76 * @param array $params
77 *
78 * @return array
6a488035
TO
79 */
80function civicrm_api3_entity_tag_create($params) {
6a488035
TO
81 return _civicrm_api3_entity_tag_common($params, 'add');
82}
83
84/**
0965e988 85 * Mark entity tag as removed.
6a488035
TO
86 *
87 * @param array $params
88 *
89 * @return array
90 */
91function civicrm_api3_entity_tag_delete($params) {
92
93 return _civicrm_api3_entity_tag_common($params, 'remove');
94}
11e09c59
TO
95
96/**
0965e988
EM
97 * Modify metadata.
98 *
d0997921 99 * @param array $params
6a488035
TO
100 */
101function _civicrm_api3_entity_tag_delete_spec(&$params) {
102 // set as not required as tag_id also acceptable & no either/or std yet
103 $params['id']['api.required'] = 0;
104}
105
106/**
0965e988 107 * Helper function for formatting tags (part of api v2 legacy).
6a488035 108 *
72b3a70c 109 * @param array $params
77b97be7 110 * @param string $op
6a488035 111 *
a6c01b45 112 * @return array
6a488035
TO
113 */
114function _civicrm_api3_entity_tag_common($params, $op = 'add') {
115
116 $entityIDs = array();
6a488035
TO
117 $entityTable = 'civicrm_contact';
118 if (is_array($params)) {
119 foreach ($params as $n => $v) {
120 if ((substr($n, 0, 10) == 'contact_id') || (substr($n, 0, 9) == 'entity_id')) {
121 $entityIDs[] = $v;
122 }
123 elseif (substr($n, 0, 6) == 'tag_id') {
124 $tagIDs[] = $v;
125 }
126 elseif (substr($n, 0, 12) == 'entity_table') {
127 $entityTable = $v;
128 }
129 }
130 }
131 if (empty($entityIDs)) {
132 return civicrm_api3_create_error('contact_id is a required field');
133 }
134
135 if (empty($tagIDs)) {
136 return civicrm_api3_create_error('tag_id is a required field');
137 }
138
139 $values = array('is_error' => 0);
140 if ($op == 'add') {
141 $values['total_count'] = $values['added'] = $values['not_added'] = 0;
142 foreach ($tagIDs as $tagID) {
143 list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable);
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) {
152 list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable);
153 $values['total_count'] += $te;
154 $values['removed'] += $r;
155 $values['not_removed'] += $nr;
156 }
157 }
158 return $values;
159}