view get_coordinates_for_listed_books.php @ 18:7d147b03db12

add default page
author Calvin Yeh <cyeh@mpipw-berlin.mpg.com>
date Thu, 23 Mar 2017 11:13:23 +0100
parents 30b59e7b88c0
children
line wrap: on
line source

<?php
include_once('config/Lib_mb_utf8.php');
include_once('config/config.php');

set_time_limit(0);
ini_set('memory_limit', '-1');

$link_mysql = mysql_connect($mysql_server, $mysql_user, $mysql_password);
mysql_query("SET NAMES utf8");

if (!$link_mysql) {
        die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db($mysql_database, $link_mysql);
if (!$db_selected) {
        die ('Can\'t use foo : ' . mysql_error());
}

function readCsvFile($fileName){
	global $server_host;

	if ($fp = fopen($server_host."LGSearch/csv_files/".$fileName,"r") ) {

		// $fp=fopen($server_host."LGSearch/csv_files/".$fileName,"r");
		$data=fgetcsv($fp);
		$columnNameMappingArray=['BOOK_ID','LEVEL1','LEVEL2',
	                'Name','PERIOD','TimeSpan:begin','TimeSpan:end','PAGE','SECTION','CONTENT',
	                'Description']; //columns from input array/file
		$columnNameArray=['Address','LEVEL1','LEVEL2',
	                'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
			'PAGE','SECTION','CONTENT',
	                'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; //columns for output array/file (for map.php)
	    /*
		$columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2',
	                'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
			'PAGE','SECTION','CONTENT',
	                'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; //columns for output array/file (for map.php)
		*/
		$columnNameArray=array_flip($columnNameArray); //key, value swap in order to keep the order of the columns after merge
		$bookArray=array();
		$count=0;
		$line=0;
		while(!feof($fp)){
			$data=fgetcsv($fp);
			if(sizeof($data)<sizeof($columnNameMappingArray)){
				continue;
			}
			///$bookId=$data[0];
	                foreach($columnNameMappingArray as $idx=>$newName){ //use column names as the array indices (instead of 0,1,2,... as from $data)
	                        $bookInfo[$newName]=$data[$idx];
	                }
	                $bookId=$bookInfo['BOOK_ID'];
			$coordinateArray=getCoordinateFromDatabase($bookId);
			foreach($coordinateArray as $coordinate){ //merge a record from $booArray and ots cooresponding coordinateArray
				$bookArray[$count]=array_merge($bookInfo,$coordinate);
				$bookArray[$count]=array_merge($columnNameArray,$bookArray[$count]);	
				$count++;
			}
		}
		fclose($fp);

	} else {
		// file open failed
    	error_log("error when opening file: ".$server_host."LGSearch/csv_files/".$fileName, 0);
		return array();
	}

	return $bookArray;
}
function writeCsvFile($fileName,$bookArray){
	/*
	$columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2',
                'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
		'PAGE','SECTION','CONTENT',
                'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description'];
    */
	$columnNameArray=['Address','LEVEL1','LEVEL2',
                'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
		'PAGE','SECTION','CONTENT',
                'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description'];

	if ($fp=fopen("./datasets/".$fileName,"w")) {

		//$fp=fopen("./datasets/".$fileName,"w");
		fputcsv($fp,$columnNameArray);
	
		foreach($bookArray as $book){
			fputcsv($fp,$book);	
		}
		fclose($fp);
	} else {
    	error_log("error when opening file: "."./datasets/".$fileName, 0);
	}

}
function getCoordinateFromDatabase($bookId){
	/*
	$query="SELECT place_name AS Address, admin_type AS ADMIN_TYPE, x AS Longitude, y AS Latitude,
			chgis_id AS CHGIS_ID, 1820_id AS 1820_ID, 1911_id AS 1911_ID, cbdb_id AS CBDB_ID 
			FROM coordinates_books 
			WHERE books_id='".$bookId."'";
	*/
	$query="SELECT place_name AS Address, x AS Longitude, y AS Latitude,
			chgis_id AS CHGIS_ID, 1820_id AS 1820_ID, 1911_id AS 1911_ID, cbdb_id AS CBDB_ID 
			FROM coordinates_books 
			WHERE books_id='".$bookId."'";
	$result = mysql_query($query);
	if (!$result) {
		echo mysql_error();
		return;
	}
	$coordinateArray=array();
	while ($row = mysql_fetch_assoc($result)) {
		$coordinateArray[]=$row;
	}
	return $coordinateArray;
}

$fileName=$_GET['file'];
$name=$_GET['name'];
$bookArray=readCsvFile($fileName);
writeCsvFile($fileName,$bookArray);
header("Location: map.php?mode=1&file=".$fileName."&name=".$name);
?>