/** * Format the address params to have reasonable values. * * @param array $params * (reference ) an assoc array of name/value pairs. */ public static function fixAddress(&$params) { if (!empty($params['billing_street_address'])) { //Check address is coming from online contribution / registration page //Fixed :CRM-5076 $billing = array('street_address' => 'billing_street_address', 'city' => 'billing_city', 'postal_code' => 'billing_postal_code', 'state_province' => 'billing_state_province', 'state_province_id' => 'billing_state_province_id', 'country' => 'billing_country', 'country_id' => 'billing_country_id'); foreach ($billing as $key => $val) { if ($value = CRM_Utils_Array::value($val, $params)) { if (!empty($params[$key])) { unset($params[$val]); } else { //add new key and removed old $params[$key] = $value; unset($params[$val]); } } } } /* Split the zip and +4, if it's in US format */ if (!empty($params['postal_code']) && preg_match('/^(\\d{4,5})[+-](\\d{4})$/', $params['postal_code'], $match)) { $params['postal_code'] = $match[1]; $params['postal_code_suffix'] = $match[2]; } // add country id if not set if ((!isset($params['country_id']) || !is_numeric($params['country_id'])) && isset($params['country'])) { $country = new CRM_Core_DAO_Country(); $country->name = $params['country']; if (!$country->find(TRUE)) { $country->name = NULL; $country->iso_code = $params['country']; $country->find(TRUE); } $params['country_id'] = $country->id; } // add state_id if state is set if ((!isset($params['state_province_id']) || !is_numeric($params['state_province_id'])) && isset($params['state_province'])) { if (!empty($params['state_province'])) { $state_province = new CRM_Core_DAO_StateProvince(); $state_province->name = $params['state_province']; // add country id if present if (!empty($params['country_id'])) { $state_province->country_id = $params['country_id']; } if (!$state_province->find(TRUE)) { unset($state_province->name); $state_province->abbreviation = $params['state_province']; $state_province->find(TRUE); } $params['state_province_id'] = $state_province->id; if (empty($params['country_id'])) { // set this here since we have it $params['country_id'] = $state_province->country_id; } } else { $params['state_province_id'] = 'null'; } } // add county id if county is set // CRM-7837 if ((!isset($params['county_id']) || !is_numeric($params['county_id'])) && isset($params['county']) && !empty($params['county'])) { $county = new CRM_Core_DAO_County(); $county->name = $params['county']; if (isset($params['state_province_id'])) { $county->state_province_id = $params['state_province_id']; } if ($county->find(TRUE)) { $params['county_id'] = $county->id; } } // currently copy values populates empty fields with the string "null" // and hence need to check for the string null if (isset($params['state_province_id']) && is_numeric($params['state_province_id']) && (!isset($params['country_id']) || empty($params['country_id']))) { // since state id present and country id not present, hence lets populate it // jira issue http://issues.civicrm.org/jira/browse/CRM-56 $params['country_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $params['state_province_id'], 'country_id'); } //special check to ignore non numeric values if they are not //detected by formRule(sometimes happens due to internet latency), also allow user to unselect state/country if (isset($params['state_province_id'])) { if (empty($params['state_province_id'])) { $params['state_province_id'] = 'null'; } elseif (!is_numeric($params['state_province_id']) || (int) $params['state_province_id'] < 1000) { // CRM-3393 ( the hacky 1000 check) $params['state_province_id'] = 'null'; } } if (isset($params['country_id'])) { if (empty($params['country_id'])) { $params['country_id'] = 'null'; } elseif (!is_numeric($params['country_id']) || (int) $params['country_id'] < 1000) { // CRM-3393 ( the hacky 1000 check) $params['country_id'] = 'null'; } } // add state and country names from the ids if (isset($params['state_province_id']) && is_numeric($params['state_province_id'])) { $params['state_province'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($params['state_province_id']); } if (isset($params['country_id']) && is_numeric($params['country_id'])) { $params['country'] = CRM_Core_PseudoConstant::country($params['country_id']); } $config = CRM_Core_Config::singleton(); $asp = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::ADDRESS_STANDARDIZATION_PREFERENCES_NAME, 'address_standardization_provider'); // clean up the address via USPS web services if enabled if ($asp === 'USPS' && $params['country_id'] == 1228) { CRM_Utils_Address_USPS::checkAddress($params); // do street parsing again if enabled, since street address might have changed $parseStreetAddress = CRM_Utils_Array::value('street_address_parsing', CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'address_options'), FALSE); if ($parseStreetAddress && !empty($params['street_address'])) { foreach (array('street_number', 'street_name', 'street_unit', 'street_number_suffix') as $fld) { unset($params[$fld]); } // main parse string. $parseString = CRM_Utils_Array::value('street_address', $params); $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($parseString); // merge parse address in to main address block. $params = array_merge($params, $parsedFields); } } // add latitude and longitude and format address if needed if (!empty($config->geocodeMethod) && $config->geocodeMethod != 'CRM_Utils_Geocode_OpenStreetMaps' && empty($params['manual_geo_code'])) { $class = $config->geocodeMethod; $class::format($params); } }
/** * Process the mapped fields and map it into the uploaded file. */ public function postProcess() { $importJobParams = array('doGeocodeAddress' => $this->controller->exportValue('DataSource', 'doGeocodeAddress'), 'invalidRowCount' => $this->get('invalidRowCount'), 'conflictRowCount' => $this->get('conflictRowCount'), 'onDuplicate' => $this->get('onDuplicate'), 'dedupe' => $this->get('dedupe'), 'newGroupName' => $this->controller->exportValue($this->_name, 'newGroupName'), 'newGroupDesc' => $this->controller->exportValue($this->_name, 'newGroupDesc'), 'newGroupType' => $this->controller->exportValue($this->_name, 'newGroupType'), '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'), 'mapFields' => $this->get('fields'), 'contactType' => $this->get('contactType'), 'contactSubType' => $this->get('contactSubType'), 'primaryKeyName' => $this->get('primaryKeyName'), 'statusFieldName' => $this->get('statusFieldName'), 'statusID' => $this->get('statusID'), 'totalRowCount' => $this->get('totalRowCount')); $tableName = $this->get('importTableName'); $importJob = new CRM_Contact_Import_ImportJob($tableName); $importJob->setJobParams($importJobParams); // If ACL applies to the current user, update cache before running the import. if (!CRM_Core_Permission::check('view all contacts')) { $session = CRM_Core_Session::singleton(); $userID = $session->get('userID'); CRM_ACL_BAO_Cache::updateEntry($userID); } CRM_Utils_Address_USPS::disable($this->_disableUSPS); // run the import $importJob->runImport($this); // Clear all caches, forcing any searches to recheck the ACLs or group membership as the import // may have changed it. CRM_Contact_BAO_Contact_Utils::clearContactCaches(); // add all the necessary variables to the form $importJob->setFormVariables($this); // check if there is any error occurred $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)); } //hack to clean db //if job complete drop table. $importJob->isComplete(TRUE); }
/** * @param $disabled * * @return void */ public static function disable($disable = TRUE) { self::$_disabled = $disable; }
/** * format the address params to have reasonable values * * @param array $params (reference ) an assoc array of name/value pairs * * @return void * @access public * @static */ static function fixAddress(&$params) { if (CRM_Utils_Array::value('billing_street_address', $params)) { //Check address is comming from online contribution / registration page //Fixed :CRM-5076 $billing = array('street_address' => 'billing_street_address', 'city' => 'billing_city', 'postal_code' => 'billing_postal_code', 'state_province' => 'billing_state_province', 'state_province_id' => 'billing_state_province_id', 'country' => 'billing_country', 'country_id' => 'billing_country_id'); foreach ($billing as $key => $val) { if ($value = CRM_Utils_Array::value($val, $params)) { if (CRM_Utils_Array::value($key, $params)) { unset($params[$val]); } else { //add new key and removed old $params[$key] = $value; unset($params[$val]); } } } } /* Split the zip and +4, if it's in US format */ if (CRM_Utils_Array::value('postal_code', $params) && preg_match('/^(\\d{4,5})[+-](\\d{4})$/', $params['postal_code'], $match)) { $params['postal_code'] = $match[1]; $params['postal_code_suffix'] = $match[2]; } // add country id if not set if ((!isset($params['country_id']) || !is_numeric($params['country_id'])) && isset($params['country'])) { $country =& new CRM_Core_DAO_Country(); $country->name = $params['country']; if (!$country->find(true)) { $country->name = null; $country->iso_code = $params['country']; $country->find(true); } $params['country_id'] = $country->id; } // add state_id if state is set if ((!isset($params['state_province_id']) || !is_numeric($params['state_province_id'])) && isset($params['state_province'])) { if (!empty($params['state_province'])) { $state_province =& new CRM_Core_DAO_StateProvince(); $state_province->name = $params['state_province']; // add country id if present if (isset($params['country_id'])) { $state_province->country_id = $params['country_id']; } if (!$state_province->find(true)) { $state_province->name = null; $state_province->abbreviation = $params['state_province']; $state_province->find(true); } $params['state_province_id'] = $state_province->id; } else { $params['state_province_id'] = 'null'; } } // currently copy values populates empty fields with the string "null" // and hence need to check for the string null if (isset($params['state_province_id']) && is_numeric($params['state_province_id']) && (!isset($params['country_id']) || empty($params['country_id']))) { // since state id present and country id not present, hence lets populate it // jira issue http://issues.civicrm.org/jira/browse/CRM-56 $params['country_id'] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_StateProvince', $params['state_province_id'], 'country_id'); } //special check to ignore non numeric values if they are not //detected by formRule(sometimes happens due to internet latency), also allow user to unselect state/country if (isset($params['state_province_id'])) { if (!trim($params['state_province_id'])) { $params['state_province_id'] = 'null'; } else { if (!is_numeric($params['state_province_id']) || (int) $params['state_province_id'] < 1000) { // CRM-3393 ( the hacky 1000 check) $params['state_province_id'] = 'null'; } } } if (isset($params['country_id'])) { if (!trim($params['country_id'])) { $params['country_id'] = 'null'; } else { if (!is_numeric($params['country_id']) || (int) $params['country_id'] < 1000) { // CRM-3393 ( the hacky 1000 check) $params['country_id'] = 'null'; } } } // add state and country names from the ids if (isset($params['state_province_id']) && is_numeric($params['state_province_id'])) { $params['state_province'] = CRM_Core_PseudoConstant::stateProvinceAbbreviation($params['state_province_id']); } if (isset($params['country_id']) && is_numeric($params['country_id'])) { $params['country'] = CRM_Core_PseudoConstant::country($params['country_id']); } $config =& CRM_Core_Config::singleton(); require_once 'CRM/Core/BAO/Preferences.php'; $asp = CRM_Core_BAO_Preferences::value('address_standardization_provider'); // clean up the address via USPS web services if enabled if ($asp === 'USPS') { require_once 'CRM/Utils/Address/USPS.php'; CRM_Utils_Address_USPS::checkAddress($params); } // add latitude and longitude and format address if needed if (!empty($config->geocodeMethod)) { require_once str_replace('_', DIRECTORY_SEPARATOR, $config->geocodeMethod) . '.php'; eval($config->geocodeMethod . '::format( $params );'); } }