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