Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
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. | | |
15 | | | | |
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. | | |
20 | | | | |
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 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | /** | |
31 | * File for the CiviCRM APIv3 entity tag functions | |
32 | * | |
33 | * @package CiviCRM_APIv3 | |
34 | * @subpackage API_EntityTag | |
35 | * | |
36 | * @copyright CiviCRM LLC (c) 2004-2013 | |
37 | * @version $Id: EntityTag.php 30879 2010-11-22 15:45:55Z shot $ | |
38 | */ | |
39 | ||
40 | /** | |
41 | * Include utility functions | |
42 | */ | |
43 | ||
44 | /** | |
45 | * {@getfields EntityTag_get} | |
46 | * @example EntityTagGet.php | |
47 | * | |
48 | * @param array $params | |
49 | * | |
50 | * @return array | |
51 | */ | |
52 | function civicrm_api3_entity_tag_get($params) { | |
53 | ||
54 | ||
55 | $values = CRM_Core_BAO_EntityTag::getTag($params['entity_id'], $params['entity_table']); | |
56 | $result = array(); | |
57 | foreach ($values as $v) { | |
58 | $result[$v] = array('tag_id' => $v); | |
59 | } | |
60 | return civicrm_api3_create_success($result, $params); | |
61 | } | |
11e09c59 TO |
62 | |
63 | /** | |
6a488035 TO |
64 | * Adjust Metadata for Get action |
65 | * | |
66 | * The metadata is used for setting defaults, documentation & validation | |
67 | * @param array $params array or parameters determined by getfields | |
68 | */ | |
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'; | |
73 | } | |
74 | ||
75 | /** | |
76 | * | |
77 | * @param <type> $params | |
78 | * | |
79 | * @return <type> | |
80 | * @todo EM 7 Jan 2011 - believe this should be deleted | |
81 | * @deprecated | |
82 | */ | |
83 | function civicrm_api3_entity_tag_display($params) { | |
84 | ||
85 | civicrm_api3_verify_one_mandatory($params, NULL, array('entity_id', 'contact_id')); | |
86 | ||
87 | $entityID = NULL; | |
88 | $entityTable = 'civicrm_contact'; | |
89 | ||
90 | if (!($entityID = CRM_Utils_Array::value('entity_id', $params))) { | |
91 | $entityID = CRM_Utils_Array::value('contact_id', $params); | |
92 | } | |
93 | ||
94 | ||
95 | if (CRM_Utils_Array::value('entity_table', $params)) { | |
96 | $entityTable = $params['entity_table']; | |
97 | } | |
98 | ||
99 | $values = CRM_Core_BAO_EntityTag::getTag($entityID, $entityTable); | |
100 | $result = array(); | |
101 | $tags = CRM_Core_PseudoConstant::tag(); | |
102 | foreach ($values as $v) { | |
103 | $result[] = $tags[$v]; | |
104 | } | |
105 | return implode(',', $result); | |
106 | } | |
107 | ||
108 | /** | |
109 | * | |
110 | * @param array $params | |
111 | * | |
112 | * @return array | |
113 | * {@getfields EntityTag_create} | |
114 | * @example EntityTagCreate.php | |
115 | */ | |
116 | function civicrm_api3_entity_tag_create($params) { | |
117 | ||
118 | ||
119 | return _civicrm_api3_entity_tag_common($params, 'add'); | |
120 | } | |
121 | ||
122 | /** | |
123 | *{@getfields EntityTag_delete} | |
124 | * @example EntityTagGet.php | |
125 | * | |
126 | * @param array $params | |
127 | * | |
128 | * @return array | |
129 | */ | |
130 | function civicrm_api3_entity_tag_delete($params) { | |
131 | ||
132 | return _civicrm_api3_entity_tag_common($params, 'remove'); | |
133 | } | |
11e09c59 TO |
134 | |
135 | /** | |
6a488035 TO |
136 | * modify metadata |
137 | */ | |
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; | |
141 | } | |
142 | ||
143 | /** | |
144 | * | |
145 | * @param <type> $params | |
146 | * @param <type> $op | |
147 | * | |
148 | * @return <type> | |
149 | */ | |
150 | function _civicrm_api3_entity_tag_common($params, $op = 'add') { | |
151 | ||
152 | $entityIDs = array(); | |
153 | $tagsIDs = 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')) { | |
158 | $entityIDs[] = $v; | |
159 | } | |
160 | elseif (substr($n, 0, 6) == 'tag_id') { | |
161 | $tagIDs[] = $v; | |
162 | } | |
163 | elseif (substr($n, 0, 12) == 'entity_table') { | |
164 | $entityTable = $v; | |
165 | } | |
166 | } | |
167 | } | |
168 | if (empty($entityIDs)) { | |
169 | return civicrm_api3_create_error('contact_id is a required field'); | |
170 | } | |
171 | ||
172 | if (empty($tagIDs)) { | |
173 | return civicrm_api3_create_error('tag_id is a required field'); | |
174 | } | |
175 | ||
176 | $values = array('is_error' => 0); | |
177 | if ($op == 'add') { | |
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; | |
184 | } | |
185 | } | |
186 | else { | |
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; | |
193 | } | |
194 | } | |
195 | return $values; | |
196 | } | |
197 |