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