<?php
/*
* @author Shawn T. McFarlane shawngo.com@gmail.com
*  http://www.shawngo.com
* geocircle.php can be found at http://www.weberdev.com/get_example-3548.html
*  - it is used for coordinate conversions.. It's ok.. email me if you find a better one!! :D
*
* I have my own MySQL Utility Class called DB_Connect2.php which can be found at my site
*  http://www.shawngo.com/xdbconnect_tutorial.php
*
* The images do weird things.. idk... have fun with it.. make changes..
* - I just wanted a relatively simple script to generate what's needed.
* - thought maybe a php noob might get a kick out of it!! :D
*
*
* This script is licensed under the "Do whatever you want with it" License Agreement.
* If you improve it, modify it, whatever, gimme a hollar.
*
*/
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0"false);
// HTTP/1.0
header("Pragma: no-cache");
//XML Header

header("content-type:application/vnd.google-earth.kml+xml");
//header("content-type:text/xml"); // for viewing the xml output and debugging

$kml_output ='<?xml version="1.0" encoding="UTF-8"?>';
$kml_output.='<kml xmlns="http://earth.google.com/kml/2.0">';

include_once(
'includes/DB_Connect2.php');
$utility = new DB_Connect2();

// threw this in for kicks
$kml_output.='
 <NetworkLinkControl>
  <message>Hello NetworkLink</message>
  <linkName>Over-ride the name set by the calling NetworkLink</linkName>
  <linkDescription>Over-rides the description set by the calling NetworkLink</linkDescription>
  <minRefreshPeriod>Over-rides the refreshInterval set in the calling NetworkLink</minRefreshPeriod>
  <cookie>myCookie=session3</cookie>
 </NetworkLinkControl>
 '
;
 
$kml_output.='<Document>
 '
;

 
$kml_output.='<description>International fishing locations using Google Maps and Google Earth. http://www.shawngo.com/hotspots.php</description>
  <name>Shawngo.com Fishing Locations</name>
  <visibility>1</visibility>
    <Style id="normalPlacemark">
      <IconStyle>
        <Icon>
          <href>root://icons/palette-3.png</href>
        </Icon>
      </IconStyle>
    </Style>'
;

// these are the fields in my database of fishing locations
//hotspotid.description.timeofday.tackle.
//fulldescription.submitby.city.region.
//country.longitude.latitude.dateadded.
//typeid.link.imglink.password.dateof

   
$sql "SELECT * FROM hotspots WHERE typeid = 1";
   
$results $utility->query_db($sql)
     or die(
mysql_error());

    while (
$row mysql_fetch_array ($results)) {
        
// get data
        
$description $row['description'];
        
$submitby $row['submitby'];
        
$city $row['city'];
        
$region $row['region'];
        
$country $row['country'];
        
$longitude $row['longitude'];
        
$latitude $row['latitude'];
        
$link $row['link'];
        
$timeofday $row['timeofday'];
        
$tackle $row['tackle'];
        
$fulldescription $row['fulldescription'];

        
// here i manage to grab the image links and slam em in there
        
$imglink "";
        
$imglinks "";
        
$ln "";
        
$linksout "";
        
$imglink $row['imglink'];
        
$imglinks explode(",",$imglink);
        
$ln strlen($imglink);

        include_once(
'hotspots/greatcircle.php'); // used for the following 2 lines to get nice formatted DMS coords.. which google earth messes up
        
$lat_GPS  decimal2degree($latitude,'LAT');
        
$long_GPS decimal2degree($longitude,'LON');

        
// this is output for my images
        
if($ln 10){
            
$linksout "";
            foreach (
$imglinks as $linko){
                
$linksout .= '<a href="'.$linko.'" target="_blank"><img style="padding: 4px; height:100px; width:100px;" src="'.$linko.'"></a>';
            }
        }else{
          
$linskout "";
        }

        
// the descriptions in the db can get rather lengthy (< 250 chars) so i substring that biznitch
        
$comments ="";
        
$ln strlen($fulldescription);
        
$i 0;
        for(
$i 0$i $ln$i+=55){
            
$comments .= substr($fulldescription,$i,55).'-<br>';
        }
        
// drop the info into a *bleh* table
        
$kml_description '
        <table style="font: 12px Arial; ">
            <tr><td><a href="hotspots.php?findme=z&longitude='
.$longitude.'&latitude='.$latitude.'"><u><b>'.$description.'</b></u></a></td></tr>
            <tr><td><b>City:</b> '
.$city.'<br>
                    <b>Region:</b> '
.$region.'<br>
                    <b>Country:</b> '
.$country.'<br>
                    <a href="'
.$link.'" target="_blank">Link To Area</a><br>
                    <b>Time of Day:</b> '
.$timeofday.'<br>
                    <b>Taclke/Fly Used:</b> '
.$tackle.'<br>
                    <b>GPS Coordinates (DMS): </b>'
.$lat_GPS.' &nbsp;&nbsp; '.$long_GPS.' <br>
                    <b>Lat:</b> '
.substr($latitude,0,15).' &nbsp;&nbsp;&nbsp;<b>Long</b>: '.substr($longitude,0,15).'<br><br>
                    <b>Full Description:</b><br>
                    <div style="width:325px; padding: 2px; border: 1px solid #000; ">"'
.$comments.'"<br>
                    '
.$linksout.'<br></div>
            </td></tr>
         </table>'
;

        
$kml_description "<![CDATA[".$kml_description."]]>";
    
// drop some place mark shiznit
    
$kml_output .="<Placemark>";
    
$kml_output .="   <description>$kml_description</description>";
    
$kml_output .="   <name>$description</name>";
    
$kml_output .="    <styleUrl>#exampleStyleMap</styleUrl>";
    
$kml_output .="   <LookAt>";
    
$kml_output .="     <longitude>$longitude</longitude>";
    
$kml_output .="     <latitude>$latitude</latitude>";
    
$kml_output .="     <range>697.2848266005969</range>";
    
$kml_output .="     <tilt>42.83182834834368</tilt>";
    
$kml_output .="     <heading>0.001955870229629829</heading>";
    
$kml_output .="   </LookAt>";
    
$kml_output .="   <Point>";
    
$kml_output .="     <coordinates>$longitude,$latitude,0</coordinates>";
    
$kml_output .="   </Point>";
    
$kml_output .="  </Placemark>";
    }
  
// end the document structure
 
$kml_output .='</Document>
</kml>'
;

// send output to you all to enjoy
echo $kml_output;
?>