Work on import module

This commit is contained in:
Laurent Destailleur 2009-10-01 22:17:18 +00:00
parent 492f26f04b
commit ecd690e4d1
6 changed files with 51 additions and 153 deletions

View File

@ -1,100 +0,0 @@
<?php
/* Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
* Copyright (C) 2007-2009 Laurent Destailleur <eldy@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
* \file htdocs/imports/ajaximport.php
* \brief File to return Ajax response on Fields move in import page
* \version $Id$
*/
if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal
if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1');
if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1');
if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1');
if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1');
// This is to make Dolibarr working with Plesk
set_include_path($_SERVER['DOCUMENT_ROOT'].'/htdocs');
// Retrieve the entity in the cookie
$entityCookieName = "DOLENTITYID_dolibarr";
if (isset($_COOKIE[$entityCookieName])) $_SESSION["dol_entity"] = $_COOKIE[$entityCookieName];
require('../main.inc.php');
require_once(DOL_DOCUMENT_ROOT."/imports/import.class.php");
require_once(DOL_DOCUMENT_ROOT.'/includes/modules/import/modules_import.php');
// Enregistrement de la position des champs
dol_syslog("AjaxImport boxorder=".$_GET['boxorder']." userid=".$_GET['userid']." datatoimport=".$_GET["datatoimport"], LOG_DEBUG);
$part=split(':',$_GET['boxorder']);
$colonne=$part[0];
$list=$part[1];
dol_syslog('AjaxImport column='.$colonne.' list='.$list);
$fuser=new User($db);
$fuser->id=$_GET['userid'];
$fuser->fetch();
// Init object $objimport that describe the predefined import
$objimport=new Import($db);
$datatoimport=isset($_GET["datatoimport"])? $_GET["datatoimport"] : (isset($_POST["datatoimport"])?$_POST["datatoimport"]:'');
$objimport->load_arrays($fuser,$datatoimport);
// Init targets fields array
$fieldstarget=$objimport->array_import_fields[0];
// Reinit match arrays. We redefine array_match_file_to_database
$serialized_array_match_file_to_database='';
$array_match_file_to_database=array();
$fieldsarray=split(',',$list);
$pos=0;
foreach($fieldsarray as $fieldnb) // For each elem in list. fieldnb start from 1 to ...
{
// Get name of database fields at position $pos and put it into $namefield
$posbis=0;$namefield='';
foreach($fieldstarget as $key => $val) // key: val:
{
//dol_syslog('AjaxImport key='.$key.' val='.$val);
if ($posbis < $pos)
{
$posbis++;
continue;
}
// We found the key of targets that is at position pos
$namefield=$key;
//dol_syslog('AjaxImport Field name found for file field nb '.$fieldnb.'='.$namefield);
break;
}
if ($fieldnb && $namefield)
{
$array_match_file_to_database[$fieldnb]=$namefield;
if ($serialized_array_match_file_to_database) $serialized_array_match_file_to_database.=',';
$serialized_array_match_file_to_database.=($fieldnb.'='.$namefield);
}
$pos++;
}
// We save new matching in session
$_SESSION["dol_array_match_file_to_database"]=$serialized_array_match_file_to_database;
dol_syslog('AjaxImport dol_array_match_file_to_database='.$serialized_array_match_file_to_database);
?>

View File

