$info) { $fields[$field['field_name'] .'_'. $column] = $field['widget']['label'] .' '. $column .' ('. $field['field_name'] .')'; } } } return $fields; } /** * Implementation of hook_node_import_prepare(). */ function content_node_import_prepare(&$node, $preview = FALSE) { $errors = array(); $globals = $node->content_import_node; $types = (array) content_types(); if (isset($types[$node->type])) { $content_type = $types[$node->type]; foreach ($content_type['fields'] as $field) { $field_name = $field['field_name']; // Find the column names for this field type and process each. $db_info = content_database_info($field); foreach ($db_info['columns'] as $column => $info) { $dummy_name = $field['field_name'] .'_'. $column; // Force a node value for missing data. if (empty($node->$dummy_name)) { $values = array(0 => $globals['fields'][$dummy_name]); } else { // Explode multiple values to create the $delta and $value for each. if ($field['multiple']) { $values = explode('||', $node->$dummy_name); } else { $values = array(0 => $node->$dummy_name); } } foreach ($values as $delta => $value) { switch ($field['type']) { case 'date': case 'datestamp': // Get properly formatted date for value column. // Assume timezone and offset values are correct as input. if ($column == 'value') { include_once(drupal_get_path('module', 'date') .'/date.inc'); if ($field['type'] == 'date') { // Is the imported date a unix timestamp? If so convert it to ISO date. if (is_numeric($value) && date_is_valid($value, DATE_UNIX)) { $value = date_unix2iso($value); } // If the imported date is a (partial) ISO date, convert it // to date's ISO date. else if (($date_array = date_iso2array($value)) != 'ERROR') { $value = date_array2iso($date_array); } // Otherwise, use strtotime and unix2iso to convert // partial ISO or text date into full ISO. else { $value = date_unix2iso(strtotime($value)); } } else { // Is this already a unix timestamp? If not try strototime. if (!is_numeric($value) || !date_is_valid($value, DATE_UNIX)) { $value = strtotime($value); } } } break; case 'nodereference': // If $value is numeric use it, otherwise // find a node title that matches and get nid. if (intval($value) > 0) { $value = intval($value); } else if($value != '') { $referenced_nodes = array(); $sql = "SELECT nid, title FROM {node} WHERE title SOUNDS LIKE '%s'"; $result = db_query($sql, $value); $record_found = db_fetch_object($result); if ($record_found) { $value = $record_found->nid; } } break; case 'userreference': // If $value is numeric use it, otherwise // find a user name that matches and get uid. if (intval($value) > 0) { $value = intval($value); } else { if (($account = user_load(array('name' => $author['name'])))) { $value = $account->uid; } } break; case 'number_integer': // Make sure numeric data is brought in as numeric values. $value = intval($value); break; case 'number_decimal': $value = floatval($value); break; case 'image': $value=trim($value); //this will correct whitespace typos in the csv if ($column =='fid' && !empty($node->$dummy_name)){ $node->$dummy_name=file_create_path($field['widget']['image_path'].'/'.$value); if (!$node->$dummy_name || !file_exists($node->$dummy_name) ) { $errors[]=t('This file does not exist: ').$field['widget']['image_path'].'/'.$value; unset($node->$dummy_name); break; } // your content filters must allow images! if ($preview){ $node->body .= theme('image', $node->$dummy_name, $value, $value); } } break; case 'file_audio': $value=trim($value); //this will correct whitespace typos in the csv if ($column =='fid' && !empty($node->$dummy_name)){ $node->$dummy_name=file_create_path($field['widget']['upload_path'].'/'.$value); if (!$node->$dummy_name || !file_exists($node->$dummy_name) ) { $errors[]=t('This file does not exist: ').$field['widget']['upload_path'].'/'.$value; unset($node->$dummy_name); break; } } break; } // Set the correct node field values. if (!is_array($node->$field['field_name'])) { $node->$field_name = array($delta => array($column => $value)); } else { $node->$field_name += array($delta => array($column => $value)); } } } } } return $errors; } /* * post-process the node, adding files */ function content_node_import_postprocess(&$node, $preview = FALSE, $any_errors=FALSE){ // no post-node-processing on preview. if ($preview) return; $files=array(); $types = (array) content_types(); if (isset($types[$node->type])) { $content_type = $types[$node->type]; foreach ($content_type['fields'] as $fieldname=>$field) { $dummy_name="{$fieldname}_fid"; if (isset($node->$dummy_name)){ // fid is set. switch($field['type']){ case 'image': case "file_audio": if (!isset($files[$fieldname])){ // first pass, or last time failed. $files[$fieldname]=array( 'fid'=>db_next_id('{files}_fid'), 'filepath'=> $node->$dummy_name, 'filename'=>basename($node->$dummy_name), 'filemime'=>mime_content_type($node->$dummy_name), 'filesize'=>filesize($node->$dummy_name), ); $ok=db_query("INSERT into {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s','%s','%s',%d)", $files[$fieldname]['fid'], $node->nid, $files[$fieldname]['filename'], $files[$fieldname]['filepath'], $files[$fieldname]['filemime'], $files[$fieldname]['filesize'] ); if (!$ok){ // this will try again on the next pass of this loop, and finally not add it to the files array. unset($files[$fieldname]); } } break; } } } } // second pass, save images foreach($files as $fieldname=>$file){ $node->$fieldname=array(array( 'fid'=>$file['fid'], )); $node->{$fieldname.'_fid'}=$file['fid']; $node->{$fieldname}[0]['alt']=(isset($node->{$fieldname.'_alt'}))? $node->{$fieldname.'_alt'} : $file['filename']; $node->{$fieldname}[0]['title']=(isset($node->{$fieldname.'_title'}))? $node->{$fieldname.'_title'} : $file['filename']; } // re-save node with new file data node_save($node); // Unset the dummy column value. unset($node->$dummy_name); } // add support for depracated function if (!function_exists('mime_content_type')) { function mime_content_type($filename) { $finfo = finfo_open(FILEINFO_MIME); $mimetype = finfo_file($finfo, $filename); finfo_close($finfo); return $mimetype; } } /** * Implementation of hook_node_import_global(). */ function content_node_import_global($type, $global_values) { $form = array(); $form['content_import_node']['fields'] = array( '#type' => 'fieldset', '#title' => t('Field options'), '#description' => t('Select the default values you want to assign to all imported fields unless specifically set otherwise in the CSV file'), '#tree' => TRUE, ); $types = (array) content_types(); if (isset($types[$type])) { $content_type = $types[$type]; foreach ($content_type['fields'] as $field) { // Create a default value for each field column by creating a dummy name for each column. $db_info = content_database_info($field); foreach ($db_info['columns'] as $column => $info) { $value = ((!empty($field['widget']['default_value'][0]['value'])) ? $field['widget']['default_value'][0]['value'] : ''); $form['content_import_node']['fields'][$field['field_name'] .'_'. $column] = array( '#type' => 'textfield', '#title' => $field['widget']['label'] .' '. $column, ); if (!empty($field['widget']['default_value'][0]['value'])){ $form['content_import_node']['fields'][$field['field_name'] .'_'. $column]['#default_value']=$field['widget']['default_value'][0]['value']; } } } } return $form; }