/
Tag.php
239 lines (202 loc) · 7.17 KB
/
Tag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 3.1 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2009 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License along with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* Definition of the Tag of the CRM API.
* More detailed documentation can be found
* {@link http://objectledge.org/confluence/display/CRM/CRM+v1.0+Public+APIs
* here}
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2009
* $Id$
*
*/
/**
* Files required for this package
*/
require_once 'api/utils.php';
/**
* Most API functions take in associative arrays ( name => value pairs
* as parameters. Some of the most commonly used parameters are
* described below
*
* @param array $params an associative array used in construction
/ retrieval of the object
* @param array $returnProperties the limited set of object properties that
* need to be returned to the caller
*
*/
/**
* Add a Tag. Tags are used to classify CRM entities (including Contacts, Groups and Actions).
*
* @param array $params an associative array used in construction / retrieval of the object
* @return $tag object a new tag object
* @access public
*/
function crm_create_tag($params)
{
require_once 'CRM/Core/BAO/Tag.php';
$error = _crm_check_required_fields($params, 'CRM_Core_DAO_Tag');
if (is_a($error, 'CRM_Core_Error')) {
return $error;
}
$ids = array();
return CRM_Core_BAO_Tag::add($params, $ids);
}
/**
* Get a Tag.
*
* This api is used for finding an existing tag.
* Either id or name of tag are required parameters for this api.
*
* @param array $params an associative array of name/value pairs.
*
* @return $tagBAO object of CRM_Core_BAO_Tag
* @access public
*/
function crm_get_tag($params)
{
require_once 'CRM/Core/BAO/Tag.php';
$tagBAO =& new CRM_Core_BAO_Tag();
if ( ! is_array($params) ) {
return _crm_error('params is not an array.');
}
if ( ! isset($params['id']) && ! isset($params['name']) ) {
return _crm_error('Required parameters missing.');
}
$properties = array('id', 'name', 'description', 'parent_id');
foreach ( $properties as $name) {
if (array_key_exists($name, $params)) {
$tagBAO->$name = $params[$name];
}
}
if ( ! $tagBAO->find(true) ) {
return _crm_error('Exact match not found.');
}
return $tagBAO;
}
/**
* Deletes an existing Tag
*
* @param object $tag valid tag object
* @return NULL | error if delete successfull then NULL otherwise object of CRM_Core_Error
* @access public
*/
function crm_delete_tag(&$tag)
{
require_once 'CRM/Core/BAO/Tag.php';
if ( ! isset($tag->id) ) {
return _crm_error('Invalid Tag object passed in');
}
if (CRM_Core_BAO_Tag::del($tag->id)) {
return null;
} else {
return _crm_error('Error while deleting Tag object');
}
}
/**
* Assigns an entity (e.g. Individual, Organization, Group, Contact_action) to a Tag (i.e. 'tags' that entity).
*
* @param $tag object valid tag object
* @param $entity object valid entity object
*
* @return $entityTag object A new Entity tag object
* @access public
*/
function crm_create_entity_tag(&$tag, &$entity)
{
require_once 'CRM/Core/BAO/EntityTag.php';
if ( ! isset($tag->id) || ! isset($entity->id)) {
return _crm_error('Required parameters missing');
}
$params = array ('tag_id' => $tag->id,
'contact_id' => $entity->id,
'entity_table' => 'civicrm_contact'
);
return CRM_Core_BAO_EntityTag::add($params);
}
/**
* Returns all entities assigned to a specific Tag. Optionally filtered by entity_type.
*
* @param $tag object Valid Tag object.
* @param $entity_type enum Optional filter for type of entity being queried. Valid values: 'Individual', 'Organization', 'Household', 'Group', 'Contact_action'.
*
* @return $entities Array An array of entity objects (Individuals and/or Organizations and/or etc.).
* @access public
*/
function crm_get_entities_by_tag(&$tag, $entity_type = null)
{
require_once 'CRM/Core/BAO/EntityTag.php';
if ( ! isset($tag->id) ) {
return _crm_error('Invalid tag object passed in');
}
$contactIDs=& CRM_Core_BAO_EntityTag::getEntitiesByTag($tag);
$entities = array();
foreach($contactIDs as $Id) {
$params = array('contact_id' => $Id );
if($entity_type != null) {
$temp = clone(crm_get_contact($params));
if($entity_type == $temp->contact_type)
$entities[] = $temp;
} else {
$entities[] =clone(crm_get_contact($params));
}
}
return $entities;
}
/**
* Returns all Tags assigned to a single entity instance. For example, you can use this API to find out what tag(s) have been assigned to a particular organization.
*
* @param $entity object Valid object of one of the supported entity types.
*
* @return array An array of Tag objects.
* @access public
*/
function crm_tags_by_entity(&$entity)
{
require_once 'CRM/Core/BAO/EntityTag.php';
if (! isset($entity->id)) {
return _crm_error('Required parameters missing');
}
$entityID=$entity->id;
return CRM_Core_BAO_EntityTag::getTag($entityTable = 'civicrm_contact', $entityID);
}
/**
* Deletes an existing entity tag assignment.
*
* @param $entity_tag object Valid entity_tag object.
* @access public
*/
function crm_delete_entity_tag(&$entity_tag)
{
require_once 'CRM/Core/BAO/EntityTag.php';
if ( ! isset($entity_tag->id)) {
return _crm_error('Required parameters missing');
}
$params=array('id' => $entity_tag->id );
return CRM_Core_BAO_EntityTag::del($params);
}