Ejemplo n.º 1
0
/**
 *  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 array of newly created tag property values.
 * @access public
 */
function civicrm_tag_create(&$params)
{
    _civicrm_initialize();
    if (!is_array($params)) {
        return civicrm_create_error(ts('Input parameters is not an array'));
    }
    if (empty($params)) {
        return civicrm_create_error(ts('No input parameters present'));
    }
    $error = _civicrm_check_required_fields($params, 'CRM_Core_DAO_Tag');
    if ($error['is_error']) {
        return civicrm_create_error($error['error_message']);
    }
    require_once 'CRM/Core/BAO/Tag.php';
    $ids = array('tag' => CRM_Utils_Array::value('tag', $params));
    if (CRM_Utils_Array::value('tag', $params)) {
        $ids['tag'] = $params['tag'];
    }
    $tagBAO = CRM_Core_BAO_Tag::add($params, $ids);
    if (is_a($tagBAO, 'CRM_Core_Error')) {
        return civicrm_create_error("Tag is not created");
    } else {
        $values = array();
        _civicrm_object_to_array($tagBAO, $values);
        $tag = array();
        $tag['tag_id'] = $values['id'];
        $tag['is_error'] = 0;
    }
    return $tag;
}
Ejemplo n.º 2
0
/**
 *  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 array of newly created tag property values.
 * @access public
 * @todo Erik Hommel 15/12/2010 : check if function is ok for update
 */
function civicrm_tag_create(&$params)
{
    _civicrm_initialize();
    $errorScope = CRM_Core_TemporaryErrorScope::useException();
    try {
        civicrm_verify_mandatory($params, 'CRM_Core_DAO_Tag', array('name'));
        if (!array_key_exists('used_for', $params)) {
            $params['used_for'] = "civicrm_contact";
        }
        require_once 'CRM/Core/BAO/Tag.php';
        $ids = array('tag' => CRM_Utils_Array::value('tag', $params));
        if (CRM_Utils_Array::value('tag', $params)) {
            $ids['tag'] = $params['tag'];
        }
        $tagBAO = CRM_Core_BAO_Tag::add($params, $ids);
        if (is_a($tagBAO, 'CRM_Core_Error')) {
            return civicrm_create_error("Tag is not created");
        } else {
            $values = array();
            _civicrm_object_to_array($tagBAO, $values);
            $tag = array();
            $tag['tag_id'] = $values['id'];
            $tag['name'] = $values['name'];
            $tag['is_error'] = 0;
        }
        return $tag;
    } catch (PEAR_Exception $e) {
        return civicrm_create_error($e->getMessage());
    } catch (Exception $e) {
        return civicrm_create_error($e->getMessage());
    }
}
Ejemplo n.º 3
0
/**
 *  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);
}
Ejemplo n.º 4
0
/**
 *  Add a Tag. Tags are used to classify CRM entities (including Contacts, Groups and Actions).
 *
 * Allowed @params array keys are:
 *
 * {@example TagCreate.php}
 *
 * @return array of newly created tag property values.
 * {@getfields tag_create}
 * @access public
 */
function civicrm_api3_tag_create($params)
{
    $ids = array('tag' => CRM_Utils_Array::value('tag', $params));
    if (CRM_Utils_Array::value('tag', $params)) {
        $ids['tag'] = $params['tag'];
    }
    if (CRM_Utils_Array::value('id', $params)) {
        $ids['tag'] = $params['id'];
    }
    $tagBAO = CRM_Core_BAO_Tag::add($params, $ids);
    $values = array();
    _civicrm_api3_object_to_array($tagBAO, $values[$tagBAO->id]);
    return civicrm_api3_create_success($values, $params, 'tag', 'create', $tagBAO);
}
Ejemplo n.º 5
0
 /**
  * Function to process the form
  *
  * @access public
  * @return None
  */
 function postProcess()
 {
     $params = $ids = array();
     // store the submitted values in an array
     $params = $this->exportValues();
     $ids['tag'] = $this->_id;
     if ($this->_action == CRM_CORE_ACTION_DELETE) {
         if ($this->_id > 0) {
             CRM_Core_BAO_Tag::del($this->_id);
         }
     } else {
         CRM_Core_BAO_Tag::add($params, $ids);
     }
 }
