3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * This class contains functions for managing Tag(tag) for a contact
32 * @copyright CiviCRM LLC (c) 2004-2013
36 class CRM_Core_BAO_EntityTag
extends CRM_Core_DAO_EntityTag
{
40 * Given a contact id, it returns an array of tag id's the
43 * @param int $entityID id of the entity usually the contactID.
44 * @param string $entityTable name of the entity table usually 'civicrm_contact'
47 ) reference $tag array of catagory id's the contact belongs to.
52 static function &getTag($entityID, $entityTable = 'civicrm_contact') {
55 $entityTag = new CRM_Core_BAO_EntityTag();
56 $entityTag->entity_id
= $entityID;
57 $entityTag->entity_table
= $entityTable;
60 while ($entityTag->fetch()) {
61 $tags[$entityTag->tag_id
] = $entityTag->tag_id
;
67 * takes an associative array and creates a entityTag object
69 * the function extract all the params it needs to initialize the create a
70 * group object. the params array could contain additional unused name/value
73 * @param array $params (reference ) an assoc array of name/value pairs
75 * @return object CRM_Core_BAO_EntityTag object
79 static function add(&$params) {
80 $dataExists = self
::dataExists($params);
85 $entityTag = new CRM_Core_BAO_EntityTag();
86 $entityTag->copyValues($params);
88 // dont save the object if it already exists, CRM-1276
89 if (!$entityTag->find(TRUE)) {
92 //invoke post hook on entityTag
93 // we are using this format to keep things consistent between the single and bulk operations
94 // so a bit different from other post hooks
95 $object = array(0 => array(0 => $params['entity_id']), 1 => $params['entity_table']);
96 CRM_Utils_Hook
::post('create', 'EntityTag', $params['tag_id'], $object);
102 * Check if there is data to create the object
104 * @params array $params (reference ) an assoc array of name/value pairs
110 static function dataExists(&$params) {
111 return ($params['tag_id'] == 0) ?
FALSE : TRUE;
115 * Function to delete the tag for a contact
117 * @param array $params (reference ) an assoc array of name/value pairs
119 * @return object CRM_Core_BAO_EntityTag object
124 static function del(&$params) {
125 $entityTag = new CRM_Core_BAO_EntityTag();
126 $entityTag->copyValues($params);
127 $entityTag->delete();
129 //invoke post hook on entityTag
130 $object = array(0 => array(0 => $params['entity_id']), 1 => $params['entity_table']);
131 CRM_Utils_Hook
::post('delete', 'EntityTag', $params['tag_id'], $object);
135 * Given an array of entity ids and entity table, add all the entity to the tags
137 * @param array $entityIds (reference ) the array of entity ids to be added
138 * @param int $tagId the id of the tag
139 * @params string $entityTable name of entity table default:civicrm_contact
141 * @return array (total, added, notAdded) count of enities added to tag
145 static function addEntitiesToTag(&$entityIds, $tagId, $entityTable = 'civicrm_contact') {
146 $numEntitiesAdded = 0;
147 $numEntitiesNotAdded = 0;
148 $entityIdsAdded = array();
150 foreach ($entityIds as $entityId) {
151 $tag = new CRM_Core_DAO_EntityTag();
153 $tag->entity_id
= $entityId;
154 $tag->tag_id
= $tagId;
155 $tag->entity_table
= $entityTable;
158 $entityIdsAdded[] = $entityId;
162 $numEntitiesNotAdded++
;
166 //invoke post hook on entityTag
167 $object = array($entityIdsAdded, $entityTable);
168 CRM_Utils_Hook
::post('create', 'EntityTag', $tagId, $object);
170 // reset the group contact cache for all groups
171 // if tags are being used in a smart group
172 CRM_Contact_BAO_GroupContactCache
::remove();
174 return array(count($entityIds), $numEntitiesAdded, $numEntitiesNotAdded);
178 * Given an array of entity ids and entity table, remove entity(s) tags
180 * @param array $entityIds (reference ) the array of entity ids to be removed
181 * @param int $tagId the id of the tag
182 * @params string $entityTable name of entity table default:civicrm_contact
184 * @return array (total, removed, notRemoved) count of entities removed from tags
188 static function removeEntitiesFromTag(&$entityIds, $tagId, $entityTable = 'civicrm_contact') {
189 $numEntitiesRemoved = 0;
190 $numEntitiesNotRemoved = 0;
191 $entityIdsRemoved = array();
193 foreach ($entityIds as $entityId) {
194 $tag = new CRM_Core_DAO_EntityTag();
196 $tag->entity_id
= $entityId;
197 $tag->tag_id
= $tagId;
198 $tag->entity_table
= $entityTable;
201 $entityIdsRemoved[] = $entityId;
202 $numEntitiesRemoved++
;
205 $numEntitiesNotRemoved++
;
209 //invoke post hook on entityTag
210 $object = array($entityIdsRemoved, $entityTable);
211 CRM_Utils_Hook
::post('delete', 'EntityTag', $tagId, $object);
213 // reset the group contact cache for all groups
214 // if tags are being used in a smart group
215 CRM_Contact_BAO_GroupContactCache
::remove();
217 return array(count($entityIds), $numEntitiesRemoved, $numEntitiesNotRemoved);
221 * takes an associative array and creates tag entity record for all tag entities
223 * @param array $params (reference ) an assoc array of name/value pairs
224 * @param array $contactId contact id
230 static function create(&$params, $entityTable, $entityID) {
231 // get categories for the entity id
232 $entityTag = CRM_Core_BAO_EntityTag
::getTag($entityID, $entityTable);
234 // get the list of all the categories
235 $allTag = CRM_Core_BAO_Tag
::getTags($entityTable);
237 // this fix is done to prevent warning generated by array_key_exits incase of empty array is given as input
238 if (!is_array($params)) {
242 // this fix is done to prevent warning generated by array_key_exits incase of empty array is given as input
243 if (!is_array($entityTag)) {
244 $entityTag = array();
247 // check which values has to be inserted/deleted for contact
248 foreach ($allTag as $key => $varValue) {
249 $tagParams['entity_table'] = $entityTable;
250 $tagParams['entity_id'] = $entityID;
251 $tagParams['tag_id'] = $key;
253 if (array_key_exists($key, $params) && !array_key_exists($key, $entityTag)) {
254 // insert a new record
255 CRM_Core_BAO_EntityTag
::add($tagParams);
257 elseif (!array_key_exists($key, $params) && array_key_exists($key, $entityTag)) {
258 // delete a record for existing contact
259 CRM_Core_BAO_EntityTag
::del($tagParams);
265 * This function returns all entities assigned to a specific tag
267 * @param object $tag an object of a tag.
269 * @return array $entityIds array of entity ids
272 function getEntitiesByTag($tag) {
273 $entityIds = array();
274 $entityTagDAO = new CRM_Core_DAO_EntityTag();
275 $entityTagDAO->tag_id
= $tag->id
;
276 $entityTagDAO->find();
277 while ($entityTagDAO->fetch()) {
278 $entityIds[] = $entityTagDAO->entity_id
;
284 * Function to get contact tags
286 static function getContactTags($contactID, $count = FALSE) {
287 $contactTags = array();
289 $select = "SELECT name ";
292 $select = "SELECT count(*) as cnt";
297 INNER JOIN civicrm_entity_tag et ON ( ct.id = et.tag_id AND
298 et.entity_id = {$contactID} AND
299 et.entity_table = 'civicrm_contact' AND
302 $dao = CRM_Core_DAO
::executeQuery($query);
309 while ($dao->fetch()) {
310 $contactTags[] = $dao->name
;
317 * Function to get child contact tags given parentId
319 static function getChildEntityTags($parentId, $entityId, $entityTable = 'civicrm_contact') {
320 $entityTags = array();
321 $query = "SELECT ct.id as tag_id, name FROM civicrm_tag ct
322 INNER JOIN civicrm_entity_tag et ON ( et.entity_id = {$entityId} AND
323 et.entity_table = '{$entityTable}' AND et.tag_id = ct.id)
324 WHERE ct.parent_id = {$parentId}";
326 $dao = CRM_Core_DAO
::executeQuery($query);
328 while ($dao->fetch()) {
329 $entityTags[$dao->tag_id
] = array(
330 'id' => $dao->tag_id
,
331 'name' => $dao->name
,
339 * Function to merge two tags: tag B into tag A.
341 function mergeTags($tagAId, $tagBId) {
342 $queryParams = array(1 => array($tagBId, 'Integer'),
343 2 => array($tagAId, 'Integer'),
346 // re-compute used_for field
347 $query = "SELECT id, name, used_for FROM civicrm_tag WHERE id IN (%1, %2)";
348 $dao = CRM_Core_DAO
::executeQuery($query, $queryParams);
350 while ($dao->fetch()) {
351 $label = ($dao->id
== $tagAId) ?
'tagA' : 'tagB';
352 $tags[$label] = $dao->name
;
353 $tags["{$label}_used_for"] = $dao->used_for ?
explode(",", $dao->used_for
) : array();
355 $usedFor = array_merge($tags["tagA_used_for"], $tags["tagB_used_for"]);
356 $usedFor = implode(',', array_unique($usedFor));
357 $tags["tagB_used_for"] = explode(",", $usedFor);
359 // get all merge queries together
361 // 1. update entity tag entries
362 "UPDATE IGNORE civicrm_entity_tag SET tag_id = %1 WHERE tag_id = %2",
363 // 2. update used_for info for tag B
364 "UPDATE civicrm_tag SET used_for = '{$usedFor}' WHERE id = %1",
365 // 3. remove tag A, if tag A is getting merged into B
366 "DELETE FROM civicrm_tag WHERE id = %2",
367 // 4. remove duplicate entity tag records
368 "DELETE et2.* from civicrm_entity_tag et1 INNER JOIN civicrm_entity_tag et2 ON et1.entity_table = et2.entity_table AND et1.entity_id = et2.entity_id AND et1.tag_id = et2.tag_id WHERE et1.id < et2.id",
369 // 5. remove orphaned entity_tags
370 "DELETE FROM civicrm_entity_tag WHERE tag_id = %2",
372 $tables = array('civicrm_entity_tag', 'civicrm_tag');
374 // Allow hook_civicrm_merge() to add SQL statements for the merge operation AND / OR
375 // perform any other actions like logging
376 CRM_Utils_Hook
::merge('sqls', $sqls, $tagAId, $tagBId, $tables);
378 // call the SQL queries in one transaction
379 $transaction = new CRM_Core_Transaction();
380 foreach ($sqls as $sql) {
381 CRM_Core_DAO
::executeQuery($sql, $queryParams, TRUE, NULL, TRUE);
383 $transaction->commit();
385 $tags['status'] = TRUE;