<?php //initialize civicrm $rootPath = '/var/www/drupal7/sites/all/modules/civicrm/'; require_once $rootPath . 'civicrm.config.php'; require_once $rootPath . 'CRM/Core/Config.php'; $config = CRM_Core_Config::singleton(); CRM_Core_DAO::$_dbColumnValueCache = NULL; //readfile $fileName = 'MyImport2.csv'; CRM_Core_Error::debug('$fileName', $fileName); exit; //$fileName = 'test1.csv'; $read = fopen($fileName, 'r'); $rows = fgetcsv($read); $contactSubType = array('Associate Supplier' => 'Associate', 'Foodservice Operator' => 'Food_Service_Operator'); $stateProvince = CRM_Core_PseudoConstant::stateProvinceForCountry(1039, 'abbreviation'); $errors['org'] = $errors['ind'] = $errors['mem'] = array(); $totalImported = 0; while ($rows = fgetcsv($read)) { CRM_Core_Error::debug('$rows', $rows[0] . '/n \\n'); $contactResult = civicrm_api3('Contact', 'get', array('return' => 'id', 'external_identifier' => $rows[0])); $membershipResult = array(); if (empty($contactResult['id'])) { $contactParams = array('external_identifier' => $rows[0], 'contact_type' => 'Organization', 'sort_name' => $rows[3], 'contact_sub_type' => $contactSubType[$rows[2]], 'display_name' => $rows[3], 'source' => 'Import via script', 'organization_name' => $rows[5], 'email' => $rows[19], 'api.Address.create' => array('location_type_id' => 3, 'is_primary' => 1, 'street_address' => $rows[7], 'city' => $rows[13], 'state_province_id' => array_search($rows[14], $stateProvince), 'postal_code' => $rows[15], 'country_id' => 1039), 'api.Phone.create' => array(array('location_type_id' => 3, 'is_primary' => 1, 'phone' => $rows[11], 'phone_type_id' => 1), array('location_type_id' => 3, 'is_primary' => 1, 'phone' => $rows[12], 'phone_type_id' => 3))); $contactResult = civicrm_api3('Contact', 'create', $contactParams); } else { $errors['org'][] = $rows[0]; // check for membership for CFRA ID $membershipResult = civicrm_api3('Membership', 'get', array('contact_id' => $contactResult['id'], 'return' => 'id')); }
/** * Reset the various system caches and some important static variables. */ public static function flushCache() { // flush out all cache entries so we can reload new data // a bit aggressive, but livable for now $cache = CRM_Utils_Cache::singleton(); $cache->flush(); // also reset the various static memory caches // reset the memory or array cache CRM_Core_BAO_Cache::deleteGroup('contact fields', NULL, FALSE); // reset ACL cache CRM_ACL_BAO_Cache::resetCache(); // reset various static arrays used here CRM_Contact_BAO_Contact::$_importableFields = CRM_Contact_BAO_Contact::$_exportableFields = CRM_Contribute_BAO_Contribution::$_importableFields = CRM_Contribute_BAO_Contribution::$_exportableFields = CRM_Pledge_BAO_Pledge::$_exportableFields = CRM_Contribute_BAO_Query::$_contributionFields = CRM_Core_BAO_CustomField::$_importFields = CRM_Core_BAO_Cache::$_cache = CRM_Core_DAO::$_dbColumnValueCache = NULL; CRM_Core_OptionGroup::flushAll(); CRM_Utils_PseudoConstant::flushAll(); }
/** * Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record * * @param string $daoName * Name of the DAO (Example: CRM_Contact_DAO_Contact to retrieve value from a contact). * @param int $searchValue * Value of the column you want to search by. * @param string $returnColumn * Name of the column you want to GET the value of. * @param string $searchColumn * Name of the column you want to search by. * @param bool $force * Skip use of the cache. * * @return string|null * Value of $returnColumn in the retrieved record */ public static function getFieldValue($daoName, $searchValue, $returnColumn = 'name', $searchColumn = 'id', $force = FALSE) { if (empty($searchValue) || trim(strtolower($searchValue)) == 'null') { // adding this year since developers forget to check for an id // or for the 'null' (which is a bad DAO kludge) // and hence we get the first value in the db CRM_Core_Error::fatal(); } $cacheKey = "{$daoName}:{$searchValue}:{$returnColumn}:{$searchColumn}"; if (self::$_dbColumnValueCache === NULL) { self::$_dbColumnValueCache = array(); } if (!array_key_exists($cacheKey, self::$_dbColumnValueCache) || $force) { $object = new $daoName(); $object->{$searchColumn} = $searchValue; $object->selectAdd(); $object->selectAdd($returnColumn); $result = NULL; if ($object->find(TRUE)) { $result = $object->{$returnColumn}; } $object->free(); self::$_dbColumnValueCache[$cacheKey] = $result; } return self::$_dbColumnValueCache[$cacheKey]; }