Ejemplo n.º 6
0
 function addTag($param)
 {
     if (array_key_exists($param['name'], $this->tags)) {
         echo "\n- exists already: " . $param['name'];
         return;
     }
     $key = array('tag' => '');
     if ($param['parent']) {
         if (array_key_exists($param['parent'], $this->tags)) {
             $param['parent_id'] = $this->tags[$param['parent']];
         } else {
             $param['parent_id'] = $this->addTag(array(parent => '', name => $param['parent']));
         }
         $tag = CRM_Core_BAO_Tag::add($param, $key);
         echo "\n" . $tag->id . ": create " . $param['name'] . " below " . $param['parent'];
     } else {
         $tag = CRM_Core_BAO_Tag::add($param, $key);
         echo "\n" . $tag->id . ": create " . $param['name'] . " (root)";
     }
     $this->tags[$param['name']] = $tag->id;
     return $tag->id;
 }
Ejemplo n.º 7
0
 /**
  * Process the mapped fields and map it into the uploaded file
  * preview the file and extract some summary statistics
  *
  * @return void
  * @access public
  */
 public function postProcessOld()
 {
     $doGeocodeAddress = $this->controller->exportValue('DataSource', 'doGeocodeAddress');
     $invalidRowCount = $this->get('invalidRowCount');
     $conflictRowCount = $this->get('conflictRowCount');
     $onDuplicate = $this->get('onDuplicate');
     $newGroupName = $this->controller->exportValue($this->_name, 'newGroupName');
     $newGroupDesc = $this->controller->exportValue($this->_name, 'newGroupDesc');
     $groups = $this->controller->exportValue($this->_name, 'groups');
     $allGroups = $this->get('groups');
     $newTagName = $this->controller->exportValue($this->_name, 'newTagName');
     $newTagDesc = $this->controller->exportValue($this->_name, 'newTagDesc');
     $tag = $this->controller->exportValue($this->_name, 'tag');
     $allTags = $this->get('tag');
     $mapper = $this->controller->exportValue('MapField', 'mapper');
     $mapperKeys = array();
     $mapperLocTypes = array();
     $mapperPhoneTypes = array();
     $mapperRelated = array();
     $mapperRelatedContactType = array();
     $mapperRelatedContactDetails = array();
     $mapperRelatedContactLocType = array();
     $mapperRelatedContactPhoneType = array();
     foreach ($mapper as $key => $value) {
         $mapperKeys[$key] = $mapper[$key][0];
         if (is_numeric($mapper[$key][1])) {
             $mapperLocTypes[$key] = $mapper[$key][1];
         } else {
             $mapperLocTypes[$key] = NULL;
         }
         if (CRM_Utils_Array::value($key, $mapperKeys) == 'phone') {
             $mapperPhoneTypes[$key] = $mapper[$key][2];
         } else {
             $mapperPhoneTypes[$key] = NULL;
         }
         list($id, $first, $second) = explode('_', $mapper[$key][0]);
         if ($first == 'a' && $second == 'b' || $first == 'b' && $second == 'a') {
             $relationType = new CRM_Contact_DAO_RelationshipType();
             $relationType->id = $id;
             $relationType->find(TRUE);
             $fieldName = "contact_type_{$second}";
             $mapperRelatedContactType[$key] = $relationType->{$fieldName};
             $mapperRelated[$key] = $mapper[$key][0];
             $mapperRelatedContactDetails[$key] = $mapper[$key][1];
             $mapperRelatedContactLocType[$key] = $mapper[$key][2];
             $mapperRelatedContactPhoneType[$key] = $mapper[$key][3];
         } else {
             $mapperRelated[$key] = NULL;
             $mapperRelatedContactType[$key] = NULL;
             $mapperRelatedContactDetails[$key] = NULL;
             $mapperRelatedContactLocType[$key] = NULL;
             $mapperRelatedContactPhoneType[$key] = NULL;
         }
     }
     $parser = new CRM_Contact_Import_Parser_Contact($mapperKeys, $mapperLocTypes, $mapperPhoneTypes, $mapperRelated, $mapperRelatedContactType, $mapperRelatedContactDetails, $mapperRelatedContactLocType, $mapperRelatedContactPhoneType);
     $mapFields = $this->get('fields');
     $locationTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id');
     $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id');
     foreach ($mapper as $key => $value) {
         $header = array();
         list($id, $first, $second) = explode('_', $mapper[$key][0]);
         if ($first == 'a' && $second == 'b' || $first == 'b' && $second == 'a') {
             $relationType = new CRM_Contact_DAO_RelationshipType();
             $relationType->id = $id;
             $relationType->find(TRUE);
             $header[] = $relationType->name_a_b;
             $header[] = ucwords(str_replace("_", " ", $mapper[$key][1]));
             if (isset($mapper[$key][2])) {
                 $header[] = $locationTypes[$mapper[$key][2]];
             }
             if (isset($mapper[$key][3])) {
                 $header[] = $phoneTypes[$mapper[$key][3]];
             }
         } else {
             if (isset($mapFields[$mapper[$key][0]])) {
                 $header[] = $mapFields[$mapper[$key][0]];
                 if (isset($mapper[$key][1])) {
                     $header[] = $locationTypes[$mapper[$key][1]];
                 }
                 if (isset($mapper[$key][2])) {
                     $header[] = $phoneTypes[$mapper[$key][2]];
                 }
             }
         }
         $mapperFields[] = implode(' - ', $header);
     }
     $tableName = $this->get('importTableName');
     //print "Running parser on table: $tableName<br/>";
     $parser->run($tableName, $mapperFields, CRM_Import_Parser::MODE_IMPORT, $this->get('contactType'), $this->get('primaryKeyName'), $this->get('statusFieldName'), $onDuplicate, $this->get('statusID'), $this->get('totalRowCount'), $doGeocodeAddress, CRM_Contact_Import_Parser::DEFAULT_TIMEOUT, $this->get('contactSubType'), $this->get('dedupe'));
     // add the new contacts to selected groups
     $contactIds =& $parser->getImportedContacts();
     // add the new related contacts to selected groups
     $relatedContactIds =& $parser->getRelatedImportedContacts();
     $this->set('relatedCount', count($relatedContactIds));
     $newGroupId = NULL;
     //changed below if-statement "if ($newGroup) {" to "if ($newGroupName) {"
     if ($newGroupName) {
         /* Create a new group */
         $gParams = array('name' => $newGroupName, 'title' => $newGroupName, 'description' => $newGroupDesc, 'is_active' => TRUE);
         $group = CRM_Contact_BAO_Group::create($gParams);
         $groups[] = $newGroupId = $group->id;
     }
     if (is_array($groups)) {
         $groupAdditions = array();
         foreach ($groups as $groupId) {
             $addCount = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
             if (!empty($relatedContactIds)) {
                 $addRelCount = CRM_Contact_BAO_GroupContact::addContactsToGroup($relatedContactIds, $groupId);
             }
             $totalCount = $addCount[1] + $addRelCount[1];
             if ($groupId == $newGroupId) {
                 $name = $newGroupName;
                 $new = TRUE;
             } else {
                 $name = $allGroups[$groupId];
                 $new = FALSE;
             }
             $groupAdditions[] = array('url' => CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $groupId), 'name' => $name, 'added' => $totalCount, 'notAdded' => $addCount[2] + $addRelCount[2], 'new' => $new);
         }
         $this->set('groupAdditions', $groupAdditions);
     }
     $newTagId = NULL;
     if ($newTagName) {
         /* Create a new Tag */
         $tagParams = array('name' => $newTagName, 'title' => $newTagName, 'description' => $newTagDesc, 'is_active' => TRUE);
         $id = array();
         $addedTag = CRM_Core_BAO_Tag::add($tagParams, $id);
         $tag[$addedTag->id] = 1;
     }
     //add Tag to Import
     if (is_array($tag)) {
         $tagAdditions = array();
         foreach ($tag as $tagId => $val) {
             $addTagCount = CRM_Core_BAO_EntityTag::addContactsToTag($contactIds, $tagId);
             if (!empty($relatedContactIds)) {
                 $addRelTagCount = CRM_Core_BAO_EntityTag::addContactsToTag($relatedContactIds, $tagId);
             }
             $totalTagCount = $addTagCount[1] + $addRelTagCount[1];
             if ($tagId == $addedTag->id) {
                 $tagName = $newTagName;
                 $new = TRUE;
             } else {
                 $tagName = $allTags[$tagId];
                 $new = FALSE;
             }
             $tagAdditions[] = array('url' => CRM_Utils_System::url('civicrm/contact/search', 'reset=1&force=1&context=smog&id=' . $tagId), 'name' => $tagName, 'added' => $totalTagCount, 'notAdded' => $addTagCount[2] + $addRelTagCount[2], 'new' => $new);
         }
         $this->set('tagAdditions', $tagAdditions);
     }
     // add all the necessary variables to the form
     $parser->set($this, CRM_Import_Parser::MODE_IMPORT);
     // check if there is any error occured
     $errorStack = CRM_Core_Error::singleton();
     $errors = $errorStack->getErrors();
     $errorMessage = array();
     if (is_array($errors)) {
         foreach ($errors as $key => $value) {
             $errorMessage[] = $value['message'];
         }
         // there is no fileName since this is a sql import
         // so fudge it
         $config = CRM_Core_Config::singleton();
         $errorFile = $config->uploadDir . "sqlImport.error.log";
         if ($fd = fopen($errorFile, 'w')) {
             fwrite($fd, implode('\\n', $errorMessage));
         }
         fclose($fd);
         $this->set('errorFile', $errorFile);
         $urlParams = 'type=' . CRM_Import_Parser::ERROR . '&parser=CRM_Contact_Import_Parser';
         $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlparams));
         $urlParams = 'type=' . CRM_Import_Parser::CONFLICT . '&parser=CRM_Contact_Import_Parser';
         $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
         $urlParams = 'type=' . CRM_Import_Parser::NO_MATCH . '&parser=CRM_Contact_Import_Parser';
         $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', $urlParams));
     }
 }
