--- /tmp/dsg/dolibarr/htdocs/core/modules/import/github_19.0.3_import_csv.modules.php
+++ /tmp/dsg/dolibarr/htdocs/core/modules/import/client_import_csv.modules.php
@@ -36,7 +36,19 @@
- /**
- * @var DoliDB Database handler.
- */
- public $db;
-
- /**
- * @var string Code of driver
+ /**
+ * @var DoliDB Database handler.
+ */
+ public $db;
+
+ public $datatoimport;
+
+ /**
+ * @var string Error code (or message)
+ */
+ public $error = '';
+
+ /**
+ * @var string[] Error codes (or messages)
+ */
+ public $errors = array();
+
+ /**
+ * @var int ID
@@ -47,3 +59,3 @@
- * @var string label
- */
- public $label;
+ * @var string label
+ */
+ public $label;
@@ -54,3 +66,3 @@
- * Dolibarr version of driver
- * @var string
- */
+ * Dolibarr version of driver
+ * @var string
+ */
@@ -76,4 +87,0 @@
-
- public $charset = '';
-
- public $col;
@@ -88 +96 @@
- public function __construct($db, $datatoimport)
+ public function __construct($db, $datatoimport)
@@ -91,2 +98,0 @@
-
- parent::__construct();
@@ -95 +101 @@
- $this->separator = (GETPOST('separator') ? GETPOST('separator') : (!getDolGlobalString('IMPORT_CSV_SEPARATOR_TO_USE') ? ',' : $conf->global->IMPORT_CSV_SEPARATOR_TO_USE));
+ $this->separator = (GETPOST('separator') ?GETPOST('separator') : (empty($conf->global->IMPORT_CSV_SEPARATOR_TO_USE) ? ',' : $conf->global->IMPORT_CSV_SEPARATOR_TO_USE));
@@ -111,3 +117 @@
- if (preg_match('/^societe_/', $datatoimport)) {
- $this->thirdpartyobject = new Societe($this->db);
- }
+ if (preg_match('/^societe_/', $datatoimport)) $this->thirpartyobject = new Societe($this->db);
@@ -117 +121 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -124 +128 @@
- public function write_header_example($outputlangs)
+ public function write_header_example($outputlangs)
@@ -126 +130 @@
- // phpcs:enable
+ // phpcs:enable
@@ -130 +134 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -138 +142 @@
- public function write_title_example($outputlangs, $headerlinefields)
+ public function write_title_example($outputlangs, $headerlinefields)
@@ -140 +144 @@
- // phpcs:enable
+ // phpcs:enable
@@ -145 +149 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -153 +157 @@
- public function write_record_example($outputlangs, $contentlinevalues)
+ public function write_record_example($outputlangs, $contentlinevalues)
@@ -155 +159 @@
- // phpcs:enable
+ // phpcs:enable
@@ -160 +164 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -167 +171 @@
- public function write_footer_example($outputlangs)
+ public function write_footer_example($outputlangs)
@@ -169 +173 @@
- // phpcs:enable
+ // phpcs:enable
@@ -174 +178 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -179,3 +183,3 @@
- * @return int Return integer <0 if KO, >=0 if OK
- */
- public function import_open_file($file)
+ * @return int <0 if KO, >=0 if OK
+ */
+ public function import_open_file($file)
@@ -183 +187 @@
- // phpcs:enable
+ // phpcs:enable
@@ -192 +196,2 @@
- if (!$this->handle) {
+ if (!$this->handle)
+ {
@@ -196 +201,3 @@
- } else {
+ }
+ else
+ {
@@ -204 +211 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -209,3 +216,16 @@
- * @return int Return integer <0 if KO, >=0 if OK
- */
- public function import_get_nb_of_lines($file)
+ * @return int <0 if KO, >=0 if OK
+ */
+ 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()
@@ -213,14 +233 @@
- // phpcs:enable
- return dol_count_nb_of_line($file);
- }
-
-
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
- /**
- * Input header line from file
- *
- * @return int Return integer <0 if KO, >=0 if OK
- */
- public function import_read_header()
- {
- // phpcs:enable
+ // phpcs:enable
@@ -231 +238 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -235,3 +242,3 @@
- * @return array|boolean 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()
+ * @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()
@@ -239 +246 @@
- // phpcs:enable
+ // phpcs:enable
@@ -245,3 +252 @@
- if ($arrayres === false) {
- return false;
- }
+ if ($arrayres === false) return false;
@@ -252,4 +257,8 @@
- if ($arrayres && is_array($arrayres)) {
- foreach ($arrayres as $key => $val) {
- if (getDolGlobalString('IMPORT_CSV_FORCE_CHARSET')) { // Forced charset
- if (strtolower($conf->global->IMPORT_CSV_FORCE_CHARSET) == 'utf8') {
+ if ($arrayres && is_array($arrayres))
+ {
+ foreach ($arrayres as $key => $val)
+ {
+ if (!empty($conf->global->IMPORT_CSV_FORCE_CHARSET)) // Forced charset
+ {
+ if (strtolower($conf->global->IMPORT_CSV_FORCE_CHARSET) == 'utf8')
+ {
@@ -258,2 +267,4 @@
- } else {
- $newarrayres[$key]['val'] = mb_convert_encoding($val, 'UTF-8', 'ISO-8859-1');
+ }
+ else
+ {
+ $newarrayres[$key]['val'] = utf8_encode($val);
@@ -262,2 +273,5 @@
- } else { // Autodetect format (UTF8 or ISO)
- if (utf8_check($val)) {
+ }
+ else // Autodetect format (UTF8 or ISO)
+ {
+ if (utf8_check($val))
+ {
@@ -266,2 +280,4 @@
- } else {
- $newarrayres[$key]['val'] = mb_convert_encoding($val, 'UTF-8', 'ISO-8859-1');
+ }
+ else
+ {
+ $newarrayres[$key]['val'] = utf8_encode($val);
@@ -279 +295 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -285 +301 @@
- public function import_close_file()
+ public function import_close_file()
@@ -287 +303 @@
- // phpcs:enable
+ // phpcs:enable
@@ -293 +309 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -303,3 +319,3 @@
- * @return int Return integer <0 if KO, >0 if OK
- */
- public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
+ * @return int <0 if KO, >0 if OK
+ */
+ public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
@@ -307 +323 @@
- // phpcs:enable
+ // phpcs:enable
@@ -309 +325 @@
- global $thirdparty_static; // Specific to thirdparty import
+ global $thirdparty_static; // Specific to thirdparty import
@@ -320,2 +336 @@
- //var_dump($arrayrecord); exit;
-
+ //var_dump($arrayrecord);
@@ -328 +343,2 @@
- if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0]['val']))) {
+ if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0]['val'])))
+ {
@@ -333 +349,3 @@
- } else {
+ }
+ else
+ {
@@ -338 +356,2 @@
- foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
+ foreach ($objimport->array_import_tables[0] as $alias => $tablename)
+ {
@@ -347 +366,2 @@
- if (!isset($tablewithentity_cache[$tablename])) { // keep this test with "isset"
+ if (!isset($tablewithentity_cache[$tablename])) // keep this test with "isset"
+ {
@@ -350 +370,2 @@
- if ($resql) {
+ if ($resql)
+ {
@@ -352,7 +373,2 @@
- if ($obj) {
- $tablewithentity_cache[$tablename] = 1; // table contains entity field
- } else {
- $tablewithentity_cache[$tablename] = 0; // table does not contain entity field
- }
- } else {
- dol_print_error($this->db);
+ if ($obj) $tablewithentity_cache[$tablename] = 1; // table contains entity field
+ else $tablewithentity_cache[$tablename] = 0; // table does not contains entity field
@@ -360 +376,4 @@
- } else {
+ else dol_print_error($this->db);
+ }
+ else
+ {
@@ -364,8 +383,5 @@
- // Define array to convert fields ('c.ref', ...) into column index (1, ...)
- $arrayfield = array();
- foreach ($sort_array_match_file_to_database as $key => $val) {
- $arrayfield[$val] = ($key - 1);
- }
-
- // $arrayrecord start at key 0
- // $sort_array_match_file_to_database start at key 1
+ // array of fields to column index
+ $arrayfield = array();
+ foreach ($sort_array_match_file_to_database as $key => $val) {
+ $arrayfield[$val] = ($key - 1);
+ }
@@ -374,9 +390,9 @@
- 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)
+ {
@@ -385,6 +401 @@
- 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
- }
-
- //var_dump($newval);var_dump($val);
- //var_dump($objimport->array_import_convertvalue[0][$val]);
+ 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
@@ -395 +406,2 @@
- if (preg_match('/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval == '')) {
+ if (preg_match('/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval == ''))
+ {
@@ -400,85 +412,277 @@
- } else {
- // Test format only if field is not a missing mandatory field (field may be a value or empty but not mandatory)
- // 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 'Newval 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);
- if ($class == 'CGenericDic') {
- $classinstance->element = $objimport->array_import_convertvalue[0][$val]['element'];
- $classinstance->table_element = $objimport->array_import_convertvalue[0][$val]['table_element'];
- }
-
- // 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(getDolGlobalInt('CHARTOFACCOUNTS'));
- //var_dump($tmpchartofaccount->ref.' - '.$arrayrecord[0]['val']);
- if ((! (getDolGlobalInt('CHARTOFACCOUNTS') > 0)) || $tmpchartofaccount->ref != $arrayrecord[0]['val'])
- {
- $this->errors[$error]['lib']=$langs->trans('ErrorImportOfChartLimitedToCurrentChart', $tmpchartofaccount->ref);
- $this->errors[$error]['type']='RESTRICTONCURRENCTCHART';
- $errorforthistable++;
- $error++;
- }*/
- $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart.
- }
-
- $result = call_user_func_array(array($classinstance, $method), $param_array);
-
- // If duplicate record found
- if (!($classinstance->id != '') && $result == -2) {
- $this->errors[$error]['lib'] = $langs->trans('ErrorMultipleRecordFoundFromRef', $newval);
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
- $error++;
- }
-
- // 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;
- } elseif (! $error) {
- if (!empty($objimport->array_import_convertvalue[0][$val]['dict'])) {
- $this->errors[$error]['lib'] = $langs->trans('ErrorFieldValueNotIn', num2Alpha($key - 1), $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', num2Alpha($key - 1), $newval, $langs->transnoentitiesnoconv($objimport->array_import_convertvalue[0][$val]['element']));
- } else {
- $this->errors[$error]['lib'] = 'ErrorBadDefinitionOfImportProfile';
- }
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $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';
+ $tmpchartofaccount = new AccountancySystem($this->db);
+ $tmpchartofaccount->fetch($conf->global->CHARTOFACCOUNTS);
+ var_dump($tmpchartofaccount->ref.' - '.$arrayrecord[0]['val']);
+ if ((! ($conf->global->CHARTOFACCOUNTS > 0)) || $tmpchartofaccount->ref != $arrayrecord[0]['val'])
+ {
+ $this->errors[$error]['lib']=$langs->trans('ErrorImportOfChartLimitedToCurrentChart', $tmpchartofaccount->ref);
+ $this->errors[$error]['type']='RESTRICTONCURRENCTCHART';
+ $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++;
+ }
+ }
+ 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:..."
+ 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;
+
+ // 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;
+ }
+
+ $resql = $this->db->query($sql);
+ if ($resql)
+ {
+ $num = $this->db->num_rows($resql);
+ $i = 0;
+ while ($i < $num)
+ {
+ $obj = $this->db->fetch_object($resql);
+ if ($obj) $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
+ $i++;
@@ -487,216 +691,2 @@
- }
- } 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', num2Alpha($key - 1), $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', num2Alpha($key - 1), $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->thirdpartyobject->get_codeclient(0, 0);
- $newval = $this->thirdpartyobject->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') {
- $this->thirdpartyobject->get_codefournisseur(0, 1);
- $newval = $this->thirdpartyobject->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->thirdpartyobject->get_codecompta('customer');
- $newval = $this->thirdpartyobject->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->thirdpartyobject->get_codecompta('supplier');
- $newval = $this->thirdpartyobject->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') {
- if (strtolower($newval) == 'auto') {
- $defaultref = '';
-
- $classModForNumber = $objimport->array_import_convertvalue[0][$val]['class'];
- $pathModForNumber = $objimport->array_import_convertvalue[0][$val]['path'];
-
- if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
- require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
- $modForNumber = new $classModForNumber();
-
- $tmpobject = null;
- // Set the object with the date property when we can
- if (!empty($objimport->array_import_convertvalue[0][$val]['classobject'])) {
- $pathForObject = $objimport->array_import_convertvalue[0][$val]['pathobject'];
- require_once DOL_DOCUMENT_ROOT.$pathForObject;
- $tmpclassobject = $objimport->array_import_convertvalue[0][$val]['classobject'];
- $tmpobject = new $tmpclassobject($this->db);
- foreach ($arrayfield as $tmpkey => $tmpval) { // $arrayfield is array('c.ref'=>0, ...)
- if (in_array($tmpkey, array('t.date', 'c.date_commande'))) {
- $tmpobject->date = dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]]['val'], 1);
- }
- }
- }
-
- $defaultref = $modForNumber->getNextValue(null, $tmpobject);
- }
- if (is_numeric($defaultref) && $defaultref <= 0) { // If error
- $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, $arrayfield, ($key - 1)));
- if (empty($classinstance->error) && empty($classinstance->errors)) {
- $newval = $res; // We get new value computed.
- } else {
- $this->errors[$error]['type'] = 'CLASSERROR';
- $this->errors[$error]['lib'] = implode(
- "\n",
- array_merge([$classinstance->error], $classinstance->errors)
- );
- $errorforthistable++;
- $error++;
- }
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'numeric') {
- $newval = price2num($newval);
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'accountingaccount') {
- if (!getDolGlobalString('ACCOUNTING_MANAGE_ZERO')) {
- $newval = rtrim(trim($newval), "0");
- } else {
- $newval = trim($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];
- $table = $reg[2];
- $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;
- }
-
- $resql = $this->db->query($sql);
- if ($resql) {
- $num = $this->db->num_rows($resql);
- $i = 0;
- while ($i < $num) {
- $obj = $this->db->fetch_object($resql);
- if ($obj) {
- $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
- }
- $i++;
- }
- } else {
+ else
+ {
@@ -708 +698,2 @@
- if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
+ if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey]))
+ {
@@ -710,4 +701,2 @@
- if (!empty($filter)) {
- $tableforerror .= ':'.$filter;
- }
- $this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorFieldValueNotIn', num2Alpha($key - 1), $newval, $field, $tableforerror);
+ if (!empty($filter)) $tableforerror .= ':'.$filter;
+ $this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorFieldValueNotIn', $key, $newval, $field, $tableforerror);
@@ -715 +704 @@
- $errorforthistable++;
+ $errorforthistable++;
@@ -718,4 +707,5 @@
- } elseif (!preg_match('/'.$objimport->array_import_regex[0][$val].'/i', $newval)) {
- // If test is just a static regex
- //if ($key == 19) print "xxx".$newval."zzz".$objimport->array_import_regex[0][$val]."
";
- $this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorWrongValueForField', num2Alpha($key - 1), $newval, $objimport->array_import_regex[0][$val]);
+ }
+ // 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]."
";
+ $this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorWrongValueForField', $key, $newval, $objimport->array_import_regex[0][$val]);
@@ -728,9 +717,0 @@
- // Check HTML injection
- $inj = testSqlAndScriptInject($newval, 0);
- if ($inj) {
- $this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorHtmlInjectionForField', num2Alpha($key - 1), dol_trunc($newval, 100));
- $this->errors[$error]['type'] = 'HTMLINJECTION';
- $errorforthistable++;
- $error++;
- }
-
@@ -741,34 +722,11 @@
- // Define $listfields and $listvalues to build the SQL request
- if (isModEnabled("socialnetworks") && strpos($fieldname, "socialnetworks") !== false) {
- if (!in_array("socialnetworks", $listfields)) {
- $listfields[] = "socialnetworks";
- $socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array
- $listvalues[$socialkey] = '';
- }
- //var_dump($newval); var_dump($arrayrecord[($key - 1)]['type']);
- if (!empty($newval) && $arrayrecord[($key - 1)]['type'] > 0) {
- $socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array
- //var_dump('sk='.$socialkey); // socialkey=19
- $socialnetwork = explode("_", $fieldname)[1];
- if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == "null") {
- $json = new stdClass();
- $json->$socialnetwork = $newval;
- $listvalues[$socialkey] = json_encode($json);
- } else {
- $jsondata = $listvalues[$socialkey];
- $json = json_decode($jsondata);
- $json->$socialnetwork = $newval;
- $listvalues[$socialkey] = json_encode($json);
- }
- }
- } else {
- $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 - 1)]['type'] < 0) {
- $listvalues[] = ($newval == '0' ? (int) $newval : "null");
- } elseif (empty($newval) && $arrayrecord[($key - 1)]['type'] == 0) {
- $listvalues[] = "''";
- } else {
- $listvalues[] = "'".$this->db->escape($newval)."'";
- }
- }
+ // Define $listfields and $listvalues to build SQL request
+ $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 - 1)]['type'] < 0) {
+ $listvalues[] = ($newval == '0' ? $newval : "null");
+ } elseif (empty($newval) && $arrayrecord[($key - 1)]['type'] == 0) {
+ $listvalues[] = "''";
+ } else {
+ $listvalues[] = "'".$this->db->escape($newval)."'";
+ }
@@ -780,69 +738,33 @@
- // We process here all the fields that were declared into the array $this->import_fieldshidden_array of the descriptor file.
- // Previously we processed the ->import_fields_array.
- 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 $tmpkey => $tmpval) {
- if (!preg_match('/^'.preg_quote($alias, '/').'\./', $tmpkey)) {
- continue; // Not a field of current table
- }
- if ($tmpval == 'user->id') {
- $listfields[] = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $tmpkey);
- $listvalues[] = ((int) $user->id);
- } elseif (preg_match('/^lastrowid-/', $tmpval)) {
- $tmp = explode('-', $tmpval);
- $lastinsertid = (isset($last_insert_id_array[$tmp[1]])) ? $last_insert_id_array[$tmp[1]] : 0;
- $keyfield = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $tmpkey);
- $listfields[] = $keyfield;
- $listvalues[] = (int) $lastinsertid;
- //print $tmpkey."-".$tmpval."-".$listfields."-".$listvalues."
";exit;
- } elseif (preg_match('/^const-/', $tmpval)) {
- $tmp = explode('-', $tmpval, 2);
- $listfields[] = preg_replace('/^'.preg_quote($alias, '/').'\./', '', $tmpkey);
- $listvalues[] = "'".$this->db->escape($tmp[1])."'";
- } elseif (preg_match('/^rule-/', $tmpval)) {
- $fieldname = $tmpkey;
- if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
- if ($objimport->array_import_convertvalue[0][$fieldname]['rule'] == 'compute') {
- $file = (empty($objimport->array_import_convertvalue[0][$fieldname]['classfile']) ? $objimport->array_import_convertvalue[0][$fieldname]['file'] : $objimport->array_import_convertvalue[0][$fieldname]['classfile']);
- $class = $objimport->array_import_convertvalue[0][$fieldname]['class'];
- $method = $objimport->array_import_convertvalue[0][$fieldname]['method'];
- $type = $objimport->array_import_convertvalue[0][$fieldname]['type'];
- $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, $arrayfield, ($key - 1)));
- if (empty($classinstance->error) && empty($classinstance->errors)) {
- $fieldArr = explode('.', $fieldname);
- if (count($fieldArr) > 0) {
- $fieldname = $fieldArr[1];
- }
-
- // Set $listfields and $listvalues
- $listfields[] = $fieldname;
- if ($type == 'int') {
- $listvalues[] = (int) $res;
- } elseif ($type == 'double') {
- $listvalues[] = (float) $res;
- } else {
- $listvalues[] = "'".$this->db->escape($res)."'";
- }
- } else {
- $this->errors[$error]['type'] = 'CLASSERROR';
- $this->errors[$error]['lib'] = implode(
- "\n",
- array_merge([$classinstance->error], $classinstance->errors)
- );
- $errorforthistable++;
- $error++;
- }
- }
- }
- } else {
- $this->errors[$error]['lib'] = 'Bad value of profile setup '.$tmpval.' 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++;
+ }
+ }
@@ -854 +776,2 @@
- if (!$errorforthistable) {
+ if (!$errorforthistable)
+ {
@@ -856 +779,2 @@
- if (!empty($listfields)) {
+ if (!empty($listfields))
+ {
@@ -860,7 +783,0 @@
- $is_table_category_link = false;
- $fname = 'rowid';
- if (strpos($tablename, '_categorie_') !== false) {
- $is_table_category_link = true;
- $fname='*';
- }
-
@@ -871 +788,2 @@
- $sqlSelect = "SELECT ".$fname." FROM ".$tablename;
+ $sqlSelect = 'SELECT rowid FROM '.$tablename;
+
@@ -873,2 +791,2 @@
- $where = array(); // filters to forge SQL request
- $filters = array(); // filters to forge output error message
+ $where = array();
+ $filters = array();
@@ -878,22 +796,4 @@
- if (isModEnabled("socialnetworks") && strpos($key, "socialnetworks") !== false) {
- $tmp = explode("_", $key);
- $key = $tmp[0];
- $socialnetwork = $tmp[1];
- $jsondata = $data[$key];
- $json = json_decode($jsondata);
- $stringtosearch = json_encode($socialnetwork).':'.json_encode($json->$socialnetwork);
- //var_dump($stringtosearch);
- //var_dump($this->db->escape($stringtosearch)); // This provide a value for sql string (but not for a like)
- $where[] = $key." LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch))."%'";
- $filters[] = $col." LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch))."%'";
- //var_dump($where[1]); // This provide a value for sql string inside a like
- } else {
- $where[] = $key.' = '.$data[$key];
- $filters[] = $col.' = '.$data[$key];
- }
- }
- if (!empty($tablewithentity_cache[$tablename])) {
- $where[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")";
- $filters[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")";
- }
- $sqlSelect .= " WHERE ".implode(' AND ', $where);
+ $where[] = $key.' = '.$data[$key];
+ $filters[] = $col.' = '.$data[$key];
+ }
+ $sqlSelect .= ' WHERE '.implode(' AND ', $where);
@@ -903,3 +803,2 @@
- $num_rows = $this->db->num_rows($resql);
- if ($num_rows == 1) {
- $res = $this->db->fetch_object($resql);
+ $res = $this->db->fetch_object($resql);
+ if ($resql->num_rows == 1) {
@@ -907,3 +805,0 @@
- if ($is_table_category_link) {
- $lastinsertid = 'linktable';
- } // used to apply update on tables like llx_categorie_product and avoid being blocked for all file content if at least one entry already exists
@@ -911 +807 @@
- } elseif ($num_rows > 1) {
+ } elseif ($resql->num_rows > 1) {
@@ -918 +814,3 @@
- } else {
+ }
+ else
+ {
@@ -930,10 +828,4 @@
- $sqlSelect = "SELECT rowid FROM ".$tablename;
-
- if (empty($keyfield)) {
- $keyfield = 'rowid';
- }
- $sqlSelect .= " WHERE ".$keyfield." = ".((int) $lastinsertid);
-
- if (!empty($tablewithentity_cache[$tablename])) {
- $sqlSelect .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")";
- }
+ $sqlSelect = 'SELECT rowid FROM '.$tablename;
+
+ if (empty($keyfield)) $keyfield = 'rowid';
+ $sqlSelect .= ' WHERE '.$keyfield.' = '.$lastinsertid;
@@ -944 +836 @@
- if ($this->db->num_rows($resql) == 1) {
+ if ($resql->num_rows == 1) {
@@ -951 +843,3 @@
- } else {
+ }
+ else
+ {
@@ -960,7 +853,0 @@
- // We db escape social network field because he isn't in field creation
- if (in_array("socialnetworks", $listfields)) {
- $socialkey = array_search("socialnetworks", $listfields);
- $tmpsql = $listvalues[$socialkey];
- $listvalues[$socialkey] = "'".$this->db->escape($tmpsql)."'";
- }
-
@@ -968 +855 @@
- $sqlstart = "UPDATE ".$tablename;
+ $sqlstart = 'UPDATE '.$tablename;
@@ -973,16 +860,6 @@
- $set[] = $key." = ".$val; // $val was escaped/sanitized previously
- }
- $sqlstart .= " SET ".implode(', ', $set).", import_key = '".$this->db->escape($importid)."'";
-
- if (empty($keyfield)) {
- $keyfield = 'rowid';
- }
- $sqlend = " WHERE ".$keyfield." = ".((int) $lastinsertid);
-
- if ($is_table_category_link) {
- $sqlend = " WHERE " . implode(' AND ', $where);
- }
-
- if (!empty($tablewithentity_cache[$tablename])) {
- $sqlend .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")";
- }
+ $set[] = $key.' = '.$val;
+ }
+ $sqlstart .= ' SET '.implode(', ', $set);
+
+ if (empty($keyfield)) $keyfield = 'rowid';
+ $sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid;
@@ -997 +874,3 @@
- } else {
+ }
+ else
+ {
@@ -1008,7 +886,0 @@
- // We db escape social network field because he isn't in field creation
- if (in_array("socialnetworks", $listfields)) {
- $socialkey = array_search("socialnetworks", $listfields);
- $tmpsql = $listvalues[$socialkey];
- $listvalues[$socialkey] = "'".$this->db->escape($tmpsql)."'";
- }
-
@@ -1016,2 +888,2 @@
- $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."'";
@@ -1019,2 +891,2 @@
- $sqlstart .= ", entity";
- $sqlend .= ", ".$conf->entity;
+ $sqlstart .= ', entity';
+ $sqlend .= ', '.$conf->entity;
@@ -1023,2 +895,2 @@
- $sqlstart .= ", ".$objimport->array_import_tables_creator[0][$alias];
- $sqlend .= ", ".$user->id;
+ $sqlstart .= ', '.$objimport->array_import_tables_creator[0][$alias];
+ $sqlend .= ', '.$user->id;
@@ -1026,2 +898,2 @@
- $sql = $sqlstart.$sqlend.")";
- //dol_syslog("import_csv.modules", LOG_DEBUG);
+ $sql = $sqlstart.$sqlend.')';
+ dol_syslog("import_csv.modules", LOG_DEBUG);
@@ -1030 +902,2 @@
- if ($sql) {
+ if ($sql)
+ {
@@ -1032,6 +905,7 @@
- if ($resql) {
- if (!$is_table_category_link) {
- $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
+ {
@@ -1052,3 +926 @@
- if ($error) {
- break;
- }
+ if ($error) break;
@@ -1057,6 +929,2 @@
- if ($updatedone) {
- $this->nbupdate++;
- }
- if ($insertdone) {
- $this->nbinsert++;
- }
+ if ($updatedone) $this->nbupdate++;
+ if ($insertdone) $this->nbinsert++;
@@ -1078 +946 @@
-}
+};
--- /tmp/dsg/dolibarr/htdocs/core/modules/import/github_19.0.3_import_xlsx.modules.php
+++ /tmp/dsg/dolibarr/htdocs/core/modules/import/client_import_xlsx.modules.php
@@ -2,2 +2,2 @@
-/* Copyright (C) 2006-2012 Laurent Destailleur
- * Copyright (C) 2009-2012 Regis Houssin
+/* Copyright (C) 2006-2012 Laurent Destailleur
+ * Copyright (C) 2009-2012 Regis Houssin
@@ -28,5 +28 @@
-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';
@@ -40,7 +36,19 @@
- /**
- * @var DoliDB Database handler.
- */
- public $db;
-
- /**
- * @var string Code of driver
+ /**
+ * @var DoliDB Database handler.
+ */
+ public $db;
+
+ public $datatoimport;
+
+ /**
+ * @var string Error code (or message)
+ */
+ public $error = '';
+
+ /**
+ * @var string[] Error codes (or messages)
+ */
+ public $errors = array();
+
+ /**
+ * @var int ID
@@ -51,3 +59,3 @@
- * @var string label
- */
- public $label;
+ * @var string label
+ */
+ public $label;
@@ -58,3 +66,3 @@
- * Dolibarr version of driver
- * @var string
- */
+ * Dolibarr version of driver
+ * @var string
+ */
@@ -69 +77 @@
- public $file; // Path of file
+ public $file; // Path of file
@@ -76,4 +83,0 @@
-
- public $nbinsert = 0; // # of insert done during the import
-
- public $nbupdate = 0; // # of update done during the import
@@ -94 +98 @@
- public function __construct($db, $datatoimport)
+ public function __construct($db, $datatoimport)
@@ -97,2 +100,0 @@
-
- parent::__construct();
@@ -110,13 +112,10 @@
- require_once DOL_DOCUMENT_ROOT.'/includes/phpoffice/phpspreadsheet/src/autoloader.php';
- require_once DOL_DOCUMENT_ROOT.'/includes/Psr/autoloader.php';
- require_once PHPEXCELNEW_PATH.'Spreadsheet.php';
- $this->workbook = new Spreadsheet();
-
- // If driver use an external library, put its name here
- if (!class_exists('ZipArchive')) { // For Excel2007
- $langs->load("errors");
- $this->error = $langs->trans('ErrorPHPNeedModule', 'zip');
- return;
- }
- $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';
@@ -125,3 +124 @@
- if (preg_match('/^societe_/', $datatoimport)) {
- $this->thirdpartyobject = new Societe($this->db);
- }
+ if (preg_match('/^societe_/', $datatoimport)) $this->thirpartyobject = new Societe($this->db);
@@ -131 +128 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -138,18 +135,27 @@
- 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
@@ -163,3 +169,3 @@
- public function write_title_example($outputlangs, $headerlinefields)
- {
- // phpcs:enable
+ public function write_title_example($outputlangs, $headerlinefields)
+ {
+ // phpcs:enable
@@ -168,3 +174,3 @@
- $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;
@@ -181 +187 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -189,4 +195,4 @@
- public function write_record_example($outputlangs, $contentlinevalues)
- {
- // phpcs:enable
- $col = 1;
+ public function write_record_example($outputlangs, $contentlinevalues)
+ {
+ // phpcs:enable
+ $col = 0;
@@ -202 +208 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -209,3 +215,3 @@
- public function write_footer_example($outputlangs)
- {
- // phpcs:enable
+ public function write_footer_example($outputlangs)
+ {
+ // phpcs:enable
@@ -214 +220 @@
- $objWriter = new PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->workbook);
+ $objWriter = new PHPExcel_Writer_Excel2007($this->workbook);
@@ -226 +232 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -231,5 +237,5 @@
- * @return int Return integer <0 if KO, >=0 if OK
- */
- public function import_open_file($file)
- {
- // phpcs:enable
+ * @return int <0 if KO, >=0 if OK
+ */
+ public function import_open_file($file)
+ {
+ // phpcs:enable
@@ -239,3 +245,3 @@
- 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();
@@ -250 +256 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -255,6 +261,6 @@
- * @return int Return integer <0 if KO, >=0 if OK
- */
- public function import_get_nb_of_lines($file)
- {
- // phpcs:enable
- $reader = new Xlsx();
+ * @return int <0 if KO, >=0 if OK
+ */
+ public function import_get_nb_of_lines($file)
+ {
+ // phpcs:enable
+ $reader = new PHPExcel_Reader_Excel2007();
@@ -272 +278 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -276,5 +282,5 @@
- * @return int Return integer <0 if KO, >=0 if OK
- */
- public function import_read_header()
- {
- // phpcs:enable
+ * @return int <0 if KO, >=0 if OK
+ */
+ public function import_read_header()
+ {
+ // phpcs:enable
@@ -283,4 +289,2 @@
- $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++) {
@@ -293 +297 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -297,5 +301,5 @@
- * @return array|boolean 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
+ * @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
@@ -305 +309 @@
- if ($this->record > $rowcount) {
+ if ($this->record > $rowcount)
@@ -307 +310,0 @@
- }
@@ -309,4 +312,2 @@
- $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++) {
@@ -321 +322 @@
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
+ // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
@@ -327,3 +328,3 @@
- public function import_close_file()
- {
- // phpcs:enable
+ public function import_close_file()
+ {
+ // phpcs:enable
@@ -332 +332,0 @@
- return 0;
@@ -336,2 +336,2 @@
- // 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
@@ -347,5 +347,5 @@
- * @return int Return integer <0 if KO, >0 if OK
- */
- public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
- {
- // phpcs:enable
+ * @return int <0 if KO, >0 if OK
+ */
+ public function import_insert($arrayrecord, $array_match_file_to_database, $objimport, $maxfields, $importid, $updatekeys)
+ {
+ // phpcs:enable
@@ -353 +353 @@
- global $thirdparty_static; // Specific to thirdparty import
+ global $thirdparty_static; // Specific to thirdparty import
@@ -364,2 +364 @@
- //var_dump($arrayrecord); exit;
-
+ //var_dump($arrayrecord);
@@ -372 +371,2 @@
- if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[1]['val']))) {
+ if (count($arrayrecord) == 0 || (count($arrayrecord) == 1 && empty($arrayrecord[0]['val'])))
+ {
@@ -377 +377,3 @@
- } else {
+ }
+ else
+ {
@@ -382 +384,2 @@
- foreach ($objimport->array_import_tables[0] as $alias => $tablename) {
+ foreach ($objimport->array_import_tables[0] as $alias => $tablename)
+ {
@@ -391,2 +394,3 @@
- if (!isset($tablewithentity_cache[$tablename])) { // keep this test with "isset"
- dol_syslog("Check if table " . $tablename . " has an entity field");
+ if (!isset($tablewithentity_cache[$tablename])) // keep this test with "isset"
+ {
+ dol_syslog("Check if table ".$tablename." has an entity field");
@@ -394 +398,2 @@
- if ($resql) {
+ if ($resql)
+ {
@@ -396,7 +401,2 @@
- if ($obj) {
- $tablewithentity_cache[$tablename] = 1; // table contains entity field
- } else {
- $tablewithentity_cache[$tablename] = 0; // table does not contain entity field
- }
- } else {
- dol_print_error($this->db);
+ if ($obj) $tablewithentity_cache[$tablename] = 1; // table contains entity field
+ else $tablewithentity_cache[$tablename] = 0; // table does not contains entity field
@@ -404 +404,4 @@
- } else {
+ else dol_print_error($this->db);
+ }
+ else
+ {
@@ -408,8 +411,5 @@
- // Define array to convert fields ('c.ref', ...) into column index (1, ...)
- $arrayfield = array();
- foreach ($sort_array_match_file_to_database as $key => $val) {
- $arrayfield[$val] = ($key);
- }
-
- // $arrayrecord start at key 1
- // $sort_array_match_file_to_database start at key 1
+ // array of fields to column index
+ $arrayfield = array();
+ foreach ($sort_array_match_file_to_database as $key => $val) {
+ $arrayfield[$val] = ($key - 1);
+ }
@@ -418,9 +418,9 @@
- 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)
+ {
@@ -429,6 +429 @@
- 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
- }
-
- //var_dump($newval);var_dump($val);
- //var_dump($objimport->array_import_convertvalue[0][$val]);
+ 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
@@ -439,2 +434,3 @@
- if (preg_match('/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval == '')) {
- $this->errors[$error]['lib'] = $langs->trans('ErrorMissingMandatoryValue', num2Alpha($key - 1));
+ if (preg_match('/\*/', $objimport->array_import_fields[0][$val]) && ((string) $newval == ''))
+ {
+ $this->errors[$error]['lib'] = $langs->trans('ErrorMissingMandatoryValue', $key);
@@ -444,84 +440,276 @@
- } else {
- // Test format only if field is not a missing mandatory field (field may be a value or empty but not mandatory)
- // 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 'Newval 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);
- if ($class == 'CGenericDic') {
- $classinstance->element = $objimport->array_import_convertvalue[0][$val]['element'];
- $classinstance->table_element = $objimport->array_import_convertvalue[0][$val]['table_element'];
- }
-
- // 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(getDolGlobalInt('CHARTOFACCOUNTS'));
- //var_dump($tmpchartofaccount->ref.' - '.$arrayrecord[0]['val']);
- if ((! (getDolGlobalInt('CHARTOFACCOUNTS') > 0)) || $tmpchartofaccount->ref != $arrayrecord[0]['val'])
- {
- $this->errors[$error]['lib']=$langs->trans('ErrorImportOfChartLimitedToCurrentChart', $tmpchartofaccount->ref);
- $this->errors[$error]['type']='RESTRICTONCURRENCTCHART';
- $errorforthistable++;
- $error++;
- }*/
- $param_array = array('', $newval, 0, $arrayrecord[0]['val']); // Param to fetch parent from account, in chart.
- }
-
- $result = call_user_func_array(array($classinstance, $method), $param_array);
-
- // If duplicate record found
- if (!($classinstance->id != '') && $result == -2) {
- $this->errors[$error]['lib'] = $langs->trans('ErrorMultipleRecordFoundFromRef', $newval);
- $this->errors[$error]['type'] = 'FOREIGNKEY';
- $errorforthistable++;
- $error++;
- }
-
- // 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;
- } elseif (! $error) {
- 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++;
+ }
+ // 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';
+ $tmpchartofaccount = new AccountancySystem($this->db);
+ $tmpchartofaccount->fetch($conf->global->CHARTOFACCOUNTS);
+ var_dump($tmpchartofaccount->ref.' - '.$arrayrecord[0]['val']);
+ if ((! ($conf->global->CHARTOFACCOUNTS > 0)) || $tmpchartofaccount->ref != $arrayrecord[0]['val'])
+ {
+ $this->errors[$error]['lib']=$langs->trans('ErrorImportOfChartLimitedToCurrentChart', $tmpchartofaccount->ref);
+ $this->errors[$error]['type']='RESTRICTONCURRENCTCHART';
+ $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++;
+ }
+ }
+ 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:..."
+ 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;
+
+ // 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;
+ }
+
+ $resql = $this->db->query($sql);
+ if ($resql)
+ {
+ $num = $this->db->num_rows($resql);
+ $i = 0;
+ while ($i < $num)
+ {
+ $obj = $this->db->fetch_object($resql);
+ if ($obj) $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
+ $i++;
@@ -530,216 +718,2 @@
- }
- } 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->thirdpartyobject->get_codeclient(0, 0);
- $newval = $this->thirdpartyobject->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') {
- $this->thirdpartyobject->get_codefournisseur(0, 1);
- $newval = $this->thirdpartyobject->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->thirdpartyobject->get_codecompta('customer');
- $newval = $this->thirdpartyobject->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->thirdpartyobject->get_codecompta('supplier');
- $newval = $this->thirdpartyobject->code_compta_fournisseur;
- if (empty($newval)) {
- $arrayrecord[($key)]['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') {
- if (strtolower($newval) == 'auto') {
- $defaultref = '';
-
- $classModForNumber = $objimport->array_import_convertvalue[0][$val]['class'];
- $pathModForNumber = $objimport->array_import_convertvalue[0][$val]['path'];
-
- if (!empty($classModForNumber) && !empty($pathModForNumber) && is_readable(DOL_DOCUMENT_ROOT.$pathModForNumber)) {
- require_once DOL_DOCUMENT_ROOT.$pathModForNumber;
- $modForNumber = new $classModForNumber();
-
- $tmpobject = null;
- // Set the object with the date property when we can
- if (!empty($objimport->array_import_convertvalue[0][$val]['classobject'])) {
- $pathForObject = $objimport->array_import_convertvalue[0][$val]['pathobject'];
- require_once DOL_DOCUMENT_ROOT.$pathForObject;
- $tmpclassobject = $objimport->array_import_convertvalue[0][$val]['classobject'];
- $tmpobject = new $tmpclassobject($this->db);
- foreach ($arrayfield as $tmpkey => $tmpval) { // $arrayfield is array('c.ref'=>1, ...)
- if (in_array($tmpkey, array('t.date', 'c.date_commande'))) {
- $tmpobject->date = dol_stringtotime($arrayrecord[$arrayfield[$tmpkey]]['val'], 1);
- }
- }
- }
-
- $defaultref = $modForNumber->getNextValue(null, $tmpobject);
- }
- if (is_numeric($defaultref) && $defaultref <= 0) { // If error
- $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, $arrayfield, $key));
- if (empty($classinstance->error) && empty($classinstance->errors)) {
- $newval = $res; // We get new value computed.
- } else {
- $this->errors[$error]['type'] = 'CLASSERROR';
- $this->errors[$error]['lib'] = implode(
- "\n",
- array_merge([$classinstance->error], $classinstance->errors)
- );
- $errorforthistable++;
- $error++;
- }
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'numeric') {
- $newval = price2num($newval);
- } elseif ($objimport->array_import_convertvalue[0][$val]['rule'] == 'accountingaccount') {
- if (!getDolGlobalString('ACCOUNTING_MANAGE_ZERO')) {
- $newval = rtrim(trim($newval), "0");
- } else {
- $newval = trim($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];
- $table = $reg[2];
- $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;
- }
-
- $resql = $this->db->query($sql);
- if ($resql) {
- $num = $this->db->num_rows($resql);
- $i = 0;
- while ($i < $num) {
- $obj = $this->db->fetch_object($resql);
- if ($obj) {
- $this->cachefieldtable[$cachekey][] = $obj->aliasfield;
- }
- $i++;
- }
- } else {
+ else
+ {
@@ -751 +725,2 @@
- if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey])) {
+ if (!is_array($this->cachefieldtable[$cachekey]) || !in_array($newval, $this->cachefieldtable[$cachekey]))
+ {
@@ -753,3 +728 @@
- if (!empty($filter)) {
- $tableforerror .= ':' . $filter;
- }
+ if (!empty($filter)) $tableforerror .= ':'.$filter;
@@ -758 +731 @@
- $errorforthistable++;
+ $errorforthistable++;
@@ -761,3 +734,4 @@
- } elseif (!preg_match('/' . $objimport->array_import_regex[0][$val] . '/i', $newval)) {
- // If test is just a static regex
- //if ($key == 19) print "xxx".$newval."zzz".$objimport->array_import_regex[0][$val]."
";
+ }
+ // 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]."
";
@@ -771,9 +744,0 @@
- // Check HTML injection
- $inj = testSqlAndScriptInject($newval, 0);
- if ($inj) {
- $this->errors[$error]['lib'] = $langs->transnoentitiesnoconv('ErrorHtmlInjectionForField', $key, dol_trunc($newval, 100));
- $this->errors[$error]['type'] = 'HTMLINJECTION';
- $errorforthistable++;
- $error++;
- }
-
@@ -784,33 +749,7 @@
- // Define $listfields and $listvalues to build the SQL request
- if (isModEnabled("socialnetworks") && strpos($fieldname, "socialnetworks") !== false) {
- if (!in_array("socialnetworks", $listfields)) {
- $listfields[] = "socialnetworks";
- $socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array. Example socialkey=19
- $listvalues[$socialkey] = '';
- }
- if (!empty($newval) && $arrayrecord[($key)]['type'] > 0) {
- $socialkey = array_search("socialnetworks", $listfields); // Return position of 'socialnetworks' key in array. Example socialkey=19
- $socialnetwork = explode("_", $fieldname)[1];
- if (empty($listvalues[$socialkey]) || $listvalues[$socialkey] == "null") {
- $json = new stdClass();
- $json->$socialnetwork = $newval;
- $listvalues[$socialkey] = json_encode($json);
- } else {
- $jsondata = $listvalues[$socialkey];
- $json = json_decode($jsondata);
- $json->$socialnetwork = $newval;
- $listvalues[$socialkey] = json_encode($json);
- }
- }
- } else {
- $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' ? (int) $newval : "null");
- } elseif (empty($newval) && $arrayrecord[($key)]['type'] == 0) {
- $listvalues[] = "''";
- } else {
- $listvalues[] = "'".$this->db->escape($newval)."'";
- }
- }
+ // Define $listfields and $listvalues to build SQL request
+ $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 - 1)]['type'] < 0) $listvalues[] = ($newval == '0' ? $newval : "null");
+ elseif (empty($newval) && $arrayrecord[($key - 1)]['type'] == 0) $listvalues[] = "''";
+ else $listvalues[] = "'".$this->db->escape($newval)."'";
@@ -822,67 +761,33 @@
- // We process here all the fields that were declared into the array $this->import_fieldshidden_array of the descriptor file.
- // Previously we processed the ->import_fields_array.
- 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[] = ((int) $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[] = (int) $lastinsertid;
- //print $key."-".$val."-".$listfields."-".$listvalues."
";exit;
- } elseif (preg_match('/^const-/', $val)) {
- $tmp = explode('-', $val, 2);
- $listfields[] = preg_replace('/^' . preg_quote($alias, '/') . '\./', '', $key);
- $listvalues[] = "'".$this->db->escape($tmp[1])."'";
- } elseif (preg_match('/^rule-/', $val)) {
- $fieldname = $key;
- if (!empty($objimport->array_import_convertvalue[0][$fieldname])) {
- if ($objimport->array_import_convertvalue[0][$fieldname]['rule'] == 'compute') {
- $file = (empty($objimport->array_import_convertvalue[0][$fieldname]['classfile']) ? $objimport->array_import_convertvalue[0][$fieldname]['file'] : $objimport->array_import_convertvalue[0][$fieldname]['classfile']);
- $class = $objimport->array_import_convertvalue[0][$fieldname]['class'];
- $method = $objimport->array_import_convertvalue[0][$fieldname]['method'];
- $type = $objimport->array_import_convertvalue[0][$fieldname]['type'];
- $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, $arrayfield, $key));
- $fieldArr = explode('.', $fieldname);
- if (count($fieldArr) > 0) {
- $fieldname = $fieldArr[1];
- }
-
- // Set $listfields and $listvalues
- $listfields[] = $fieldname;
- if ($type == 'int') {
- $listvalues[] = (int) $res;
- } elseif ($type == 'double') {
- $listvalues[] = (float) $res;
- } else {
- $listvalues[] = "'".$this->db->escape($res)."'";
- }
- } else {
- $this->errors[$error]['type'] = 'CLASSERROR';
- $this->errors[$error]['lib'] = implode(
- "\n",
- array_merge([$classinstance->error], $classinstance->errors)
- );
- $errorforthistable++;
- $error++;
- }
- }
- } 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++;
+ }
+ }
@@ -894 +799,2 @@
- if (!$errorforthistable) {
+ if (!$errorforthistable)
+ {
@@ -896 +802,2 @@
- if (!empty($listfields)) {
+ if (!empty($listfields))
+ {
@@ -899,8 +805,0 @@
-
- $is_table_category_link = false;
- $fname = 'rowid';
- if (strpos($tablename, '_categorie_') !== false) {
- $is_table_category_link = true;
- $fname='*';
- }
-
@@ -911 +810 @@
- $sqlSelect = "SELECT ".$fname." FROM " . $tablename;
+ $sqlSelect = 'SELECT rowid FROM '.$tablename;
@@ -914,3 +813,2 @@
-
- $where = array(); // filters to forge SQL request
- $filters = array(); // filters to forge output error message
+ $where = array();
+ $filters = array();
@@ -920,22 +818,4 @@
- if (isModEnabled("socialnetworks") && strpos($key, "socialnetworks") !== false) {
- $tmp = explode("_", $key);
- $key = $tmp[0];
- $socialnetwork = $tmp[1];
- $jsondata = $data[$key];
- $json = json_decode($jsondata);
- $stringtosearch = json_encode($socialnetwork).':'.json_encode($json->$socialnetwork);
- //var_dump($stringtosearch);
- //var_dump($this->db->escape($stringtosearch)); // This provide a value for sql string (but not for a like)
- $where[] = $key." LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch))."%'";
- $filters[] = $col." LIKE '%".$this->db->escape($this->db->escapeforlike($stringtosearch))."%'";
- //var_dump($where[1]); // This provide a value for sql string inside a like
- } else {
- $where[] = $key.' = '.$data[$key];
- $filters[] = $col.' = '.$data[$key];
- }
- }
- if (!empty($tablewithentity_cache[$tablename])) {
- $where[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")";
- $filters[] = "entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")";
- }
- $sqlSelect .= " WHERE " . implode(' AND ', $where);
+ $where[] = $key.' = '.$data[$key];
+ $filters[] = $col.' = '.$data[$key];
+ }
+ $sqlSelect .= ' WHERE '.implode(' AND ', $where);
@@ -945,3 +825,2 @@
- $num_rows = $this->db->num_rows($resql);
- if ($num_rows == 1) {
- $res = $this->db->fetch_object($resql);
+ $res = $this->db->fetch_object($resql);
+ if ($resql->num_rows == 1) {
@@ -949,3 +827,0 @@
- if ($is_table_category_link) {
- $lastinsertid = 'linktable';
- } // used to apply update on tables like llx_categorie_product and avoid being blocked for all file content if at least one entry already exists
@@ -953 +829 @@
- } elseif ($num_rows > 1) {
+ } elseif ($resql->num_rows > 1) {
@@ -960 +836,3 @@
- } else {
+ }
+ else
+ {
@@ -972,11 +850,4 @@
- $sqlSelect = "SELECT rowid FROM " . $tablename;
-
-
- if (empty($keyfield)) {
- $keyfield = 'rowid';
- }
- $sqlSelect .= " WHERE ".$keyfield." = ".((int) $lastinsertid);
-
- if (!empty($tablewithentity_cache[$tablename])) {
- $sqlSelect .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")";
- }
+ $sqlSelect = 'SELECT rowid FROM '.$tablename;
+
+ if (empty($keyfield)) $keyfield = 'rowid';
+ $sqlSelect .= ' WHERE '.$keyfield.' = '.$lastinsertid;
@@ -987 +858 @@
- if ($this->db->num_rows($resql) == 1) {
+ if ($resql->num_rows == 1) {
@@ -994 +865,3 @@
- } else {
+ }
+ else
+ {
@@ -1003,7 +875,0 @@
- // We db escape social network field because he isn't in field creation
- if (in_array("socialnetworks", $listfields)) {
- $socialkey = array_search("socialnetworks", $listfields);
- $tmpsql = $listvalues[$socialkey];
- $listvalues[$socialkey] = "'".$this->db->escape($tmpsql)."'";
- }
-
@@ -1011 +877 @@
- $sqlstart = "UPDATE " . $tablename;
+ $sqlstart = 'UPDATE '.$tablename;
@@ -1016,18 +882,8 @@
- $set[] = $key." = ".$val; // $val was escaped/sanitized previously
- }
- $sqlstart .= " SET " . implode(', ', $set) . ", import_key = '" . $this->db->escape($importid) . "'";
-
- if (empty($keyfield)) {
- $keyfield = 'rowid';
- }
- $sqlend = " WHERE " . $keyfield . " = ".((int) $lastinsertid);
-
- if ($is_table_category_link) {
- $sqlend = " WHERE " . implode(' AND ', $where);
- }
-
- if (!empty($tablewithentity_cache[$tablename])) {
- $sqlend .= " AND entity IN (".getEntity($this->getElementFromTableWithPrefix($tablename)).")";
- }
-
- $sql = $sqlstart . $sqlend;
+ $set[] = $key.' = '.$val;
+ }
+ $sqlstart .= ' SET '.implode(', ', $set);
+
+ if (empty($keyfield)) $keyfield = 'rowid';
+ $sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid;
+
+ $sql = $sqlstart.$sqlend;
@@ -1040 +896,3 @@
- } else {
+ }
+ else
+ {
@@ -1051,7 +908,0 @@
- // We db escape social network field because he isn't in field creation
- if (in_array("socialnetworks", $listfields)) {
- $socialkey = array_search("socialnetworks", $listfields);
- $tmpsql = $listvalues[$socialkey];
- $listvalues[$socialkey] = "'".$this->db->escape($tmpsql)."'";
- }
-
@@ -1059,2 +910,2 @@
- $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."'";
@@ -1062,2 +913,2 @@
- $sqlstart .= ", entity";
- $sqlend .= ", " . $conf->entity;
+ $sqlstart .= ', entity';
+ $sqlend .= ', '.$conf->entity;
@@ -1066,2 +917,2 @@
- $sqlstart .= ", " . $objimport->array_import_tables_creator[0][$alias];
- $sqlend .= ", " . $user->id;
+ $sqlstart .= ', '.$objimport->array_import_tables_creator[0][$alias];
+ $sqlend .= ', '.$user->id;
@@ -1069,2 +920,2 @@
- $sql = $sqlstart . $sqlend . ")";
- //dol_syslog("import_xlsx.modules", LOG_DEBUG);
+ $sql = $sqlstart.$sqlend.')';
+ dol_syslog("import_xlsx.modules", LOG_DEBUG);
@@ -1073 +924,2 @@
- if ($sql) {
+ if ($sql)
+ {
@@ -1075,6 +927,7 @@
- if ($resql) {
- if (!$is_table_category_link) {
- $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
+ {
@@ -1095,3 +948 @@
- if ($error) {
- break;
- }
+ if ($error) break;
@@ -1100,6 +951,2 @@
- if ($updatedone) {
- $this->nbupdate++;
- }
- if ($insertdone) {
- $this->nbinsert++;
- }
+ if ($updatedone) $this->nbupdate++;
+ if ($insertdone) $this->nbinsert++;
--- /tmp/dsg/dolibarr/htdocs/core/modules/import/github_19.0.3_modules_import.php
+++ /tmp/dsg/dolibarr/htdocs/core/modules/import/client_modules_import.php
@@ -33,8 +33,8 @@
- /**
- * @var DoliDB Database handler.
- */
- public $db;
-
- public $datatoimport;
-
- /**
+ /**
+ * @var DoliDB Database handler.
+ */
+ public $db;
+
+ public $datatoimport;
+
+ /**
@@ -45,12 +45,2 @@
- /**
- * @var string[] Error codes (or messages)
- */
- public $errors = array();
-
- /**
- * @var string[] warnings codes (or messages)
- */
- public $warnings = array();
-
- /**
- * @var string Code of driver
+ /**
+ * @var int id of driver
@@ -60,4 +50,4 @@
- /**
- * @var string label
- */
- public $label;
+ /**
+ * @var string label
+ */
+ public $label;
@@ -68,3 +58,3 @@
- * Dolibarr version of driver
- * @var string
- */
+ * Dolibarr version of driver
+ * @var string
+ */
@@ -88,77 +78,8 @@
- /**
- * @var string charset
- */
- public $charset;
-
- /**
- * @var string picto
- */
- public $picto;
-
- /**
- * @var string description
- */
- public $desc;
-
- /**
- * @var string escape
- */
- public $escape;
-
- /**
- * @var string enclosure
- */
- public $enclosure;
-
- /**
- * @var Societe thirdparty
- */
- public $thirdpartyobject;
-
- /**
- * @var array Element mapping from table name
- */
- public static $mapTableToElement = array(
- 'actioncomm' => 'agenda',
- 'adherent' => 'member',
- 'adherent_type' => 'member_type',
- //'bank_account' => 'bank_account',
- 'categorie' => 'category',
- //'commande' => 'commande',
- //'commande_fournisseur' => 'commande_fournisseur',
- 'contrat' => 'contract',
- 'entrepot' => 'stock',
- //'expensereport' => 'expensereport',
- 'facture' => 'invoice',
- //'facture_fourn' => 'facture_fourn',
- 'fichinter' => 'intervention',
- //'holiday' => 'holiday',
- //'product' => 'product',
- 'product_price' => 'productprice',
- 'product_fournisseur_price' => 'productsupplierprice',
- 'projet' => 'project',
- //'propal' => 'propal',
- //'societe' => 'societe',
- 'socpeople' => 'contact',
- //'supplier_proposal' => 'supplier_proposal',
- //'ticket' => 'ticket',
- );
-
- /**
- * Constructor
- */
- public function __construct()
- {
- global $hookmanager;
-
- if (is_object($hookmanager)) {
- $hookmanager->initHooks(array('import'));
- $parameters = array();
- $reshook = $hookmanager->executeHooks('constructModeleImports', $parameters, $this);
- if ($reshook >= 0 && !empty($hookmanager->resArray)) {
- foreach ($hookmanager->resArray as $mapList) {
- self::$mapTableToElement[$mapList['table']] = $mapList['element'];
- }
- }
- }
- }
+
+ /**
+ * Constructor
+ */
+ public function __construct()
+ {
+ }
+
@@ -169,5 +90,5 @@
- * @return int Id
- */
- public function getDriverId()
- {
- return $this->id;
+ * @return string Id
+ */
+ public function getDriverId()
+ {
+ return $this->id;
@@ -181,3 +102,3 @@
- public function getDriverLabel()
- {
- return $this->label;
+ public function getDriverLabel()
+ {
+ return $this->label;
@@ -191,3 +112,3 @@
- public function getDriverDesc()
- {
- return $this->desc;
+ public function getDriverDesc()
+ {
+ return $this->desc;
@@ -201,3 +122,3 @@
- public function getDriverExtension()
- {
- return $this->extension;
+ public function getDriverExtension()
+ {
+ return $this->extension;
@@ -211,3 +132,3 @@
- public function getDriverVersion()
- {
- return $this->version;
+ public function getDriverVersion()
+ {
+ return $this->version;
@@ -221,3 +142,3 @@
- public function getLibLabel()
- {
- return $this->label_lib;
+ public function getLibLabel()
+ {
+ return $this->label_lib;
@@ -231,16 +152,18 @@
- public function getLibVersion()
- {
- return $this->version_lib;
- }
-
-
- /**
- * Load into memory list of available import format
- *
- * @param DoliDB $db Database handler
- * @param integer $maxfilenamelength Max length of value to show
- * @return array List of templates
- */
- public function listOfAvailableImportFormat($db, $maxfilenamelength = 0)
- {
- dol_syslog(get_class($this)."::listOfAvailableImportFormat");
+ 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");
@@ -253,26 +176,29 @@
- 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++;
+ }
+ }
+ }
@@ -290 +216 @@
- public function getPictoForKey($key)
+ public function getPictoForKey($key)
@@ -296,6 +222,6 @@
- * Return label of driver import
- *
- * @param string $key Key
- * @return string
- */
- public function getDriverLabelForKey($key)
+ * Renvoi libelle d'un driver import
+ *
+ * @param string $key Key
+ * @return string
+ */
+ public function getDriverLabelForKey($key)
@@ -307,6 +233,6 @@
- * Return description of import drivervoi la description d'un driver import
- *
- * @param string $key Key
- * @return string
- */
- public function getDriverDescForKey($key)
+ * Renvoi la description d'un driver import
+ *
+ * @param string $key Key
+ * @return string
+ */
+ public function getDriverDescForKey($key)
@@ -323 +249 @@
- public function getDriverVersionForKey($key)
+ public function getDriverVersionForKey($key)
@@ -334 +260 @@
- public function getLibLabelForKey($key)
+ public function getLibLabelForKey($key)
@@ -345 +271 @@
- public function getLibVersionForKey($key)
+ public function getLibVersionForKey($key)
@@ -349,18 +274,0 @@
-
- /**
- * Get element from table name with prefix
- *
- * @param string $tableNameWithPrefix Table name with prefix
- * @return string Element name or table element as default
- */
- public function getElementFromTableWithPrefix($tableNameWithPrefix)
- {
- $tableElement = preg_replace('/^'.preg_quote($this->db->prefix(), '/').'/', '', $tableNameWithPrefix);
- $element = $tableElement;
-
- if (isset(self::$mapTableToElement[$tableElement])) {
- $element = self::$mapTableToElement[$tableElement];
- }
-
- return $element;
- }