dolibarr 19.0.3
import.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
4 * Copyright (C) 2020 Ahmad Jamaly Rabib <rabib@metroworks.co.jp>
5 * Copyright (C) 2021 Frédéric France <frederic.france@netlogic.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
30class Import
31{
35 public $db;
36
40 public $error = '';
41
45 public $errors = array();
46
50 public $errno;
51
52 public $array_import_module;
53 public $array_import_perms;
54 public $array_import_icon;
55 public $array_import_code;
56 public $array_import_label;
57 public $array_import_tables;
58 public $array_import_tables_creator;
59 public $array_import_fields;
60 public $array_import_fieldshidden;
61 public $array_import_entities;
62 public $array_import_regex;
63 public $array_import_updatekeys;
64 public $array_import_preselected_updatekeys;
65 public $array_import_examplevalues;
66 public $array_import_convertvalue;
67 public $array_import_run_sql_after;
68
69 // To store import templates
70 public $id;
71 public $hexa; // List of fields in the export profile
72 public $datatoimport;
73 public $model_name; // Name of export profile
74 public $fk_user;
75
76
82 public function __construct($db)
83 {
84 $this->db = $db;
85 }
86
87
88 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
96 public function load_arrays($user, $filter = '')
97 {
98 // phpcs:enable
99 global $langs, $conf;
100
101 dol_syslog(get_class($this)."::load_arrays user=".$user->id." filter=".$filter);
102
103 $i = 0;
104
105 require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
106 $modulesdir = dolGetModulesDirs();
107
108 // Load list of modules
109 foreach ($modulesdir as $dir) {
110 $handle = @opendir(dol_osencode($dir));
111 if (!is_resource($handle)) {
112 continue;
113 }
114
115 // Search module files
116 while (($file = readdir($handle)) !== false) {
117 if (!preg_match("/^(mod.*)\.class\.php/i", $file, $reg)) {
118 continue;
119 }
120
121 $modulename = $reg[1];
122
123 // Defined if module is enabled
124 $enabled = true;
125 $part = strtolower(preg_replace('/^mod/i', '', $modulename));
126 // Adds condition for propal module
127 if ($part === 'propale') {
128 $part = 'propal';
129 }
130 if (empty($conf->$part->enabled)) {
131 $enabled = false;
132 }
133
134 if (empty($enabled)) {
135 continue;
136 }
137
138 // Init load class
139 $file = $dir."/".$modulename.".class.php";
140 $classname = $modulename;
141 require_once $file;
142 $module = new $classname($this->db);
143
144 if (isset($module->import_code) && is_array($module->import_code)) {
145 foreach ($module->import_code as $r => $value) {
146 if ($filter && ($filter != $module->import_code[$r])) {
147 continue;
148 }
149
150 // Test if permissions are ok
151 /*$perm=$module->import_permission[$r][0];
152 //print_r("$perm[0]-$perm[1]-$perm[2]<br>");
153 if ($perm[2])
154 {
155 $bool=$user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]};
156 }
157 else
158 {
159 $bool=$user->rights->{$perm[0]}->{$perm[1]};
160 }
161 if ($perm[0]=='user' && $user->admin) $bool=true;
162 //print $bool." $perm[0]"."<br>";
163 */
164
165 // Load lang file
166 $langtoload = $module->getLangFilesArray();
167 if (is_array($langtoload)) {
168 foreach ($langtoload as $key) {
169 $langs->load($key);
170 }
171 }
172
173 // Permission
174 $this->array_import_perms[$i] = $user->rights->import->run;
175 // Icon
176 $this->array_import_icon[$i] = (isset($module->import_icon[$r]) ? $module->import_icon[$r] : $module->picto);
177 // Code of dataset export
178 $this->array_import_code[$i] = $module->import_code[$r];
179 // Label of dataset export
180 $this->array_import_label[$i] = $module->getImportDatasetLabel($r);
181 // Array of tables to import (key=alias, value=tablename)
182 $this->array_import_tables[$i] = $module->import_tables_array[$r];
183 // Array of tables creator field to import (key=alias, value=creator field name)
184 $this->array_import_tables_creator[$i] = (isset($module->import_tables_creator_array[$r]) ? $module->import_tables_creator_array[$r] : '');
185 // Array of fields to import (key=field, value=label)
186 $this->array_import_fields[$i] = $module->import_fields_array[$r];
187 // Array of hidden fields to import (key=field, value=label)
188 $this->array_import_fieldshidden[$i] = (isset($module->import_fieldshidden_array[$r]) ? $module->import_fieldshidden_array[$r] : '');
189 // Tableau des entites a exporter (cle=champ, valeur=entite)
190 $this->array_import_entities[$i] = $module->import_entities_array[$r];
191 // Tableau des alias a exporter (cle=champ, valeur=alias)
192 $this->array_import_regex[$i] = (isset($module->import_regex_array[$r]) ? $module->import_regex_array[$r] : '');
193 // Array of columns allowed as UPDATE options
194 $this->array_import_updatekeys[$i] = (isset($module->import_updatekeys_array[$r]) ? $module->import_updatekeys_array[$r] : '');
195 // Array of columns preselected as UPDATE options
196 $this->array_import_preselected_updatekeys[$i] = (isset($module->import_preselected_updatekeys_array[$r]) ? $module->import_preselected_updatekeys_array[$r] : '');
197 // Array of examples
198 $this->array_import_examplevalues[$i] = (isset($module->import_examplevalues_array[$r]) ? $module->import_examplevalues_array[$r] : '');
199 // Tableau des regles de conversion d'une valeur depuis une autre source (cle=champ, valeur=tableau des regles)
200 $this->array_import_convertvalue[$i] = (isset($module->import_convertvalue_array[$r]) ? $module->import_convertvalue_array[$r] : '');
201 // Sql request to run after import
202 $this->array_import_run_sql_after[$i] = (isset($module->import_run_sql_after_array[$r]) ? $module->import_run_sql_after_array[$r] : '');
203 // Module
204 $this->array_import_module[$i] = array('position_of_profile'=>($module->module_position.'-'.$module->import_code[$r]), 'module'=>$module);
205
206 dol_syslog("Import loaded for module ".$modulename." with index ".$i.", dataset=".$module->import_code[$r].", nb of fields=".count($module->import_fields_array[$r]));
207 $i++;
208 }
209 }
210 }
211 closedir($handle);
212 }
213 return 1;
214 }
215
216
217
218 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
229 public function build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
230 {
231 // phpcs:enable
232 global $conf, $langs;
233
234 $indice = 0;
235
236 dol_syslog(get_class($this)."::build_example_file ".$model);
237
238 // Creation de la classe d'import du model Import_XXX
239 $dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
240 $file = "import_".$model.".modules.php";
241 $classname = "Import".$model;
242 require_once $dir.$file;
243 $objmodel = new $classname($this->db, $datatoimport);
244
245 $outputlangs = $langs; // Lang for output
246 $s = '';
247
248 // Genere en-tete
249 $s .= $objmodel->write_header_example($outputlangs);
250
251 // Genere ligne de titre
252 $s .= $objmodel->write_title_example($outputlangs, $headerlinefields);
253
254 // Genere ligne de titre
255 $s .= $objmodel->write_record_example($outputlangs, $contentlinevalues);
256
257 // Genere pied de page
258 $s .= $objmodel->write_footer_example($outputlangs);
259
260 return $s;
261 }
262
269 public function create($user)
270 {
271 global $conf;
272
273 dol_syslog("Import.class.php::create");
274
275 // Check parameters
276 if (empty($this->model_name)) {
277 $this->error = 'ErrorWrongParameters';
278 return -1;
279 }
280 if (empty($this->datatoimport)) {
281 $this->error = 'ErrorWrongParameters';
282 return -1;
283 }
284 if (empty($this->hexa)) {
285 $this->error = 'ErrorWrongParameters';
286 return -1;
287 }
288
289 $this->db->begin();
290
291 $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'import_model (';
292 $sql .= 'fk_user,';
293 $sql .= ' label,';
294 $sql .= ' type,';
295 $sql .= ' field';
296 $sql .= ')';
297 $sql .= " VALUES (";
298 $sql .= (isset($this->fk_user) ? (int) $this->fk_user : 'null').",";
299 $sql .= " '".$this->db->escape($this->model_name)."',";
300 $sql .= " '".$this->db->escape($this->datatoimport)."',";
301 $sql .= " '".$this->db->escape($this->hexa)."'";
302 $sql .= ")";
303
304 $resql = $this->db->query($sql);
305 if ($resql) {
306 $this->db->commit();
307 return 1;
308 } else {
309 $this->error = $this->db->lasterror();
310 $this->errno = $this->db->lasterrno();
311 $this->db->rollback();
312 return -1;
313 }
314 }
315
322 public function fetch($id)
323 {
324 $sql = 'SELECT em.rowid, em.field, em.label, em.type';
325 $sql .= ' FROM '.MAIN_DB_PREFIX.'import_model as em';
326 $sql .= ' WHERE em.rowid = '.((int) $id);
327
328 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
329 $result = $this->db->query($sql);
330 if ($result) {
331 $obj = $this->db->fetch_object($result);
332 if ($obj) {
333 $this->id = $obj->rowid;
334 $this->hexa = $obj->field;
335 $this->model_name = $obj->label;
336 $this->datatoimport = $obj->type;
337 $this->fk_user = $obj->fk_user;
338 return 1;
339 } else {
340 $this->error = "Model not found";
341 return -2;
342 }
343 } else {
344 dol_print_error($this->db);
345 return -3;
346 }
347 }
348
356 public function delete($user, $notrigger = 0)
357 {
358 $error = 0;
359
360 $sql = "DELETE FROM ".MAIN_DB_PREFIX."import_model";
361 $sql .= " WHERE rowid=".((int) $this->id);
362
363 $this->db->begin();
364
365 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
366 $resql = $this->db->query($sql);
367 if (!$resql) {
368 $error++;
369 $this->errors[] = "Error ".$this->db->lasterror();
370 }
371
372 if (!$error) {
373 if (!$notrigger) {
374 /* Not used. This is not a business object. To convert it we must herit from CommonObject
375 // Call trigger
376 $result=$this->call_trigger('IMPORT_DELETE',$user);
377 if ($result < 0) $error++;
378 // End call triggers
379 */
380 }
381 }
382
383 // Commit or rollback
384 if ($error) {
385 foreach ($this->errors as $errmsg) {
386 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
387 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
388 }
389 $this->db->rollback();
390 return -1 * $error;
391 } else {
392 $this->db->commit();
393 return 1;
394 }
395 }
396}
Class to manage imports.
__construct($db)
Constructor.
fetch($id)
Load an import profil from database.
build_example_file($model, $headerlinefields, $contentlinevalues, $datatoimport)
Build an import example file.
create($user)
Save an export model in database.
load_arrays($user, $filter='')
Load description int this->array_import_module, this->array_import_fields, ... of an importable datas...
dolGetModulesDirs($subdir='')
Return list of modules directories.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.