Ejemplo n.º 8
0
 private function _tagImportedContactsWithNewTag($contactIds, $newTagName, $newTagDesc)
 {
     $newTagId = NULL;
     if ($newTagName) {
         /* Create a new Tag */
         $tagParams = array('name' => $newTagName, 'title' => $newTagName, 'description' => $newTagDesc, 'is_selectable' => TRUE, 'used_for' => 'civicrm_contact');
         $id = array();
         $addedTag = CRM_Core_BAO_Tag::add($tagParams, $id);
         $this->_tag[$addedTag->id] = 1;
     }
     //add Tag to Import
     if (is_array($this->_tag)) {
         $tagAdditions = array();
         foreach ($this->_tag as $tagId => $val) {
             $addTagCount = CRM_Core_BAO_EntityTag::addEntitiesToTag($contactIds, $tagId);
             $totalTagCount = $addTagCount[1];
             if (isset($addedTag) && $tagId == $addedTag->id) {
                 $tagName = $newTagName;
                 $new = TRUE;
             } else {
                 $tagName = $this->_allTags[$tagId];
                 $new = FALSE;
             }
             $tagAdditions[] = array('url' => CRM_Utils_System::url('civicrm/contact/search', 'reset=1&force=1&context=smog&id=' . $tagId), 'name' => $tagName, 'added' => $totalTagCount, 'notAdded' => $addTagCount[2], 'new' => $new);
         }
         return $tagAdditions;
     }
     return FALSE;
 }
