--- /tmp/dsg/dolibarr/htdocs/core/modules/import/github_import_csv.modules.php
+++ /tmp/dsg/dolibarr/htdocs/core/modules/import/client_import_csv.modules.php
@@ -33,12 +33,12 @@
*/
class ImportCsv extends ModeleImports
{
- /**
- * @var DoliDB Database handler.
- */
- public $db;
-
- public $datatoimport;
+ /**
+ * @var DoliDB Database handler.
+ */
+ public $db;
+
+ public $datatoimport;
/**
* @var string Error code (or message)
@@ -50,22 +50,22 @@
*/
public $errors = array();
- /**
+ /**
* @var int ID
*/
public $id;
/**
- * @var string label
- */
- public $label;
+ * @var string label
+ */
+ public $label;
public $extension; // Extension of files imported by driver
/**
- * Dolibarr version of driver
- * @var string
- */
+ * Dolibarr version of driver
+ * @var string
+ */
public $version = 'dolibarr';
public $label_lib; // Label of external lib used by driver
@@ -93,7 +93,7 @@
* @param DoliDB $db Database handler
* @param string $datatoimport String code describing import set (ex: 'societe_1')
*/
- public function __construct($db, $datatoimport)
+ public function __construct($db, $datatoimport)
{
global $conf, $langs;
$this->db = $db;
@@ -118,20 +118,20 @@
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Output header of an example file for this format
*
* @param Translate $outputlangs Output language
* @return string Empty string
*/
- public function write_header_example($outputlangs)
+ public function write_header_example($outputlangs)
{
- // phpcs:enable
+ // phpcs:enable
return '';
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Output title line of an example file for this format
*
@@ -139,14 +139,14 @@
* @param array $headerlinefields Array of fields name
* @return string String output
*/
- public function write_title_example($outputlangs, $headerlinefields)
+ public function write_title_example($outputlangs, $headerlinefields)
{
- // phpcs:enable
+ // phpcs:enable
$s = join($this->separator, array_map('cleansep', $headerlinefields));
return $s."\n";
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Output record of an example file for this format
*
@@ -154,37 +154,37 @@
* @param array $contentlinevalues Array of lines
* @return string String output
*/
- public function write_record_example($outputlangs, $contentlinevalues)
+ public function write_record_example($outputlangs, $contentlinevalues)
{
- // phpcs:enable
+ // phpcs:enable
$s = join($this->separator, array_map('cleansep', $contentlinevalues));
return $s."\n";
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Output footer of an example file for this format
*
* @param Translate $outputlangs Output language
* @return string Empty string
*/
- public function write_footer_example($outputlangs)
+ public function write_footer_example($outputlangs)
{
- // phpcs:enable
+ // phpcs:enable
return '';
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Open input file
*
* @param string $file Path of filename
* @return int <0 if KO, >=0 if OK
*/
- public function import_open_file($file)
+ public function import_open_file($file)
{
- // phpcs:enable
+ // phpcs:enable
global $langs;
$ret = 1;
@@ -198,7 +198,9 @@
$langs->load("errors");
$this->error = $langs->trans("ErrorFailToOpenFile", $file);
$ret = -1;
- } else {
+ }
+ else
+ {
$this->file = $file;
}
@@ -206,42 +208,42 @@
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return nb of records. File must be closed.
*
* @param string $file Path of filename
* @return int <0 if KO, >=0 if OK
*/
- public function import_get_nb_of_lines($file)
+ public function import_get_nb_of_lines($file)
+ {
+ // phpcs:enable
+ return dol_count_nb_of_line($file);
+ }
+
+
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ /**
+ * Input header line from file
+ *
+ * @return int <0 if KO, >=0 if OK
+ */
+ public function import_read_header()
{
- // phpcs:enable
- return dol_count_nb_of_line($file);
- }
-
-
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Input header line from file
- *
- * @return int <0 if KO, >=0 if OK
- */
- public function import_read_header()
- {
- // phpcs:enable
+ // phpcs:enable
return 0;
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return array of next record in input file.
*
* @return Array Array of field values. Data are UTF8 encoded. [fieldpos] => (['val']=>val, ['type']=>-1=null,0=blank,1=not empty string)
*/
- public function import_read_record()
+ public function import_read_record()
{
- // phpcs:enable
+ // phpcs:enable
global $conf;
$arrayres = fgetcsv($this->handle, 100000, $this->separator, $this->enclosure, $this->escape);
@@ -262,17 +264,22 @@
{
$newarrayres[$key]['val'] = $val;
$newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
- } else {
+ }
+ else
+ {
$newarrayres[$key]['val'] = utf8_encode($val);
$newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
}
- } else // Autodetect format (UTF8 or ISO)
+ }
+ else // Autodetect format (UTF8 or ISO)
{
if (utf8_check($val))
{
$newarrayres[$key]['val'] = $val;
$newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
- } else {
+ }
+ else
+ {
$newarrayres[$key]['val'] = utf8_encode($val);
$newarrayres[$key]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we considere it's null
}
@@ -285,21 +292,21 @@
return $newarrayres;
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Close file handle
*
* @return integer
*/
- public function import_close_file()
+ public function import_close_file()
{
- // phpcs:enable
+ // phpcs:enable
fclose($this->handle);
return 0;
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Insert a record into database
*
@@ -311,11 +318,11 @@
* @param array $updatekeys Array of keys to use to try to do an update first before insert. This field are defined into the module descriptor.
* @return int <0 if KO, >0 if OK
*/
- public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
+ public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
{
- // phpcs:enable
+ // phpcs:enable
global $langs, $conf, $user;
- global $thirdparty_static; // Specific to thirdparty import
+ global $thirdparty_static; // Specific to thirdparty import
global $tablewithentity_cache; // Cache to avoid to call desc at each rows on tables
$error = 0;
@@ -339,7 +346,9 @@
$this->warnings[$warning]['lib'] = $langs->trans('EmptyLine');
$this->warnings[$warning]['type'] = 'EMPTY';
$warning++;
- } else {
+ }
+ else
+ {
$last_insert_id_array = array(); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id (eg: extrafields fk_object will be set with the last inserted object's id)
$updatedone = false;
$insertdone = false;
@@ -363,24 +372,27 @@
$obj = $this->db->fetch_object($resql);
if ($obj) $tablewithentity_cache[$tablename] = 1; // table contains entity field
else $tablewithentity_cache[$tablename] = 0; // table does not contains entity field
- } else dol_print_error($this->db);
- } else {
+ }
+ else dol_print_error($this->db);
+ }
+ else
+ {
//dol_syslog("Table ".$tablename." check for entity into cache is ".$tablewithentity_cache[$tablename]);
}
// array of fields to column index
- $arrayfield = array();
- foreach ($sort_array_match_file_to_database as $key => $val) {
- $arrayfield[$val] = ($key - 1);
- }
+ $arrayfield = array();
+ foreach ($sort_array_match_file_to_database as $key => $val) {
+ $arrayfield[$val] = ($key - 1);
+ }
// Loop on each fields in the match array: $key = 1..n, $val=alias of field (s.nom)
foreach ($sort_array_match_file_to_database as $key => $val)
{
- $fieldalias = preg_replace('/\..*$/i', '', $val);
- $fieldname = preg_replace('/^.*\./i', '', $val);
-
- if ($alias != $fieldalias) continue; // Not a field of current table
+ $fieldalias = preg_replace('/\..*$/i', '', $val);
+ $fieldname = preg_replace('/^.*\./i', '', $val);
+
+ if ($alias != $fieldalias) continue; // Not a field of current table
if ($key <= $maxfields)
{
@@ -399,43 +411,47 @@
$error++;
}
// Test format only if field is not a missing mandatory field (field may be a value or empty but not mandatory)
- else {
- // We convert field if required
- if (!empty($objimport->array_import_convertvalue[0][$val]))
- {
- //print 'Must convert '.$newval.' with rule '.join(',',$objimport->array_import_convertvalue[0][$val]).'. ';
- if ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeid'
- || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromref'
- || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel'
- )
- {
- // New val can be an id or ref. If it start with id: it is forced to id, if it start with ref: it is forced to ref. It not, we try to guess.
- $isidorref = 'id';
- if (!is_numeric($newval) && $newval != '' && !preg_match('/^id:/i', $newval)) $isidorref = 'ref';
-
- $newval = preg_replace('/^(id|ref):/i', '', $newval); // Remove id: or ref: that was used to force if field is id or ref
- //print 'Val is now '.$newval.' and is type '.$isidorref."
\n";
-
- if ($isidorref == 'ref') { // If value into input import file is a ref, we apply the function defined into descriptor
- $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
- $class = $objimport->array_import_convertvalue[0][$val]['class'];
- $method = $objimport->array_import_convertvalue[0][$val]['method'];
- if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] != '')
+ else
+ {
+ // We convert field if required
+ if (!empty($objimport->array_import_convertvalue[0][$val]))
+ {
+ //print 'Must convert '.$newval.' with rule '.join(',',$objimport->array_import_convertvalue[0][$val]).'. ';
+ if ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeid'
+ || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromref'
+ || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel'
+ )
+ {
+ // New val can be an id or ref. If it start with id: it is forced to id, if it start with ref: it is forced to ref. It not, we try to guess.
+ $isidorref = 'id';
+ if (!is_numeric($newval) && $newval != '' && !preg_match('/^id:/i', $newval)) $isidorref = 'ref';
+
+ $newval = preg_replace('/^(id|ref):/i', '', $newval); // Remove id: or ref: that was used to force if field is id or ref
+ //print 'Val is now '.$newval.' and is type '.$isidorref."
\n";
+
+ if ($isidorref == 'ref') // If value into input import file is a ref, we apply the function defined into descriptor
+ {
+ $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
+ $class = $objimport->array_import_convertvalue[0][$val]['class'];
+ $method = $objimport->array_import_convertvalue[0][$val]['method'];
+ if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] != '')
+ {
+ $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval];
+ }
+ else
{
- $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval];
- } else {
- $resultload = dol_include_once($file);
- if (empty($resultload))
- {
- dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method);
- break;
- }
- $classinstance = new $class($this->db);
- // Try the fetch from code or ref
- $param_array = array('', $newval);
- if ($class == 'AccountingAccount') {
- //var_dump($arrayrecord[0]['val']);
- /*include_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancysystem.class.php';
+ $resultload = dol_include_once($file);
+ if (empty($resultload))
+ {
+ dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method);
+ break;
+ }
+ $classinstance = new $class($this->db);
+ // Try the fetch from code or ref
+ $param_array = array('', $newval);
+ if ($class == 'AccountingAccount') {
+ //var_dump($arrayrecord[0]['val']);
+ /*include_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancysystem.class.php';
$tmpchartofaccount = new AccountancySystem($this->db);
$tmpchartofaccount->fetch($conf->global->CHARTOFACCOUNTS);
var_dump($tmpchartofaccount->ref.' - '.$arrayrecord[0]['val']);
@@ -446,179 +462,202 @@
$errorforthistable++;
$error++;
}*/
- $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart.
- }
-
- call_user_func_array(array($classinstance, $method), $param_array);
- // If not found, try the fetch from label
- if (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel')
- {
- $param_array = array('', '', $newval);
- call_user_func_array(array($classinstance, $method), $param_array);
- }
- $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] = $classinstance->id;
- //print 'We have made a '.$class.'->'.$method.' to get id from code '.$newval.'. ';
- if ($classinstance->id != '') // id may be 0, it is a found value
- {
- $newval = $classinstance->id;
- } else {
- if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
- elseif (!empty($objimport->array_import_convertvalue[0][$val]['element'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldRefNotIn', $key, $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['element']));
- else $this->errors[$error]['lib'] = 'ErrorBadDefinitionOfImportProfile';
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
- $error++;
- }
- }
- }
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeandlabel') {
- $isidorref = 'id';
- if (!is_numeric($newval) && $newval != '' && !preg_match('/^id:/i', $newval)) $isidorref = 'ref';
- $newval = preg_replace('/^(id|ref):/i', '', $newval);
-
- if ($isidorref == 'ref') {
- $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
- $class = $objimport->array_import_convertvalue[0][$val]['class'];
- $method = $objimport->array_import_convertvalue[0][$val]['method'];
- $codefromfield = $objimport->array_import_convertvalue[0][$val]['codefromfield'];
- $code = $arrayrecord[$arrayfield[$codefromfield]]['val'];
- if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval] != '') {
- $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval];
- } else {
- $resultload = dol_include_once($file);
- if (empty($resultload)) {
- dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method.', code='.$code);
- break;
- }
- $classinstance = new $class($this->db);
- // Try the fetch from code and ref
- $param_array = array('', $newval, $code);
- call_user_func_array(array($classinstance, $method), $param_array);
- $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval] = $classinstance->id;
- if ($classinstance->id > 0) // we found record
- {
- $newval = $classinstance->id;
- } else {
- if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
- else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
- $error++;
- }
- }
- }
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'zeroifnull') {
- if (empty($newval)) $newval = '0';
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchscalefromcodeunits') {
- $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
- $class = $objimport->array_import_convertvalue[0][$val]['class'];
- $method = $objimport->array_import_convertvalue[0][$val]['method'];
- $units = $objimport->array_import_convertvalue[0][$val]['units'];
- if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval] != '')
- {
- $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval];
- } else {
- $resultload = dol_include_once($file);
- if (empty($resultload))
- {
- dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method.', units='.$units);
- break;
- }
- $classinstance = new $class($this->db);
- // Try the fetch from code or ref
- call_user_func_array(array($classinstance, $method), array('', '', $newval, $units));
- $scaleorid = (($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
- $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval] = $scaleorid;
- //print 'We have made a '.$class.'->'.$method." to get a value from key '".$newval."' and we got '".$scaleorid."'.";exit;
- if ($classinstance->id > 0) // we found record
- {
- $newval = $scaleorid ? $scaleorid : 0;
- } else {
- if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
- else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
- $error++;
- }
- }
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomercodeifauto') {
- if (strtolower($newval) == 'auto')
- {
- $this->thirpartyobject->get_codeclient(0, 0);
- $newval = $this->thirpartyobject->code_client;
- //print 'code_client='.$newval;
- }
- if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsuppliercodeifauto') {
- if (strtolower($newval) == 'auto')
- {
- $newval = $this->thirpartyobject->get_codefournisseur(0, 1);
- $newval = $this->thirpartyobject->code_fournisseur;
- //print 'code_fournisseur='.$newval;
- }
- if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomeraccountancycodeifauto') {
- if (strtolower($newval) == 'auto')
- {
- $this->thirpartyobject->get_codecompta('customer');
- $newval = $this->thirpartyobject->code_compta;
- //print 'code_compta='.$newval;
- }
- if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsupplieraccountancycodeifauto') {
- if (strtolower($newval) == 'auto')
- {
- $this->thirpartyobject->get_codecompta('supplier');
- $newval = $this->thirpartyobject->code_compta_fournisseur;
- if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
- //print 'code_compta_fournisseur='.$newval;
- }
- if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getrefifauto') {
- $defaultref = '';
- // TODO provide the $modTask (module of generation of ref) as parameter of import_insert function
- $obj = empty($conf->global->PROJECT_TASK_ADDON) ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON;
- if (!empty($conf->global->PROJECT_TASK_ADDON) && is_readable(DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.".php"))
- {
- require_once DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.'.php';
- $modTask = new $obj;
- $defaultref = $modTask->getNextValue(null, null);
- }
- if (is_numeric($defaultref) && $defaultref <= 0) $defaultref = '';
- $newval = $defaultref;
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'compute') {
- $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
- $class = $objimport->array_import_convertvalue[0][$val]['class'];
- $method = $objimport->array_import_convertvalue[0][$val]['method'];
- $resultload = dol_include_once($file);
- if (empty($resultload))
- {
- dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method);
- break;
- }
- $classinstance = new $class($this->db);
- $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord));
- if ($res < 0) {
- if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
- else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
- $error++;
- } else {
- $newval = $arrayrecord[($key - 1)]['val']; //We get new value computed.
- }
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'numeric') {
- $newval = price2num($newval);
- }
-
- //print 'Val to use as insert is '.$newval.'
';
- }
-
- // Test regexp
+ $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart.
+ }
+
+ call_user_func_array(array($classinstance, $method), $param_array);
+ // If not found, try the fetch from label
+ if (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel')
+ {
+ $param_array = array('', '', $newval);
+ call_user_func_array(array($classinstance, $method), $param_array);
+ }
+ $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] = $classinstance->id;
+ //print 'We have made a '.$class.'->'.$method.' to get id from code '.$newval.'. ';
+ if ($classinstance->id != '') // id may be 0, it is a found value
+ {
+ $newval = $classinstance->id;
+ }
+ else
+ {
+ if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
+ elseif (!empty($objimport->array_import_convertvalue[0][$val]['element'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldRefNotIn', $key, $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['element']));
+ else $this->errors[$error]['lib'] = 'ErrorBadDefinitionOfImportProfile';
+ $this->errors[$error]['type'] = 'FOREIGNKEY';
+ $errorforthistable++;
+ $error++;
+ }
+ }
+ }
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeandlabel')
+ {
+ $isidorref = 'id';
+ if (!is_numeric($newval) && $newval != '' && !preg_match('/^id:/i', $newval)) $isidorref = 'ref';
+ $newval = preg_replace('/^(id|ref):/i', '', $newval);
+
+ if ($isidorref == 'ref') {
+ $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
+ $class = $objimport->array_import_convertvalue[0][$val]['class'];
+ $method = $objimport->array_import_convertvalue[0][$val]['method'];
+ $codefromfield = $objimport->array_import_convertvalue[0][$val]['codefromfield'];
+ $code = $arrayrecord[$arrayfield[$codefromfield]]['val'];
+ if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval] != '') {
+ $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval];
+ } else {
+ $resultload = dol_include_once($file);
+ if (empty($resultload)) {
+ dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method.', code='.$code);
+ break;
+ }
+ $classinstance = new $class($this->db);
+ // Try the fetch from code and ref
+ $param_array = array('', $newval, $code);
+ call_user_func_array(array($classinstance, $method), $param_array);
+ $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval] = $classinstance->id;
+ if ($classinstance->id > 0) // we found record
+ {
+ $newval = $classinstance->id;
+ } else {
+ if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
+ else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
+ $this->errors[$error]['type'] = 'FOREIGNKEY';
+ $errorforthistable++;
+ $error++;
+ }
+ }
+ }
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'zeroifnull')
+ {
+ if (empty($newval)) $newval = '0';
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchscalefromcodeunits')
+ {
+ $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
+ $class = $objimport->array_import_convertvalue[0][$val]['class'];
+ $method = $objimport->array_import_convertvalue[0][$val]['method'];
+ $units = $objimport->array_import_convertvalue[0][$val]['units'];
+ if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval] != '')
+ {
+ $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval];
+ }
+ else
+ {
+ $resultload = dol_include_once($file);
+ if (empty($resultload))
+ {
+ dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method.', units='.$units);
+ break;
+ }
+ $classinstance = new $class($this->db);
+ // Try the fetch from code or ref
+ call_user_func_array(array($classinstance, $method), array('', '', $newval, $units));
+ $scaleorid = (($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
+ $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval] = $scaleorid;
+ //print 'We have made a '.$class.'->'.$method." to get a value from key '".$newval."' and we got '".$scaleorid."'.";exit;
+ if ($classinstance->id > 0) // we found record
+ {
+ $newval = $scaleorid ? $scaleorid : 0;
+ }
+ else
+ {
+ if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
+ else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
+ $this->errors[$error]['type'] = 'FOREIGNKEY';
+ $errorforthistable++;
+ $error++;
+ }
+ }
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomercodeifauto')
+ {
+ if (strtolower($newval) == 'auto')
+ {
+ $this->thirpartyobject->get_codeclient(0, 0);
+ $newval = $this->thirpartyobject->code_client;
+ //print 'code_client='.$newval;
+ }
+ if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsuppliercodeifauto')
+ {
+ if (strtolower($newval) == 'auto')
+ {
+ $newval = $this->thirpartyobject->get_codefournisseur(0, 1);
+ $newval = $this->thirpartyobject->code_fournisseur;
+ //print 'code_fournisseur='.$newval;
+ }
+ if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomeraccountancycodeifauto')
+ {
+ if (strtolower($newval) == 'auto')
+ {
+ $this->thirpartyobject->get_codecompta('customer');
+ $newval = $this->thirpartyobject->code_compta;
+ //print 'code_compta='.$newval;
+ }
+ if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsupplieraccountancycodeifauto')
+ {
+ if (strtolower($newval) == 'auto')
+ {
+ $this->thirpartyobject->get_codecompta('supplier');
+ $newval = $this->thirpartyobject->code_compta_fournisseur;
+ if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
+ //print 'code_compta_fournisseur='.$newval;
+ }
+ if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getrefifauto')
+ {
+ $defaultref = '';
+ // TODO provide the $modTask (module of generation of ref) as parameter of import_insert function
+ $obj = empty($conf->global->PROJECT_TASK_ADDON) ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON;
+ if (!empty($conf->global->PROJECT_TASK_ADDON) && is_readable(DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.".php"))
+ {
+ require_once DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.'.php';
+ $modTask = new $obj;
+ $defaultref = $modTask->getNextValue(null, null);
+ }
+ if (is_numeric($defaultref) && $defaultref <= 0) $defaultref = '';
+ $newval = $defaultref;
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'compute')
+ {
+ $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
+ $class = $objimport->array_import_convertvalue[0][$val]['class'];
+ $method = $objimport->array_import_convertvalue[0][$val]['method'];
+ $resultload = dol_include_once($file);
+ if (empty($resultload))
+ {
+ dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method);
+ break;
+ }
+ $classinstance = new $class($this->db);
+ $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord));
+ if ($res < 0) {
+ if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
+ else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
+ $this->errors[$error]['type'] = 'FOREIGNKEY';
+ $errorforthistable++;
+ $error++;
+ }
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'numeric')
+ {
+ $newval = price2num($newval);
+ }
+
+ //print 'Val to use as insert is '.$newval.'
';
+ }
+
+ // Test regexp
if (!empty($objimport->array_import_regex[0][$val]) && ($newval != ''))
{
// If test is "Must exist in a field@table or field@table:..."
- $reg = array();
if (preg_match('/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg))
{
$field = $reg[1];
@@ -648,7 +687,9 @@
if ($obj) $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
$i++;
}
- } else {
+ }
+ else
+ {
dol_print_error($this->db);
}
}
@@ -660,13 +701,13 @@
if (!empty($filter)) $tableforerror .= ':'.$filter;
$this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorFieldValueNotIn', $key, $newval, $field, $tableforerror);
$this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
+ $errorforthistable++;
$error++;
}
}
// If test is just a static regex
elseif (!preg_match('/'.$objimport->array_import_regex[0][$val].'/i', $newval)) {
- //if ($key == 19) print "xxx".$newval."zzz".$objimport->array_import_regex[0][$val]."
";
+ //if ($key == 19) print "xxx".$newval."zzz".$objimport->array_import_regex[0][$val]."
";
$this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorWrongValueForField', $key, $newval, $objimport->array_import_regex[0][$val]);
$this->errors[$error]['type'] = 'REGEX';
$errorforthistable++;
@@ -683,12 +724,12 @@
// Note: arrayrecord (and 'type') is filled with ->import_read_record called by import.php page before calling import_insert
if (empty($newval) && $arrayrecord[($key - 1)]['type'] < 0) {
- $listvalues[] = ($newval == '0' ? $newval : "null");
- } elseif (empty($newval) && $arrayrecord[($key - 1)]['type'] == 0) {
- $listvalues[] = "''";
- } else {
- $listvalues[] = "'".$this->db->escape($newval)."'";
- }
+ $listvalues[] = ($newval == '0' ? $newval : "null");
+ } elseif (empty($newval) && $arrayrecord[($key - 1)]['type'] == 0) {
+ $listvalues[] = "''";
+ } else {
+ $listvalues[] = "'".$this->db->escape($newval)."'";
+ }
}
$i++;
}
@@ -696,33 +737,37 @@
// We add hidden fields (but only if there is at least one field to add into table)
if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0]))
{
- // Loop on each hidden fields to add them into listfields/listvalues
- foreach ($objimport->array_import_fieldshidden[0] as $key => $val)
- {
- if (!preg_match('/^'.preg_quote($alias, '/').'\./', $key)) continue; // Not a field of current table
- if ($val == 'user->id')
- {
- $listfields[] = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
- $listvalues[] = $user->id;
- } elseif (preg_match('/^lastrowid-/', $val))
- {
- $tmp = explode('-', $val);
- $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
+ // Loop on each hidden fields to add them into listfields/listvalues
+ foreach ($objimport->array_import_fieldshidden[0] as $key => $val)
+ {
+ if (!preg_match('/^'.preg_quote($alias, '/').'\./', $key)) continue; // Not a field of current table
+ if ($val == 'user->id')
+ {
+ $listfields[] = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
+ $listvalues[] = $user->id;
+ }
+ elseif (preg_match('/^lastrowid-/', $val))
+ {
+ $tmp = explode('-', $val);
+ $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
$keyfield = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
- $listfields[] = $keyfield;
- $listvalues[] = $lastinsertid;
- //print $key."-".$val."-".$listfields."-".$listvalues."
";exit;
- } elseif (preg_match('/^const-/', $val))
- {
- $tmp = explode('-', $val, 2);
- $listfields[] = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
- $listvalues[] = "'".$tmp[1]."'";
- } else {
- $this->errors[$error]['lib'] = 'Bad value of profile setup '.$val.' for array_import_fieldshidden';
- $this->errors[$error]['type'] = 'Import profile setup';
- $error++;
- }
- }
+ $listfields[] = $keyfield;
+ $listvalues[] = $lastinsertid;
+ //print $key."-".$val."-".$listfields."-".$listvalues."
";exit;
+ }
+ elseif (preg_match('/^const-/', $val))
+ {
+ $tmp = explode('-', $val, 2);
+ $listfields[] = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
+ $listvalues[] = "'".$tmp[1]."'";
+ }
+ else
+ {
+ $this->errors[$error]['lib'] = 'Bad value of profile setup '.$val.' for array_import_fieldshidden';
+ $this->errors[$error]['type'] = 'Import profile setup';
+ $error++;
+ }
+ }
}
//print 'listfields='.$listfields.'
listvalues='.$listvalues.'
';
@@ -766,7 +811,9 @@
} else {
// No record found with filters, insert will be tried below
}
- } else {
+ }
+ else
+ {
//print 'E';
$this->errors[$error]['lib'] = $this->db->lasterror();
$this->errors[$error]['type'] = 'SQL';
@@ -793,7 +840,9 @@
// force $lastinsertid to 0 so we INSERT below.
$lastinsertid = 0;
}
- } else {
+ }
+ else
+ {
//print 'E';
$this->errors[$error]['lib'] = $this->db->lasterror();
$this->errors[$error]['type'] = 'SQL';
@@ -822,7 +871,9 @@
if ($resql) {
// No error, update has been done. $this->db->db->affected_rows can be 0 if data hasn't changed
$updatedone = true;
- } else {
+ }
+ else
+ {
//print 'E';
$this->errors[$error]['lib'] = $this->db->lasterror();
$this->errors[$error]['type'] = 'SQL';
@@ -835,7 +886,7 @@
if (!$error && !$updatedone) {
// Build SQL INSERT request
$sqlstart = 'INSERT INTO '.$tablename.'('.implode(', ', $listfields).', import_key';
- $sqlend = ') VALUES('.implode(', ', $listvalues).", '".$this->db->escape($importid)."'";
+ $sqlend = ') VALUES('.implode(', ', $listvalues).", '".$importid."'";
if (!empty($tablewithentity_cache[$tablename])) {
$sqlstart .= ', entity';
$sqlend .= ', '.$conf->entity;
@@ -845,7 +896,7 @@
$sqlend .= ', '.$user->id;
}
$sql = $sqlstart.$sqlend.')';
- //dol_syslog("import_csv.modules", LOG_DEBUG);
+ dol_syslog("import_csv.modules", LOG_DEBUG);
// Run insert request
if ($sql)
@@ -853,9 +904,11 @@
$resql = $this->db->query($sql);
if ($resql)
{
- $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that child tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr).
- $insertdone = true;
- } else {
+ $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that child tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr).
+ $insertdone = true;
+ }
+ else
+ {
//print 'E';
$this->errors[$error]['lib'] = $this->db->lasterror();
$this->errors[$error]['type'] = 'SQL';
@@ -870,7 +923,7 @@
}*/
}
- if ($error) break;
+ if ($error) break;
}
if ($updatedone) $this->nbupdate++;
--- /tmp/dsg/dolibarr/htdocs/core/modules/import/github_import_xlsx.modules.php
+++ /tmp/dsg/dolibarr/htdocs/core/modules/import/client_import_xlsx.modules.php
@@ -25,11 +25,7 @@
* \brief File to load import files with Excel format
*/
-use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
-use PhpOffice\PhpSpreadsheet\Spreadsheet;
-use PhpOffice\PhpSpreadsheet\Style\Alignment;
-
-require_once DOL_DOCUMENT_ROOT . '/core/modules/import/modules_import.php';
+require_once DOL_DOCUMENT_ROOT.'/core/modules/import/modules_import.php';
/**
@@ -37,14 +33,14 @@
*/
class ImportXlsx extends ModeleImports
{
- /**
- * @var DoliDB Database handler.
- */
- public $db;
-
- public $datatoimport;
-
- /**
+ /**
+ * @var DoliDB Database handler.
+ */
+ public $db;
+
+ public $datatoimport;
+
+ /**
* @var string Error code (or message)
*/
public $error = '';
@@ -54,22 +50,22 @@
*/
public $errors = array();
- /**
+ /**
* @var int ID
*/
public $id;
/**
- * @var string label
- */
- public $label;
+ * @var string label
+ */
+ public $label;
public $extension; // Extension of files imported by driver
/**
- * Dolibarr version of driver
- * @var string
- */
+ * Dolibarr version of driver
+ * @var string
+ */
public $version = 'dolibarr';
public $label_lib; // Label of external lib used by driver
@@ -78,7 +74,7 @@
public $separator;
- public $file; // Path of file
+ public $file; // Path of file
public $handle; // Handle fichier
@@ -99,7 +95,7 @@
* @param DoliDB $db Database handler
* @param string $datatoimport String code describing import set (ex: 'societe_1')
*/
- public function __construct($db, $datatoimport)
+ public function __construct($db, $datatoimport)
{
global $conf, $langs;
$this->db = $db;
@@ -111,54 +107,58 @@
$this->extension = 'xlsx'; // Extension for generated file by this driver
$this->picto = 'mime/xls'; // Picto (This is not used by the example file code as Mime type, too bad ...)
$this->version = '1.0'; // Driver version
+
// If driver use an external library, put its name here
- require_once DOL_DOCUMENT_ROOT . '/includes/phpoffice/autoloader.php';
- require_once DOL_DOCUMENT_ROOT . '/includes/Psr/autoloader.php';
- require_once PHPEXCELNEW_PATH . 'Spreadsheet.php';
- $this->workbook = new Spreadsheet();
-
- //if ($this->id == 'excel2007new')
- {
- if (!class_exists('ZipArchive')) // For Excel2007
- {
- $langs->load("errors");
- $this->error = $langs->trans('ErrorPHPNeedModule', 'zip');
- return -1;
- }
- }
- $this->label_lib = 'PhpSpreadSheet';
- $this->version_lib = '1.8.0';
+ require_once PHPEXCEL_PATH.'PHPExcel.php';
+ require_once PHPEXCEL_PATH.'PHPExcel/Style/Alignment.php';
+ if (!class_exists('ZipArchive')) // For Excel2007, PHPExcel need ZipArchive
+ {
+ $langs->load("errors");
+ $this->error = $langs->trans('ErrorPHPNeedModule', 'zip');
+ return -1;
+ }
+ $this->label_lib = 'PhpExcel';
+ $this->version_lib = '1.8.0';
$this->datatoimport = $datatoimport;
if (preg_match('/^societe_/', $datatoimport)) $this->thirpartyobject = new Societe($this->db);
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Output header of an example file for this format
*
* @param Translate $outputlangs Output language
* @return string Empty string
*/
- public function write_header_example($outputlangs)
- {
- // phpcs:enable
- global $user, $conf, $langs, $file;
- // create a temporary object, the final output will be generated in footer
- $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs) . ' - Dolibarr ' . DOL_VERSION);
- $this->workbook->getProperties()->setTitle($outputlangs->trans("Import") . ' - ' . $file);
- $this->workbook->getProperties()->setSubject($outputlangs->trans("Import") . ' - ' . $file);
- $this->workbook->getProperties()->setDescription($outputlangs->trans("Import") . ' - ' . $file);
-
- $this->workbook->setActiveSheetIndex(0);
- $this->workbook->getActiveSheet()->setTitle($outputlangs->trans("Sheet"));
- $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
-
- return '';
- }
-
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ public function write_header_example($outputlangs)
+ {
+ // phpcs:enable
+ global $user, $conf, $langs;
+ // create a temporary object, the final output will be generated in footer
+ if (!empty($conf->global->MAIN_USE_FILECACHE_EXPORT_EXCEL_DIR)) {
+ $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_discISAM;
+ $cacheSettings = array(
+ 'dir' => $conf->global->MAIN_USE_FILECACHE_EXPORT_EXCEL_DIR
+ );
+ PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
+ }
+
+ $this->workbook = new PHPExcel();
+ $this->workbook->getProperties()->setCreator($user->getFullName($outputlangs).' - Dolibarr '.DOL_VERSION);
+ $this->workbook->getProperties()->setTitle($outputlangs->trans("Import").' - '.$file);
+ $this->workbook->getProperties()->setSubject($outputlangs->trans("Import").' - '.$file);
+ $this->workbook->getProperties()->setDescription($outputlangs->trans("Import").' - '.$file);
+
+ $this->workbook->setActiveSheetIndex(0);
+ $this->workbook->getActiveSheet()->setTitle($outputlangs->trans("Sheet"));
+ $this->workbook->getActiveSheet()->getDefaultRowDimension()->setRowHeight(16);
+
+ return '';
+ }
+
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Output title line of an example file for this format
*
@@ -166,14 +166,14 @@
* @param array $headerlinefields Array of fields name
* @return string String output
*/
- public function write_title_example($outputlangs, $headerlinefields)
- {
- // phpcs:enable
+ public function write_title_example($outputlangs, $headerlinefields)
+ {
+ // phpcs:enable
global $conf;
$this->workbook->getActiveSheet()->getStyle('1')->getFont()->setBold(true);
- $this->workbook->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_LEFT);
-
- $col = 1;
+ $this->workbook->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
+
+ $col = 0;
foreach ($headerlinefields as $field) {
$this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, 1, $outputlangs->transnoentities($field));
// set autowidth
@@ -184,7 +184,7 @@
return ''; // final output will be generated in footer
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Output record of an example file for this format
*
@@ -192,10 +192,10 @@
* @param array $contentlinevalues Array of lines
* @return string Empty string
*/
- public function write_record_example($outputlangs, $contentlinevalues)
- {
- // phpcs:enable
- $col = 1;
+ public function write_record_example($outputlangs, $contentlinevalues)
+ {
+ // phpcs:enable
+ $col = 0;
$row = 2;
foreach ($contentlinevalues as $cell) {
$this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, $row, $cell);
@@ -205,19 +205,19 @@
return ''; // final output will be generated in footer
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Output footer of an example file for this format
*
* @param Translate $outputlangs Output language
* @return string String output
*/
- public function write_footer_example($outputlangs)
- {
- // phpcs:enable
+ public function write_footer_example($outputlangs)
+ {
+ // phpcs:enable
// return the file content as a string
$tempfile = tempnam(sys_get_temp_dir(), 'dol');
- $objWriter = new PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->workbook);
+ $objWriter = new PHPExcel_Writer_Excel2007($this->workbook);
$objWriter->save($tempfile);
$this->workbook->disconnectWorksheets();
unset($this->workbook);
@@ -229,22 +229,22 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Open input file
*
* @param string $file Path of filename
* @return int <0 if KO, >=0 if OK
*/
- public function import_open_file($file)
- {
- // phpcs:enable
+ public function import_open_file($file)
+ {
+ // phpcs:enable
global $langs;
$ret = 1;
- dol_syslog(get_class($this) . "::open_file file=" . $file);
-
- $reader = new Xlsx();
+ dol_syslog(get_class($this)."::open_file file=".$file);
+
+ $reader = new PHPExcel_Reader_Excel2007();
$this->workbook = $reader->load($file);
$this->record = 1;
$this->file = $file;
@@ -253,17 +253,17 @@
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return nb of records. File must be closed.
*
* @param string $file Path of filename
* @return int <0 if KO, >=0 if OK
*/
- public function import_get_nb_of_lines($file)
- {
- // phpcs:enable
- $reader = new Xlsx();
+ public function import_get_nb_of_lines($file)
+ {
+ // phpcs:enable
+ $reader = new PHPExcel_Reader_Excel2007();
$this->workbook = $reader->load($file);
$rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
@@ -275,46 +275,42 @@
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Input header line from file
*
* @return int <0 if KO, >=0 if OK
*/
- public function import_read_header()
- {
- // phpcs:enable
+ public function import_read_header()
+ {
+ // phpcs:enable
// This is not called by the import code !!!
$this->headers = array();
- $xlsx = new Xlsx();
- $info = $xlsx->listWorksheetinfo($this->file);
- $countcolumns = $info[0]['totalColumns'];
- for ($col = 1; $col <= $countcolumns; $col++) {
+ $colcount = PHPExcel_Cell::columnIndexFromString($this->workbook->getActiveSheet()->getHighestDataColumn());
+ for ($col = 0; $col < $colcount; $col++) {
$this->headers[$col] = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, 1)->getValue();
}
return 0;
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Return array of next record in input file.
*
* @return Array Array of field values. Data are UTF8 encoded. [fieldpos] => (['val']=>val, ['type']=>-1=null,0=blank,1=not empty string)
*/
- public function import_read_record()
- {
- // phpcs:enable
+ public function import_read_record()
+ {
+ // phpcs:enable
global $conf;
$rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
if ($this->record > $rowcount)
return false;
$array = array();
- $xlsx = new Xlsx();
- $info = $xlsx->listWorksheetinfo($this->file);
- $countcolumns = $info[0]['totalColumns'];
- for ($col = 1; $col <= $countcolumns; $col++) {
+ $colcount = PHPExcel_Cell::columnIndexFromString($this->workbook->getActiveSheet()->getHighestDataColumn(0));
+ for ($col = 0; $col < $colcount; $col++) {
$val = $this->workbook->getActiveSheet()->getCellByColumnAndRow($col, $this->record)->getValue();
$array[$col]['val'] = $val;
$array[$col]['type'] = (dol_strlen($val) ? 1 : -1); // If empty we consider it null
@@ -323,22 +319,22 @@
return $array;
}
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Close file handle
*
* @return integer
*/
- public function import_close_file()
- {
- // phpcs:enable
+ public function import_close_file()
+ {
+ // phpcs:enable
$this->workbook->disconnectWorksheets();
unset($this->workbook);
}
- // What is this doing here ? it is common to all imports, is should be in the parent class
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // What is this doing here ? it is common to all imports, is should be in the parent class
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
/**
* Insert a record into database
*
@@ -350,11 +346,11 @@
* @param array $updatekeys Array of keys to use to try to do an update first before insert. This field are defined into the module descriptor.
* @return int <0 if KO, >0 if OK
*/
- public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
- {
- // phpcs:enable
+ public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
+ {
+ // phpcs:enable
global $langs, $conf, $user;
- global $thirdparty_static; // Specific to thirdparty import
+ global $thirdparty_static; // Specific to thirdparty import
global $tablewithentity_cache; // Cache to avoid to call desc at each rows on tables
$error = 0;
@@ -372,17 +368,21 @@
//var_dump($sort_array_match_file_to_database);
- if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[1]['val']))) {
+ if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0]['val'])))
+ {
//print 'W';
$this->warnings[$warning]['lib'] = $langs->trans('EmptyLine');
$this->warnings[$warning]['type'] = 'EMPTY';
$warning++;
- } else {
+ }
+ else
+ {
$last_insert_id_array = array(); // store the last inserted auto_increment id for each table, so that dependent tables can be inserted with the appropriate id (eg: extrafields fk_object will be set with the last inserted object's id)
$updatedone = false;
$insertdone = false;
// For each table to insert, me make a separate insert
- foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
+ foreach ($objimport->array_import_tables[0] as $alias => $tablename)
+ {
// Build sql request
$sql = '';
$listfields = array();
@@ -393,78 +393,93 @@
// Define $tablewithentity_cache[$tablename] if not already defined
if (!isset($tablewithentity_cache[$tablename])) // keep this test with "isset"
{
- dol_syslog("Check if table " . $tablename . " has an entity field");
+ dol_syslog("Check if table ".$tablename." has an entity field");
$resql = $this->db->DDLDescTable($tablename, 'entity');
- if ($resql) {
+ if ($resql)
+ {
$obj = $this->db->fetch_object($resql);
if ($obj) $tablewithentity_cache[$tablename] = 1; // table contains entity field
else $tablewithentity_cache[$tablename] = 0; // table does not contains entity field
- } else dol_print_error($this->db);
- } else {
+ }
+ else dol_print_error($this->db);
+ }
+ else
+ {
//dol_syslog("Table ".$tablename." check for entity into cache is ".$tablewithentity_cache[$tablename]);
}
- // array of fields to column index
- $arrayfield = array();
- foreach ($sort_array_match_file_to_database as $key => $val) {
- $arrayfield[$val] = ($key - 1);
- }
+ // array of fields to column index
+ $arrayfield = array();
+ foreach ($sort_array_match_file_to_database as $key => $val) {
+ $arrayfield[$val] = ($key - 1);
+ }
// Loop on each fields in the match array: $key = 1..n, $val=alias of field (s.nom)
- foreach ($sort_array_match_file_to_database as $key => $val) {
- $fieldalias = preg_replace('/\..*$/i', '', $val);
- $fieldname = preg_replace('/^.*\./i', '', $val);
-
- if ($alias != $fieldalias) continue; // Not a field of current table
-
- if ($key <= $maxfields) {
+ foreach ($sort_array_match_file_to_database as $key => $val)
+ {
+ $fieldalias = preg_replace('/\..*$/i', '', $val);
+ $fieldname = preg_replace('/^.*\./i', '', $val);
+
+ if ($alias != $fieldalias) continue; // Not a field of current table
+
+ if ($key <= $maxfields)
+ {
// Set $newval with value to insert and set $listvalues with sql request part for insert
$newval = '';
- if ($arrayrecord[($key)]['type'] > 0) $newval = $arrayrecord[($key)]['val']; // If type of field into input file is not empty string (so defined into input file), we get value
+ if ($arrayrecord[($key - 1)]['type'] > 0) $newval = $arrayrecord[($key - 1)]['val']; // If type of field into input file is not empty string (so defined into input file), we get value
// Make some tests on $newval
// Is it a required field ?
- if (preg_match('/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval == '')) {
+ if (preg_match('/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval == ''))
+ {
$this->errors[$error]['lib'] = $langs->trans('ErrorMissingMandatoryValue', $key);
$this->errors[$error]['type'] = 'NOTNULL';
$errorforthistable++;
$error++;
}
// Test format only if field is not a missing mandatory field (field may be a value or empty but not mandatory)
- else {
- // We convert field if required
- if (!empty($objimport->array_import_convertvalue[0][$val])) {
- //print 'Must convert '.$newval.' with rule '.join(',',$objimport->array_import_convertvalue[0][$val]).'. ';
- if (
- $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeid'
- || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromref'
- || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel'
- ) {
- // New val can be an id or ref. If it start with id: it is forced to id, if it start with ref: it is forced to ref. It not, we try to guess.
- $isidorref = 'id';
- if (!is_numeric($newval) && $newval != '' && !preg_match('/^id:/i', $newval)) $isidorref = 'ref';
- $newval = preg_replace('/^(id|ref):/i', '', $newval); // Remove id: or ref: that was used to force if field is id or ref
- //print 'Val is now '.$newval.' and is type '.$isidorref."
\n";
-
- if ($isidorref == 'ref') { // If value into input import file is a ref, we apply the function defined into descriptor
- $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
- $class = $objimport->array_import_convertvalue[0][$val]['class'];
- $method = $objimport->array_import_convertvalue[0][$val]['method'];
- if ($this->cacheconvert[$file . '_' . $class . '_' . $method . '_'][$newval] != '') {
- $newval = $this->cacheconvert[$file . '_' . $class . '_' . $method . '_'][$newval];
- } else {
- $resultload = dol_include_once($file);
- if (empty($resultload)) {
- dol_print_error('', 'Error trying to call file=' . $file . ', class=' . $class . ', method=' . $method);
- break;
- }
- $classinstance = new $class($this->db);
- // Try the fetch from code or ref
- $param_array = array('', $newval);
- if ($class == 'AccountingAccount') {
- //var_dump($arrayrecord[0]['val']);
- /*include_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancysystem.class.php';
+ else
+ {
+ // We convert field if required
+ if (!empty($objimport->array_import_convertvalue[0][$val]))
+ {
+ //print 'Must convert '.$newval.' with rule '.join(',',$objimport->array_import_convertvalue[0][$val]).'. ';
+ if ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeid'
+ || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromref'
+ || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel'
+ )
+ {
+ // New val can be an id or ref. If it start with id: it is forced to id, if it start with ref: it is forced to ref. It not, we try to guess.
+ $isidorref = 'id';
+ if (!is_numeric($newval) && $newval != '' && !preg_match('/^id:/i', $newval)) $isidorref = 'ref';
+ $newval = preg_replace('/^(id|ref):/i', '', $newval); // Remove id: or ref: that was used to force if field is id or ref
+ //print 'Val is now '.$newval.' and is type '.$isidorref."
\n";
+
+ if ($isidorref == 'ref') // If value into input import file is a ref, we apply the function defined into descriptor
+ {
+ $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
+ $class = $objimport->array_import_convertvalue[0][$val]['class'];
+ $method = $objimport->array_import_convertvalue[0][$val]['method'];
+ if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] != '')
+ {
+ $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval];
+ }
+ else
+ {
+ $resultload = dol_include_once($file);
+ if (empty($resultload))
+ {
+ dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method);
+ break;
+ }
+ $classinstance = new $class($this->db);
+ // Try the fetch from code or ref
+ $param_array = array('', $newval);
+ if ($class == 'AccountingAccount')
+ {
+ //var_dump($arrayrecord[0]['val']);
+ /*include_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancysystem.class.php';
$tmpchartofaccount = new AccountancySystem($this->db);
$tmpchartofaccount->fetch($conf->global->CHARTOFACCOUNTS);
var_dump($tmpchartofaccount->ref.' - '.$arrayrecord[0]['val']);
@@ -475,211 +490,251 @@
$errorforthistable++;
$error++;
}*/
- $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart.
- }
- call_user_func_array(array($classinstance, $method), $param_array);
- // If not found, try the fetch from label
- if (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel') {
- $param_array = array('', '', $newval);
- call_user_func_array(array($classinstance, $method), $param_array);
- }
- $this->cacheconvert[$file . '_' . $class . '_' . $method . '_'][$newval] = $classinstance->id;
- //print 'We have made a '.$class.'->'.$method.' to get id from code '.$newval.'. ';
- if ($classinstance->id != '') // id may be 0, it is a found value
- {
- $newval = $classinstance->id;
- } else {
- if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
- elseif (!empty($objimport->array_import_convertvalue[0][$val]['element'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldRefNotIn', $key, $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['element']));
- else $this->errors[$error]['lib'] = 'ErrorBadDefinitionOfImportProfile';
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
- $error++;
- }
- }
- }
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeandlabel') {
- $isidorref = 'id';
- if (!is_numeric($newval) && $newval != '' && !preg_match('/^id:/i', $newval)) $isidorref = 'ref';
- $newval = preg_replace('/^(id|ref):/i', '', $newval);
-
- if ($isidorref == 'ref') {
- $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
- $class = $objimport->array_import_convertvalue[0][$val]['class'];
- $method = $objimport->array_import_convertvalue[0][$val]['method'];
- $codefromfield = $objimport->array_import_convertvalue[0][$val]['codefromfield'];
- $code = $arrayrecord[$arrayfield[$codefromfield]]['val'];
- if ($this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $code][$newval] != '') {
- $newval = $this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $code][$newval];
- } else {
- $resultload = dol_include_once($file);
- if (empty($resultload)) {
- dol_print_error('', 'Error trying to call file=' . $file . ', class=' . $class . ', method=' . $method . ', code=' . $code);
- break;
- }
- $classinstance = new $class($this->db);
- // Try the fetch from code and ref
- $param_array = array('', $newval, $code);
- call_user_func_array(array($classinstance, $method), $param_array);
- $this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $code][$newval] = $classinstance->id;
- if ($classinstance->id > 0) // we found record
- {
- $newval = $classinstance->id;
- } else {
- if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
- else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
- $error++;
- }
- }
- }
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'zeroifnull') {
- if (empty($newval)) $newval = '0';
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchscalefromcodeunits') {
- $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
- $class = $objimport->array_import_convertvalue[0][$val]['class'];
- $method = $objimport->array_import_convertvalue[0][$val]['method'];
- $units = $objimport->array_import_convertvalue[0][$val]['units'];
- if ($this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $units][$newval] != '') {
- $newval = $this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $units][$newval];
- } else {
- $resultload = dol_include_once($file);
- if (empty($resultload)) {
- dol_print_error('', 'Error trying to call file=' . $file . ', class=' . $class . ', method=' . $method . ', units=' . $units);
- break;
- }
- $classinstance = new $class($this->db);
- // Try the fetch from code or ref
- call_user_func_array(array($classinstance, $method), array('', '', $newval, $units));
- $scaleorid = (($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
- $this->cacheconvert[$file . '_' . $class . '_' . $method . '_' . $units][$newval] = $scaleorid;
- //print 'We have made a '.$class.'->'.$method." to get a value from key '".$newval."' and we got '".$scaleorid."'.";exit;
- if ($classinstance->id > 0) // we found record
- {
- $newval = $scaleorid ? $scaleorid : 0;
- } else {
- if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
- else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
- $error++;
- }
- }
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomercodeifauto') {
- if (strtolower($newval) == 'auto') {
- $this->thirpartyobject->get_codeclient(0, 0);
- $newval = $this->thirpartyobject->code_client;
- //print 'code_client='.$newval;
- }
- if (empty($newval)) $arrayrecord[($key)]['type'] = -1; // If we get empty value, we will use "null"
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsuppliercodeifauto') {
- if (strtolower($newval) == 'auto') {
- $newval = $this->thirpartyobject->get_codefournisseur(0, 1);
- $newval = $this->thirpartyobject->code_fournisseur;
- //print 'code_fournisseur='.$newval;
- }
- if (empty($newval)) $arrayrecord[($key)]['type'] = -1; // If we get empty value, we will use "null"
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomeraccountancycodeifauto') {
- if (strtolower($newval) == 'auto') {
- $this->thirpartyobject->get_codecompta('customer');
- $newval = $this->thirpartyobject->code_compta;
- //print 'code_compta='.$newval;
- }
- if (empty($newval)) $arrayrecord[($key)]['type'] = -1; // If we get empty value, we will use "null"
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsupplieraccountancycodeifauto') {
- if (strtolower($newval) == 'auto') {
- $this->thirpartyobject->get_codecompta('supplier');
- $newval = $this->thirpartyobject->code_compta_fournisseur;
- if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
- //print 'code_compta_fournisseur='.$newval;
- }
- if (empty($newval)) $arrayrecord[($key)]['type'] = -1; // If we get empty value, we will use "null"
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getrefifauto') {
- $defaultref = '';
- // TODO provide the $modTask (module of generation of ref) as parameter of import_insert function
- $obj = empty($conf->global->PROJECT_TASK_ADDON) ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON;
- if (!empty($conf->global->PROJECT_TASK_ADDON) && is_readable(DOL_DOCUMENT_ROOT . "/core/modules/project/task/" . $conf->global->PROJECT_TASK_ADDON . ".php")) {
- require_once DOL_DOCUMENT_ROOT . "/core/modules/project/task/" . $conf->global->PROJECT_TASK_ADDON . '.php';
- $modTask = new $obj;
- $defaultref = $modTask->getNextValue(null, null);
- }
- if (is_numeric($defaultref) && $defaultref <= 0) $defaultref = '';
- $newval = $defaultref;
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'compute') {
- $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
- $class = $objimport->array_import_convertvalue[0][$val]['class'];
- $method = $objimport->array_import_convertvalue[0][$val]['method'];
- $resultload = dol_include_once($file);
- if (empty($resultload)) {
- dol_print_error('', 'Error trying to call file=' . $file . ', class=' . $class . ', method=' . $method);
- break;
- }
- $classinstance = new $class($this->db);
- $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord));
- if ($res < 0) {
- if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
- else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
- $error++;
- } else {
- $newval = $arrayrecord[($key)]['val']; //We get new value computed.
- }
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'numeric') {
- $newval = price2num($newval);
- }
-
- //print 'Val to use as insert is '.$newval.'
';
- }
-
- // Test regexp
- if (!empty($objimport->array_import_regex[0][$val]) && ($newval != '')) {
+ $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart.
+ }
+ call_user_func_array(array($classinstance, $method), $param_array);
+ // If not found, try the fetch from label
+ if (!($classinstance->id != '') && $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeorlabel')
+ {
+ $param_array = array('', '', $newval);
+ call_user_func_array(array($classinstance, $method), $param_array);
+ }
+ $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'][$newval] = $classinstance->id;
+ //print 'We have made a '.$class.'->'.$method.' to get id from code '.$newval.'. ';
+ if ($classinstance->id != '') // id may be 0, it is a found value
+ {
+ $newval = $classinstance->id;
+ }
+ else
+ {
+ if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
+ elseif (!empty($objimport->array_import_convertvalue[0][$val]['element'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldRefNotIn', $key, $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['element']));
+ else $this->errors[$error]['lib'] = 'ErrorBadDefinitionOfImportProfile';
+ $this->errors[$error]['type'] = 'FOREIGNKEY';
+ $errorforthistable++;
+ $error++;
+ }
+ }
+ }
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeandlabel')
+ {
+ $isidorref = 'id';
+ if (!is_numeric($newval) && $newval != '' && !preg_match('/^id:/i', $newval)) $isidorref = 'ref';
+ $newval = preg_replace('/^(id|ref):/i', '', $newval);
+
+ if ($isidorref == 'ref') {
+ $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
+ $class = $objimport->array_import_convertvalue[0][$val]['class'];
+ $method = $objimport->array_import_convertvalue[0][$val]['method'];
+ $codefromfield = $objimport->array_import_convertvalue[0][$val]['codefromfield'];
+ $code = $arrayrecord[$arrayfield[$codefromfield]]['val'];
+ if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval] != '') {
+ $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval];
+ } else {
+ $resultload = dol_include_once($file);
+ if (empty($resultload)) {
+ dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method.', code='.$code);
+ break;
+ }
+ $classinstance = new $class($this->db);
+ // Try the fetch from code and ref
+ $param_array = array('', $newval, $code);
+ call_user_func_array(array($classinstance, $method), $param_array);
+ $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$code][$newval] = $classinstance->id;
+ if ($classinstance->id > 0) // we found record
+ {
+ $newval = $classinstance->id;
+ } else {
+ if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
+ else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
+ $this->errors[$error]['type'] = 'FOREIGNKEY';
+ $errorforthistable++;
+ $error++;
+ }
+ }
+ }
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'zeroifnull')
+ {
+ if (empty($newval)) $newval = '0';
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeunits' || $objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchscalefromcodeunits')
+ {
+ $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
+ $class = $objimport->array_import_convertvalue[0][$val]['class'];
+ $method = $objimport->array_import_convertvalue[0][$val]['method'];
+ $units = $objimport->array_import_convertvalue[0][$val]['units'];
+ if ($this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval] != '')
+ {
+ $newval = $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval];
+ }
+ else
+ {
+ $resultload = dol_include_once($file);
+ if (empty($resultload))
+ {
+ dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method.', units='.$units);
+ break;
+ }
+ $classinstance = new $class($this->db);
+ // Try the fetch from code or ref
+ call_user_func_array(array($classinstance, $method), array('', '', $newval, $units));
+ $scaleorid = (($objimport->array_import_convertvalue[0][$val]['rule'] == 'fetchidfromcodeunits') ? $classinstance->id : $classinstance->scale);
+ $this->cacheconvert[$file.'_'.$class.'_'.$method.'_'.$units][$newval] = $scaleorid;
+ //print 'We have made a '.$class.'->'.$method." to get a value from key '".$newval."' and we got '".$scaleorid."'.";exit;
+ if ($classinstance->id > 0) // we found record
+ {
+ $newval = $scaleorid ? $scaleorid : 0;
+ }
+ else
+ {
+ if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'scale', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
+ else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
+ $this->errors[$error]['type'] = 'FOREIGNKEY';
+ $errorforthistable++;
+ $error++;
+ }
+ }
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomercodeifauto')
+ {
+ if (strtolower($newval) == 'auto')
+ {
+ $this->thirpartyobject->get_codeclient(0, 0);
+ $newval = $this->thirpartyobject->code_client;
+ //print 'code_client='.$newval;
+ }
+ if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsuppliercodeifauto')
+ {
+ if (strtolower($newval) == 'auto')
+ {
+ $newval = $this->thirpartyobject->get_codefournisseur(0, 1);
+ $newval = $this->thirpartyobject->code_fournisseur;
+ //print 'code_fournisseur='.$newval;
+ }
+ if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getcustomeraccountancycodeifauto')
+ {
+ if (strtolower($newval) == 'auto')
+ {
+ $this->thirpartyobject->get_codecompta('customer');
+ $newval = $this->thirpartyobject->code_compta;
+ //print 'code_compta='.$newval;
+ }
+ if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getsupplieraccountancycodeifauto')
+ {
+ if (strtolower($newval) == 'auto')
+ {
+ $this->thirpartyobject->get_codecompta('supplier');
+ $newval = $this->thirpartyobject->code_compta_fournisseur;
+ if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
+ //print 'code_compta_fournisseur='.$newval;
+ }
+ if (empty($newval)) $arrayrecord[($key - 1)]['type'] = -1; // If we get empty value, we will use "null"
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'getrefifauto')
+ {
+ $defaultref = '';
+ // TODO provide the $modTask (module of generation of ref) as parameter of import_insert function
+ $obj = empty($conf->global->PROJECT_TASK_ADDON) ? 'mod_task_simple' : $conf->global->PROJECT_TASK_ADDON;
+ if (!empty($conf->global->PROJECT_TASK_ADDON) && is_readable(DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.".php"))
+ {
+ require_once DOL_DOCUMENT_ROOT."/core/modules/project/task/".$conf->global->PROJECT_TASK_ADDON.'.php';
+ $modTask = new $obj;
+ $defaultref = $modTask->getNextValue(null, null);
+ }
+ if (is_numeric($defaultref) && $defaultref <= 0) $defaultref = '';
+ $newval = $defaultref;
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'compute')
+ {
+ $file = (empty($objimport->array_import_convertvalue[0][$val]['classfile']) ? $objimport->array_import_convertvalue[0][$val]['file'] : $objimport->array_import_convertvalue[0][$val]['classfile']);
+ $class = $objimport->array_import_convertvalue[0][$val]['class'];
+ $method = $objimport->array_import_convertvalue[0][$val]['method'];
+ $resultload = dol_include_once($file);
+ if (empty($resultload))
+ {
+ dol_print_error('', 'Error trying to call file='.$file.', class='.$class.', method='.$method);
+ break;
+ }
+ $classinstance = new $class($this->db);
+ $res = call_user_func_array(array($classinstance, $method), array(&$arrayrecord));
+ if ($res < 0) {
+ if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', $key, $newval, 'code', $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['dict']));
+ else $this->errors[$error]['lib'] = 'ErrorFieldValueNotIn';
+ $this->errors[$error]['type'] = 'FOREIGNKEY';
+ $errorforthistable++;
+ $error++;
+ }
+ }
+ elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'numeric')
+ {
+ $newval = price2num($newval);
+ }
+
+ //print 'Val to use as insert is '.$newval.'
';
+ }
+
+ // Test regexp
+ if (!empty($objimport->array_import_regex[0][$val]) && ($newval != ''))
+ {
// If test is "Must exist in a field@table or field@table:..."
- $reg = array();
- if (preg_match('/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg)) {
+ if (preg_match('/^(.+)@([^:]+)(:.+)?$/', $objimport->array_import_regex[0][$val], $reg))
+ {
$field = $reg[1];
$table = $reg[2];
- $filter = !empty($reg[3]) ? substr($reg[3], 1) : '';
-
- $cachekey = $field . '@' . $table;
- if (!empty($filter)) $cachekey .= ':' . $filter;
+ $filter = !empty($reg[3]) ?substr($reg[3], 1) : '';
+
+ $cachekey = $field.'@'.$table;
+ if (!empty($filter)) $cachekey .= ':'.$filter;
// Load content of field@table into cache array
if (!is_array($this->cachefieldtable[$cachekey])) // If content of field@table not already loaded into cache
{
- $sql = "SELECT " . $field . " as aliasfield FROM " . $table;
- if (!empty($filter)) {
- $sql .= ' WHERE ' . $filter;
+ $sql = "SELECT ".$field." as aliasfield FROM ".$table;
+ if (!empty($filter))
+ {
+ $sql .= ' WHERE '.$filter;
}
$resql = $this->db->query($sql);
- if ($resql) {
+ if ($resql)
+ {
$num = $this->db->num_rows($resql);
$i = 0;
- while ($i < $num) {
+ while ($i < $num)
+ {
$obj = $this->db->fetch_object($resql);
if ($obj) $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
$i++;
}
- } else {
+ }
+ else
+ {
dol_print_error($this->db);
}
}
// Now we check cache is not empty (should not) and key is into cache
- if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
+ if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey]))
+ {
$tableforerror = $table;
- if (!empty($filter)) $tableforerror .= ':' . $filter;
+ if (!empty($filter)) $tableforerror .= ':'.$filter;
$this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorFieldValueNotIn', $key, $newval, $field, $tableforerror);
$this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
+ $errorforthistable++;
$error++;
}
}
// If test is just a static regex
- elseif (!preg_match('/' . $objimport->array_import_regex[0][$val] . '/i', $newval)) {
- //if ($key == 19) print "xxx".$newval."zzz".$objimport->array_import_regex[0][$val]."
";
+ elseif (!preg_match('/'.$objimport->array_import_regex[0][$val].'/i', $newval)) {
+ //if ($key == 19) print "xxx".$newval."zzz".$objimport->array_import_regex[0][$val]."
";
$this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorWrongValueForField', $key, $newval, $objimport->array_import_regex[0][$val]);
$this->errors[$error]['type'] = 'REGEX';
$errorforthistable++;
@@ -695,53 +750,64 @@
$listfields[] = $fieldname;
// Note: arrayrecord (and 'type') is filled with ->import_read_record called by import.php page before calling import_insert
- if (empty($newval) && $arrayrecord[($key)]['type'] < 0) $listvalues[] = ($newval == '0' ? $newval : "null");
- elseif (empty($newval) && $arrayrecord[($key)]['type'] == 0) $listvalues[] = "''";
- else $listvalues[] = "'" . $this->db->escape($newval) . "'";
+ if (empty($newval) && $arrayrecord[($key - 1)]['type'] < 0) $listvalues[] = ($newval == '0' ? $newval : "null");
+ elseif (empty($newval) && $arrayrecord[($key - 1)]['type'] == 0) $listvalues[] = "''";
+ else $listvalues[] = "'".$this->db->escape($newval)."'";
}
$i++;
}
// We add hidden fields (but only if there is at least one field to add into table)
- if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0])) {
- // Loop on each hidden fields to add them into listfields/listvalues
- foreach ($objimport->array_import_fieldshidden[0] as $key => $val) {
- if (!preg_match('/^' . preg_quote($alias, '/') . '\./', $key)) continue; // Not a field of current table
- if ($val == 'user->id') {
- $listfields[] = preg_replace('/^' . preg_quote($alias, '/') . '\./', '', $key);
- $listvalues[] = $user->id;
- } elseif (preg_match('/^lastrowid-/', $val)) {
- $tmp = explode('-', $val);
- $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
- $keyfield = preg_replace('/^' . preg_quote($alias, '/') . '\./', '', $key);
- $listfields[] = $keyfield;
- $listvalues[] = $lastinsertid;
- //print $key."-".$val."-".$listfields."-".$listvalues."
";exit;
- } elseif (preg_match('/^const-/', $val)) {
- $tmp = explode('-', $val, 2);
- $listfields[] = preg_replace('/^' . preg_quote($alias, '/') . '\./', '', $key);
- $listvalues[] = "'" . $tmp[1] . "'";
- } else {
- $this->errors[$error]['lib'] = 'Bad value of profile setup ' . $val . ' for array_import_fieldshidden';
- $this->errors[$error]['type'] = 'Import profile setup';
- $error++;
- }
- }
+ if (!empty($listfields) && is_array($objimport->array_import_fieldshidden[0]))
+ {
+ // Loop on each hidden fields to add them into listfields/listvalues
+ foreach ($objimport->array_import_fieldshidden[0] as $key => $val)
+ {
+ if (!preg_match('/^'.preg_quote($alias, '/').'\./', $key)) continue; // Not a field of current table
+ if ($val == 'user->id')
+ {
+ $listfields[] = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
+ $listvalues[] = $user->id;
+ }
+ elseif (preg_match('/^lastrowid-/', $val))
+ {
+ $tmp = explode('-', $val);
+ $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
+ $keyfield = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
+ $listfields[] = $keyfield;
+ $listvalues[] = $lastinsertid;
+ //print $key."-".$val."-".$listfields."-".$listvalues."
";exit;
+ }
+ elseif (preg_match('/^const-/', $val))
+ {
+ $tmp = explode('-', $val, 2);
+ $listfields[] = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $key);
+ $listvalues[] = "'".$tmp[1]."'";
+ }
+ else
+ {
+ $this->errors[$error]['lib'] = 'Bad value of profile setup '.$val.' for array_import_fieldshidden';
+ $this->errors[$error]['type'] = 'Import profile setup';
+ $error++;
+ }
+ }
}
//print 'listfields='.$listfields.'
listvalues='.$listvalues.'
';
// If no error for this $alias/$tablename, we have a complete $listfields and $listvalues that are defined
// so we can try to make the insert or update now.
- if (!$errorforthistable) {
+ if (!$errorforthistable)
+ {
//print "$alias/$tablename/$listfields/$listvalues
";
- if (!empty($listfields)) {
+ if (!empty($listfields))
+ {
$updatedone = false;
$insertdone = false;
if (!empty($updatekeys)) {
// We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
if (empty($lastinsertid)) { // No insert done yet for a parent table
- $sqlSelect = 'SELECT rowid FROM ' . $tablename;
+ $sqlSelect = 'SELECT rowid FROM '.$tablename;
$data = array_combine($listfields, $listvalues);
$where = array();
@@ -749,10 +815,10 @@
foreach ($updatekeys as $key) {
$col = $objimport->array_import_updatekeys[0][$key];
$key = preg_replace('/^.*\./i', '', $key);
- $where[] = $key . ' = ' . $data[$key];
- $filters[] = $col . ' = ' . $data[$key];
- }
- $sqlSelect .= ' WHERE ' . implode(' AND ', $where);
+ $where[] = $key.' = '.$data[$key];
+ $filters[] = $col.' = '.$data[$key];
+ }
+ $sqlSelect .= ' WHERE '.implode(' AND ', $where);
$resql = $this->db->query($sqlSelect);
if ($resql) {
@@ -767,7 +833,9 @@
} else {
// No record found with filters, insert will be tried below
}
- } else {
+ }
+ else
+ {
//print 'E';
$this->errors[$error]['lib'] = $this->db->lasterror();
$this->errors[$error]['type'] = 'SQL';
@@ -779,10 +847,10 @@
// a direct insert into subtable extrafields, but when me wake an update, the insertid is defined and the child record
// may already exists. So we rescan the extrafield table to know if record exists or not for the rowid.
// Note: For extrafield tablename, we have in importfieldshidden_array an enty 'extra.fk_object'=>'lastrowid-tableparent' so $keyfield is 'fk_object'
- $sqlSelect = 'SELECT rowid FROM ' . $tablename;
+ $sqlSelect = 'SELECT rowid FROM '.$tablename;
if (empty($keyfield)) $keyfield = 'rowid';
- $sqlSelect .= ' WHERE ' . $keyfield . ' = ' . $lastinsertid;
+ $sqlSelect .= ' WHERE '.$keyfield.' = '.$lastinsertid;
$resql = $this->db->query($sqlSelect);
if ($resql) {
@@ -794,7 +862,9 @@
// force $lastinsertid to 0 so we INSERT below.
$lastinsertid = 0;
}
- } else {
+ }
+ else
+ {
//print 'E';
$this->errors[$error]['lib'] = $this->db->lasterror();
$this->errors[$error]['type'] = 'SQL';
@@ -804,26 +874,28 @@
if (!empty($lastinsertid)) {
// Build SQL UPDATE request
- $sqlstart = 'UPDATE ' . $tablename;
+ $sqlstart = 'UPDATE '.$tablename;
$data = array_combine($listfields, $listvalues);
$set = array();
foreach ($data as $key => $val) {
- $set[] = $key . ' = ' . $val;
- }
- $sqlstart .= ' SET ' . implode(', ', $set);
+ $set[] = $key.' = '.$val;
+ }
+ $sqlstart .= ' SET '.implode(', ', $set);
if (empty($keyfield)) $keyfield = 'rowid';
- $sqlend = ' WHERE ' . $keyfield . ' = ' . $lastinsertid;
-
- $sql = $sqlstart . $sqlend;
+ $sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid;
+
+ $sql = $sqlstart.$sqlend;
// Run update request
$resql = $this->db->query($sql);
if ($resql) {
// No error, update has been done. $this->db->db->affected_rows can be 0 if data hasn't changed
$updatedone = true;
- } else {
+ }
+ else
+ {
//print 'E';
$this->errors[$error]['lib'] = $this->db->lasterror();
$this->errors[$error]['type'] = 'SQL';
@@ -835,26 +907,30 @@
// Update not done, we do insert
if (!$error && !$updatedone) {
// Build SQL INSERT request
- $sqlstart = 'INSERT INTO ' . $tablename . '(' . implode(', ', $listfields) . ', import_key';
- $sqlend = ') VALUES(' . implode(', ', $listvalues) . ", '" . $this->db->escape($importid) . "'";
+ $sqlstart = 'INSERT INTO '.$tablename.'('.implode(', ', $listfields).', import_key';
+ $sqlend = ') VALUES('.implode(', ', $listvalues).", '".$importid."'";
if (!empty($tablewithentity_cache[$tablename])) {
$sqlstart .= ', entity';
- $sqlend .= ', ' . $conf->entity;
+ $sqlend .= ', '.$conf->entity;
}
if (!empty($objimport->array_import_tables_creator[0][$alias])) {
- $sqlstart .= ', ' . $objimport->array_import_tables_creator[0][$alias];
- $sqlend .= ', ' . $user->id;
+ $sqlstart .= ', '.$objimport->array_import_tables_creator[0][$alias];
+ $sqlend .= ', '.$user->id;
}
- $sql = $sqlstart . $sqlend . ')';
- //dol_syslog("import_xlsx.modules", LOG_DEBUG);
+ $sql = $sqlstart.$sqlend.')';
+ dol_syslog("import_xlsx.modules", LOG_DEBUG);
// Run insert request
- if ($sql) {
+ if ($sql)
+ {
$resql = $this->db->query($sql);
- if ($resql) {
- $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that child tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr).
- $insertdone = true;
- } else {
+ if ($resql)
+ {
+ $last_insert_id_array[$tablename] = $this->db->last_insert_id($tablename); // store the last inserted auto_increment id for each table, so that child tables can be inserted with the appropriate id. This must be done just after the INSERT request, else we risk losing the id (because another sql query will be issued somewhere in Dolibarr).
+ $insertdone = true;
+ }
+ else
+ {
//print 'E';
$this->errors[$error]['lib'] = $this->db->lasterror();
$this->errors[$error]['type'] = 'SQL';
@@ -869,7 +945,7 @@
}*/
}
- if ($error) break;
+ if ($error) break;
}
if ($updatedone) $this->nbupdate++;
--- /tmp/dsg/dolibarr/htdocs/core/modules/import/github_modules_import.php
+++ /tmp/dsg/dolibarr/htdocs/core/modules/import/client_modules_import.php
@@ -30,34 +30,34 @@
*/
class ModeleImports
{
- /**
- * @var DoliDB Database handler.
- */
- public $db;
-
- public $datatoimport;
-
- /**
+ /**
+ * @var DoliDB Database handler.
+ */
+ public $db;
+
+ public $datatoimport;
+
+ /**
* @var string Error code (or message)
*/
public $error = '';
- /**
+ /**
* @var int id of driver
*/
public $id;
- /**
- * @var string label
- */
- public $label;
+ /**
+ * @var string label
+ */
+ public $label;
public $extension; // Extension of files imported by driver
/**
- * Dolibarr version of driver
- * @var string
- */
+ * Dolibarr version of driver
+ * @var string
+ */
public $version = 'dolibarr';
public $label_lib; // Label of external lib used by driver
@@ -77,9 +77,9 @@
/**
- * Constructor
- */
- public function __construct()
+ * Constructor
+ */
+ public function __construct()
{
}
@@ -89,9 +89,9 @@
*
* @return string Id
*/
- public function getDriverId()
- {
- return $this->id;
+ public function getDriverId()
+ {
+ return $this->id;
}
/**
@@ -99,9 +99,9 @@
*
* @return string Label
*/
- public function getDriverLabel()
- {
- return $this->label;
+ public function getDriverLabel()
+ {
+ return $this->label;
}
/**
@@ -109,9 +109,9 @@
*
* @return string Description
*/
- public function getDriverDesc()
- {
- return $this->desc;
+ public function getDriverDesc()
+ {
+ return $this->desc;
}
/**
@@ -119,9 +119,9 @@
*
* @return string Driver suffix
*/
- public function getDriverExtension()
- {
- return $this->extension;
+ public function getDriverExtension()
+ {
+ return $this->extension;
}
/**
@@ -129,9 +129,9 @@
*
* @return string Driver version
*/
- public function getDriverVersion()
- {
- return $this->version;
+ public function getDriverVersion()
+ {
+ return $this->version;
}
/**
@@ -139,9 +139,9 @@
*
* @return string Label of external lib
*/
- public function getLibLabel()
- {
- return $this->label_lib;
+ public function getLibLabel()
+ {
+ return $this->label_lib;
}
/**
@@ -149,23 +149,23 @@
*
* @return string Version of external lib
*/
- public function getLibVersion()
- {
- return $this->version_lib;
- }
-
-
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Charge en memoire et renvoie la liste des modeles actifs
- *
- * @param DoliDB $db Database handler
- * @param integer $maxfilenamelength Max length of value to show
- * @return array List of templates
- */
- public function liste_modeles($db, $maxfilenamelength = 0)
- {
- // phpcs:enable
+ public function getLibVersion()
+ {
+ return $this->version_lib;
+ }
+
+
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ /**
+ * Charge en memoire et renvoie la liste des modeles actifs
+ *
+ * @param DoliDB $db Database handler
+ * @param integer $maxfilenamelength Max length of value to show
+ * @return array List of templates
+ */
+ public function liste_modeles($db, $maxfilenamelength = 0)
+ {
+ // phpcs:enable
dol_syslog(get_class($this)."::liste_modeles");
$dir = DOL_DOCUMENT_ROOT."/core/modules/import/";
@@ -173,35 +173,35 @@
// Recherche des fichiers drivers imports disponibles
$i = 0;
- if (is_resource($handle))
- {
- while (($file = readdir($handle)) !== false)
- {
- if (preg_match("/^import_(.*)\.modules\.php/i", $file, $reg))
- {
- $moduleid = $reg[1];
-
- // Loading Class
- $file = $dir."/import_".$moduleid.".modules.php";
- $classname = "Import".ucfirst($moduleid);
-
- require_once $file;
- $module = new $classname($db, '');
-
- // Picto
- $this->picto[$module->id] = $module->picto;
- // Driver properties
- $this->driverlabel[$module->id] = $module->getDriverLabel('');
- $this->driverdesc[$module->id] = $module->getDriverDesc('');
- $this->driverversion[$module->id] = $module->getDriverVersion('');
- // If use an external lib
- $this->liblabel[$module->id] = $module->getLibLabel('');
- $this->libversion[$module->id] = $module->getLibVersion('');
-
- $i++;
- }
- }
- }
+ if (is_resource($handle))
+ {
+ while (($file = readdir($handle)) !== false)
+ {
+ if (preg_match("/^import_(.*)\.modules\.php/i", $file, $reg))
+ {
+ $moduleid = $reg[1];
+
+ // Loading Class
+ $file = $dir."/import_".$moduleid.".modules.php";
+ $classname = "Import".ucfirst($moduleid);
+
+ require_once $file;
+ $module = new $classname($db, '');
+
+ // Picto
+ $this->picto[$module->id] = $module->picto;
+ // Driver properties
+ $this->driverlabel[$module->id] = $module->getDriverLabel('');
+ $this->driverdesc[$module->id] = $module->getDriverDesc('');
+ $this->driverversion[$module->id] = $module->getDriverVersion('');
+ // If use an external lib
+ $this->liblabel[$module->id] = $module->getLibLabel('');
+ $this->libversion[$module->id] = $module->getLibVersion('');
+
+ $i++;
+ }
+ }
+ }
return array_keys($this->driverlabel);
}
@@ -213,7 +213,7 @@
* @param string $key Key
* @return string
*/
- public function getPictoForKey($key)
+ public function getPictoForKey($key)
{
return $this->picto[$key];
}
@@ -224,7 +224,7 @@
* @param string $key Key
* @return string
*/
- public function getDriverLabelForKey($key)
+ public function getDriverLabelForKey($key)
{
return $this->driverlabel[$key];
}
@@ -235,7 +235,7 @@
* @param string $key Key
* @return string
*/
- public function getDriverDescForKey($key)
+ public function getDriverDescForKey($key)
{
return $this->driverdesc[$key];
}
@@ -246,7 +246,7 @@
* @param string $key Key
* @return string
*/
- public function getDriverVersionForKey($key)
+ public function getDriverVersionForKey($key)
{
return $this->driverversion[$key];
}
@@ -257,7 +257,7 @@
* @param string $key Key
* @return string
*/
- public function getLibLabelForKey($key)
+ public function getLibLabelForKey($key)
{
return $this->liblabel[$key];
}
@@ -268,7 +268,7 @@
* @param string $key Key
* @return string
*/
- public function getLibVersionForKey($key)
+ public function getLibVersionForKey($key)
{
return $this->libversion[$key];
}