/** * process the form after the input has been submitted and validated * * @access public * @return None */ function postProcess() { $tagId = $this->controller->exportValue('AddToTag', 'tag_id'); $this->_name = $this->_tags[$tagId]; list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addContactsToTag($this->_contactIds, $tagId); $status = array('Contact(s) tagged as: ' . $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); }
/** * 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)); } }
/** * Process the mapped fields and map it into the uploaded file * preview the file and extract some summary statistics * * @return void * @access public */ function postProcess() { $fileName = $this->controller->exportValue('UploadFile', 'uploadFile'); $skipColumnHeader = $this->controller->exportValue('UploadFile', 'skipColumnHeader'); $invalidRowCount = $this->get('invalidRowCount'); $conflictRowCount = $this->get('conflictRowCount'); $onDuplicate = $this->get('onDuplicate'); $newGroup = $this->controller->exportValue($this->_name, 'newGroup'); $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'); $tagForContact = $this->controller->exportValue($this->_name, 'tag'); $allTags = $this->get('tag'); $seperator = ','; $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 (!is_numeric($mapper[$key][2])) { $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); eval('$mapperRelatedContactType[$key] = $relationType->contact_type_' . $second . ';'); $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_Import_Parser_Contact($mapperKeys, $mapperLocTypes, $mapperPhoneTypes, $mapperRelated, $mapperRelatedContactType, $mapperRelatedContactDetails, $mapperRelatedContactLocType, $mapperRelatedContactPhoneType); $mapFields = $this->get('fields'); $locationTypes = CRM_Core_PseudoConstant::locationType(); $phoneTypes = CRM_Core_SelectValues::phoneType(); 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); } $parser->run($fileName, $seperator, $mapperFields, $skipColumnHeader, CRM_IMPORT_PARSER_MODE_IMPORT, $this->get('contactType'), $onDuplicate); // 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('domain_id' => CRM_Core_Config::domainID(), '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], 'new' => $new); } $this->set('groupAdditions', $groupAdditions); } if (is_array($tagForContact)) { $tagAddition = array(); require_once "CRM/Core/BAO/EntityTag.php"; foreach ($tagForContact as $tagId => $selected) { $taggedContacts = CRM_Core_BAO_EntityTag::addContactsToTag($contactIds, $tagId); $tagName = $allTags[$tagId]; $tagAdditions[] = array('name' => $tagName, 'added' => $taggedContacts[1], 'notAdded' => $taggedContacts[2]); } $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(); $config =& CRM_Core_Config::singleton(); if (is_array($errors)) { foreach ($errors as $key => $value) { $errorMessage[] = $value['message']; } $errorFile = $fileName . '.error.log'; if ($fd = fopen($errorFile, 'w')) { fwrite($fd, implode('\\n', $errorMessage)); } fclose($fd); $this->set('errorFile', $errorFile); $this->set('downloadErrorRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=1')); $this->set('downloadConflictRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=2')); $this->set('downloadMismatchRecordsUrl', CRM_Utils_System::url('civicrm/export', 'type=4')); } }
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; }
/** * * @param <type> $params * @param <type> $op * @return <type> */ function civicrm_entity_tag_common(&$params, $op = 'add') { $contactIDs = array(); $tagsIDs = array(); if (is_array($params)) { foreach ($params as $n => $v) { if (substr($n, 0, 10) == 'contact_id') { $contactIDs[] = $v; } else { if (substr($n, 0, 6) == 'tag_id') { $tagIDs[] = $v; } } } } if (empty($contactIDs)) { 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($tc, $a, $na) = CRM_Core_BAO_EntityTag::addContactsToTag($contactIDs, $tagID); $values['total_count'] += $tc; $values['added'] += $a; $values['not_added'] += $na; } } else { $values['total_count'] = $values['removed'] = $values['not_removed'] = 0; foreach ($tagIDs as $tagID) { list($tc, $r, $nr) = CRM_Core_BAO_EntityTag::removeContactsFromTag($contactIDs, $tagID); $values['total_count'] += $tc; $values['removed'] += $r; $values['not_removed'] += $nr; } } return $values; }
/** * process the form after the input has been submitted and validated * * @access public * @return None */ public function postProcess() { $tagId = $this->controller->exportValue('AddToTag', 'tag'); $this->_name = array(); foreach ($tagId as $key => $dnc) { $this->_name[] = $this->_tags[$key]; list($total, $added, $notAdded) = CRM_Core_BAO_EntityTag::addContactsToTag($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); }