Ejemplo n.º 9
0
 static function processTags()
 {
     $skipTagCreate = $skipEntityAction = $entityId = NULL;
     $action = CRM_Utils_Type::escape($_POST['action'], 'String');
     $parentId = CRM_Utils_Type::escape($_POST['parentId'], 'Integer');
     if ($_POST['entityId']) {
         $entityId = CRM_Utils_Type::escape($_POST['entityId'], 'Integer');
     }
     $entityTable = CRM_Utils_Type::escape($_POST['entityTable'], 'String');
     if ($_POST['skipTagCreate']) {
         $skipTagCreate = CRM_Utils_Type::escape($_POST['skipTagCreate'], 'Integer');
     }
     if ($_POST['skipEntityAction']) {
         $skipEntityAction = CRM_Utils_Type::escape($_POST['skipEntityAction'], 'Integer');
     }
     // check if user has selected existing tag or is creating new tag
     // this is done to allow numeric tags etc.
     $tagValue = explode(':::', $_POST['tagID']);
     $createNewTag = FALSE;
     $tagID = $tagValue[0];
     if (isset($tagValue[1]) && $tagValue[1] == 'value') {
         $createNewTag = TRUE;
     }
     $tagInfo = array();
     // if action is select
     if ($action == 'select') {
         // check the value of tagID
         // if numeric that means existing tag
         // else create new tag
         if (!$skipTagCreate && $createNewTag) {
             $params = array('name' => $tagID, 'parent_id' => $parentId);
             $tagObject = CRM_Core_BAO_Tag::add($params, CRM_Core_DAO::$_nullArray);
             $tagInfo = array('name' => $tagID, 'id' => $tagObject->id, 'action' => $action);
             $tagID = $tagObject->id;
         }
         if (!$skipEntityAction && $entityId) {
             // save this tag to contact
             $params = array('entity_table' => $entityTable, 'entity_id' => $entityId, 'tag_id' => $tagID);
             CRM_Core_BAO_EntityTag::add($params);
         }
         // if action is delete
     } elseif ($action == 'delete') {
         if (!is_numeric($tagID)) {
             $tagID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $tagID, 'id', 'name');
         }
         if (!$skipEntityAction && $entityId) {
             // delete this tag entry for the entity
             $params = array('entity_table' => $entityTable, 'entity_id' => $entityId, 'tag_id' => $tagID);
             CRM_Core_BAO_EntityTag::del($params);
         }
         $tagInfo = array('id' => $tagID, 'action' => $action);
     }
     CRM_Utils_JSON::output($tagInfo);
 }
