Merge pull request #864 from eileenmcnaughton/CRM-12160
[civicrm-core.git] / api / v2 / EntityTag.php
CommitLineData
6a488035
TO
1<?php
2// $Id: EntityTag.php 45502 2013-02-08 13:32:55Z kurund $
3
4
5/*
6 +--------------------------------------------------------------------+
7 | CiviCRM version 4.3 |
8 +--------------------------------------------------------------------+
9 | Copyright CiviCRM LLC (c) 2004-2013 |
10 +--------------------------------------------------------------------+
11 | This file is a part of CiviCRM. |
12 | |
13 | CiviCRM is free software; you can copy, modify, and distribute it |
14 | under the terms of the GNU Affero General Public License |
15 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
16 | |
17 | CiviCRM is distributed in the hope that it will be useful, but |
18 | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
20 | See the GNU Affero General Public License for more details. |
21 | |
22 | You should have received a copy of the GNU Affero General Public |
23 | License and the CiviCRM Licensing Exception along |
24 | with this program; if not, contact CiviCRM LLC |
25 | at info[AT]civicrm[DOT]org. If you have questions about the |
26 | GNU Affero General Public License or the licensing of CiviCRM, |
27 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
28 +--------------------------------------------------------------------+
29*/
30
31/**
32 * File for the CiviCRM APIv2 entity tag functions
33 *
34 * @package CiviCRM_APIv2
35 * @subpackage API_EntityTag
36 *
37 * @copyright CiviCRM LLC (c) 2004-2013
38 * @version $Id: EntityTag.php 45502 2013-02-08 13:32:55Z kurund $
39 */
40
41/**
42 * Include utility functions
43 */
44require_once 'api/v2/utils.php';
45
46/**
47 *
48 * @param <type> $params
49 *
50 * @return <type>
51 */
52function civicrm_entity_tag_get(&$params) {
53 if (!is_array($params)) {
54 return civicrm_create_error(ts('params should be an array.'));
55 }
56
57 $entityID = NULL;
58 $entityTable = 'civicrm_contact';
59
60 if (!($entityID = CRM_Utils_Array::value('entity_id', $params))) {
61 $entityID = CRM_Utils_Array::value('contact_id', $params);
62 }
63
64 if (empty($entityID)) {
65 return civicrm_create_error(ts('entity_id is a required field.'));
66 }
67
68 if (CRM_Utils_Array::value('entity_table', $params)) {
69 $entityTable = $params['entity_table'];
70 }
71
72 require_once 'CRM/Core/BAO/EntityTag.php';
73 $values = CRM_Core_BAO_EntityTag::getTag($entityID, $entityTable);
74 $result = array();
75 foreach ($values as $v) {
76 $result[] = array('tag_id' => $v);
77 }
78 return $result;
79}
80
81/**
82 *
83 * @param <type> $params
84 *
85 * @return <type>
86 */
87function civicrm_entity_tag_display(&$params) {
88 if (!is_array($params)) {
89 return civicrm_create_error(ts('params should be an array.'));
90 }
91
92 $entityID = NULL;
93 $entityTable = 'civicrm_contact';
94
95 if (!($entityID = CRM_Utils_Array::value('entity_id', $params))) {
96 $entityID = CRM_Utils_Array::value('contact_id', $params);
97 }
98
99 if (empty($entityID)) {
100 return civicrm_create_error(ts('entity_id is a required field.'));
101 }
102
103 if (CRM_Utils_Array::value('entity_table', $params)) {
104 $entityTable = $params['entity_table'];
105 }
106
107 require_once 'CRM/Core/BAO/EntityTag.php';
108 $values = CRM_Core_BAO_EntityTag::getTag($entityID, $entityTable);
109 $result = array();
110 $tags = CRM_Core_PseudoConstant::tag();
111 foreach ($values as $v) {
112 $result[] = $tags[$v];
113 }
114 return implode(',', $result);
115}
116
117/**
118 * Returns all entities assigned to a specific Tag.
119 *
120 * @param $params Array an array valid Tag id
121 *
122 * @return $entities Array An array of entity ids.
123 * @access public
124 */
125function civicrm_tag_entities_get(&$params) {
126 require_once 'CRM/Core/BAO/Tag.php';
127 require_once 'CRM/Core/BAO/EntityTag.php';
128 $tag = new CRM_Core_BAO_Tag();
129 $tag->id = CRM_Utils_Array::value('tag_id', $params) ? $params['tag_id'] : NULL;
130 $entities = CRM_Core_BAO_EntityTag::getEntitiesByTag($tag);
131 return $entities;
132}
133
134/**
135 *
136 * @param <type> $params
137 *
138 * @return <type>
139 * @deprecated
140 */
141function civicrm_entity_tag_add(&$params) {
142 return civicrm_entity_tag_common($params, 'add');
143}
144
145/**
146 *
147 * @param <type> $params
148 *
149 * @return <type>
150 */
151function civicrm_entity_tag_create(&$params) {
152 return civicrm_entity_tag_common($params, 'add');
153}
154
155/**
156 *
157 * @param <type> $params
158 *
159 * @return <type>
160 * @deprecated
161 */
162function civicrm_entity_tag_remove(&$params) {
163 return civicrm_entity_tag_common($params, 'remove');
164}
165
166/**
167 *
168 * @param array $params
169 *
170 * @return <type>
171 */
172function civicrm_entity_tag_delete(&$params) {
173 return civicrm_entity_tag_common($params, 'remove');
174}
175
176/**
177 *
178 * @param <type> $params
179 * @param <type> $op
180 *
181 * @return <type>
182 */
183function civicrm_entity_tag_common(&$params, $op = 'add') {
184 $entityIDs = array();
185 $tagsIDs = array();
186 $entityTable = 'civicrm_contact';
187 if (is_array($params)) {
188 foreach ($params as $n => $v) {
189 if ((substr($n, 0, 10) == 'contact_id') || (substr($n, 0, 9) == 'entity_id')) {
190 $entityIDs[] = $v;
191 }
192 elseif (substr($n, 0, 6) == 'tag_id') {
193 $tagIDs[] = $v;
194 }
195 elseif (substr($n, 0, 12) == 'entity_table') {
196 $entityTable = $v;
197 }
198 }
199 }
200 if (empty($entityIDs)) {
201 return civicrm_create_error(ts('contact_id is a required field'));
202 }
203
204 if (empty($tagIDs)) {
205 return civicrm_create_error(ts('tag_id is a required field'));
206 }
207
208 require_once 'CRM/Core/BAO/EntityTag.php';
209 $values = array('is_error' => 0);
210 if ($op == 'add') {
211 $values['total_count'] = $values['added'] = $values['not_added'] = 0;
212 foreach ($tagIDs as $tagID) {
213 list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable);
214 $values['total_count'] += $te;
215 $values['added'] += $a;
216 $values['not_added'] += $na;
217 }
218 }
219 else {
220 $values['total_count'] = $values['removed'] = $values['not_removed'] = 0;
221 foreach ($tagIDs as $tagID) {
222 list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable);
223 $values['total_count'] += $te;
224 $values['removed'] += $r;
225 $values['not_removed'] += $nr;
226 }
227 }
228 return $values;
229}
230