ソースを参照

fix file scanner

Jad Meouchy 9 年 前
コミット
69eb94aa5b
3 ファイル変更60 行追加11 行削除
  1. 48 10
      api/cron-filescanner.php
  2. 12 1
      api/db.php
  3. 0 0
      data/create subdirectories for companies.txt

+ 48 - 10
api/loadXLS.php → api/cron-filescanner.php

@@ -1,7 +1,7 @@
 <?php
 
 include 'phpexcel/PHPExcel.php';
-include 'db.inc.php';
+include 'db.php';
 
 ini_set('memory_limit', '256M');
 
@@ -38,12 +38,48 @@ $columns_to_fields = array(
 $fields_by_col = array();
 
 
-$id_company = 1;
+$path = '../data';
 
 
-//TODO: scan respective directories for new files
+//scan respective directories for new files
+$companies = getCompanyDirectories();
+
+for($c = 0; $c < count($companies); $c++)
+{
+	$company = $companies[$c];
+	
+	$id_company = $company['id_company'];
+	$name = $company['name'];
+	$ftp_directory = $company['ftp_directory'];
+	
+	$ftp_full_path = "$path/$ftp_directory";
+	
+	
+	echo "scanning #$id_company $name\t$ftp_full_path:\t";
+	
+	
+	//check if the directory exists
+	if(!file_exists($ftp_full_path) || !is_dir($ftp_full_path))
+	{
+		echo "directory not found\n";
+		continue;
+	}
+
+	
+	//find all spreadsheets
+	$files = glob("$ftp_full_path/*.xls");
+	
+	echo 'found ' . count($files) . ' excel spreadsheet(s)' . PHP_EOL;
+	
+	
+	//go through each one
+	foreach($files as $filename)
+	{
+		processFile($filename, $id_company);
+		echo PHP_EOL;
+	}
+}
 
-processFile('../test.xls', $id_company);
 
 
 function processFile($filename, $id_company)
@@ -52,11 +88,13 @@ function processFile($filename, $id_company)
 	
 	if(!file_exists($filename))
 	{
-		echo "file doesn't exist\n";
+		echo "file doesn't exist" . PHP_EOL;
 		return;
 	}
 	
 	
+	
+	
 	$file_size = filesize($filename);
 	$file_hash = md5_file($filename);
 	
@@ -84,7 +122,7 @@ function processFile($filename, $id_company)
 	if(count($rows))
 	{
 		$id_file = $rows[0]['id_file'];
-		echo "re-running file #$id_file\n";
+		echo "re-processing file #$id_file" . PHP_EOL;
 	}
 	else
 	{
@@ -94,14 +132,14 @@ function processFile($filename, $id_company)
 		
 		if(!$stmt_file->execute($params))
 		{
-			echo "\nPDO::errorInfo():\n";
+			echo "PDO::errorInfo():" . PHP_EOL;
 			print_r($dbh->errorInfo());
 			return;
 		}
 		
 		$id_file = $dbh->lastInsertId();
 		
-		echo "processing new file #$id_file\n";
+		echo "processing new file #$id_file" . PHP_EOL;
 	}
 
 	
@@ -132,7 +170,7 @@ function processFile($filename, $id_company)
 		
 	if(!$stmt_row)
 	{
-		echo "\nPDO::errorInfo():\n";
+		echo "PDO::errorInfo():" . PHP_EOL;
 		print_r($dbh->errorInfo());
 		return;
 	}
@@ -156,7 +194,7 @@ function processFile($filename, $id_company)
 		
 		$reader->setReadDataOnly(false);
 		
-		echo "Report detected as: $type\n";
+		echo "xls detected as: $type" . PHP_EOL;
 		
 		// load the file
 		$excel = $reader->load($filename);

+ 12 - 1
api/db.php

@@ -11,7 +11,7 @@ function getCompanies()
 	global $dbh;
 	
 	$stmt = $dbh->prepare(
-		"SELECT id_company, name, description
+		"SELECT id_company, name, ftp_directory, description
 		FROM company");
 								
 	$stmt->execute();
@@ -21,6 +21,17 @@ function getCompanies()
 	return $rows;
 }
 
+function getCompanyDirectories()
+{
+	global $dbh;
+	
+	$stmt = $dbh->prepare("SELECT id_company, name, ftp_directory FROM company");
+	$stmt->execute();
+	$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
+	
+	return $rows;
+}
+
 function getCompanyData($id_company)
 {
 	global $dbh;

+ 0 - 0
data/create subdirectories for companies.txt