Ejemplo n.º 10
0
 /**
  * Process the form submission.
  */
 public function postProcess()
 {
     $params = $ids = array();
     // store the submitted values in an array
     $params = $this->exportValues();
     $ids['tag'] = $this->_id;
     if ($this->_action == CRM_Core_Action::ADD || $this->_action == CRM_Core_Action::UPDATE) {
         $params['used_for'] = implode(",", $params['used_for']);
     }
     $params['is_tagset'] = 0;
     if ($this->_isTagSet) {
         $params['is_tagset'] = 1;
     }
     if (!isset($params['is_reserved'])) {
         $params['is_reserved'] = 0;
     }
     if (!isset($params['is_selectable'])) {
         $params['is_selectable'] = 0;
     }
     if ($this->_action == CRM_Core_Action::DELETE) {
         if ($this->_id > 0) {
             $tag = civicrm_api3('tag', 'getsingle', array('id' => $this->_id));
             CRM_Core_BAO_Tag::del($this->_id);
             CRM_Core_Session::setStatus(ts('The tag \'%1\' has been deleted.', array(1 => $tag['name'])), ts('Deleted'), 'success');
         }
     } else {
         $tag = CRM_Core_BAO_Tag::add($params, $ids);
         CRM_Core_Session::setStatus(ts('The tag \'%1\' has been saved.', array(1 => $tag->name)), ts('Saved'), 'success');
     }
 }
