2016-02-23 23:03:52 +01:00
#!/usr/bin/env php
< ? php
/* Copyright ( C ) 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2016-07-15 16:58:26 +02:00
* Copyright ( C ) 2016 Juanjo Menent < jmenent @ 2 byte . es >
2016-02-23 23:03:52 +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
* the Free Software Foundation ; either version 3 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 , see < http :// www . gnu . org / licenses />.
*
* WARNING , THIS WILL LOAD MASS DATA ON YOUR INSTANCE
*/
/**
* \file dev / initdata / import - thirdparties . php
2018-07-26 20:19:52 +02:00
* \brief Script example to insert thirdparties from a csv file .
2016-02-23 23:03:52 +01:00
* To purge data , you can have a look at purge - data . php
*/
// Test si mode batch
$sapi_type = php_sapi_name ();
$script_file = basename ( __FILE__ );
$path = dirname ( __FILE__ ) . '/' ;
if ( substr ( $sapi_type , 0 , 3 ) == 'cgi' ) {
echo " Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer mailing-send.php en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI. \n " ;
exit ;
}
// Recupere root dolibarr
2019-01-27 11:55:16 +01:00
$path = preg_replace ( '/import-users.php/i' , '' , $_SERVER [ " PHP_SELF " ]);
2018-07-26 11:57:25 +02:00
require $path . " ../../htdocs/master.inc.php " ;
2016-02-23 23:03:52 +01:00
include_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
$delimiter = ',' ;
$enclosure = '"' ;
$linelength = 10000 ;
$escape = '/' ;
// Global variables
$version = DOL_VERSION ;
$confirmed = 1 ;
$error = 0 ;
/*
* Main
*/
@ set_time_limit ( 0 );
print " ***** " . $script_file . " ( " . $version . " ) pid= " . dol_getmypid () . " ***** \n " ;
2019-01-27 11:55:16 +01:00
dol_syslog ( $script_file . " launched with arg " . implode ( ',' , $argv ));
2016-02-23 23:03:52 +01:00
$mode = $argv [ 1 ];
$filepath = $argv [ 2 ];
$filepatherr = $filepath . '.err' ;
//$defaultlang = empty($argv[3])?'en_US':$argv[3];
$startlinenb = empty ( $argv [ 3 ]) ? 1 : $argv [ 3 ];
$endlinenb = empty ( $argv [ 4 ]) ? 0 : $argv [ 4 ];
2019-01-27 11:55:16 +01:00
if ( empty ( $mode ) || ! in_array ( $mode , array ( 'test' , 'confirm' , 'confirmforced' )) || empty ( $filepath )) {
2016-02-23 23:03:52 +01:00
print " Usage: $script_file (test|confirm|confirmforced) filepath.csv [startlinenb] [endlinenb] \n " ;
print " Usage: $script_file test myfilepath.csv 2 1002 \n " ;
print " \n " ;
exit ( - 1 );
}
if ( ! file_exists ( $filepath )) {
print " Error: File " . $filepath . " not found. \n " ;
print " \n " ;
exit ( - 1 );
}
2019-01-27 11:55:16 +01:00
$ret = $user -> fetch ( '' , 'admin' );
2016-02-23 23:03:52 +01:00
if ( ! $ret > 0 )
{
print 'A user with login "admin" and all permissions must be created to use this script.' . " \n " ;
exit ;
}
$user -> getrights ();
// Ask confirmation
if ( ! $confirmed )
{
print " Hit Enter to continue or CTRL+C to stop... \n " ;
$input = trim ( fgets ( STDIN ));
}
2019-02-10 12:59:09 +01:00
// Open input and output files
2016-02-23 23:03:52 +01:00
$fhandle = fopen ( $filepath , 'r' );
if ( ! $fhandle )
{
print 'Error: Failed to open file ' . $filepath . " \n " ;
exit ( 1 );
}
$fhandleerr = fopen ( $filepatherr , 'w' );
if ( ! $fhandleerr )
{
print 'Error: Failed to open file ' . $filepatherr . " \n " ;
exit ( 1 );
}
//$langs->setDefaultLang($defaultlang);
$db -> begin ();
$i = 0 ;
2016-02-24 17:27:28 +01:00
$nboflines = 0 ;
2016-02-23 23:03:52 +01:00
while ( $fields = fgetcsv ( $fhandle , $linelength , $delimiter , $enclosure , $escape ))
{
$i ++ ;
$errorrecord = 0 ;
2018-07-26 20:19:52 +02:00
2016-02-23 23:03:52 +01:00
if ( $startlinenb && $i < $startlinenb ) continue ;
if ( $endlinenb && $i > $endlinenb ) continue ;
2018-07-26 20:19:52 +02:00
2016-02-24 17:27:28 +01:00
$nboflines ++ ;
2018-07-26 20:19:52 +02:00
2016-02-23 23:03:52 +01:00
$object = new User ( $db );
$object -> statut = 1 ;
2018-07-26 20:19:52 +02:00
2019-01-27 11:55:16 +01:00
$tmp = explode ( ' ' , $fields [ 3 ], 2 );
2016-02-23 23:03:52 +01:00
$object -> firstname = trim ( $tmp [ 0 ]);
$object -> lastname = trim ( $tmp [ 1 ]);
if ( $object -> lastname ) $object -> login = strtolower ( substr ( $object -> firstname , 0 , 1 )) . strtolower ( substr ( $object -> lastname , 0 ));
else $object -> login = strtolower ( $object -> firstname );
2019-01-27 11:55:16 +01:00
$object -> login = preg_replace ( '/ /' , '' , $object -> login );
2016-02-23 23:03:52 +01:00
$object -> password = 'init' ;
2018-07-26 20:19:52 +02:00
2016-02-23 23:03:52 +01:00
print " Process line nb " . $i . " , login " . $object -> login ;
2018-07-26 20:19:52 +02:00
2016-02-23 23:03:52 +01:00
$ret = $object -> create ( $user );
if ( $ret < 0 )
{
print " - Error in create result code = " . $ret . " - " . $object -> errorsToString ();
$errorrecord ++ ;
}
2018-07-26 20:19:52 +02:00
else
2016-02-23 23:03:52 +01:00
{
print " - Creation OK with login " . $object -> login . " - id = " . $ret ;
}
print " \n " ;
2018-07-26 20:19:52 +02:00
if ( $errorrecord )
2016-02-23 23:03:52 +01:00
{
fwrite ( $fhandleerr , 'Error on record nb ' . $i . " - " . $object -> errorsToString () . " \n " );
$error ++ ; // $errorrecord will be reset
}
}
// commit or rollback
2016-02-24 17:27:28 +01:00
print " Nb of lines qualified: " . $nboflines . " \n " ;
print " Nb of errors: " . $error . " \n " ;
2016-02-23 23:03:52 +01:00
if ( $mode != 'confirmforced' && ( $error || $mode != 'confirm' ))
{
print " Rollback any changes. \n " ;
$db -> rollback ();
}
else
{
print " Commit all changes. \n " ;
$db -> commit ();
}
$db -> close ();
fclose ( $fhandle );
fclose ( $fhandleerr );
exit ( $error );