From 03f43d7c5561feb2276585a3028e831e74df1a40 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Mon, 11 Apr 2016 15:19:58 +0200
Subject: [PATCH] Removed a TODO by moving detection of line of records of a
 file to import into the import driver.

---
 .../modules/import/import_csv.modules.php     | 13 +++++++++++
 .../modules/import/import_xlsx.modules.php    | 23 +++++++++++++++++++
 htdocs/imports/import.php                     |  4 ++--
 3 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php
index 8f25a38d322..85689edcf7c 100644
--- a/htdocs/core/modules/import/import_csv.modules.php
+++ b/htdocs/core/modules/import/import_csv.modules.php
@@ -49,6 +49,7 @@ class ImportCsv extends ModeleImports
 
 	var $separator;
 
+	var $file;      // Path of file
 	var $handle;    // Handle fichier
 
 	var $cacheconvert=array();      // Array to cache list of value found after a convertion
@@ -236,6 +237,18 @@ class ImportCsv extends ModeleImports
 		return $ret;
 	}
 
+	
+	/**
+	 * 	Return nb of records. File must be closed.
+	 *
+	 * 	@return		int		<0 if KO, >=0 if OK
+	 */
+	function import_get_nb_of_lines($file)
+	{
+	   return dol_count_nb_of_line($file);
+    }
+    
+
 	/**
 	 * 	Input header line from file
 	 *
diff --git a/htdocs/core/modules/import/import_xlsx.modules.php b/htdocs/core/modules/import/import_xlsx.modules.php
index 34139820ba8..a821540f29b 100644
--- a/htdocs/core/modules/import/import_xlsx.modules.php
+++ b/htdocs/core/modules/import/import_xlsx.modules.php
@@ -49,6 +49,7 @@ class Importxlsx extends ModeleImports
 
 	var $separator;
 
+    var $file;      // Path of file
 	var $handle;    // Handle fichier
 
 	var $cacheconvert=array();      // Array to cache list of value found after a convertion
@@ -276,9 +277,31 @@ class Importxlsx extends ModeleImports
 		$reader = new PHPExcel_Reader_Excel2007();
 		$this->workbook = $reader->load($file);
 		$this->record = 1;
+		$this->file = $file;
+
 		return $ret;
 	}
 
+	
+	/**
+	 * 	Return nb of records. File must be closed.
+	 *
+	 * 	@return		int		<0 if KO, >=0 if OK
+	 */
+	function import_get_nb_of_lines($file)
+	{
+		$reader = new PHPExcel_Reader_Excel2007();
+		$this->workbook = $reader->load($file);
+	    
+	    $rowcount = $this->workbook->getActiveSheet()->getHighestDataRow();
+	    
+	    $this->workbook->disconnectWorksheets();
+	    unset($this->workbook);
+	    
+		return $rowcount;
+    }
+    
+
 	/**
 	 * 	Input header line from file
 	 *
diff --git a/htdocs/imports/import.php b/htdocs/imports/import.php
index 23d5a61415b..eae01880f90 100644
--- a/htdocs/imports/import.php
+++ b/htdocs/imports/import.php
@@ -1142,6 +1142,7 @@ if ($step == 5 && $datatoimport)
 	// Load source fields in input file
 	$fieldssource=array();
 	$result=$obj->import_open_file($conf->import->dir_temp.'/'.$filetoimport,$langs);
+
 	if ($result >= 0)
 	{
 		// Read first line
@@ -1156,8 +1157,7 @@ if ($step == 5 && $datatoimport)
 		$obj->import_close_file();
 	}
 
-  // TODO, remove this, since it only works for csv
-	$nboflines=dol_count_nb_of_line($conf->import->dir_temp.'/'.$filetoimport);
+	$nboflines=$obj->import_get_nb_of_lines($conf->import->dir_temp.'/'.$filetoimport);
 
 	$param='&leftmenu=import&format='.$format.'&datatoimport='.$datatoimport.'&filetoimport='.urlencode($filetoimport).'&nboflines='.$nboflines.'&separator='.urlencode($separator).'&enclosure='.urlencode($enclosure);
 	$param2 = $param;  // $param2 = $param without excludefirstline and endatlinenb
-- 
GitLab