/** * process the form after the input has been submitted and validated * * @access public * * @return void */ public function postProcess() { //get the submitted values in an array $params = $this->controller->exportValues($this->_name); $contactTags = $tagList = array(); // check if contact tags exists if (!empty($params['tag'])) { $contactTags = $params['tag']; } // check if tags are selected from taglists if (!empty($params['contact_taglist'])) { foreach ($params['contact_taglist'] as $val) { if ($val) { if (is_numeric($val)) { $tagList[$val] = 1; } else { $tagIDs = explode(',', $val); if (!empty($tagIDs)) { foreach ($tagIDs as $tagID) { if (is_numeric($tagID)) { $tagList[$tagID] = 1; } } } } } } } $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE, TRUE); foreach ($tagSets as $key => $value) { $this->_tags[$key] = $value['name']; } // merge contact and taglist tags $allTags = CRM_Utils_Array::crmArrayMerge($contactTags, $tagList); $this->_name = array(); foreach ($allTags as $key => $dnc) { $this->_name[] = $this->_tags[$key]; list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addEntitiesToTag($this->_contactIds, $key); $status = array(ts('%count contact tagged', array('count' => $added, 'plural' => '%count contacts tagged'))); if ($notAdded) { $status[] = ts('%count contact already had this tag', array('count' => $notAdded, 'plural' => '%count contacts already had this tag')); } $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; CRM_Core_Session::setStatus($status, ts("Added Tag <em>%1</em>", array(1 => $this->_tags[$key])), 'success', array('expires' => 0)); } }
/** * * @param <type> $params * @param <type> $op * * @return <type> */ function civicrm_entity_tag_common(&$params, $op = 'add') { $entityIDs = array(); $tagsIDs = array(); $entityTable = 'civicrm_contact'; if (is_array($params)) { foreach ($params as $n => $v) { if (substr($n, 0, 10) == 'contact_id' || substr($n, 0, 9) == 'entity_id') { $entityIDs[] = $v; } elseif (substr($n, 0, 6) == 'tag_id') { $tagIDs[] = $v; } elseif (substr($n, 0, 12) == 'entity_table') { $entityTable = $v; } } } if (empty($entityIDs)) { return civicrm_create_error(ts('contact_id is a required field')); } if (empty($tagIDs)) { return civicrm_create_error(ts('tag_id is a required field')); } require_once 'CRM/Core/BAO/EntityTag.php'; $values = array('is_error' => 0); if ($op == 'add') { $values['total_count'] = $values['added'] = $values['not_added'] = 0; foreach ($tagIDs as $tagID) { list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable); $values['total_count'] += $te; $values['added'] += $a; $values['not_added'] += $na; } } else { $values['total_count'] = $values['removed'] = $values['not_removed'] = 0; foreach ($tagIDs as $tagID) { list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable); $values['total_count'] += $te; $values['removed'] += $r; $values['not_removed'] += $nr; } } return $values; }
/** * Helper function for formatting tags (part of api v2 legacy). * * @param array $params * @param string $op * * @return array */ function _civicrm_api3_entity_tag_common($params, $op = 'add') { $entityIDs = array(); $entityTable = 'civicrm_contact'; if (is_array($params)) { foreach ($params as $n => $v) { if (substr($n, 0, 10) == 'contact_id' || substr($n, 0, 9) == 'entity_id') { $entityIDs[] = $v; } elseif (substr($n, 0, 6) == 'tag_id') { $tagIDs[] = $v; } elseif (substr($n, 0, 12) == 'entity_table') { $entityTable = $v; } } } if (empty($entityIDs)) { return civicrm_api3_create_error('contact_id is a required field'); } if (empty($tagIDs)) { if ($op == 'remove') { $tagIDs = array_keys(CRM_Core_BAO_EntityTag::getContactTags($entityIDs[0])); } else { return civicrm_api3_create_error('tag_id is a required field'); } } $values = array('is_error' => 0); if ($op == 'add') { $values['total_count'] = $values['added'] = $values['not_added'] = 0; foreach ($tagIDs as $tagID) { list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable, CRM_Utils_Array::value('check_permissions', $params)); $values['total_count'] += $te; $values['added'] += $a; $values['not_added'] += $na; } } else { $values['total_count'] = $values['removed'] = $values['not_removed'] = 0; foreach ($tagIDs as $tagID) { list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable, CRM_Utils_Array::value('check_permissions', $params)); $values['total_count'] += $te; $values['removed'] += $r; $values['not_removed'] += $nr; } } if (empty($values['added']) && empty($values['removed'])) { $values['is_error'] = 1; $values['error_message'] = "Unable to {$op} tags"; } return $values; }
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; }
/** * process the form after the input has been submitted and validated * * @access public * * @return None */ public function postProcess() { //get the submitted values in an array $params = $this->controller->exportValues($this->_name); $contactTags = $tagList = array(); // check if contact tags exists if (CRM_Utils_Array::value('tag', $params)) { $contactTags = $params['tag']; } // check if tags are selected from taglists if (CRM_Utils_Array::value('contact_taglist', $params)) { foreach ($params['contact_taglist'] as $val) { if ($val) { if (is_numeric($val)) { $tagList[$val] = 1; } else { $tagIDs = explode(',', $val); if (!empty($tagIDs)) { foreach ($tagIDs as $tagID) { if (is_numeric($tagID)) { $tagList[$tagID] = 1; } } } } } } } $tagSets = CRM_Core_BAO_Tag::getTagsUsedFor('civicrm_contact', FALSE, TRUE); foreach ($tagSets as $key => $value) { $this->_tags[$key] = $value['name']; } // merge contact and taglist tags $allTags = CRM_Utils_Array::crmArrayMerge($contactTags, $tagList); $this->_name = array(); foreach ($allTags as $key => $dnc) { $this->_name[] = $this->_tags[$key]; list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addEntitiesToTag($this->_contactIds, $key); $status = array('Contact(s) tagged as: ' . implode(',', $this->_name), 'Total Selected Contact(s): ' . $total); } if ($added) { $status[] = 'Total Contact(s) tagged: ' . $added; } if ($notAdded) { $status[] = 'Total Contact(s) already tagged: ' . $notAdded; } CRM_Core_Session::setStatus($status); }