public function __construct($tableName = NULL, $createSql = NULL, $createTable = FALSE) { $dao = new CRM_Core_DAO(); $db = $dao->getDatabaseConnection(); if ($createTable) { if (!$createSql) { CRM_Core_Error::fatal('Either an existing table name or an SQL query to build one are required'); } // FIXME: we should regen this table's name if it exists rather than drop it if (!$tableName) { $tableName = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE)); } $db->query("DROP TABLE IF EXISTS {$tableName}"); $db->query("CREATE TABLE {$tableName} ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci {$createSql}"); } if (!$tableName) { CRM_Core_Error::fatal('Import Table is required.'); } $this->_tableName = $tableName; //initialize the properties. $properties = array('mapperKeys', 'mapperRelated', 'mapperLocTypes', 'mapperPhoneTypes', 'mapperImProviders', 'mapperWebsiteTypes', 'mapperRelatedContactType', 'mapperRelatedContactDetails', 'mapperRelatedContactLocType', 'mapperRelatedContactPhoneType', 'mapperRelatedContactImProvider', 'mapperRelatedContactWebsiteType'); foreach ($properties as $property) { $this->{"_{$property}"} = array(); } }
public function __construct($tableName = null, $createSql = null, $createTable = false) { $dao = new CRM_Core_DAO(); $db = $dao->getDatabaseConnection(); if ($createTable) { if (!$createSql) { CRM_Core_Error::fatal('Either an existing table name or an SQL query to build one are required'); } // FIXME: we should regen this table's name if it exists rather than drop it if (!$tableName) { $tableName = 'civicrm_import_job_' . md5(uniqid(rand(), true)); } $db->query("DROP TABLE IF EXISTS {$tableName}"); $db->query("CREATE TABLE {$tableName} ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci {$createSql}"); } if (!$tableName) { CRM_Core_Error::fatal('Import Table is required.'); } $this->_tableName = $tableName; $this->_mapperKeys = array(); $this->_mapperLocTypes = array(); $this->_mapperPhoneTypes = array(); $this->_mapperImProviders = array(); $this->_mapperRelated = array(); $this->_mapperRelatedContactType = array(); $this->_mapperRelatedContactDetails = array(); $this->_mapperRelatedContactLocType = array(); $this->_mapperRelatedContactPhoneType = array(); $this->_mapperRelatedContactImProvider = array(); }
/** * Test the to csv function. * * @param string $fileName * * @dataProvider getCsvFiles */ public function testToCsv($fileName) { $dataSource = new CRM_Import_DataSource_Csv(); $params = array('uploadFile' => array('name' => __DIR__ . '/' . $fileName), 'skipColumnHeader' => TRUE); // Get the PEAR::DB object $dao = new CRM_Core_DAO(); $db = $dao->getDatabaseConnection(); $form = new CRM_Contact_Import_Form_DataSource(); $form->controller = new CRM_Contact_Import_Controller(); $dataSource->postProcess($params, $db, $form); $tableName = $form->get('importTableName'); $this->assertEquals(4, CRM_Core_DAO::singleValueQuery("SELECT LENGTH(last_name) FROM {$tableName}"), $fileName . ' failed on last_name'); $this->assertEquals(21, CRM_Core_DAO::singleValueQuery("SELECT LENGTH(email) FROM {$tableName}"), $fileName . ' failed on email'); CRM_Core_DAO::executeQuery("DROP TABLE {$tableName}"); }
/** * Call the DataSource's postProcess method to take over * and then setup some common data structures for the next step * * @return void * @access public */ public function postProcess() { $this->controller->resetPage('MapField'); if ($this->_dataSourceIsValid) { // Setup the params array $this->_params = $this->controller->exportValues($this->_name); $storeParams = array('onDuplicate' => 'onDuplicate', 'dedupe' => 'dedupe', 'contactType' => 'contactType', 'contactSubType' => 'subType', 'dateFormats' => 'dateFormats', 'savedMapping' => 'savedMapping'); foreach ($storeParams as $storeName => $storeValueName) { ${$storeName} = $this->exportValue($storeValueName); $this->set($storeName, ${$storeName}); } $this->set('dataSource', $this->_params['dataSource']); $this->set('skipColumnHeader', CRM_Utils_Array::value('skipColumnHeader', $this->_params)); $session = CRM_Core_Session::singleton(); $session->set('dateTypes', $dateFormats); // Get the PEAR::DB object $dao = new CRM_Core_DAO(); $db = $dao->getDatabaseConnection(); //hack to prevent multiple tables. $this->_params['import_table_name'] = $this->get('importTableName'); if (!$this->_params['import_table_name']) { $this->_params['import_table_name'] = 'civicrm_import_job_' . md5(uniqid(rand(), TRUE)); } $this->_dataSourceClass->postProcess($this->_params, $db, $this); // We should have the data in the DB now, parse it $importTableName = $this->get('importTableName'); $fieldNames = $this->_prepareImportTable($db, $importTableName); $mapper = array(); $parser = new CRM_Contact_Import_Parser_Contact($mapper); $parser->setMaxLinesToProcess(100); $parser->run($importTableName, $mapper, CRM_Import_Parser::MODE_MAPFIELD, $contactType, $fieldNames['pk'], $fieldNames['status'], CRM_Import_Parser::DUPLICATE_SKIP, NULL, NULL, FALSE, CRM_Contact_Import_Parser::DEFAULT_TIMEOUT, $contactSubType, $dedupe); // add all the necessary variables to the form $parser->set($this); } else { CRM_Core_Error::fatal("Invalid DataSource on form post. This shouldn't happen!"); } }
/** * Set variables up before form is built. */ public function preProcess() { $dataSource = $this->get('dataSource'); $skipColumnHeader = $this->get('skipColumnHeader'); $this->_mapperFields = $this->get('fields'); $this->_importTableName = $this->get('importTableName'); $this->_onDuplicate = $this->get('onDuplicate'); $highlightedFields = array(); $highlightedFields[] = 'email'; $highlightedFields[] = 'external_identifier'; //format custom field names, CRM-2676 switch ($this->get('contactType')) { case CRM_Import_Parser::CONTACT_INDIVIDUAL: $contactType = 'Individual'; $highlightedFields[] = 'first_name'; $highlightedFields[] = 'last_name'; break; case CRM_Import_Parser::CONTACT_HOUSEHOLD: $contactType = 'Household'; $highlightedFields[] = 'household_name'; break; case CRM_Import_Parser::CONTACT_ORGANIZATION: $contactType = 'Organization'; $highlightedFields[] = 'organization_name'; break; } $this->_contactType = $contactType; if ($this->_onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { unset($this->_mapperFields['id']); } else { $highlightedFields[] = 'id'; } if ($this->_onDuplicate != CRM_Import_Parser::DUPLICATE_NOCHECK) { //Mark Dedupe Rule Fields as required, since it's used in matching contact foreach (array('Individual', 'Household', 'Organization') as $cType) { $ruleParams = array('contact_type' => $cType, 'used' => 'Unsupervised'); $this->_dedupeFields[$cType] = CRM_Dedupe_BAO_Rule::dedupeRuleFields($ruleParams); } //Modify mapper fields title if fields are present in dedupe rule if (is_array($this->_dedupeFields[$contactType])) { foreach ($this->_dedupeFields[$contactType] as $val) { if ($valTitle = CRM_Utils_Array::value($val, $this->_mapperFields)) { $this->_mapperFields[$val] = $valTitle . ' (match to contact)'; } } } } // retrieve and highlight required custom fields $formattedFieldNames = $this->formatCustomFieldName($this->_mapperFields); self::$customFields = CRM_Core_BAO_CustomField::getFields($this->_contactType); foreach (self::$customFields as $key => $attr) { if (!empty($attr['is_required'])) { $highlightedFields[] = "custom_{$key}"; } } $this->assign('highlightedFields', $highlightedFields); $this->_formattedFieldNames[$contactType] = $this->_mapperFields = array_merge($this->_mapperFields, $formattedFieldNames); $columnNames = array(); //get original col headers from csv if present. if ($dataSource == 'CRM_Import_DataSource_CSV' && $skipColumnHeader) { $columnNames = $this->get('originalColHeader'); } else { // get the field names from the temp. DB table $dao = new CRM_Core_DAO(); $db = $dao->getDatabaseConnection(); $columnsQuery = "SHOW FIELDS FROM {$this->_importTableName}\n WHERE Field NOT LIKE '\\_%'"; $columnsResult = $db->query($columnsQuery); while ($row = $columnsResult->fetchRow(DB_FETCHMODE_ASSOC)) { $columnNames[] = $row['Field']; } } $showColNames = TRUE; if ($dataSource == 'CRM_Import_DataSource_CSV' && !$skipColumnHeader) { $showColNames = FALSE; } $this->assign('showColNames', $showColNames); $this->_columnCount = count($columnNames); $this->_columnNames = $columnNames; $this->assign('columnNames', $columnNames); //$this->_columnCount = $this->get( 'columnCount' ); $this->assign('columnCount', $this->_columnCount); $this->_dataValues = $this->get('dataValues'); $this->assign('dataValues', $this->_dataValues); $this->assign('rowDisplayCount', 2); }
/** * Clean up the import table we used. * * @return void */ public function postProcess() { $dao = new CRM_Core_DAO(); $db = $dao->getDatabaseConnection(); $importTableName = $this->get('importTableName'); // do a basic sanity check here if (strpos($importTableName, 'civicrm_import_job_') === 0) { $query = "DROP TABLE IF EXISTS {$importTableName}"; $db->query($query); } }
/** * Update the record with PK $id in the import database table * * @param int $id * @param array $params * * @return void * @access public */ public function updateImportRecord($id, &$params) { $statusFieldName = $this->_statusFieldName; $primaryKeyName = $this->_primaryKeyName; if ($statusFieldName && $primaryKeyName) { $dao = new CRM_Core_DAO(); $db = $dao->getDatabaseConnection(); $query = "UPDATE {$this->_tableName}\n SET {$statusFieldName} = ?,\n {$statusFieldName}Msg = ?\n WHERE {$primaryKeyName} = ?"; $args = array($params[$statusFieldName], CRM_Utils_Array::value("{$statusFieldName}Msg", $params), $id); //print "Running query: $query<br/>With arguments: ".$params[$statusFieldName].", ".$params["${statusFieldName}Msg"].", $id<br/>"; $db->query($query, $args); } }
/** * @param $dsn * @param string $fileName * @param bool $lineMode */ public static function loadCaseSampleData($fileName, $lineMode = FALSE) { $dao = new CRM_Core_DAO(); $db = $dao->getDatabaseConnection(); $domain = new CRM_Core_DAO_Domain(); $domain->find(TRUE); $multiLingual = (bool) $domain->locales; $smarty = CRM_Core_Smarty::singleton(); $smarty->assign('multilingual', $multiLingual); $smarty->assign('locales', explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales)); if (!$lineMode) { $string = $smarty->fetch($fileName); // change \r\n to fix windows issues $string = str_replace("\r\n", "\n", $string); //get rid of comments starting with # and -- $string = preg_replace("/^#[^\n]*\$/m", "\n", $string); $string = preg_replace("/^(--[^-]).*/m", "\n", $string); $queries = preg_split('/;$/m', $string); foreach ($queries as $query) { $query = trim($query); if (!empty($query)) { $res =& $db->query($query); if (PEAR::isError($res)) { die("Cannot execute {$query}: " . $res->getMessage()); } } } } else { $fd = fopen($fileName, "r"); while ($string = fgets($fd)) { $string = preg_replace("/^#[^\n]*\$/m", "\n", $string); $string = preg_replace("/^(--[^-]).*/m", "\n", $string); $string = trim($string); if (!empty($string)) { $res =& $db->query($string); if (PEAR::isError($res)) { die("Cannot execute {$string}: " . $res->getMessage()); } } } } }