Ejemplo n.º 11
0
 private function _tagImportedContactsWithNewTag($contactIds, $newTagName, $newTagDesc)
 {
     $newTagId = null;
     if ($newTagName) {
         /* Create a new Tag */
         $tagParams = array('name' => $newTagName, 'title' => $newTagName, 'description' => $newTagDesc, 'is_active' => true);
         require_once 'CRM/Core/BAO/Tag.php';
         $id = array();
         $addedTag = CRM_Core_BAO_Tag::add($tagParams, $id);
         $this->_tag[$addedTag->id] = 1;
     }
     //add Tag to Import
     if (is_array($this->_tag)) {
         $tagAdditions = array();
         require_once "CRM/Core/BAO/EntityTag.php";
         foreach ($this->_tag as $tagId => $val) {
             $addTagCount = CRM_Core_BAO_EntityTag::addContactsToTag($contactIds, $tagId);
             $totalTagCount = $addTagCount[1];
             if ($tagId == $addedTag->id) {
                 $tagName = $newTagName;
                 $new = true;
             } else {
                 $tagName = $this->_allTags[$tagId];
                 $new = false;
             }
             $tagAdditions[] = array('url' => CRM_Utils_System::url('civicrm/contact/search', 'reset=1&force=1&context=smog&id=' . $tagId), 'name' => $tagName, 'added' => $totalTagCount, 'notAdded' => $addTagCount[2], 'new' => $new);
         }
         return $tagAdditions;
     }
     return false;
 }
Ejemplo n.º 12
0
 static function processTags()
 {
     $skipTagCreate = $skipEntityAction = $entityId = null;
     $action = CRM_Utils_Type::escape($_POST['action'], 'String');
     $parentId = CRM_Utils_Type::escape($_POST['parentId'], 'Integer');
     if ($_POST['entityId']) {
         $entityId = CRM_Utils_Type::escape($_POST['entityId'], 'Integer');
     }
     $entityTable = CRM_Utils_Type::escape($_POST['entityTable'], 'String');
     if ($_POST['skipTagCreate']) {
         $skipTagCreate = CRM_Utils_Type::escape($_POST['skipTagCreate'], 'Integer');
     }
     if ($_POST['skipEntityAction']) {
         $skipEntityAction = CRM_Utils_Type::escape($_POST['skipEntityAction'], 'Integer');
     }
     $tagID = $_POST['tagID'];
     require_once 'CRM/Core/BAO/EntityTag.php';
     $tagInfo = array();
     // if action is select
     if ($action == 'select') {
         // check the value of tagID
         // if numeric that means existing tag
         // else create new tag
         if (!$skipTagCreate && !is_numeric($tagID)) {
             $params = array('name' => $tagID, 'parent_id' => $parentId);
             require_once 'CRM/Core/BAO/Tag.php';
             $tagObject = CRM_Core_BAO_Tag::add($params, CRM_Core_DAO::$_nullArray);
             $tagInfo = array('name' => $tagID, 'id' => $tagObject->id, 'action' => $action);
             $tagID = $tagObject->id;
         }
         if (!$skipEntityAction && $entityId) {
             // save this tag to contact
             $params = array('entity_table' => $entityTable, 'entity_id' => $entityId, 'tag_id' => $tagID);
             CRM_Core_BAO_EntityTag::add($params);
         }
     } elseif ($action == 'delete') {
         // if action is delete
         if (!is_numeric($tagID)) {
             $tagID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Tag', $tagID, 'id', 'name');
         }
         if ($entityId) {
             // delete this tag entry for the entity
             $params = array('entity_table' => $entityTable, 'entity_id' => $entityId, 'tag_id' => $tagID);
             CRM_Core_BAO_EntityTag::del($params);
         }
         $tagInfo = array('id' => $tagID, 'action' => $action);
     }
     echo json_encode($tagInfo);
     CRM_Utils_System::civiExit();
 }
