Merge pull request #115 from dlobo/CRM-12088
[civicrm-core.git] / api / v3 / EntityTag.php
1 <?php
2 // $Id$
3
4 /*
5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
11 | |
12 | CiviCRM is free software; you can copy, modify, and distribute it |
13 | under the terms of the GNU Affero General Public License |
14 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | |
16 | CiviCRM is distributed in the hope that it will be useful, but |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
19 | See the GNU Affero General Public License for more details. |
20 | |
21 | You should have received a copy of the GNU Affero General Public |
22 | License and the CiviCRM Licensing Exception along |
23 | with this program; if not, contact CiviCRM LLC |
24 | at info[AT]civicrm[DOT]org. If you have questions about the |
25 | GNU Affero General Public License or the licensing of CiviCRM, |
26 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
27 +--------------------------------------------------------------------+
28 */
29
30 /**
31 * File for the CiviCRM APIv3 entity tag functions
32 *
33 * @package CiviCRM_APIv3
34 * @subpackage API_EntityTag
35 *
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: EntityTag.php 30879 2010-11-22 15:45:55Z shot $
38 */
39
40 /**
41 * Include utility functions
42 */
43
44 /**
45 * {@getfields EntityTag_get}
46 * @example EntityTagGet.php
47 *
48 * @param array $params
49 *
50 * @return array
51 */
52 function civicrm_api3_entity_tag_get($params) {
53
54
55 $values = CRM_Core_BAO_EntityTag::getTag($params['entity_id'], $params['entity_table']);
56 $result = array();
57 foreach ($values as $v) {
58 $result[$v] = array('tag_id' => $v);
59 }
60 return civicrm_api3_create_success($result, $params);
61 }
62 /*
63 * Adjust Metadata for Get action
64 *
65 * The metadata is used for setting defaults, documentation & validation
66 * @param array $params array or parameters determined by getfields
67 */
68 function _civicrm_api3_entity_tag_get_spec(&$params) {
69 $params['entity_id']['api.required'] = 1;
70 $params['entity_id']['api.aliases'] = array('contact_id');
71 $params['entity_table']['api.default'] = 'civicrm_contact';
72 }
73
74 /**
75 *
76 * @param <type> $params
77 *
78 * @return <type>
79 * @todo EM 7 Jan 2011 - believe this should be deleted
80 * @deprecated
81 */
82 function civicrm_api3_entity_tag_display($params) {
83
84 civicrm_api3_verify_one_mandatory($params, NULL, array('entity_id', 'contact_id'));
85
86 $entityID = NULL;
87 $entityTable = 'civicrm_contact';
88
89 if (!($entityID = CRM_Utils_Array::value('entity_id', $params))) {
90 $entityID = CRM_Utils_Array::value('contact_id', $params);
91 }
92
93
94 if (CRM_Utils_Array::value('entity_table', $params)) {
95 $entityTable = $params['entity_table'];
96 }
97
98 $values = CRM_Core_BAO_EntityTag::getTag($entityID, $entityTable);
99 $result = array();
100 $tags = CRM_Core_PseudoConstant::tag();
101 foreach ($values as $v) {
102 $result[] = $tags[$v];
103 }
104 return implode(',', $result);
105 }
106
107 /**
108 *
109 * @param array $params
110 *
111 * @return array
112 * {@getfields EntityTag_create}
113 * @example EntityTagCreate.php
114 */
115 function civicrm_api3_entity_tag_create($params) {
116
117
118 return _civicrm_api3_entity_tag_common($params, 'add');
119 }
120
121 /**
122 *{@getfields EntityTag_delete}
123 * @example EntityTagGet.php
124 *
125 * @param array $params
126 *
127 * @return array
128 */
129 function civicrm_api3_entity_tag_delete($params) {
130
131 return _civicrm_api3_entity_tag_common($params, 'remove');
132 }
133 /*
134 * modify metadata
135 */
136 function _civicrm_api3_entity_tag_delete_spec(&$params) {
137 // set as not required as tag_id also acceptable & no either/or std yet
138 $params['id']['api.required'] = 0;
139 }
140
141 /**
142 *
143 * @param <type> $params
144 * @param <type> $op
145 *
146 * @return <type>
147 */
148 function _civicrm_api3_entity_tag_common($params, $op = 'add') {
149
150 $entityIDs = array();
151 $tagsIDs = array();
152 $entityTable = 'civicrm_contact';
153 if (is_array($params)) {
154 foreach ($params as $n => $v) {
155 if ((substr($n, 0, 10) == 'contact_id') || (substr($n, 0, 9) == 'entity_id')) {
156 $entityIDs[] = $v;
157 }
158 elseif (substr($n, 0, 6) == 'tag_id') {
159 $tagIDs[] = $v;
160 }
161 elseif (substr($n, 0, 12) == 'entity_table') {
162 $entityTable = $v;
163 }
164 }
165 }
166 if (empty($entityIDs)) {
167 return civicrm_api3_create_error('contact_id is a required field');
168 }
169
170 if (empty($tagIDs)) {
171 return civicrm_api3_create_error('tag_id is a required field');
172 }
173
174 $values = array('is_error' => 0);
175 if ($op == 'add') {
176 $values['total_count'] = $values['added'] = $values['not_added'] = 0;
177 foreach ($tagIDs as $tagID) {
178 list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable);
179 $values['total_count'] += $te;
180 $values['added'] += $a;
181 $values['not_added'] += $na;
182 }
183 }
184 else {
185 $values['total_count'] = $values['removed'] = $values['not_removed'] = 0;
186 foreach ($tagIDs as $tagID) {
187 list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable);
188 $values['total_count'] += $te;
189 $values['removed'] += $r;
190 $values['not_removed'] += $nr;
191 }
192 }
193 return $values;
194 }
195