/** * Given an array of entity ids and entity table, add all the entity to the tags * * @param array $entityIds * (reference ) the array of entity ids to be added. * @param int $tagId * The id of the tag. * @param string $entityTable * Name of entity table default:civicrm_contact. * * @return array * (total, added, notAdded) count of enities added to tag */ public static function addEntitiesToTag(&$entityIds, $tagId, $entityTable = 'civicrm_contact') { $numEntitiesAdded = 0; $numEntitiesNotAdded = 0; $entityIdsAdded = array(); foreach ($entityIds as $entityId) { $tag = new CRM_Core_DAO_EntityTag(); $tag->entity_id = $entityId; $tag->tag_id = $tagId; $tag->entity_table = $entityTable; if (!$tag->find()) { $tag->save(); $entityIdsAdded[] = $entityId; $numEntitiesAdded++; } else { $numEntitiesNotAdded++; } } //invoke post hook on entityTag $object = array($entityIdsAdded, $entityTable); CRM_Utils_Hook::post('create', 'EntityTag', $tagId, $object); // reset the group contact cache for all groups // if tags are being used in a smart group CRM_Contact_BAO_GroupContactCache::remove(); return array(count($entityIds), $numEntitiesAdded, $numEntitiesNotAdded); }
/** * Given an array of entity ids and entity table, add all the entity to the tags. * * @param array $entityIds * (reference ) the array of entity ids to be added. * @param int $tagId * The id of the tag. * @param string $entityTable * Name of entity table default:civicrm_contact. * @param bool $applyPermissions * Should permissions be applied in this function. * * @return array * (total, added, notAdded) count of entities added to tag */ public static function addEntitiesToTag(&$entityIds, $tagId, $entityTable, $applyPermissions) { $numEntitiesAdded = 0; $numEntitiesNotAdded = 0; $entityIdsAdded = array(); foreach ($entityIds as $entityId) { // CRM-17350 - check if we have permission to edit the contact // that this tag belongs to. if ($applyPermissions && !self::checkPermissionOnEntityTag($entityId, $entityTable)) { $numEntitiesNotAdded++; continue; } $tag = new CRM_Core_DAO_EntityTag(); $tag->entity_id = $entityId; $tag->tag_id = $tagId; $tag->entity_table = $entityTable; if (!$tag->find()) { $tag->save(); $entityIdsAdded[] = $entityId; $numEntitiesAdded++; } else { $numEntitiesNotAdded++; } } //invoke post hook on entityTag $object = array($entityIdsAdded, $entityTable); CRM_Utils_Hook::post('create', 'EntityTag', $tagId, $object); CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); return array(count($entityIds), $numEntitiesAdded, $numEntitiesNotAdded); }