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