Cleanup api3 docblocks
[civicrm-core.git] / api / v3 / EntityTag.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
c28e1768 29 * This api exposes CiviCRM entity tag.
6a488035
TO
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_EntityTag
33 *
6a488035
TO
34 */
35
36/**
37 * Include utility functions
38 */
39
40/**
0965e988 41 * Get entity tags.
6a488035
TO
42 *
43 * @param array $params
44 *
45 * @return array
46 */
47function civicrm_api3_entity_tag_get($params) {
48
22e263ad 49 if (empty($params['entity_id'])) {
283c85e9 50 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
51 }
92e4c2a5 52 else {
283c85e9 53 //do legacy non-standard behaviour
54 $values = CRM_Core_BAO_EntityTag::getTag($params['entity_id'], $params['entity_table']);
55 $result = array();
56 foreach ($values as $v) {
57 $result[$v] = array('tag_id' => $v);
58 }
59 return civicrm_api3_create_success($result, $params);
6a488035 60 }
6a488035 61}
11e09c59
TO
62
63/**
0965e988
EM
64 * Adjust Metadata for Get action.
65 *
66 * The metadata is used for setting defaults, documentation & validation.
6a488035 67 *
cf470720
TO
68 * @param array $params
69 * Array or parameters determined by getfields.
6a488035
TO
70 */
71function _civicrm_api3_entity_tag_get_spec(&$params) {
6a488035
TO
72 $params['entity_id']['api.aliases'] = array('contact_id');
73 $params['entity_table']['api.default'] = 'civicrm_contact';
74}
75
6a488035 76/**
0965e988 77 * Create an entity tag.
6a488035
TO
78 *
79 * @param array $params
80 *
81 * @return array
6a488035
TO
82 */
83function civicrm_api3_entity_tag_create($params) {
6a488035
TO
84 return _civicrm_api3_entity_tag_common($params, 'add');
85}
86
87/**
0965e988 88 * Mark entity tag as removed.
6a488035
TO
89 *
90 * @param array $params
91 *
92 * @return array
93 */
94function civicrm_api3_entity_tag_delete($params) {
95
96 return _civicrm_api3_entity_tag_common($params, 'remove');
97}
11e09c59
TO
98
99/**
0965e988
EM
100 * Modify metadata.
101 *
d0997921 102 * @param array $params
6a488035
TO
103 */
104function _civicrm_api3_entity_tag_delete_spec(&$params) {
105 // set as not required as tag_id also acceptable & no either/or std yet
106 $params['id']['api.required'] = 0;
107}
108
109/**
0965e988 110 * Helper function for formatting tags (part of api v2 legacy).
6a488035 111 *
72b3a70c 112 * @param array $params
77b97be7 113 * @param string $op
6a488035 114 *
a6c01b45 115 * @return array
6a488035
TO
116 */
117function _civicrm_api3_entity_tag_common($params, $op = 'add') {
118
119 $entityIDs = array();
6a488035
TO
120 $entityTable = 'civicrm_contact';
121 if (is_array($params)) {
122 foreach ($params as $n => $v) {
123 if ((substr($n, 0, 10) == 'contact_id') || (substr($n, 0, 9) == 'entity_id')) {
124 $entityIDs[] = $v;
125 }
126 elseif (substr($n, 0, 6) == 'tag_id') {
127 $tagIDs[] = $v;
128 }
129 elseif (substr($n, 0, 12) == 'entity_table') {
130 $entityTable = $v;
131 }
132 }
133 }
134 if (empty($entityIDs)) {
135 return civicrm_api3_create_error('contact_id is a required field');
136 }
137
138 if (empty($tagIDs)) {
139 return civicrm_api3_create_error('tag_id is a required field');
140 }
141
142 $values = array('is_error' => 0);
143 if ($op == 'add') {
144 $values['total_count'] = $values['added'] = $values['not_added'] = 0;
145 foreach ($tagIDs as $tagID) {
146 list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable);
147 $values['total_count'] += $te;
148 $values['added'] += $a;
149 $values['not_added'] += $na;
150 }
151 }
152 else {
153 $values['total_count'] = $values['removed'] = $values['not_removed'] = 0;
154 foreach ($tagIDs as $tagID) {
155 list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable);
156 $values['total_count'] += $te;
157 $values['removed'] += $r;
158 $values['not_removed'] += $nr;
159 }
160 }
161 return $values;
162}