function sf_prematch_import in Salesforce Suite 7.2
Same name and namespace in other branches
- 6.2 sf_prematch/sf_prematch.main.inc \sf_prematch_import()
- 7 sf_prematch/sf_prematch.main.inc \sf_prematch_import()
@todo Please document this function.
See also
1 call to sf_prematch_import()
- sf_prematch_sf_find_match in sf_prematch/
sf_prematch.module - Implements hook_sf_find_match().
File
- sf_prematch/
sf_prematch.main.inc, line 163 - Import/Export functions for sf_prematch module.
Code
function sf_prematch_import($entity_name, $bundle_name, $object, $map, $match_by) {
// This is a generic solution only for object types of "user" or "node".
// If another object type is found, no prematching will be done.
// The developer should implement their own hook_sf_find_match() function
// to match this unsupportted type.
switch ($entity_name) {
case "user":
$id_name = "uid";
$table_name = "users";
$fields_table_name = 'profile_values';
$schema = drupal_get_schema($table_name);
$fields_schema = _sf_prematch_get_user_schema();
$fields_schema_field_suffix = '';
break;
case "node":
$id_name = "nid";
$table_name = "node";
$content_type = $map->drupal_bundle;
$schema = drupal_get_schema($table_name);
$fields_table_name = 'content_type_' . $content_type;
$fields_schema = drupal_get_schema($fields_table_name);
$fields_schema_field_suffix = "_value";
break;
default:
// Undefined object type
return;
break;
}
$primary_field = $match_by['primary_field'] != '0' ? $match_by['primary_field'] : '';
$secondary_field = $match_by['secondary_field'] != '0' ? $match_by['secondary_field'] : '';
$tertiary_field = $match_by['tertiary_field'] != '0' ? $match_by['tertiary_field'] : '';
$fields_table_used = FALSE;
$where = '';
$joins = '';
// Process primary_field
if (!empty($primary_field)) {
// Look for field in fields table
$sf_field_name = array_search($primary_field, $map->fields);
if (isset($fields_schema['fields'][$primary_field . $fields_schema_field_suffix])) {
if ($object_type == 'node') {
$where .= sprintf("%s.%s = '%s'", $fields_table_name, $primary_field . $fields_schema_field_suffix, $object->{$sf_field_name});
}
if ($object_type == 'user') {
$where .= sprintf("%s_p.fid = '%s' AND %s_p.value = '%s'", $fields_table_name, $fields_schema['fields'][$primary_field], $fields_table_name, $object->{$sf_field_name});
$joins .= sprintf("INNER JOIN {%s} %s_p ", $fields_table_name, $fields_table_name);
}
$fields_table_used = TRUE;
}
else {
// Look for field in main table
if (isset($schema['fields'][$primary_field])) {
$where .= sprintf("%s.%s = '%s'", $table_name, $primary_field, $object->{$primary_field});
}
else {
// The match_by field is in neither table
drupal_set_message(t('Primary field [@primary_field] specified in map prematch not defined.', array(
'@primary_field' => $primary_field,
)));
return;
}
}
// Process secondary_field
if (!empty($secondary_field)) {
// Look for field in fields table
$sf_field_name = array_search($secondary_field, $map->fields);
if (isset($fields_schema['fields'][$secondary_field . $fields_schema_field_suffix])) {
if ($object_type == 'node') {
$where .= sprintf(" AND %s.%s = '%s'", $fields_table_name, $secondary_field . $fields_schema_field_suffix, $object->{$sf_field_name});
}
if ($object_type == 'user') {
$where .= sprintf(" AND %s_s.fid = '%s' AND %s_s.value = '%s'", $fields_table_name, $fields_schema['fields'][$secondary_field], $fields_table_name, $object->{$sf_field_name});
$joins .= sprintf("INNER JOIN {%s} %s_s ", $fields_table_name, $fields_table_name);
}
$fields_table_used = TRUE;
}
else {
// Look for field in main table
if (isset($schema['fields'][$secondary_field])) {
$where .= sprintf(" AND %s.%s = '%s'", $table_name, $secondary_field, $object->{$secondary_field});
}
else {
// The match_by field is in neither table
drupal_set_message(t('Secondary field [@secondary_field] specified in map prematch not defined.', array(
'@secondary_field' => $secondary_field,
)));
return;
}
}
// Process tertiary_field
if (!empty($tertiary_field)) {
// Look for field in fields table
$sf_field_name = array_search($tertiary_field, $map->fields);
if (isset($fields_schema['fields'][$tertiary_field . $fields_schema_field_suffix])) {
if ($object_type == 'node') {
$where .= sprintf(" AND %s.%s = '%s'", $fields_table_name, $tertiary_field . $fields_schema_field_suffix, $object->{$sf_field_name});
}
if ($object_type == 'user') {
$where .= sprintf(" AND %s_t.fid = '%s' AND %s_t.value = '%s'", $fields_table_name, $fields_schema['fields'][$tertiary_field], $fields_table_name, $object->{$sf_field_name});
$joins .= sprintf("INNER JOIN {%s} %s_t ", $fields_table_name, $fields_table_name);
}
$fields_table_used = TRUE;
}
else {
// Look for field in main table
if (isset($schema['fields'][$tertiary_field])) {
$where .= sprintf(" AND %s.%s = '%s'", $table_name, $tertiary_field, $object->{$tertiary_field});
}
else {
// The match_by field is in neither table
drupal_set_message(t('Tertiary field [@tertiary_field] specified in map prematch not defined.'), array(
'@tertiary_field' => $tertiary_field,
));
return;
}
}
}
// end of tertiary_field
}
// end of secondary_field
}
// end of primary_field
$query_str = sprintf("SELECT %s.%s FROM {%s} ", $table_name, $id_name, $table_name);
if ($fields_table_used == TRUE && $object_type == 'node') {
$query_str .= sprintf("INNER JOIN {%s} ", $fields_table_name);
$where .= sprintf(" AND %s.%s=%s.%s", $table_name, $id_name, $fields_table_name, $id_name);
}
if ($fields_table_used == TRUE && $object_type == 'user') {
$query_str .= $joins;
$where .= sprintf(" AND %s.%s=%s_p.%s", $table_name, $id_name, $fields_table_name, $id_name);
}
$query_str .= sprintf("WHERE %s", $where);
if (empty($where)) {
// We shouldn't ever get here, but just in case...
return;
}
// Now that we have constructed the query string, execute it and store the results.
$found = array();
if (strlen($query_str) > 0) {
// TODO Please convert this statement to the D7 database API syntax.
$results = db_query($query_str);
while ($f = $results
->fetchField()) {
$found[] = $f;
}
}
if (count($found) > 0) {
// Return only the first match. Someday we may want to handle multiple matches.
return reset($found);
}
else {
// Nothing found so return nothing
return;
}
}