dolibarr/htdocs/imports/class/import.class.php

480 lines
13 KiB
PHP
Raw Permalink Normal View History

2009-03-25 22:26:15 +01:00
<?php
/* Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
* Copyright (C) 2020 Ahmad Jamaly Rabib <rabib@metroworks.co.jp>
* Copyright (C) 2021-2024 Frédéric France <frederic.france@free.fr>
2025-02-03 13:37:43 +01:00
* Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
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
* 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
*/
/**
* Class to manage imports
2009-03-25 22:26:15 +01:00
*/
class Import
{
2023-04-30 03:10:20 +02:00
/**
* @var DoliDB Database handler.
*/
public $db;
/**
* @var string Error code (or message)
*/
public $error = '';
/**
* @var string[] Error codes (or messages)
*/
public $errors = array();
2023-08-01 17:51:44 +02:00
/**
* @var string DB Error number
*/
public $errno;
/**
* @var array<array{position_of_profile:string,module:DolibarrModules}>
*/
public $array_import_module;
/**
* @var int[]
*/
public $array_import_perms;
/**
* @var string[]
*/
public $array_import_icon;
/**
* @var string[]
*/
public $array_import_code;
/**
* @var string[]
*/
public $array_import_label;
/**
* @var array<string[]>
*/
public $array_import_tables;
/**
* @var array<''|array<string,string>>
*/
public $array_import_tables_creator;
/**
* @var array<array<string,string>>
*/
public $array_import_fields;
/**
* @var array<''|array<string,string>>
*/
public $array_import_fieldshidden;
/**
* @var array<''|array<string,string>>
*/
public $array_import_entities;
/**
* @var array<''|array<string,string>>
*/
public $array_import_regex;
/**
* @var array<''|array<string,string>>
*/
public $array_import_updatekeys;
/**
* @var array<''|array<string,string>>
*/
public $array_import_preselected_updatekeys;
/**
* @var array<''|array<string,string>>
*/
public $array_import_examplevalues;
/**
* @var array<''|array<array{rule:string,file:string,class:string,method:string}>>
*/
public $array_import_convertvalue;
/**
* @var array<''|array<string,string>>
*/
public $array_import_run_sql_after;
2009-10-04 00:32:10 +02:00
// To store import templates
/**
* @var int
*/
2023-04-26 02:05:43 +02:00
public $id;
/**
* @var string
*/
public $hexa; // List of fields in the export profile
/**
* @var string
*/
public $datatoimport;
/**
* @var string Name of export profile
*/
public $model_name;
/**
* @var int ID
*/
public $fk_user;
2009-10-04 00:32:10 +02: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
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
2009-09-08 01:49:16 +02: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
* @param string $filter Load a particular dataset only. Index will start to 0.
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, >0 if OK
2009-09-08 01:49:16 +02:00
*/
public function load_arrays($user, $filter = '')
2009-09-08 01:49:16 +02:00
{
// phpcs:enable
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
$i = 0;
2011-02-02 20:34:44 +01:00
require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
$modulesdir = dolGetModulesDirs();
2009-05-19 02:14:27 +02:00
// Load list of modules
foreach ($modulesdir as $dir) {
$handle = @opendir(dol_osencode($dir));
if (!is_resource($handle)) {
continue;
}
2011-09-20 23:21:04 +02:00
// Search module files
while (($file = readdir($handle)) !== false) {
if (!preg_match("/^(mod.*)\.class\.php/i", $file, $reg)) {
continue;
}
2011-09-20 23:21:04 +02:00
$modulename = $reg[1];
2011-09-20 23:21:04 +02:00
// Defined if module is enabled
$enabled = true;
$part = strtolower(preg_replace('/^mod/i', '', $modulename));
// Adds condition for propal module
if ($part === 'propale') {
$part = 'propal';
}
if (empty($conf->$part->enabled)) {
$enabled = false;
}
2011-09-20 23:21:04 +02:00
if (empty($enabled)) {
continue;
}
2011-09-20 23:21:04 +02:00
// Init load class
$file = $dir."/".$modulename.".class.php";
$classname = $modulename;
require_once $file;
2011-09-20 23:21:04 +02:00
$module = new $classname($this->db);
'@phan-var-force DolibarrModules $module';
2011-09-20 23:21:04 +02:00
if (isset($module->import_code) && is_array($module->import_code)) {
foreach ($module->import_code as $r => $value) { // @phan-suppress-current-line PhanTypeMismatchForeach
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]};
2011-09-20 23:21:04 +02:00
}
else
2011-09-20 23:07:29 +02: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
$langtoload = $module->getLangFilesArray();
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
Fix #28071 - New branch to fix bad merge (#28083) * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Qual: Introduce getDataToShowPhoto to prepare generic code * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Fix missing trans * Fix langs * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Remove useless files in web templates * Clean code * Fix duplicate translation key * Fix duplicate translation key * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Fix duplicate key * Fix $object * Debug v19 * WIP SMSing * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * WIP EMAILINGS_SUPPORT_ALSO_SMS * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * WIP SMSing * Debug the "validate" feature * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Clean code * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Move rights->x->y into hasRight('x', 'y') * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Enhance rector to fix empty($user->rights->modulex->perm1) * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Fix template to use v19 dev rules * Fix use v19 dev rules * Fix phpunit * Debug v19 * Clean code * Use rector to convert user->rights into user->hasRight * Clean code * Use rector to convert user->rights into user->hasRight * Use rector to convert user->rights into user->hasRight * Clean code * Fix phpcs * add editorconfig for sql files (#27999) Co-authored-by: Laurent Destailleur <eldy@destailleur.fr> * add model_pdf field in llx_ticket-ticket.sql (#27996) * add model_pdf field in llx_ticket-ticket.sql * Update 19.0.0-20.0.0.sql * Update 19.0.0-20.0.0.sql * Improve wording in README (#27994) * fix phpstan (#27989) * fix phpstan * Update UserRightsToFunction.php --------- Co-authored-by: Laurent Destailleur <eldy@destailleur.fr> * Qual: Fix spelling/working in datapolicy translations (#28006) # Qual: Fix spelling/wording in datapolicy translations Fixed some spelling and wording in datapolicy translations. * qual: phpstan for htdocs/ticket/class/ticketstats.class.php (#27986) htdocs/ticket/class/ticketstats.class.php 98 Parameter #1 $year (string) of method TicketStats::getNbByMonth() should be compatible with parameter $year (int) of method Stats::getNbByMonth() * Merge branch '19.0' of git@github.com:Dolibarr/dolibarr.git into develop * Fix user with readonly perm on email template must be able to read. * Fix doc * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Better message * Add missing fields in merge of thirdparty * Debug v19 selection of ticket printer per terminal * Merge branch 'develop' of git@github.com:Dolibarr/dolibarr.git into develop * Use constant * NEW: Adding a recipient on emails sent, change status to sent partialy. * fix travis (#28052) * fix travis * Update partnership.class.php * fix php doc (#28047) * fix undefined array key (#28048) * Add region and departament for Cuba (#28046) * Update llx_10_c_regions.sql Add Cuba Regions (id_country=77) * Update llx_20_c_departements.sql Add Provinces Cuba (id country=77) * Find the typo (#28050) * Find the typo * clean code * add last_main_doc field to product (#28045) * add las_main_doc field to product * add field fetch * NEW Add Categorie filter for ActionComm (#28041) * New Add Categorie filter for ActionComm New Add Categorie filter for ActionComm * Fix space errors Fix space errors * Fix space errors 2 Fix space errors 2 * Update cunits.class.php (#28056) FIX: error SQL when creating a Cunit * Update codespell-lines-ignore.txt to avoid PR merge conflict --------- Co-authored-by: Laurent Destailleur <eldy@destailleur.fr> Co-authored-by: Frédéric FRANCE <frederic34@users.noreply.github.com> Co-authored-by: thibdrev <thibault.drevet@gmail.com> Co-authored-by: sonikf <93765174+sonikf@users.noreply.github.com> Co-authored-by: Ikarus <44511582+LeKarSol@users.noreply.github.com> Co-authored-by: Anthony Damhet <73399671+EchoLoGeek@users.noreply.github.com> Co-authored-by: Quentin-Seekness <72733832+Quentin-Seekness@users.noreply.github.com>
2024-02-09 15:58:49 +01:00
$this->array_import_perms[$i] = $user->hasRight('import', 'run');
2011-09-20 23:21:04 +02:00
// Icon
$this->array_import_icon[$i] = (isset($module->import_icon[$r]) ? $module->import_icon[$r] : $module->picto);
2023-04-08 15:08:55 +02:00
// Code of dataset export
$this->array_import_code[$i] = $module->import_code[$r];
2023-04-08 15:08:55 +02:00
// Label of dataset export
$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)
$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)
$this->array_import_tables_creator[$i] = (isset($module->import_tables_creator_array[$r]) ? $module->import_tables_creator_array[$r] : '');
// Array of fields to import (key=field, value=label)
$this->array_import_fields[$i] = $module->import_fields_array[$r];
// 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] : '');
// Array of entities to export (key=field, value=entity)
$this->array_import_entities[$i] = $module->import_entities_array[$r];
// Array of aliases to export (key=field, value=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] : '');
// 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] : '');
// Array of columns preselected as UPDATE options
// import_preselected_updatekeys_array does not exist - backward compatibility ? @phan-suppress-next-line PhanUndeclaredProperty
$this->array_import_preselected_updatekeys[$i] = (isset($module->import_preselected_updatekeys_array[$r]) ? $module->import_preselected_updatekeys_array[$r] : '');
// 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)
$this->array_import_convertvalue[$i] = (isset($module->import_convertvalue_array[$r]) ? $module->import_convertvalue_array[$r] : '');
// Sql request to run after import
$this->array_import_run_sql_after[$i] = (isset($module->import_run_sql_after_array[$r]) ? $module->import_run_sql_after_array[$r] : '');
// Module
$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
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
}
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
}
// 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
*
2025-02-03 13:37:43 +01:00
* @param string $model Name of import engine ('csv', ...)
* @param string[] $headerlinefields Array of values for first line of example file
* @param string[] $contentlinevalues Array of values for content line of example file
* @param string $datatoimport Dataset to import
* @return string Return integer <0 if KO, >0 if OK
2009-09-08 01:49:16 +02:00
*/
public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
2009-03-25 22:26:15 +01:00
{
// phpcs:enable
global $conf, $langs;
2009-03-25 22:26:15 +01: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
// Create the import class for the model Import_XXX
$dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
2009-09-08 01:49:16 +02:00
$file = "import_".$model.".modules.php";
$classname = "Import".$model;
require_once $dir.$file;
$objmodel = new $classname($this->db, $datatoimport);
2025-02-03 13:37:43 +01:00
'@phan-var-force ModeleImports $objmodel';
2009-03-25 22:26:15 +01: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
/**
2024-08-13 20:29:54 +02:00
* Save an import model in database
2011-09-10 14:21:41 +02:00
*
* @param User $user Object user that save
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, >0 if OK
2009-09-12 04:07:25 +02:00
*/
public function create($user)
2009-09-12 04:07:25 +02:00
{
global $conf;
dol_syslog("Import.class.php::create");
// Check parameters
if (empty($this->model_name)) {
2023-12-04 12:36:45 +01:00
$this->error = 'ErrorWrongParameters';
return -1;
}
if (empty($this->datatoimport)) {
2023-12-04 12:36:45 +01:00
$this->error = 'ErrorWrongParameters';
return -1;
}
if (empty($this->hexa)) {
2023-12-04 12:36:45 +01:00
$this->error = 'ErrorWrongParameters';
return -1;
}
2009-09-12 04:07:25 +02:00
$this->db->begin();
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
$sql .= 'fk_user,';
$sql .= ' label,';
$sql .= ' type,';
$sql .= ' field';
$sql .= ')';
$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
$resql = $this->db->query($sql);
if ($resql) {
2009-09-12 04:07:25 +02:00
$this->db->commit();
return 1;
2020-05-21 15:05:19 +02:00
} else {
$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
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, >0 if OK
2009-09-12 04:07:25 +02:00
*/
public function fetch($id)
2009-09-12 04:07:25 +02:00
{
$sql = 'SELECT em.rowid, em.field, em.label, em.type';
$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);
if ($result) {
2009-09-12 04:07:25 +02:00
$obj = $this->db->fetch_object($result);
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 {
$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<0,1> $notrigger 0=launch triggers after, 1=disable triggers
2023-12-01 19:51:32 +01:00
* @return int Return integer <0 if KO, >0 if OK
2009-09-12 04:07:25 +02:00
*/
public function delete($user, $notrigger = 0)
2009-09-12 04:07:25 +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);
if (!$resql) {
2023-12-04 12:36:45 +01:00
$error++;
$this->errors[] = "Error ".$this->db->lasterror();
}
2009-09-12 04:07:25 +02:00
/* Not used. This is not a business object. To convert it we must herit from CommonObject
if (!$error) {
// Call trigger
$result=$this->call_trigger('IMPORT_DELETE',$user);
if ($result < 0) $error++;
// End call triggers
2009-09-12 04:07:25 +02:00
}
}
*/
2009-09-12 04:07:25 +02:00
// Commit or rollback
if ($error) {
foreach ($this->errors as $errmsg) {
2009-09-12 04:07:25 +02:00
dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
2009-09-12 04:07:25 +02:00
}
$this->db->rollback();
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
}