Ejemplo n.º 13
0
 /**
  * Function to save entity tags when it is not save used AJAX
  *
  * @param array   $params      associated array
  * @param int     $entityId    entity id, eg: contact id, activity id, case id, file id
  * @param string  $entityTable entity table
  * @param object  $form        form object
  *
  * @return void
  * @access public
  * @static
  */
 static function postProcess(&$params, $entityId, $entityTable = 'civicrm_contact', &$form)
 {
     if ($form && !empty($form->_entityTagValues)) {
         $existingTags = $form->_entityTagValues;
     } else {
         $existingTags = CRM_Core_BAO_EntityTag::getTag($entityId, $entityTable);
     }
     if ($form) {
         // if the key is missing from the form response then all the tags were deleted / cleared
         // in that case we create empty tagset params so that below logic works and tagset are
         // deleted correctly
         foreach ($form->_tagsetInfo as $tagsetName => $tagsetInfo) {
             $tagsetId = substr($tagsetName, strlen('parentId_'));
             if (empty($params[$tagsetId])) {
                 $params[$tagsetId] = '';
             }
         }
     }
     // when form is submitted with tagset values below logic will work and in the case when all tags in a tagset
     // are deleted we will have to set $params[tagset id] = '' which is done by above logic
     foreach ($params as $parentId => $value) {
         $newTagIds = array();
         $realTagIds = array();
         if ($value) {
             $tagsIDs = explode(',', $value);
             foreach ($tagsIDs as $tagId) {
                 if (!is_numeric($tagId)) {
                     // check if user has selected existing tag or is creating new tag
                     // this is done to allow numeric tags etc.
                     $tagValue = explode(':::', $tagId);
                     if (isset($tagValue[1]) && $tagValue[1] == 'value') {
                         $tagParams = array('name' => $tagValue[0], 'parent_id' => $parentId);
                         $tagObject = CRM_Core_BAO_Tag::add($tagParams, CRM_Core_DAO::$_nullArray);
                         $tagId = $tagObject->id;
                     }
                 }
                 $realTagIds[] = $tagId;
                 if ($form && $form->_action != CRM_Core_Action::UPDATE) {
                     $newTagIds[] = $tagId;
                 } elseif (!array_key_exists($tagId, $existingTags)) {
                     $newTagIds[] = $tagId;
                 }
             }
         }
         // Any existing entity tags from this tagset missing from the $params should be deleted
         $deleteSQL = "DELETE FROM civicrm_entity_tag\n                    USING civicrm_entity_tag, civicrm_tag\n                    WHERE civicrm_tag.id=civicrm_entity_tag.tag_id\n                      AND civicrm_entity_tag.entity_table='{$entityTable}'\n                      AND entity_id={$entityId} AND parent_id={$parentId}";
         if (!empty($realTagIds)) {
             $deleteSQL .= " AND tag_id NOT IN (" . implode(', ', $realTagIds) . ");";
         }
         CRM_Core_DAO::executeQuery($deleteSQL);
         if (!empty($newTagIds)) {
             // New tag ids can be inserted directly into the db table.
             $insertValues = array();
             foreach ($newTagIds as $tagId) {
                 $insertValues[] = "( {$tagId}, {$entityId}, '{$entityTable}' ) ";
             }
             $insertSQL = 'INSERT INTO civicrm_entity_tag ( tag_id, entity_id, entity_table )
       VALUES ' . implode(', ', $insertValues) . ';';
             CRM_Core_DAO::executeQuery($insertSQL);
         }
     }
 }