5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * File for the CiviCRM APIv3 entity tag functions
33 * @package CiviCRM_APIv3
34 * @subpackage API_EntityTag
36 * @copyright CiviCRM LLC (c) 2004-2013
37 * @version $Id: EntityTag.php 30879 2010-11-22 15:45:55Z shot $
41 * Include utility functions
45 * {@getfields EntityTag_get}
46 * @example EntityTagGet.php
48 * @param array $params
52 function civicrm_api3_entity_tag_get($params) {
55 $values = CRM_Core_BAO_EntityTag
::getTag($params['entity_id'], $params['entity_table']);
57 foreach ($values as $v) {
58 $result[$v] = array('tag_id' => $v);
60 return civicrm_api3_create_success($result, $params);
64 * Adjust Metadata for Get action
66 * The metadata is used for setting defaults, documentation & validation
67 * @param array $params array or parameters determined by getfields
69 function _civicrm_api3_entity_tag_get_spec(&$params) {
70 $params['entity_id']['api.required'] = 1;
71 $params['entity_id']['api.aliases'] = array('contact_id');
72 $params['entity_table']['api.default'] = 'civicrm_contact';
77 * @param <type> $params
80 * @todo EM 7 Jan 2011 - believe this should be deleted
83 function civicrm_api3_entity_tag_display($params) {
85 civicrm_api3_verify_one_mandatory($params, NULL, array('entity_id', 'contact_id'));
88 $entityTable = 'civicrm_contact';
90 if (!($entityID = CRM_Utils_Array
::value('entity_id', $params))) {
91 $entityID = CRM_Utils_Array
::value('contact_id', $params);
95 if (CRM_Utils_Array
::value('entity_table', $params)) {
96 $entityTable = $params['entity_table'];
99 $values = CRM_Core_BAO_EntityTag
::getTag($entityID, $entityTable);
101 $tags = CRM_Core_PseudoConstant
::tag();
102 foreach ($values as $v) {
103 $result[] = $tags[$v];
105 return implode(',', $result);
110 * @param array $params
113 * {@getfields EntityTag_create}
114 * @example EntityTagCreate.php
116 function civicrm_api3_entity_tag_create($params) {
119 return _civicrm_api3_entity_tag_common($params, 'add');
123 *{@getfields EntityTag_delete}
124 * @example EntityTagGet.php
126 * @param array $params
130 function civicrm_api3_entity_tag_delete($params) {
132 return _civicrm_api3_entity_tag_common($params, 'remove');
138 function _civicrm_api3_entity_tag_delete_spec(&$params) {
139 // set as not required as tag_id also acceptable & no either/or std yet
140 $params['id']['api.required'] = 0;
145 * @param <type> $params
150 function _civicrm_api3_entity_tag_common($params, $op = 'add') {
152 $entityIDs = array();
154 $entityTable = 'civicrm_contact';
155 if (is_array($params)) {
156 foreach ($params as $n => $v) {
157 if ((substr($n, 0, 10) == 'contact_id') ||
(substr($n, 0, 9) == 'entity_id')) {
160 elseif (substr($n, 0, 6) == 'tag_id') {
163 elseif (substr($n, 0, 12) == 'entity_table') {
168 if (empty($entityIDs)) {
169 return civicrm_api3_create_error('contact_id is a required field');
172 if (empty($tagIDs)) {
173 return civicrm_api3_create_error('tag_id is a required field');
176 $values = array('is_error' => 0);
178 $values['total_count'] = $values['added'] = $values['not_added'] = 0;
179 foreach ($tagIDs as $tagID) {
180 list($te, $a, $na) = CRM_Core_BAO_EntityTag
::addEntitiesToTag($entityIDs, $tagID, $entityTable);
181 $values['total_count'] +
= $te;
182 $values['added'] +
= $a;
183 $values['not_added'] +
= $na;
187 $values['total_count'] = $values['removed'] = $values['not_removed'] = 0;
188 foreach ($tagIDs as $tagID) {
189 list($te, $r, $nr) = CRM_Core_BAO_EntityTag
::removeEntitiesFromTag($entityIDs, $tagID, $entityTable);
190 $values['total_count'] +
= $te;
191 $values['removed'] +
= $r;
192 $values['not_removed'] +
= $nr;