version 5.x status ?

CommentFileSizeAuthor
#4 import_script_cck_0.txt6.71 KBgreenmachine

Comments

coupet’s picture

---Cross-Posting ---
Snippets to import data into two related CCK nodes from CSV data
http://groups.drupal.org/node/2197#comments

http://groups.drupal.org/user/1795
So, making an adjustment in the code (taking into account that node is an array and not an object until we make it so, just before node_save), the full snippet works perfectly as follows (notice that we have inserted the assignment to node['taxonomy'], and commented out the previous call to db_query after the call to node_save, which now requires nothing further):

<?php

// Written by Victor Kane - victorkane at awebfactory dot com dot ar
// *** This script affects your database
//
// CONFIGURATION

// Change the value below to TRUE when you want to run the script After running, immediately
// change back to FALSE in order to prevent accidentally executing this script twice.
$active = TRUE;

// CODE
include_once "includes/bootstrap.inc";
include_once("includes/common.inc");
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$content_type = 'contacto';
$content_name = 'Contacto';
$archivo_datos = 'backup/test.csv';

if ($active) {
if (user_access("administer content types")) {
insertar_contenido($content_type, $archivo_datos);
} else {
print "No tiene permisos para administer content types";
}
} else {
print "exportar-empresas no ha sido activado. Ver variable $active al principio del código fuente";
}

function insertar_contenido ($content_type, $archivo_datos) {
$handle = fopen($archivo_datos, "r");
$theHeaders = fgetcsv ($handle, 4096, "\t");
$lineno = 0;
while ($line = fgetcsv ($handle, 4096, "\t")) {
$output .= '';
$valueno = 0;
$lineno++;
$output .= '

    Línea: '.$lineno;
    $observaciones = '';
    foreach ($line as $value) {
    if ($value) {
    $output .= '
  • '.$theHeaders[$valueno].': '.$value.'
  • ';
    $observaciones .= '

  • '.$theHeaders[$valueno].': '.$value.'
  • ';
    }
    $valueno++;
    }
    $output .= '

';
$node = array();
$node['title'] = $line[3];
$node['body'] = $observaciones;
$node['type'] = $content_type;
$node['format'] = 3;
//$node['taxonomy'] = $_REQUEST['taxonomy'];
$node['name'] = $content_name;
//$node['date'] = $_REQUEST['date'];
$node['status'] = 1;
$node['promote'] = 0;
$node['sticky'] = 0;
$log = 'Importado por importar-contactos el ' . date('g:i:s a');
$node['log'] = $log;

$node['field_empresa'] = array (
0 => array(
'nid' => db_result(db_query("SELECT nid FROM {node} WHERE title = '%s'", $line[2])),
),
);

$node['field_cargo'] = array (
0 => array(
'value' => $line[22],
),
);
$node['field_saludo'] = array (
0 => array(
'value' => $line[17],
),
);
$node['field_nombres'] = array (
0 => array(
'value' => $line[54],
),
);
$node['field_apellidos'] = array (
0 => array(
'value' => $line[55],
),
);
$node['field_mvil'] = array (
0 => array(
'value' => $line[15],
),
);
$node['field_telfono_particular'] = array (
0 => array(
'value' => $line[14],
),
);
$node['field_buscapersonas'] = array (
0 => array(
'value' => $line[16],
),
);
$node['field_e_mail'] = array (
0 => array(
'value' => $line[89],
),
);

// Marc of funnymonkey.com suggestion: the integer '2' is tid
$node['taxonomy'] = array(2);

/*
// this code, from the autosave module, is unnecessary, since node_save will do it for us :)
$node['nid'] = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", 'node_nid')) + 1;
$node['vid'] = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", 'node_revisions_vid')) + 1;
*/
if ($node['title']) {
$node = (object)$node;
$node = node_submit($node);
node_save($node);
print $output;
/*
$nid = $node->nid;
$tid = 6;
db_query("INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)", $nid, $tid);
*/
}
}
}

and afterwards, using the devel module, we can see the value has been placed accordingly when we hit the "Devel load" tab, and see:

Array
(
[4] => stdClass Object
(
[tid] => 4
[vid] => 3
[name] => Nuevo
[description] =>
[weight] => 0
)

)

As for the call to node_submit, I included it because when I saw it included in the autosave module, I checked out the CCK code, and it looked like there was a lot of housekeeping done possibly on certain kinds of CCK objects, so... I may be wrong but I went ahead and included it. It does work without that call, though.

Thanks, once again, Marc!

Victor Kane
http://awebfactory.com.ar

coupet’s picture

Status: Active » Closed (duplicate)

Tougher than expected?
http://drupal.org/node/112173

coupet’s picture

Status: Closed (duplicate) » Closed (fixed)
greenmachine’s picture

StatusFileSize
new6.71 KB

In case anyone else finds this thread in hopes of an import solution for Drupal 5, here is my modified version of Victor's code (above) with some improvements and translation into English:

http://groups.drupal.org/node/2197#comment-11129

The code is also attached here. Please see the link above for instructions and context.