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);
63 * Adjust Metadata for Get action
65 * The metadata is used for setting defaults, documentation & validation
66 * @param array $params array or parameters determined by getfields
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';
76 * @param <type> $params
79 * @todo EM 7 Jan 2011 - believe this should be deleted
82 function civicrm_api3_entity_tag_display($params) {
84 civicrm_api3_verify_one_mandatory($params, NULL, array('entity_id', 'contact_id'));
87 $entityTable = 'civicrm_contact';
89 if (!($entityID = CRM_Utils_Array
::value('entity_id', $params))) {
90 $entityID = CRM_Utils_Array
::value('contact_id', $params);
94 if (CRM_Utils_Array
::value('entity_table', $params)) {
95 $entityTable = $params['entity_table'];
98 $values = CRM_Core_BAO_EntityTag
::getTag($entityID, $entityTable);
100 $tags = CRM_Core_PseudoConstant
::tag();
101 foreach ($values as $v) {
102 $result[] = $tags[$v];
104 return implode(',', $result);
109 * @param array $params
112 * {@getfields EntityTag_create}
113 * @example EntityTagCreate.php
115 function civicrm_api3_entity_tag_create($params) {
118 return _civicrm_api3_entity_tag_common($params, 'add');
122 *{@getfields EntityTag_delete}
123 * @example EntityTagGet.php
125 * @param array $params
129 function civicrm_api3_entity_tag_delete($params) {
131 return _civicrm_api3_entity_tag_common($params, 'remove');
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;
143 * @param <type> $params
148 function _civicrm_api3_entity_tag_common($params, $op = 'add') {
150 $entityIDs = 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')) {
158 elseif (substr($n, 0, 6) == 'tag_id') {
161 elseif (substr($n, 0, 12) == 'entity_table') {
166 if (empty($entityIDs)) {
167 return civicrm_api3_create_error('contact_id is a required field');
170 if (empty($tagIDs)) {
171 return civicrm_api3_create_error('tag_id is a required field');
174 $values = array('is_error' => 0);
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;
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;