CoolFace
Apppublic

kenken999/php

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
geocoding.php370 linesDownload Raw Back to public
1<?php2if( $_SERVER['PHP_SELF'] == '/'.GetTableLink("geocoding") )3{4	echo "<!DOCTYPE html>5		  <html>6			<head>7				<style type='text/css'>8					div#message {9						width: 300px;10						height: 50px auto;11						background: #fff;12						border-radius: 10px;13						margin: 100px auto 10px;14						box-shadow: 10px 20px 30px rgba(0,0,0,0.4);15						text-align: left;16						padding: 10px 20px 10px 20px;17						font: 14px Arial, sans-serif;18					}								19				</style>20			</head>21			<body style='background: #bbb'>				         22				<div id='message'> 23					<h3> How-to </h3>	24					<p> Add the following line to the 'Before process' event of your list page with a 'View as a Map' field: </p> 25					<hr />26					<p> &nbsp;&nbsp;include_once(\"geocoding.php\");</p>27					<hr />28					<p> Set for your 'View as a Map' field 'Address', 'Latitude', 'Longitude' field names. 29					 (You can also define them manually in the event's code.)</p>30					<p> Run you list page contains one 'View as a Map' field with the 'geocoding=1' parameter,  eg:</p>31					<hr />32					<p> &nbsp;&nbsp;".GetTableLink("map_list", "", "geocoding=1")."</p>33					<hr />34							35				</div>36			</body>    37		  </html>38	";  39	exit();40}41 42if( postvalue("geocoding") )43{44	global $cman, $strTableName, $strOriginalTableName;45	$gSettings = new ProjectSettings( $strTableName );46 47	$_connection = $cman->byTable( $strTableName );48	$origTableName = $strOriginalTableName;49	$goodTableName = goodFieldName($strTableName);50 51	$gData = $gSettings->getGeocodingData();52	$addressFields = array();53	54	if( !isset($addressField) )55		$addressFields = $gData["addressFields"];56	else57		$addressFields[] = $addressField;58		59	if( !isset($latField) ) 	60		$latField = $gData["latField"];61	62	if( !isset($lngField) )	63		$lngField = $gData["lngField"];64	65	// an address field is not defined or lat&lng fields are undefined while an address field is set 66	if( !isset($addressField) && !( $addressFields ) ||  $latField == "" && $lngField == "" ) 67	{  68		foreach($gSettings->getFieldsList() as $field)69		{70			if( $gSettings->getViewFormat($field) == 'Map' )71				$fieldsMapData[] = $gSettings->getMapData($field);72		}73		if( count($fieldsMapData) > 0 )74		{75			if( !( $addressFields ) )76				$addressFields[] = $fieldsMapData[0]['address'];77			78			if( $latField == "" )79				$latField = $fieldsMapData[0]['lat'];80				81			if( $lngField == "" )82				$lngField = $fieldsMapData[0]['lng'];83		}	84	}85	  86	if( !postvalue("ajax") ) 87	{88		if( !($addressFields) ||  $latField == ""  || $lngField == "" )89		{ 90			//some fields are empty91			echo "<!DOCTYPE html>92				  <html>93					<head>94						<style type='text/css'>95							div#message {96								width: 300px;97								height: 50px;98								background: #fff;99								border-radius: 10px;100								margin: 0px auto 10px;101								box-shadow: 10px 20px 30px rgba(0,0,0,0.4);102								text-align:center;103								padding: 30px;104								font: 16px Arial, sans-serif;105							}106							div#inf  {107								width: 300px;108								height: 60px;109								background: #fff;110								border-radius: 10px;111								margin: 100px auto 30px auto;112								padding: 10px 10px 10px 50px;113								box-shadow: 10px 20px 30px rgba(0,0,0,0.4);114								text-align: left;115								font: 16px Arial, sans-serif;				116							}									117						</style>118					</head>119					<body style='background: #bbb'>120						<div id='inf'> The 'Address' fields names: &nbsp;&nbsp;&nbsp;".(count($addressFields) ? implode(" ", $addressFields) : "&mdash;")121							."<br> The 'Latitude' field name:&nbsp;&nbsp;&nbsp; ".($latField ? $latField : "&mdash;")122							."<br> the 'Longtitude' field name: ".($lngField ? $lngField : "&mdash;")123						."<br></div>            124						<div id='message'> <p> Some field names were not defined </p> </div>125					</body>    126				  </html>127			";                128			exit();129		}	130		 131		$keyFields = array();132		$addresses = array();133	   134		$query = $gSettings->getSQLQuery();135	   136		$tmpWhere = $_connection->addFieldWrappers($latField).' is NULL or '.$_connection->addFieldWrappers($lngField).' is NULL';137	   138		$fieldType = $gSettings->getFieldType($latField);139		if( IsCharType($fieldType) )140			$tmpWhere.=' or '.$_connection->addFieldWrappers($latField).'=""';141		142		$fieldType = $gSettings->getFieldType($lngField);143		if( IsCharType($fieldType) )144			$tmpWhere.=' or '.$_connection->addFieldWrappers($lngField).'=""';145		146		$query->addWhere( $tmpWhere );147		  148		$qResult = $_connection->query( $query->tosql() ); 149		while( $data = $qResult->fetchAssoc() )150		{151			$address = "";152			foreach ($addressFields as $aField )153			{154				if ( isset( $data[ $aField ] ) && strlen( trim( $data[ $aField ] ) ) )155					$address.= trim( $data[ $aField ] ) . " ";156			}			157			158			$addresses[] = $address;159			160			$items = array();161			foreach($gSettings->getTableKeys() as $name)162			{163				$items[ $name ] = $data[ $name ];164			}165			$keyFields[] = $items;       166		}167		echo "<!DOCTYPE html>168		  <html>169			<head>170				<title> Geocoding </title>171				<script src='".GetRootPathForResources("include/jquery.js")."'></script>172				<script type='text/javascript'>173					function initialize() {174						var adresses = [],175							keyFields = [],176							promises = [],177							updates =[],178							errors = [], 179							i;180					   181						keyFields = ".my_json_encode($keyFields).";182						addresses = ".my_json_encode($addresses).";183 184						//traversing185						if (addresses.length > 0) {186							$('#progress p').text(addresses.length + ' address field(s) in progress');                   187							$('#progress').fadeIn();188					   189							for (i = 0; i < addresses.length; i++) {190								setTimeout( (function(i) {191									var d = $.Deferred();192									promises.push(d);193									194									return function() {195										var update = {196											keyfields: keyFields[i],197											addr: addresses[i]198										};    199										200										$('#bar').css('width', Math.round(100 * i/ (addresses.length - 1)) + '%');201										$('#counts').html((i + 1) + '/' + addresses.length + ' processed');								202										203										$.post('".GetTableLink( $goodTableName, "list")."', {ajax: 1, geocoding: 1, update: update}, function(status) {204											if (status != 'OK') {205												//Geocode was not successful206												207											    $('#error')208													.text( status )209													.fadeIn()210													.fadeOut();211													212												$('#error_report2')213													.show();214												215												errors.push('<br>&nbsp;&nbsp;' + status + ', ' + update.addr);216												217												$('div#error_list')218													.html( '<br>Geocode was not successful <br> for the&nbsp;' 219														+ errors.length + '&nbsp;address(es) <br> for the following reasons:' + errors.join('') );                                       220											}221											d.resolve();222										});  223									};224								})(i), 300 * i);225 226							}227							228							$.when.apply(null, promises).done( function() {229								setTimeout( function() {230									$('#progress').hide();                       231									$('#message')232										.hide()233										.fadeIn();234									235									$('#message p')236										.html('All done<br><br><br><button class=\'runner-btnframe\' type=\'button\' onclick=\"$(\'#message\').css(\'opacity\', 0);\">Ok</button><button class=\'runner-btnframe\' type=\'button\' onclick=\"window.location.href=\'".GetTableLink($goodTableName, "list", "geocoding=1")."\'\">Try again</button>');237									238									if (errors.length > 0) {239										$('#error_report2 span').hide();240										$('#error_list').html('<br>Geocode was not successful <br> for the&nbsp;'+ errors.length 241											+'&nbsp;address(es) <br> for the following reasons:' + errors.toString());							242									}243								}, 250);244							});		  245						} else {246							$('#message p').html('<br>All addresses were resolved before');247							$('#message').fadeIn();248						}249					}    250				</script>251				<style type='text/css'>       252					div.progress, div.message {253						width: 300px;254						height: 120px;255						background: #fff;256						border-radius: 10px;257						margin: 0px auto 10px;258						box-shadow: 10px 20px 30px rgba(0,0,0,0.4);259						text-align:center;260						padding: 30px;261						display: none;262						font: 16px Arial, sans-serif;263					}	264					div.bar-container {265						height: 24px;266						border: 1px solid #333;267						margin-bottom: 5px;268					}269					div.bar {               270						height: 100%;271						width: 0%;272						background: green;273						-webkit-transition: width 0.5s;274						-moz-transition: width 0.5s;275						-ms-transition: width 0.5s;276						-o-transition: width 0.5s;277						transition: width 0.5s;278					}279					div.error{280						display: none;281						margin: 5px;282					}283					div#error_report{284						text-align: left;285						height: auto;286					}287					div#error_report2{288						margin: 0 auto 10px;289						display: none;290						height: 150px auto;291						width: 360px;292						background: #fff;293						border-radius: 10px;294						box-shadow: 10px 20px 30px rgba(0,0,0,0.4);295						text-align: left;296						font: 16px Arial, sans-serif;	297					}298					div#error_list{299						padding-bottom: 10px;300						margin: 30px 30px auto 30px;301					}302					.runner-btnframe {303						display: inline-block;304						position: relative;305						white-space: nowrap;306						width: auto;307						z-index: 0;308						vertical-align: middle;309						margin: 0 4pt;310						font: 14px Arial, sans-serif;311						height: 30px;		312					}313					div#inf{314						width: 300px;315						height: 60px;316						background: #fff;317						border-radius: 10px;318						margin: 100px auto 30px auto;319						padding: 10px 10px 10px 50px;320						box-shadow: 10px 20px 30px rgba(0,0,0,0.4);321						text-align: left;322						font: 16px Arial, sans-serif;				323					}		324				</style>325			</head>326			<body style='background: #bbb;' onload='initialize()'>327				<div id='inf'>328				<p> Updating coordinates (<b>".$latField."</b> and <b>".$lngField."</b> fields) based on the <b>".implode(" ", $addressFields)."</b> fields values</p>	329				</div>330				<div id='progress' class='progress'>331					<p></p>332					<div id='bar-container' class='bar-container'>333						<div id='bar' class='bar'></div>334					</div>335					<div id='counts'></div>336					<div id='error' class='error'></div>337				</div>338				<div id='message' class='message'>339					<p></p>340				</div>341				<div id='error_report2'>342					<div id='error_list'></div>343				</div>344			</body>345		</html>346		";347		exit();348	}349	else 350	{351		$addr = urlencode($_POST["update"]["addr"]);352		$url = 'http://maps.googleapis.com/maps/api/geocode/json?address='.$addr.'&sensor=false';353		$get = file_get_contents($url);354		$a = my_json_decode($get);355		echo  $a['status'];356		357		if( $a['status'] == 'OK' )358		{359			$where = keyWhere($_POST["update"]["keyfields"], $strTableName);360			$lat = $a['results'][0]['geometry']['location']['lat'];361			$lng = $a['results'][0]['geometry']['location']['lng'];362			363			$sql = "update ".$_connection->addTableWrappers($origTableName)." set ".$_connection->addFieldWrappers($latField)." = ".$lat364				.", ".$_connection->addFieldWrappers($lngField)." = ".$lng." where ".$where;365			$_connection->exec( $sql );366		}  367		exit();368	}369}370?>