@ -77,10 +77,10 @@ $objimport=new Import($db);
$objimport->load_arrays($user,$datatoimport);
$objmodelimport=new ModeleImports();
$html = new Form($db);
$htmlother = new FormOther($db);
$formfile = new FormFile($db);
$sqlusedforimport='';
// Init $array_match_file_to_database from _SESSION
$serialized_array_match_file_to_database=isset($_SESSION["dol_array_match_file_to_database"])?$_SESSION["dol_array_match_file_to_database"]:'';
@ -137,7 +137,6 @@ if ($action == 'builddoc')
else
{
$mesg='<div class="ok">'.$langs->trans("FileSuccessfullyBuilt").'</div>';
$sqlusedforimport=$objimport->sqlusedforimport;
}
}
@ -490,6 +489,8 @@ if ($step == 3 && $datatoimport)
print $objimport->array_import_label[0];
print '</td></tr>';
print '<tr><td colspan="2"><b>'.$langs->trans("InformationOnSourceFile").'</b></td></tr>';
// Source file format
print '<tr><td width="25%">'.$langs->trans("SourceFileFormat").'</td>';
print '<td>';
@ -574,7 +575,17 @@ if ($step == 3 && $datatoimport)
// Page to make matching between source file and database fields
if ($step == 4 && $datatoimport)
{
$model=$format;
// Create classe to use for import
$dir = DOL_DOCUMENT_ROOT . "/includes/modules/import/";
$file = "import_".$model.".modules.php";
$classname = "Import".ucfirst($model);
require_once($dir.$file);
$obj = new $classname($db);
// Load source fields in input file
$obj->import_open_file($dir.$file,$langs);
$fieldssource=array(
1=>array('name'=>'aa','example1'=>'val1','example2'=>'val2'),
2=>array('name'=>'bb','example1'=>'valb1','example2'=>'valb2'),
@ -586,6 +597,7 @@ if ($step == 4 && $datatoimport)
8=>array('name'=>'hh','example1'=>'valc1','example2'=>'valc2'),
9=>array('name'=>'ii','example1'=>'valc1','example2'=>'valc2'),
*/ );
$obj->import_close_file();
// Load targets fields in database
$fieldstarget=$objimport->array_import_fields[0];
@ -672,6 +684,8 @@ if ($step == 4 && $datatoimport)
print $objimport->array_import_label[0];
print '</td></tr>';
print '<tr><td colspan="2"><b>'.$langs->trans("InformationOnSourceFile").'</b></td></tr>';
// Source file format
print '<tr><td width="25%">'.$langs->trans("SourceFileFormat").'</td>';
print '<td>';
@ -860,14 +874,14 @@ if ($step == 4 && $datatoimport)
//alert( \'boxorder=\' + boxorder )."\n";
//print 'var userid = \''.$user->id.'\';'."\n";
//print 'var url = "ajaximport.php";'."\n";
print 'var datatoimport = "'.$datatoimport.'";'."\n";
print 'var newlocation= \''.$_SERVER["PHP_SELF"].'?step=4&action=saveorder&boxorder=\' + boxorder + \'&datatoimport=\' + datatoimport;'."\n";
//print 'var datatoimport = "'.$datatoimport.'";'."\n";
print 'var newlocation= \''.$_SERVER["PHP_SELF"].'?step=4&format='.$format.'&datatoimport='.urlencode($datatoimport).'&filetoimport='.urlencode($filetoimport).'&action=saveorder&boxorder=\' + boxorder;'."\n";
//print 'alert(newlocation);';
//print 'o_options = new Object();'."\n";
//print 'o_options = {asynchronous:false,method: \'get\',parameters: \'step=4&boxorder=\' + boxorder + \'&userid=\' + userid + \'&datatoimport=\' + datatoimport};'."\n";
//print 'var myAjax = new Ajax.Request(url, o_options);'."\n";
// Now reload page
print 'window.location.href=newlocation';
print 'window.location.href=newlocation;'."\n";
print '}'."\n";
print "\n";
@ -981,7 +995,17 @@ if ($step == 5 && $datatoimport)
{
if (empty($dontimportfirstline)) $dontimportfirstline=0;
$model=$format;
// Create classe to use for import
$dir = DOL_DOCUMENT_ROOT . "/includes/modules/import/";
$file = "import_".$model.".modules.php";
$classname = "Import".ucfirst($model);
require_once($dir.$file);
$obj = new $classname($db);
// Load source fields in input file
$obj->import_open_file($dir.$file,$langs);
$fieldssource=array(
1=>array('name'=>'aa','example1'=>'val1','example2'=>'val2'),
2=>array('name'=>'bb','example1'=>'valb1','example2'=>'valb2'),
@ -993,6 +1017,7 @@ if ($step == 5 && $datatoimport)
8=>array('name'=>'hh','example1'=>'valc1','example2'=>'valc2'),
9=>array('name'=>'ii','example1'=>'valc1','example2'=>'valc2'),
*/ );
$obj->import_close_file();
ksort($array_match_file_to_database);
//var_dump($array_match_file_to_database);
@ -1042,6 +1067,8 @@ if ($step == 5 && $datatoimport)
print $objimport->array_import_label[0];
print '</td></tr>';
print '<tr><td colspan="2"><b>'.$langs->trans("InformationOnSourceFile").'</b></td></tr>';
// Source file format
print '<tr><td width="25%">'.$langs->trans("SourceFileFormat").'</td>';
print '<td>';
@ -1077,6 +1104,8 @@ if ($step == 5 && $datatoimport)
print '<input type="checkbox" name="nofirstline" value='.$dontimportfirstline.'>';
print '</td></tr>';
print '<tr><td colspan="2"><b>'.$langs->trans("InformationOnTargetTables").'</b></td></tr>';
// Tables imported
print '<tr><td>';
print $langs->trans("TablesTarget");
@ -1090,7 +1119,7 @@ if ($step == 5 && $datatoimport)
$alias=eregi_replace('\..*$','',$label);
$listtables[$alias]=$objimport->array_import_tables[0][$alias];
}
print sizeof($listtables)?(join(',',$listtables)):$langs->trans("Error");
print sizeof($listtables)?(join(', ',$listtables)):$langs->trans("Error");
print '</td></tr>';
// Fields imported
@ -1107,7 +1136,7 @@ if ($step == 5 && $datatoimport)
$alias=eregi_replace('\..*$','',$label);
$listfields[$i]=$label;
}
print sizeof($listfields)?(join(',',$listfields)):$langs->trans("Error");
print sizeof($listfields)?(join(', ',$listfields)):$langs->trans("Error");
print '</td></tr>';
print '</table>';

