Code comment
This commit is contained in:
parent
fe103f0492
commit
8429de031f
@ -54,7 +54,7 @@ class ImportCsv extends ModeleImports
|
|||||||
|
|
||||||
var $cacheconvert=array(); // Array to cache list of value found after a convertion
|
var $cacheconvert=array(); // Array to cache list of value found after a convertion
|
||||||
var $cachefieldtable=array(); // Array to cache list of value found into fields@tables
|
var $cachefieldtable=array(); // Array to cache list of value found into fields@tables
|
||||||
|
|
||||||
var $nbinsert = 0; // # of insert done during the import
|
var $nbinsert = 0; // # of insert done during the import
|
||||||
var $nbupdate = 0; // # of update done during the import
|
var $nbupdate = 0; // # of update done during the import
|
||||||
|
|
||||||
@ -170,7 +170,7 @@ class ImportCsv extends ModeleImports
|
|||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return nb of records. File must be closed.
|
* Return nb of records. File must be closed.
|
||||||
*
|
*
|
||||||
@ -181,7 +181,7 @@ class ImportCsv extends ModeleImports
|
|||||||
{
|
{
|
||||||
return dol_count_nb_of_line($file);
|
return dol_count_nb_of_line($file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Input header line from file
|
* Input header line from file
|
||||||
@ -269,7 +269,7 @@ class ImportCsv extends ModeleImports
|
|||||||
* @param Object $objimport Object import (contains objimport->array_import_tables, objimport->array_import_fields, objimport->array_import_convertvalue, ...)
|
* @param Object $objimport Object import (contains objimport->array_import_tables, objimport->array_import_fields, objimport->array_import_convertvalue, ...)
|
||||||
* @param int $maxfields Max number of fields to use
|
* @param int $maxfields Max number of fields to use
|
||||||
* @param string $importid Import key
|
* @param string $importid Import key
|
||||||
* @param array $updatekeys Array of keys to use to try to do update
|
* @param array $updatekeys Array of keys to use to try to do an update first before insert. This field are defined into the module descriptor.
|
||||||
* @return int <0 if KO, >0 if OK
|
* @return int <0 if KO, >0 if OK
|
||||||
*/
|
*/
|
||||||
function import_insert($arrayrecord,$array_match_file_to_database,$objimport,$maxfields,$importid,$updatekeys)
|
function import_insert($arrayrecord,$array_match_file_to_database,$objimport,$maxfields,$importid,$updatekeys)
|
||||||
@ -375,7 +375,7 @@ class ImportCsv extends ModeleImports
|
|||||||
if (! is_numeric($newval) && $newval != '' && ! preg_match('/^id:/i',$newval)) $isidorref='ref';
|
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
|
$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."<br>\n";
|
//print 'Val is now '.$newval.' and is type '.$isidorref."<br>\n";
|
||||||
|
|
||||||
if ($isidorref == 'ref') // If value into input import file is a ref, we apply the function defined into descriptor
|
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']);
|
$file=(empty($objimport->array_import_convertvalue[0][$val]['classfile'])?$objimport->array_import_convertvalue[0][$val]['file']:$objimport->array_import_convertvalue[0][$val]['classfile']);
|
||||||
@ -478,9 +478,9 @@ class ImportCsv extends ModeleImports
|
|||||||
}
|
}
|
||||||
if (is_numeric($defaultref) && $defaultref <= 0) $defaultref='';
|
if (is_numeric($defaultref) && $defaultref <= 0) $defaultref='';
|
||||||
$newval=$defaultref;
|
$newval=$defaultref;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
elseif ($objimport->array_import_convertvalue[0][$val]['rule']=='numeric')
|
elseif ($objimport->array_import_convertvalue[0][$val]['rule']=='numeric')
|
||||||
{
|
{
|
||||||
$newval = price2num($newval);
|
$newval = price2num($newval);
|
||||||
@ -588,11 +588,12 @@ class ImportCsv extends ModeleImports
|
|||||||
{
|
{
|
||||||
$updatedone = false;
|
$updatedone = false;
|
||||||
$insertdone = false;
|
$insertdone = false;
|
||||||
|
|
||||||
if (!empty($updatekeys)) {
|
if (!empty($updatekeys)) {
|
||||||
// We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
|
// We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
|
||||||
if (empty($lastinsertid)) {
|
if (empty($lastinsertid)) {
|
||||||
$sqlSelect = 'SELECT rowid FROM '.$tablename;
|
$sqlSelect = 'SELECT rowid FROM '.$tablename;
|
||||||
|
|
||||||
$data = array_combine($listfields, $listvalues);
|
$data = array_combine($listfields, $listvalues);
|
||||||
$where = array();
|
$where = array();
|
||||||
$filters = array();
|
$filters = array();
|
||||||
@ -603,7 +604,7 @@ class ImportCsv extends ModeleImports
|
|||||||
$filters[] = $col.' = '.$data[$key];
|
$filters[] = $col.' = '.$data[$key];
|
||||||
}
|
}
|
||||||
$sqlSelect.= ' WHERE '.implode(' AND ', $where);
|
$sqlSelect.= ' WHERE '.implode(' AND ', $where);
|
||||||
|
|
||||||
$resql=$this->db->query($sqlSelect);
|
$resql=$this->db->query($sqlSelect);
|
||||||
if($resql) {
|
if($resql) {
|
||||||
$res = $this->db->fetch_object($resql);
|
$res = $this->db->fetch_object($resql);
|
||||||
@ -626,23 +627,23 @@ class ImportCsv extends ModeleImports
|
|||||||
$error++;
|
$error++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($lastinsertid)) {
|
if (!empty($lastinsertid)) {
|
||||||
// Build SQL UPDATE request
|
// Build SQL UPDATE request
|
||||||
$sqlstart = 'UPDATE '.$tablename;
|
$sqlstart = 'UPDATE '.$tablename;
|
||||||
|
|
||||||
$data = array_combine($listfields, $listvalues);
|
$data = array_combine($listfields, $listvalues);
|
||||||
$set = array();
|
$set = array();
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
$set[] = $key.' = '.$val;
|
$set[] = $key.' = '.$val;
|
||||||
}
|
}
|
||||||
$sqlstart.= ' SET '.implode(', ', $set);
|
$sqlstart.= ' SET '.implode(', ', $set);
|
||||||
|
|
||||||
if(empty($keyfield)) $keyfield = 'rowid';
|
if(empty($keyfield)) $keyfield = 'rowid';
|
||||||
$sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid;
|
$sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid;
|
||||||
|
|
||||||
$sql = $sqlstart.$sqlend;
|
$sql = $sqlstart.$sqlend;
|
||||||
|
|
||||||
// Run update request
|
// Run update request
|
||||||
$resql=$this->db->query($sql);
|
$resql=$this->db->query($sql);
|
||||||
if($resql) {
|
if($resql) {
|
||||||
@ -667,14 +668,14 @@ class ImportCsv extends ModeleImports
|
|||||||
if (! empty($tablewithentity_cache[$tablename])) {
|
if (! empty($tablewithentity_cache[$tablename])) {
|
||||||
$sqlstart.= ', entity';
|
$sqlstart.= ', entity';
|
||||||
$sqlend.= ', '.$conf->entity;
|
$sqlend.= ', '.$conf->entity;
|
||||||
}
|
}
|
||||||
if (! empty($objimport->array_import_tables_creator[0][$alias])) {
|
if (! empty($objimport->array_import_tables_creator[0][$alias])) {
|
||||||
$sqlstart.= ', '.$objimport->array_import_tables_creator[0][$alias];
|
$sqlstart.= ', '.$objimport->array_import_tables_creator[0][$alias];
|
||||||
$sqlend.=', '.$user->id;
|
$sqlend.=', '.$user->id;
|
||||||
}
|
}
|
||||||
$sql = $sqlstart.$sqlend.')';
|
$sql = $sqlstart.$sqlend.')';
|
||||||
dol_syslog("import_csv.modules", LOG_DEBUG);
|
dol_syslog("import_csv.modules", LOG_DEBUG);
|
||||||
|
|
||||||
// Run insert request
|
// Run insert request
|
||||||
if ($sql)
|
if ($sql)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -54,10 +54,10 @@ class ImportXlsx extends ModeleImports
|
|||||||
|
|
||||||
var $cacheconvert=array(); // Array to cache list of value found after a convertion
|
var $cacheconvert=array(); // Array to cache list of value found after a convertion
|
||||||
var $cachefieldtable=array(); // Array to cache list of value found into fields@tables
|
var $cachefieldtable=array(); // Array to cache list of value found into fields@tables
|
||||||
|
|
||||||
var $workbook; // temporary import file
|
var $workbook; // temporary import file
|
||||||
var $record; // current record
|
var $record; // current record
|
||||||
var $headers;
|
var $headers;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -95,7 +95,7 @@ class ImportXlsx extends ModeleImports
|
|||||||
if (preg_match('/^societe_/',$datatoimport)) $this->thirpartyobject=new Societe($this->db);
|
if (preg_match('/^societe_/',$datatoimport)) $this->thirpartyobject=new Societe($this->db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Output header of an example file for this format
|
* Output header of an example file for this format
|
||||||
*
|
*
|
||||||
@ -136,17 +136,18 @@ class ImportXlsx extends ModeleImports
|
|||||||
*/
|
*/
|
||||||
function write_title_example($outputlangs,$headerlinefields)
|
function write_title_example($outputlangs,$headerlinefields)
|
||||||
{
|
{
|
||||||
global $conf;
|
global $conf;
|
||||||
$this->workbook->getActiveSheet()->getStyle('1')->getFont()->setBold(true);
|
$this->workbook->getActiveSheet()->getStyle('1')->getFont()->setBold(true);
|
||||||
$this->workbook->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
|
$this->workbook->getActiveSheet()->getStyle('1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
|
||||||
|
|
||||||
|
$col = 0;
|
||||||
|
foreach($headerlinefields as $field) {
|
||||||
|
$this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, 1, $outputlangs->transnoentities($field));
|
||||||
|
// set autowidth
|
||||||
|
//$this->workbook->getActiveSheet()->getColumnDimension($this->column2Letter($col + 1))->setAutoSize(true);
|
||||||
|
$col++;
|
||||||
|
}
|
||||||
|
|
||||||
$col = 0;
|
|
||||||
foreach($headerlinefields as $field) {
|
|
||||||
$this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, 1, $outputlangs->transnoentities($field));
|
|
||||||
// set autowidth
|
|
||||||
//$this->workbook->getActiveSheet()->getColumnDimension($this->column2Letter($col + 1))->setAutoSize(true);
|
|
||||||
$col++;
|
|
||||||
}
|
|
||||||
return ''; // final output will be generated in footer
|
return ''; // final output will be generated in footer
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -159,13 +160,14 @@ class ImportXlsx extends ModeleImports
|
|||||||
*/
|
*/
|
||||||
function write_record_example($outputlangs,$contentlinevalues)
|
function write_record_example($outputlangs,$contentlinevalues)
|
||||||
{
|
{
|
||||||
$col = 0;
|
$col = 0;
|
||||||
$row = 2;
|
$row = 2;
|
||||||
foreach($contentlinevalues as $cell) {
|
foreach($contentlinevalues as $cell) {
|
||||||
$this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, $row, $cell);
|
$this->workbook->getActiveSheet()->SetCellValueByColumnAndRow($col, $row, $cell);
|
||||||
$col++;
|
$col++;
|
||||||
}
|
}
|
||||||
return ''; // final output will be generated in footer
|
|
||||||
|
return ''; // final output will be generated in footer
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -177,15 +179,15 @@ class ImportXlsx extends ModeleImports
|
|||||||
function write_footer_example($outputlangs)
|
function write_footer_example($outputlangs)
|
||||||
{
|
{
|
||||||
// return te file content as a string
|
// return te file content as a string
|
||||||
$tempfile = tempnam(sys_get_temp_dir(), 'dol');
|
$tempfile = tempnam(sys_get_temp_dir(), 'dol');
|
||||||
$objWriter = new PHPExcel_Writer_Excel2007($this->workbook);
|
$objWriter = new PHPExcel_Writer_Excel2007($this->workbook);
|
||||||
$objWriter->save($tempfile);
|
$objWriter->save($tempfile);
|
||||||
$this->workbook->disconnectWorksheets();
|
$this->workbook->disconnectWorksheets();
|
||||||
unset($this->workbook);
|
unset($this->workbook);
|
||||||
|
|
||||||
$content = file_get_contents($tempfile);
|
$content = file_get_contents($tempfile);
|
||||||
unlink($tempfile);
|
unlink($tempfile);
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -211,10 +213,10 @@ class ImportXlsx extends ModeleImports
|
|||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return nb of records. File must be closed.
|
* Return nb of records. File must be closed.
|
||||||
*
|
*
|
||||||
* @param string $file Path of filename
|
* @param string $file Path of filename
|
||||||
* @return int <0 if KO, >=0 if OK
|
* @return int <0 if KO, >=0 if OK
|
||||||
*/
|
*/
|
||||||
@ -222,15 +224,15 @@ class ImportXlsx extends ModeleImports
|
|||||||
{
|
{
|
||||||
$reader = new PHPExcel_Reader_Excel2007();
|
$reader = new PHPExcel_Reader_Excel2007();
|
||||||
$this->workbook = $reader->load($file);
|
$this->workbook = $reader->load($file);
|
||||||
|
|
||||||
$rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
|
$rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
|
||||||
|
|
||||||
$this->workbook->disconnectWorksheets();
|
$this->workbook->disconnectWorksheets();
|
||||||
unset($this->workbook);
|
unset($this->workbook);
|
||||||
|
|
||||||
return $rowcount;
|
return $rowcount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Input header line from file
|
* Input header line from file
|
||||||
@ -292,7 +294,7 @@ class ImportXlsx extends ModeleImports
|
|||||||
* @param Object $objimport Object import (contains objimport->array_import_tables, objimport->array_import_fields, objimport->array_import_convertvalue, ...)
|
* @param Object $objimport Object import (contains objimport->array_import_tables, objimport->array_import_fields, objimport->array_import_convertvalue, ...)
|
||||||
* @param int $maxfields Max number of fields to use
|
* @param int $maxfields Max number of fields to use
|
||||||
* @param string $importid Import key
|
* @param string $importid Import key
|
||||||
* @param array $updatekeys Array of keys to use to try to do update
|
* @param array $updatekeys Array of keys to use to try to do an update first before insert. This field are defined into the module descriptor.
|
||||||
* @return int <0 if KO, >0 if OK
|
* @return int <0 if KO, >0 if OK
|
||||||
*/
|
*/
|
||||||
// What is this doing here ? it is common to all imports, is should be in the parent class
|
// What is this doing here ? it is common to all imports, is should be in the parent class
|
||||||
@ -399,7 +401,7 @@ class ImportXlsx extends ModeleImports
|
|||||||
if (! is_numeric($newval) && $newval != '' && ! preg_match('/^id:/i',$newval)) $isidorref='ref';
|
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
|
$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."<br>\n";
|
//print 'Val is now '.$newval.' and is type '.$isidorref."<br>\n";
|
||||||
|
|
||||||
if ($isidorref == 'ref') // If value into input import file is a ref, we apply the function defined into descriptor
|
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']);
|
$file=(empty($objimport->array_import_convertvalue[0][$val]['classfile'])?$objimport->array_import_convertvalue[0][$val]['file']:$objimport->array_import_convertvalue[0][$val]['classfile']);
|
||||||
@ -502,9 +504,9 @@ class ImportXlsx extends ModeleImports
|
|||||||
}
|
}
|
||||||
if (is_numeric($defaultref) && $defaultref <= 0) $defaultref='';
|
if (is_numeric($defaultref) && $defaultref <= 0) $defaultref='';
|
||||||
$newval=$defaultref;
|
$newval=$defaultref;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
elseif ($objimport->array_import_convertvalue[0][$val]['rule']=='numeric')
|
elseif ($objimport->array_import_convertvalue[0][$val]['rule']=='numeric')
|
||||||
{
|
{
|
||||||
$newval = price2num($newval);
|
$newval = price2num($newval);
|
||||||
@ -616,7 +618,7 @@ class ImportXlsx extends ModeleImports
|
|||||||
// We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
|
// We do SELECT to get the rowid, if we already have the rowid, it's to be used below for related tables (extrafields)
|
||||||
if (empty($lastinsertid)) {
|
if (empty($lastinsertid)) {
|
||||||
$sqlSelect = 'SELECT rowid FROM '.$tablename;
|
$sqlSelect = 'SELECT rowid FROM '.$tablename;
|
||||||
|
|
||||||
$data = array_combine($listfields, $listvalues);
|
$data = array_combine($listfields, $listvalues);
|
||||||
$where = array();
|
$where = array();
|
||||||
$filters = array();
|
$filters = array();
|
||||||
@ -627,7 +629,7 @@ class ImportXlsx extends ModeleImports
|
|||||||
$filters[] = $col.' = '.$data[$key];
|
$filters[] = $col.' = '.$data[$key];
|
||||||
}
|
}
|
||||||
$sqlSelect.= ' WHERE '.implode(' AND ', $where);
|
$sqlSelect.= ' WHERE '.implode(' AND ', $where);
|
||||||
|
|
||||||
$resql=$this->db->query($sqlSelect);
|
$resql=$this->db->query($sqlSelect);
|
||||||
if($resql) {
|
if($resql) {
|
||||||
$res = $this->db->fetch_object($resql);
|
$res = $this->db->fetch_object($resql);
|
||||||
@ -650,23 +652,23 @@ class ImportXlsx extends ModeleImports
|
|||||||
$error++;
|
$error++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($lastinsertid)) {
|
if (!empty($lastinsertid)) {
|
||||||
// Build SQL UPDATE request
|
// Build SQL UPDATE request
|
||||||
$sqlstart = 'UPDATE '.$tablename;
|
$sqlstart = 'UPDATE '.$tablename;
|
||||||
|
|
||||||
$data = array_combine($listfields, $listvalues);
|
$data = array_combine($listfields, $listvalues);
|
||||||
$set = array();
|
$set = array();
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
$set[] = $key.' = '.$val;
|
$set[] = $key.' = '.$val;
|
||||||
}
|
}
|
||||||
$sqlstart.= ' SET '.implode(', ', $set);
|
$sqlstart.= ' SET '.implode(', ', $set);
|
||||||
|
|
||||||
if(empty($keyfield)) $keyfield = 'rowid';
|
if(empty($keyfield)) $keyfield = 'rowid';
|
||||||
$sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid;
|
$sqlend = ' WHERE '.$keyfield.' = '.$lastinsertid;
|
||||||
|
|
||||||
$sql = $sqlstart.$sqlend;
|
$sql = $sqlstart.$sqlend;
|
||||||
|
|
||||||
// Run update request
|
// Run update request
|
||||||
$resql=$this->db->query($sql);
|
$resql=$this->db->query($sql);
|
||||||
if($resql) {
|
if($resql) {
|
||||||
@ -691,14 +693,14 @@ class ImportXlsx extends ModeleImports
|
|||||||
if (! empty($tablewithentity_cache[$tablename])) {
|
if (! empty($tablewithentity_cache[$tablename])) {
|
||||||
$sqlstart.= ', entity';
|
$sqlstart.= ', entity';
|
||||||
$sqlend.= ', '.$conf->entity;
|
$sqlend.= ', '.$conf->entity;
|
||||||
}
|
}
|
||||||
if (! empty($objimport->array_import_tables_creator[0][$alias])) {
|
if (! empty($objimport->array_import_tables_creator[0][$alias])) {
|
||||||
$sqlstart.= ', '.$objimport->array_import_tables_creator[0][$alias];
|
$sqlstart.= ', '.$objimport->array_import_tables_creator[0][$alias];
|
||||||
$sqlend.=', '.$user->id;
|
$sqlend.=', '.$user->id;
|
||||||
}
|
}
|
||||||
$sql = $sqlstart.$sqlend.')';
|
$sql = $sqlstart.$sqlend.')';
|
||||||
dol_syslog("import_xlsx.modules", LOG_DEBUG);
|
dol_syslog("import_xlsx.modules", LOG_DEBUG);
|
||||||
|
|
||||||
// Run insert request
|
// Run insert request
|
||||||
if ($sql)
|
if ($sql)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user