2009-03-25 22:26:15 +01:00
< ? php
2011-09-10 14:21:41 +02:00
/* Copyright ( C ) 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
2009-03-25 22:26:15 +01:00
*
* 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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2009-03-25 22:26:15 +01:00
* ( 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
2011-08-01 01:45:11 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2009-03-25 22:26:15 +01:00
*/
/**
2010-07-21 19:39:17 +02:00
* \file htdocs / imports / class / import . class . php
2009-03-25 22:26:15 +01:00
* \ingroup import
2009-10-04 00:32:10 +02:00
* \brief File of class to manage imports
2009-03-25 22:26:15 +01:00
*/
/**
* \class Import
2009-10-01 13:48:20 +02:00
* \brief Class to manage imports
2009-03-25 22:26:15 +01:00
*/
class Import
{
2009-10-04 00:32:10 +02:00
var $array_import_module ;
var $array_import_perms ;
var $array_import_icon ;
var $array_import_code ;
var $array_import_label ;
var $array_import_tables ;
var $array_import_fields ;
var $array_import_entities ;
2009-10-17 15:12:55 +02:00
var $array_import_regex ;
2009-10-04 00:32:10 +02:00
var $array_import_examplevalues ;
2009-11-08 20:28:44 +01:00
var $array_import_convertvalue ;
2009-10-04 00:32:10 +02:00
2009-03-25 22:26:15 +01:00
/**
2011-09-10 14:21:41 +02:00
* Constructor
*
2012-01-18 00:16:02 +01:00
* @ param DoliDB $db Database handler
2009-03-25 22:26:15 +01:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $db )
2009-03-25 22:26:15 +01:00
{
2012-01-18 00:16:02 +01:00
$this -> db = $db ;
2009-03-25 22:26:15 +01:00
}
2009-05-19 02:14:27 +02:00
2009-03-25 22:26:15 +01:00
2009-09-08 01:49:16 +02:00
/**
2012-01-18 00:16:02 +01:00
* Load description int this -> array_import_module , this -> array_import_fields , ... of an importable dataset
2011-09-10 14:21:41 +02:00
*
* @ param User $user Object user making import
2012-01-18 00:16:02 +01:00
* @ param string $filter Load a particular dataset only . Index will start to 0.
2011-09-10 14:21:41 +02:00
* @ return int < 0 if KO , > 0 if OK
2009-09-08 01:49:16 +02:00
*/
function load_arrays ( $user , $filter = '' )
{
global $langs , $conf ;
2009-03-25 22:26:15 +01:00
2011-09-20 23:21:04 +02:00
dol_syslog ( get_class ( $this ) . " ::load_arrays user= " . $user -> id . " filter= " . $filter );
2009-03-25 22:26:15 +01:00
2011-02-02 20:34:44 +01:00
$var = true ;
$i = 0 ;
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
2012-05-08 22:49:03 +02:00
$modulesdir = dolGetModulesDirs ();
2009-05-19 02:14:27 +02:00
2012-05-08 22:49:03 +02:00
// Load list of modules
foreach ( $modulesdir as $dir )
{
2011-09-20 23:21:04 +02:00
$handle =@ opendir ( dol_osencode ( $dir ));
if ( ! is_resource ( $handle )) continue ;
// Search module files
while (( $file = readdir ( $handle )) !== false )
2009-05-19 02:14:27 +02:00
{
2011-09-20 23:21:04 +02:00
if ( ! preg_match ( " /^(mod.*) \ .class \ .php/i " , $file , $reg )) continue ;
$modulename = $reg [ 1 ];
// Defined if module is enabled
$enabled = true ;
$part = strtolower ( preg_replace ( '/^mod/i' , '' , $modulename ));
if ( empty ( $conf -> $part -> enabled )) $enabled = false ;
if ( empty ( $enabled )) continue ;
// Init load class
$file = $dir . " / " . $modulename . " .class.php " ;
$classname = $modulename ;
2012-08-22 23:11:24 +02:00
require_once $file ;
2011-09-20 23:21:04 +02:00
$module = new $classname ( $this -> db );
2012-09-02 22:16:57 +02:00
if ( isset ( $module -> import_code ) && is_array ( $module -> import_code ))
2009-09-08 01:49:16 +02:00
{
2011-09-20 23:21:04 +02:00
foreach ( $module -> import_code as $r => $value )
2009-09-08 01:49:16 +02:00
{
2011-09-20 23:21:04 +02:00
if ( $filter && ( $filter != $module -> import_code [ $r ])) continue ;
2009-09-08 01:49:16 +02:00
2011-09-20 23:21:04 +02:00
// Test if permissions are ok
/* $perm = $module -> import_permission [ $r ][ 0 ];
//print_r("$perm[0]-$perm[1]-$perm[2]<br>");
if ( $perm [ 2 ])
{
$bool = $user -> rights -> $perm [ 0 ] -> $perm [ 1 ] -> $perm [ 2 ];
}
else
2011-09-20 23:07:29 +02:00
{
2011-09-20 23:21:04 +02:00
$bool = $user -> rights -> $perm [ 0 ] -> $perm [ 1 ];
}
if ( $perm [ 0 ] == 'user' && $user -> admin ) $bool = true ;
//print $bool." $perm[0]"."<br>";
*/
2011-09-20 19:34:15 +02:00
2011-09-20 23:21:04 +02:00
// Load lang file
$langtoload = $module -> getLangFilesArray ();
if ( is_array ( $langtoload ))
{
foreach ( $langtoload as $key )
2009-09-08 01:49:16 +02:00
{
2011-09-20 23:21:04 +02:00
$langs -> load ( $key );
2009-09-08 01:49:16 +02:00
}
}
2011-09-20 23:21:04 +02:00
// Permission
$this -> array_import_perms [ $i ] = $user -> rights -> import -> run ;
// Icon
$this -> array_import_icon [ $i ] = ( isset ( $module -> import_icon [ $r ]) ? $module -> import_icon [ $r ] : $module -> picto );
// Code du dataset export
$this -> array_import_code [ $i ] = $module -> import_code [ $r ];
// Libelle du dataset export
$this -> array_import_label [ $i ] = $module -> getImportDatasetLabel ( $r );
// Array of tables to import (key=alias, value=tablename)
$this -> array_import_tables [ $i ] = $module -> import_tables_array [ $r ];
// Array of tables creator field to import (key=alias, value=creator field)
2012-09-02 22:16:57 +02:00
$this -> array_import_tables_creator [ $i ] = ( isset ( $module -> import_tables_creator_array [ $r ]) ? $module -> import_tables_creator_array [ $r ] : '' );
2012-01-18 00:16:02 +01:00
// Array of fields to import (key=field, value=label)
2011-09-20 23:21:04 +02:00
$this -> array_import_fields [ $i ] = $module -> import_fields_array [ $r ];
2012-01-18 00:16:02 +01:00
// Array of hidden fields to import (key=field, value=label)
$this -> array_import_fieldshidden [ $i ] = $module -> import_fieldshidden_array [ $r ];
2011-09-20 23:21:04 +02:00
// Tableau des entites a exporter (cle=champ, valeur=entite)
$this -> array_import_entities [ $i ] = $module -> import_entities_array [ $r ];
// Tableau des alias a exporter (cle=champ, valeur=alias)
$this -> array_import_regex [ $i ] = $module -> import_regex_array [ $r ];
// Tableau des alias a exporter (cle=champ, valeur=exemple)
$this -> array_import_examplevalues [ $i ] = $module -> import_examplevalues_array [ $r ];
// Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
2012-09-02 22:16:57 +02:00
$this -> array_import_convertvalue [ $i ] = ( isset ( $module -> import_convertvalue_array [ $r ]) ? $module -> import_convertvalue_array [ $r ] : '' );
2012-01-18 00:16:02 +01:00
// Module
$this -> array_import_module [ $i ] = $module ;
2011-09-20 23:21:04 +02:00
2012-01-18 00:16:02 +01:00
dol_syslog ( " Import loaded for module " . $modulename . " with index " . $i . " , dataset= " . $module -> import_code [ $r ] . " , nb of fields= " . count ( $module -> import_fields_array [ $r ]));
2011-09-20 23:21:04 +02:00
$i ++ ;
2009-09-08 01:49:16 +02:00
}
}
2009-05-19 02:14:27 +02:00
}
2011-09-20 23:21:04 +02:00
closedir ( $handle );
2009-03-25 22:26:15 +01:00
}
2011-09-10 14:21:41 +02:00
return 1 ;
2009-03-25 22:26:15 +01:00
}
2009-09-08 01:49:16 +02:00
/**
2011-09-10 14:21:41 +02:00
* Build an import example file .
* Arrays this -> array_export_xxx are already loaded for required datatoexport
*
2011-09-20 23:21:04 +02:00
* @ param string $model Name of import engine ( 'csv' , ... )
2011-09-10 14:21:41 +02:00
* @ param string $headerlinefields Array of values for first line of example file
* @ param string $contentlinevalues Array of values for content line of example file
2012-06-17 17:44:38 +02:00
* @ param string $datatoimport Dataset to import
2011-09-10 14:21:41 +02:00
* @ return string < 0 if KO , > 0 if OK
2009-09-08 01:49:16 +02:00
*/
2012-06-17 17:44:38 +02:00
function build_example_file ( $model , $headerlinefields , $contentlinevalues , $datatoimport )
2009-03-25 22:26:15 +01:00
{
2009-09-08 01:49:16 +02:00
global $conf , $langs ;
2009-03-25 22:26:15 +01:00
2009-09-08 01:49:16 +02:00
$indice = 0 ;
2009-03-25 22:26:15 +01:00
2011-09-20 23:21:04 +02:00
dol_syslog ( get_class ( $this ) . " ::build_example_file " . $model );
2009-03-25 22:26:15 +01:00
2009-09-08 01:49:16 +02:00
// Creation de la classe d'import du model Import_XXX
2011-10-24 14:11:49 +02:00
$dir = DOL_DOCUMENT_ROOT . " /core/modules/import/ " ;
2009-09-08 01:49:16 +02:00
$file = " import_ " . $model . " .modules.php " ;
$classname = " Import " . $model ;
2012-08-22 23:11:24 +02:00
require_once $dir . $file ;
2012-06-17 17:44:38 +02:00
$objmodel = new $classname ( $this -> db , $datatoimport );
2009-03-25 22:26:15 +01:00
2009-09-08 01:49:16 +02:00
$outputlangs = $langs ; // Lang for output
$s = '' ;
2009-03-25 22:26:15 +01:00
2009-09-08 01:49:16 +02:00
// Genere en-tete
$s .= $objmodel -> write_header_example ( $outputlangs );
2009-05-19 02:14:27 +02:00
2009-09-08 01:49:16 +02:00
// Genere ligne de titre
$s .= $objmodel -> write_title_example ( $outputlangs , $headerlinefields );
2009-05-19 02:14:27 +02:00
2009-09-08 01:49:16 +02:00
// Genere ligne de titre
$s .= $objmodel -> write_record_example ( $outputlangs , $contentlinevalues );
2009-03-25 22:26:15 +01:00
2009-09-08 01:49:16 +02:00
// Genere pied de page
$s .= $objmodel -> write_footer_example ( $outputlangs );
2009-03-25 22:26:15 +01:00
2009-09-08 01:49:16 +02:00
return $s ;
}
2009-09-12 04:07:25 +02:00
/**
2011-09-10 14:21:41 +02:00
* Save an export model in database
*
* @ param User $user Object user that save
* @ return int < 0 if KO , > 0 if OK
2009-09-12 04:07:25 +02:00
*/
function create ( $user )
{
global $conf ;
dol_syslog ( " Import.class.php::create " );
// Check parameters
if ( empty ( $this -> model_name )) { $this -> error = 'ErrorWrongParameters' ; return - 1 ; }
if ( empty ( $this -> datatoimport )) { $this -> error = 'ErrorWrongParameters' ; return - 1 ; }
if ( empty ( $this -> hexa )) { $this -> error = 'ErrorWrongParameters' ; return - 1 ; }
$this -> db -> begin ();
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'import_model (' ;
2011-09-20 17:48:06 +02:00
$sql .= 'fk_user, label, type, field' ;
$sql .= ')' ;
$sql .= " VALUES ( " . ( $user -> id > 0 ? $user -> id : 0 ) . " , ' " . $this -> db -> escape ( $this -> model_name ) . " ', ' " . $this -> datatoimport . " ', ' " . $this -> hexa . " ') " ;
2009-09-12 04:07:25 +02:00
2011-09-20 23:21:04 +02:00
dol_syslog ( get_class ( $this ) . " ::create sql= " . $sql , LOG_DEBUG );
2009-09-12 04:07:25 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> error = $this -> db -> lasterror ();
$this -> errno = $this -> db -> lasterrno ();
2011-09-20 23:21:04 +02:00
dol_syslog ( get_class ( $this ) . " ::create error " . $this -> error , LOG_ERR );
2009-09-12 04:07:25 +02:00
$this -> db -> rollback ();
return - 1 ;
}
}
/**
2011-09-10 14:21:41 +02:00
* Load an import profil from database
*
* @ param int $id Id of profil to load
* @ return int < 0 if KO , > 0 if OK
2009-09-12 04:07:25 +02:00
*/
function fetch ( $id )
{
$sql = 'SELECT em.rowid, em.field, em.label, em.type' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'import_model as em' ;
$sql .= ' WHERE em.rowid = ' . $id ;
2011-09-20 23:21:04 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch sql= " . $sql , LOG_DEBUG );
2009-09-12 04:07:25 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$obj = $this -> db -> fetch_object ( $result );
if ( $obj )
{
$this -> id = $obj -> rowid ;
$this -> hexa = $obj -> field ;
$this -> model_name = $obj -> label ;
$this -> datatoimport = $obj -> type ;
$this -> fk_user = $obj -> fk_user ;
return 1 ;
}
else
{
$this -> error = " Model not found " ;
return - 2 ;
}
}
else
{
dol_print_error ( $this -> db );
return - 3 ;
}
}
/**
2011-09-10 14:21:41 +02:00
* Delete object in database
*
* @ param User $user User that delete
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
2009-09-12 04:07:25 +02:00
*/
function delete ( $user , $notrigger = 0 )
{
global $conf , $langs ;
$error = 0 ;
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " import_model " ;
$sql .= " WHERE rowid= " . $this -> id ;
$this -> db -> begin ();
dol_syslog ( get_class ( $this ) . " ::delete sql= " . $sql );
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) { $error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror (); }
if ( ! $error )
{
if ( ! $notrigger )
{
2011-09-20 17:48:06 +02:00
// Call triggers
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php' ;
2011-09-20 17:48:06 +02:00
$interface = new Interfaces ( $this -> db );
$result = $interface -> run_triggers ( 'IMPORT_DELETE' , $this , $user , $langs , $conf );
if ( $result < 0 ) { $error ++ ; $this -> errors = $interface -> errors ; }
// End call triggers
2009-09-12 04:07:25 +02:00
}
}
// Commit or rollback
if ( $error )
{
foreach ( $this -> errors as $errmsg )
{
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
}
$this -> db -> rollback ();
return - 1 * $error ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
}
2009-03-25 22:26:15 +01:00
}
?>