2009-03-25 22:26:15 +01:00
< ? php
2016-02-11 20:01:51 +01:00
/* Copyright ( C ) 2011 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2016 Raphaël Doursenaud < rdoursenaud @ gpcsolutions . fr >
2020-10-01 07:52:56 +02:00
* Copyright ( C ) 2020 Ahmad Jamaly Rabib < rabib @ metroworks . co . jp >
2021-03-16 18:13:02 +01:00
* Copyright ( C ) 2021 Frédéric France < frederic . france @ netlogic . fr >
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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// 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
*/
/**
2014-12-23 12:25:21 +01:00
* Class to manage imports
2009-03-25 22:26:15 +01:00
*/
class Import
{
2020-10-31 14:32:18 +01:00
public $array_import_module ;
public $array_import_perms ;
public $array_import_icon ;
public $array_import_code ;
public $array_import_label ;
public $array_import_tables ;
public $array_import_tables_creator ;
public $array_import_fields ;
public $array_import_fieldshidden ;
public $array_import_entities ;
public $array_import_regex ;
public $array_import_updatekeys ;
public $array_import_examplevalues ;
public $array_import_convertvalue ;
public $array_import_run_sql_after ;
2009-10-04 00:32:10 +02:00
2018-08-22 10:37:16 +02:00
/**
* @ var string Error code ( or message )
*/
2020-04-10 10:59:32 +02:00
public $error = '' ;
2018-08-21 19:08:11 +02:00
2018-08-17 19:31:22 +02:00
/**
* @ var string [] Error codes ( or messages )
*/
2018-08-21 19:08:11 +02:00
public $errors = array ();
2017-09-15 15:41:07 +02:00
2021-06-21 15:03:45 +02:00
// To store import templates
public $hexa ; // List of fields in the export profile
public $datatoimport ;
public $model_name ; // Name of export profile
public $fk_user ;
2009-10-04 00:32:10 +02:00
2020-10-31 14:32:18 +01:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
public function __construct ( $db )
{
$this -> db = $db ;
}
2009-05-19 02:14:27 +02:00
2009-03-25 22:26:15 +01:00
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
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.
2020-10-31 14:32:18 +01:00
* @ return int < 0 if KO , > 0 if OK
2009-09-08 01:49:16 +02:00
*/
2020-10-31 14:32:18 +01:00
public function load_arrays ( $user , $filter = '' )
2009-09-08 01:49:16 +02:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2020-04-10 10:59:32 +02:00
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
2020-10-31 14:32:18 +01:00
$i = 0 ;
2011-02-02 20:34:44 +01:00
2020-10-31 14:32:18 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/functions2.lib.php' ;
$modulesdir = dolGetModulesDirs ();
2009-05-19 02:14:27 +02:00
2020-10-31 14:32:18 +01:00
// Load list of modules
2021-02-26 18:20:21 +01:00
foreach ( $modulesdir as $dir ) {
2020-04-10 10:59:32 +02:00
$handle = @ opendir ( dol_osencode ( $dir ));
2021-02-26 18:20:21 +01:00
if ( ! is_resource ( $handle )) {
continue ;
}
2011-09-20 23:21:04 +02:00
// Search module files
2021-02-26 18:20:21 +01:00
while (( $file = readdir ( $handle )) !== false ) {
if ( ! preg_match ( " /^(mod.*) \ .class \ .php/i " , $file , $reg )) {
continue ;
}
2011-09-20 23:21:04 +02:00
2020-04-10 10:59:32 +02:00
$modulename = $reg [ 1 ];
2011-09-20 23:21:04 +02:00
// Defined if module is enabled
2020-04-10 10:59:32 +02:00
$enabled = true ;
$part = strtolower ( preg_replace ( '/^mod/i' , '' , $modulename ));
2020-10-01 07:52:56 +02:00
// Adds condition for propal module
2021-02-26 18:20:21 +01:00
if ( $part === 'propale' ) {
$part = 'propal' ;
}
if ( empty ( $conf -> $part -> enabled )) {
$enabled = false ;
}
2011-09-20 23:21:04 +02:00
2021-02-26 18:20:21 +01:00
if ( empty ( $enabled )) {
continue ;
}
2011-09-20 23:21:04 +02:00
// 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 );
2021-02-26 18:20:21 +01:00
if ( isset ( $module -> import_code ) && is_array ( $module -> import_code )) {
foreach ( $module -> import_code as $r => $value ) {
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 ])
{
2016-02-11 20:01:51 +01:00
$bool = $user -> rights -> { $perm [ 0 ]} -> { $perm [ 1 ]} -> { $perm [ 2 ]};
2011-09-20 23:21:04 +02:00
}
else
2011-09-20 23:07:29 +02:00
{
2016-02-11 20:01:51 +01:00
$bool = $user -> rights -> { $perm [ 0 ]} -> { $perm [ 1 ]};
2011-09-20 23:21:04 +02:00
}
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
2020-04-10 10:59:32 +02:00
$langtoload = $module -> getLangFilesArray ();
2021-02-26 18:20:21 +01:00
if ( is_array ( $langtoload )) {
foreach ( $langtoload as $key ) {
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
2020-04-10 10:59:32 +02:00
$this -> array_import_perms [ $i ] = $user -> rights -> import -> run ;
2011-09-20 23:21:04 +02:00
// Icon
2020-04-10 10:59:32 +02:00
$this -> array_import_icon [ $i ] = ( isset ( $module -> import_icon [ $r ]) ? $module -> import_icon [ $r ] : $module -> picto );
2011-09-20 23:21:04 +02:00
// Code du dataset export
2020-04-10 10:59:32 +02:00
$this -> array_import_code [ $i ] = $module -> import_code [ $r ];
2011-09-20 23:21:04 +02:00
// Libelle du dataset export
2020-04-10 10:59:32 +02:00
$this -> array_import_label [ $i ] = $module -> getImportDatasetLabel ( $r );
2011-09-20 23:21:04 +02:00
// Array of tables to import (key=alias, value=tablename)
2020-04-10 10:59:32 +02:00
$this -> array_import_tables [ $i ] = $module -> import_tables_array [ $r ];
2016-04-12 19:29:32 +02:00
// Array of tables creator field to import (key=alias, value=creator field name)
2020-04-10 10:59:32 +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)
2020-04-10 10:59:32 +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)
2021-03-16 18:13:02 +01:00
$this -> array_import_fieldshidden [ $i ] = ( isset ( $module -> import_fieldshidden_array [ $r ]) ? $module -> import_fieldshidden_array [ $r ] : '' );
2011-09-20 23:21:04 +02:00
// Tableau des entites a exporter (cle=champ, valeur=entite)
2020-04-10 10:59:32 +02:00
$this -> array_import_entities [ $i ] = $module -> import_entities_array [ $r ];
2011-09-20 23:21:04 +02:00
// Tableau des alias a exporter (cle=champ, valeur=alias)
2021-03-16 18:13:02 +01:00
$this -> array_import_regex [ $i ] = ( isset ( $module -> import_regex_array [ $r ]) ? $module -> import_regex_array [ $r ] : '' );
2016-08-22 12:25:03 +02:00
// Array of columns allowed as UPDATE options
2021-03-16 18:13:02 +01:00
$this -> array_import_updatekeys [ $i ] = ( isset ( $module -> import_updatekeys_array [ $r ]) ? $module -> import_updatekeys_array [ $r ] : '' );
2015-09-10 12:34:00 +02:00
// Array of examples
2021-03-16 18:13:02 +01:00
$this -> array_import_examplevalues [ $i ] = ( isset ( $module -> import_examplevalues_array [ $r ]) ? $module -> import_examplevalues_array [ $r ] : '' );
2011-09-20 23:21:04 +02:00
// Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
2020-04-10 10:59:32 +02:00
$this -> array_import_convertvalue [ $i ] = ( isset ( $module -> import_convertvalue_array [ $r ]) ? $module -> import_convertvalue_array [ $r ] : '' );
2016-04-11 18:08:03 +02:00
// Sql request to run after import
2020-04-10 10:59:32 +02:00
$this -> array_import_run_sql_after [ $i ] = ( isset ( $module -> import_run_sql_after_array [ $r ]) ? $module -> import_run_sql_after_array [ $r ] : '' );
2012-01-18 00:16:02 +01:00
// Module
2020-09-10 15:43:50 +02:00
$this -> array_import_module [ $i ] = array ( 'position_of_profile' => ( $module -> module_position . '-' . $module -> import_code [ $r ]), 'module' => $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
}
2020-10-31 14:32:18 +01: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
}
2020-10-31 14:32:18 +01:00
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
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
*/
2020-10-31 14:32:18 +01:00
public function build_example_file ( $model , $headerlinefields , $contentlinevalues , $datatoimport )
2009-03-25 22:26:15 +01:00
{
2020-10-31 14:32:18 +01:00
// phpcs:enable
2020-04-10 10:59:32 +02:00
global $conf , $langs ;
2009-03-25 22:26:15 +01:00
2020-04-10 10:59:32 +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
2020-04-10 10:59:32 +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 ;
2019-01-27 11:55:16 +01:00
$objmodel = new $classname ( $this -> db , $datatoimport );
2009-03-25 22:26:15 +01:00
2020-04-10 10:59:32 +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
2020-04-10 10:59:32 +02:00
$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
2020-04-10 10:59:32 +02:00
$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
2020-04-10 10:59:32 +02:00
$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
2020-04-10 10:59:32 +02:00
$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
*/
2020-10-31 14:32:18 +01:00
public function create ( $user )
2009-09-12 04:07:25 +02:00
{
global $conf ;
dol_syslog ( " Import.class.php::create " );
// Check parameters
2021-02-26 18:20:21 +01:00
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 ;
}
2009-09-12 04:07:25 +02:00
$this -> db -> begin ();
$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'import_model (' ;
2021-06-21 15:03:45 +02:00
$sql .= 'fk_user,' ;
$sql .= ' label,' ;
$sql .= ' type,' ;
$sql .= ' field' ;
2020-04-10 10:59:32 +02:00
$sql .= ')' ;
2021-06-21 15:03:45 +02:00
$sql .= " VALUES ( " ;
$sql .= ( isset ( $this -> fk_user ) ? ( int ) $this -> fk_user : 'null' ) . " , " ;
$sql .= " ' " . $this -> db -> escape ( $this -> model_name ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> datatoimport ) . " ', " ;
$sql .= " ' " . $this -> db -> escape ( $this -> hexa ) . " ' " ;
$sql .= " ) " ;
2009-09-12 04:07:25 +02:00
2020-04-10 10:59:32 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:20:21 +01:00
if ( $resql ) {
2009-09-12 04:07:25 +02:00
$this -> db -> commit ();
return 1 ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$this -> error = $this -> db -> lasterror ();
$this -> errno = $this -> db -> lasterrno ();
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
*/
2020-10-31 14:32:18 +01:00
public function fetch ( $id )
2009-09-12 04:07:25 +02:00
{
$sql = 'SELECT em.rowid, em.field, em.label, em.type' ;
2020-04-10 10:59:32 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'import_model as em' ;
2020-09-30 18:39:23 +02:00
$sql .= ' WHERE em.rowid = ' . (( int ) $id );
2009-09-12 04:07:25 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2009-09-12 04:07:25 +02:00
$result = $this -> db -> query ( $sql );
2021-02-26 18:20:21 +01:00
if ( $result ) {
2009-09-12 04:07:25 +02:00
$obj = $this -> db -> fetch_object ( $result );
2021-02-26 18:20:21 +01:00
if ( $obj ) {
2009-09-12 04:07:25 +02:00
$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 ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$this -> error = " Model not found " ;
2009-09-12 04:07:25 +02:00
return - 2 ;
}
2020-05-21 15:05:19 +02:00
} else {
2009-09-12 04:07:25 +02:00
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
*/
2020-10-31 14:32:18 +01:00
public function delete ( $user , $notrigger = 0 )
2009-09-12 04:07:25 +02:00
{
global $conf , $langs ;
2020-04-10 10:59:32 +02:00
$error = 0 ;
2009-09-12 04:07:25 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " import_model " ;
2021-03-14 12:20:23 +01:00
$sql .= " WHERE rowid= " . (( int ) $this -> id );
2009-09-12 04:07:25 +02:00
$this -> db -> begin ();
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " , LOG_DEBUG );
2009-09-12 04:07:25 +02:00
$resql = $this -> db -> query ( $sql );
2021-02-26 18:20:21 +01:00
if ( ! $resql ) {
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
}
2009-09-12 04:07:25 +02:00
2021-02-26 18:20:21 +01:00
if ( ! $error ) {
if ( ! $notrigger ) {
2014-12-23 12:25:21 +01:00
/* Not used . This is not a business object . To convert it we must herit from CommonObject
2021-02-26 18:20:21 +01:00
// Call trigger
$result = $this -> call_trigger ( 'IMPORT_DELETE' , $user );
if ( $result < 0 ) $error ++ ;
// End call triggers
*/
2009-09-12 04:07:25 +02:00
}
}
// Commit or rollback
2021-02-26 18:20:21 +01:00
if ( $error ) {
foreach ( $this -> errors as $errmsg ) {
2009-09-12 04:07:25 +02:00
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
2020-04-10 10:59:32 +02:00
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2009-09-12 04:07:25 +02:00
}
$this -> db -> rollback ();
2020-04-10 10:59:32 +02:00
return - 1 * $error ;
2020-05-21 15:05:19 +02:00
} else {
2009-09-12 04:07:25 +02:00
$this -> db -> commit ();
return 1 ;
}
}
2009-03-25 22:26:15 +01:00
}