View File

@ -153,11 +153,11 @@ class ImportCsv extends ModeleImports
/**
* \brief Open output file
* \brief Open input file
* \param file Path of filename
* \return int <0 if KO, >=0 if OK
*/
function open_file($file,$outputlangs)
function import_open_file($file)
{
global $langs;
@ -165,8 +165,7 @@ class ImportCsv extends ModeleImports
$ret=1;
$outputlangs->load("exports");
$this->handle = fread($file, "wt");
$this->handle = fopen($file, "r");
if (! $this->handle)
{
$langs->load("errors");
@ -178,19 +177,18 @@ class ImportCsv extends ModeleImports
}
/**
* \brief Output header into file
* \param langs Output language
* \brief Input header line from file
*/
function read_header($outputlangs)
function import_read_header()
{
return 0;
}
/**
* \brief Output record line into file
* \brief Input record line from file
*/
function read_record($array_alias,$array_selected_sorted,$objp,$outputlangs)
function import_read_record($array_alias,$array_selected_sorted,$objp)
{
global $conf;
if (! empty($conf->global->EXPORT_CSV_FORCE_CHARSET)) $outputlangs->charset_output=$conf->global->EXPORT_CSV_FORCE_CHARSET;
@ -220,12 +218,13 @@ class ImportCsv extends ModeleImports
/**
* \brief Close file handle
*/
function close_file()
function import_close_file()
{
fclose($this->handle);
return 0;
}
/**
* Clean a cell to respect rules of CSV file cells
* @param newvalue String to clean

View File

@ -142,40 +142,6 @@ class ModeleImports
return $this->libversion[$key];
}
/**
* \brief Lance lecture fichier
* \remarks Les tableaux array_import_xxx sont deja chargees pour le bon datatoexport
*/
function load_file($model, $array_selected)
{
global $langs;
dol_syslog("Import::load_file $model, $array_selected");
// Creation de la classe d'export du model ImportXXX
$dir = DOL_DOCUMENT_ROOT . "/includes/modules/import/";
$file = "import_".$model.".modules.php";
$classname = "Import".$model;
require_once($dir.$file);
$obj = new $classname($db);
// Execute requete import
$sql=$this->array_export_sql[0];
$resql = $this->db->query($sql);
if ($resql)
{
}
else
{
$this->error=$this->db->error();
dol_syslog("Error: sql=$sql ".$this->error, LOG_ERR);
return -1;
}
}
}

View File

@ -77,4 +77,6 @@ NbOfSourceLines=Number of lines in source file
NowClickToTestTheImport=Check import parameters you have defined. If they are correct, click on button "<b>%s</b>" to launch a simulation of import process (no data will be changed in your database, it's only a simulation for the moment)...
RunSimulateImportFile=Launch the import simulation
FieldNeedSource=This fiels in database require a data from source file
SomeMandatoryFieldHaveNoSource=Some mandatory fields have no source from data file
SomeMandatoryFieldHaveNoSource=Some mandatory fields have no source from data file
InformationOnSourceFile=Informations on source file
InformationOnTargetTables=Informations on target fields

View File

@ -77,4 +77,6 @@ NbOfSourceLines=Nombre de lignes du fichier source
NowClickToTestTheImport=Vérifiez les paramètres d'import que vous avez défini. S'ils vous conviennent, cliquez sur le bouton "<b>%s</b>" pour lancer une simulation d'import (aucune donnée ne sera modifié, il s'agit dans un premier temps d'une simple simulation)...
RunSimulateImportFile=Lancer la simulation d'import
FieldNeedSource=Ce champ en base requiert obligatoirement une donnée source
SomeMandatoryFieldHaveNoSource=Certains champs obligatoires n'ont pas de champ source issus du fichier
SomeMandatoryFieldHaveNoSource=Certains champs obligatoires n'ont pas de champ source issus du fichier
InformationOnSourceFile=Informations sur le fichier source
InformationOnTargetTables=Informations sur les champs cibles