<?php
/**
 * Filename: divesite.php
 * Function: This file shows a list of dive sites, or the details for a dive site,
 * for phpDivingLog.
 * Version:  phpDivingLog v1.8 2006-08-25
 * Last Modified: 2006-08-21
 * /***************************************************************************
 * 
 * phpDivingLog
 * Copyright (C) 2006 Lloyd Borrett - http://www.borrett.id.au
 * 
 * Adapted from code by Olaf van Zandwijk - http://enschede.vanzandwijk.net
 * 
 * For use with Diving Log by Sven Knoch - http://www.divinglog.de
 * 
 * This file and all dependant and otherwise related files are part of phpDivingLog.
 * 
 * phpDivingLog is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * phpDivingLog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * /**************************************************************************
 */

// include_once misc file
$misc_filename = "includes/misc.inc.php";
if (!file_exists($misc_filename)) {
    print "<p>Misc file '" . $misc_filename . "' not found.</p>";
    exit;
} 
include_once ($misc_filename);


function DECtoDMS($dec)
{
    // Converts decimal longitude / latitude to DMS
    // ( Degrees / minutes / seconds )

    // This is the piece of code which may appear to
    // be inefficient, but to avoid issues with floating
    // point math we extract the integer part and the float
    // part by using a string function.

    if ($dec == "") {
        $dms = "";
    } else {
        $vars = explode(".", $dec);
        $deg = $vars[0];
        $tempma = "0." . $vars[1];

        $tempma = $tempma * 3600;
        $min = floor($tempma / 60);
        $sec = $tempma - ($min * 60);

        $dms = $deg . '&#176;';
        if (($min != 0) || ($sec != 0)) {
            $dms .= ' ' . $min . '&#8242;';
            if ($sec != 0) {
                // format to xx.xx
                $s = number_format($sec, 2);
                // remove trailing zeroes
                $s = rtrim($s, "0");
                // remove a trailing decimal point
                $s = rtrim($s, ".");
                $dms .= ' ' . $s . '&#8243;';
            } 
        } 
    } 
    return $dms;
} 

function latitude_format($coord)
{
    // Change coordinates into a displayable format
    if ($coord == "") {
        $dms = "";
    } else {
        // Converts decimal latitude to degrees / minutes / seconds
        $dms = DECtoDMS(abs($coord));

        // Add North or South
        if ($coord < 0) {
            $dms .= " S";
        } elseif ($coord > 0) {
            $dms .= " N";
        } 
    } 
    return $dms;
} 

function longitude_format($coord)
{
    // Change coordinates into a displayable format
    if ($coord == "") {
        $dms = "";
    } else {
        // Converts decimal longitude to degrees / minutes / seconds
        $dms = DECtoDMS(abs($coord));

        // Add East or West
        if ($coord < 0) {
            $dms .= " W";
        } elseif ($coord > 0) {
            $dms .= " E";
        } 
    } 
    return $dms;
} 


$get_loc = check_number($_GET['loc']);
if ($get_loc != "") {

    // Get the site details from database
    $globals['placeid'] = $get_loc;
    $result = parse_mysql_query('oneplace.sql');
    if (count($result) == 0) {
        header("Location: divesite.php");
    } 

    // Get the country and city details from the Logbook table
    $globals['placeid'] = $result[0]['ID'];
    $countrycity = parse_mysql_query('countrycity.sql');

    // Lets get the City and Country table values if we have them
    $country = "-";
    $city = "-";
    if (count($countrycity) != 0) {
        if ($countrycity[0]['Country'] != "") {
            // Get the country details from database
            $globals['countryid'] = $countrycity[0]['CountryID'];
            $countrydetails = parse_mysql_query('onecountry.sql');
            if (count($countrydetails) == 0) {
                $country = $countrycity[0]['Country'];
            } else {
                $country = $countrydetails[0]['Country'];
            } 
        } 

        if ($countrycity[0]['City'] != "") {
            // Get the city details from database
            $globals['cityid'] = $countrycity[0]['CityID'];
            $citydetails = parse_mysql_query('onecity.sql');
            if (count($citydetails) == 0) {
                $city = $countrycity[0]['City'];
            } else {
                $city = $citydetails[0]['City'];
            } 
        } 
    } 

    // Get the dives at this site from database
    $globals['placeid'] = $result[0]['ID'];
    $dives = parse_mysql_query('divelocations.sql');
    $dive_count = count($dives);

    // Get the site list from database
    $sitelist = parse_mysql_query('sitelist.sql');

    // Get the page header
    $pagetitle = $_lang['dive_site_pagetitle'] . $result[0]['Place'];
    include($_config['header']);

    /**
     * //	Some test code
     * echo "<hr>\n<b>Result:</b><br>\n<code>\n";
     * print_r($result);
     * echo "</code>\n<br>\n<hr>\n";
     */

    echo "<table class=\"divetable\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n";

    // First, Previous, Next, Last links and Dive #
    $last = count($sitelist) - 1;
    $position = -1;
    for ($i = 0; $i < count($sitelist); $i++) {
        if ($sitelist[$i]['ID'] == $globals['placeid']) {
            $position = $i;
        } 
    } 

    $links = " <tr>\n";
    $links .= "  <td colspan=\"4\">\n";
    $links .= "   <table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr>\n";

    // First, Previous
    $links .= "    <td width=\"25%\" align=\"left\">";
    if ($position != 0) {
        $links .= "\n     <div class=\"divelogcrumbs\">\n";
        $links .= "      <a href=\"divesite.php?loc=" . $sitelist[0]['ID'] . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['first_site_linktitle'] . "\">" . $_lang['first'] . "</a>";
        $links .= "&nbsp;|&nbsp;";
        $links .= "<a\n";
        $links .= "       href=\"divesite.php?loc=" . $sitelist[$position - 1]['ID'] . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['previous_site_linktitle'] . "\">" . $_lang['previous'] . "</a>\n";
        $links .= "     </div>\n    ";
    } else {
        $links .= "&nbsp;";
    } 
    $links .= "</td>\n";

    // Dive Log, Dive Sites, Dive Statistics
    $links .= "    <td width=\"50%\" align=\"center\">\n";
    $links .= "     <div class=\"divelogcrumbs\">\n";
    $links .= "      <a href=\"index.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_log_linktitle'] . "\">" . $_lang['dive_log'] . "</a> | \n";
    $links .= "      <a href=\"divesite.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_sites_linktitle'] . "\">" . $_lang['dive_sites'] . "</a><br>\n";
    $links .= "      <a href=\"equipment.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_equip_linktitle'] . "\">" . $_lang['dive_equip'] . "</a> | \n";
    $links .= "      <a href=\"divestats.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_stats_linktitle'] . "\">" . $_lang['dive_stats'] . "</a>\n";
    $links .= "     </div>\n";
    $links .= "    </td>\n";

    // Next, Last
    $links .= "    <td width=\"25%\" align=\"right\">";
    if ($position != $last) {
        $links .= "\n     <div class=\"divelogcrumbs\">\n";
        $links .= "      <a href=\"divesite.php?loc=" . $sitelist[$position + 1]['ID'] . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['next_site_linktitle'] . "\">" . $_lang['next'] . "</a>";
        $links .= "&nbsp;|&nbsp;";
        $links .= "<a\n";
        $links .= "       href=\"divesite.php?loc=" . $sitelist[$last]['ID'] . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['last_site_linktitle'] . "\">" . $_lang['last'] . "</a>\n";
        $links .= "     </div>\n    ";
    } else {
        $links .= "&nbsp;";
    } 
    $links .= "</td>\n";

    $links .= "   </tr></table>\n";
    $links .= "  </td>\n";
    $links .= " </tr>\n";

    // Show the links
    echo $links;
    echo " <tr>\n  <td colspan=\"4\" class=\"spacing\">&nbsp;</td>\n </tr>\n";

    // Show main site details
    echo " <tr class=\"divetitle\">\n";
    echo "  <td>" . $_lang['place_place'] . "</td>\n";
    echo "  <td>" . $_lang['place_city'] . "</td>\n";
    echo "  <td>" . $_lang['place_country'] . "</td>\n";
    echo "  <td>" . $_lang['place_maxdepth'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    if ($result[0]['Place'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Place'] . "</td>\n";
    } 
    echo "  <td>" . $city . "</td>\n";
    echo "  <td>" . $country . "</td>\n";
    if ($result[0]['MaxDepth'] == "") {
        echo "  <td>-</td>\n";
    } else {
        if ($_config['length']) {
            echo "  <td>" . MetreToFeet($result[0]['MaxDepth'], 0) . "&nbsp;" . $_lang['unit_length_short_imp'] . "</td>\n";
        } else {
            echo "  <td>" . $result[0]['MaxDepth'] . "&nbsp;" . $_lang['unit_length_short'] . "</td>\n";
        } 
    } 
    echo " </tr>\n";

    // Show extra site details
    echo " <tr class=\"divetitle\">\n";
    echo "  <td>" . $_lang['place_lat'] . "</td>\n";
    echo "  <td>" . $_lang['place_lon'] . "</td>\n";
    echo "  <td>&nbsp;</td>\n";
    if ($result[0]['MapPath'] == "") {
        echo "  <td>&nbsp;</td>\n";
    } else {
        echo "  <td>" . $_lang['place_map'] . "</td>\n";
    } 
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    if ($result[0]['Lat'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . latitude_format($result[0]['Lat']) . "</td>\n";
    } 
    if ($result[0]['Lon'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . longitude_format($result[0]['Lon']) . "</td>\n";
    } 
    if (($result[0]['Lat'] == "") || (($result[0]['Lon'] == ""))) {
        echo "  <td>&nbsp;</td>\n";
    } else {
        echo "  <td><a href=\"http://maps.google.com/maps?f=q&amp;hl=en&amp;q=";
        echo $result[0]['Lat'] . "," . $result[0]['Lon'];
        echo "+(" . str_replace(' ', '+', $result[0]['Place']);
        echo ")&amp;ie=UTF8&amp;t=k&amp;om=1\"\n";
        echo "    target=\"_blank\"\n";
        echo "    title=\"" . $_lang['site_google_link'] . $result[0]['Place'] . "\">Google Map</a></td>\n";
    } 
    if ($result[0]['MapPath'] == "") {
        echo "  <td>&nbsp;</td>\n";
    } else {
        echo "  <td><a href=\"" . $_config['mappath_web'] . $result[0]['MapPath'];
        echo "\" rel=\"lightbox[others]\"\n";
        echo "   title=\"" . $_lang['mappic_linktitle'] . $result[0]['Place'];
        echo "\">" . $_lang['mappic'] . "</a></td>\n";
    } 
    echo " </tr>\n";

    // Show site dives if we have them
    if ($dive_count != 0) {
        echo " <tr class=\"divetitle\">\n";
        echo "  <td colspan=\"4\">" . $dive_count;
        if ($dive_count == 1) {
            echo $_lang['site_dive_single'];
        } else {
            echo $_lang['site_dive_plural'];
        } 
        echo "</td>\n";
        echo " </tr>\n";

        echo " <tr class=\"divecontent\">\n";
        echo "  <td colspan=\"4\">";
        for($i = 0; $i < $dive_count; $i++) {
            if ($i > 0) {
                echo ",\n   ";
            } 
            echo "<a href=\"index.php?nr=" . $dives[$i]['Number'] . "\"\n";
            echo "    title=\"" . $_lang['dlog_number_title'] . $dives[$i]['Number'];
            echo "\">" . $dives[$i]['Number'] . "</a>";
        } 
        echo "</td>\n";
        echo " </tr>\n";
    } 

    echo " <tr><td colspan=\"4\" class=\"spacing\">&nbsp;</td></tr>\n";

    // Comments
    // Show them if we have them
    if ($result[0]['Comments'] != "") {

        echo " <tr class=\"divesection\">\n";
        echo "  <td colspan=\"4\">" . $_lang['site_sect_comments'] . "</td>\n";
        echo " </tr>\n";

        echo " <tr><td colspan=\"4\">\n";

        $r = $result[0]['Comments'];
        $r = str_replace("\r\n", "<br>\n", $r);

        // Handle Google Map URL
        $r = str_replace('[url]', '<a href="', $r);
        $r = str_replace('[/url]', '" target="_blank" title="' . $_lang['site_google_link'] . $result[0]['Place'] . '">' . $_lang['site_google_link'] . $result[0]['Place'] . '</a>', $r);

        echo $r;
        echo "  </td></tr>\n";
    } 

    // Show the links again
    echo " <tr>\n  <td colspan=\"4\" class=\"spacing\">&nbsp;</td>\n </tr>\n";
    echo $links;

    echo "</table>\n";
} else {

    // Display the Location List

    // Get start of the list
    $start = check_number($_GET['start']);
    if ($start == "") {
        $start = 1;
    } 
    $globals['start'] = $start - 1;

    // Get number to list
    $max = check_number($_GET['max']);
    if ($max == "") {
        $max = $_config['max_list'];
    } 
    $globals['max'] = $max;

    // Get number of locations
    $count = parse_mysql_query('locationcount.sql');
    $end = $count[0]['COUNT(*)'];

    // First, Previous, Next, Last links
    $links = " <tr>\n";
    $links .= "  <td colspan=\"4\">\n";
    $links .= "   <table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tr>\n";

    // First, Previous
    $links .= "    <td width=\"25%\" align=\"left\">";
    if ($start == 1) {
        $links .= "&nbsp;";
    } else {
        $links .= "\n     <div class=\"divelogcrumbs\">\n";
        $links .= "      <a href=\"divesite.php?start=1&amp;max=" . $max . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['first_group_linktitle'] . "\">" . $_lang['first'] . "</a>";
        $previous = $start - $max;
        if ($previous < 1) {
            $previous = 1;
        } 
        $links .= "&nbsp;|&nbsp;";
        $links .= "<a\n";
        $links .= "       href=\"divesite.php?start=" . $previous . "&amp;max=" . $max . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['previous_group_linktitle'] . "\">" . $_lang['previous'] . "</a>\n";
        $links .= "     </div>\n    ";
    } 
    $links .= "</td>\n";

    // Dive Sites, Dive Statistics
    $links .= "    <td width=\"50%\" align=\"center\">\n";
    $links .= "     <div class=\"divelogcrumbs\">\n";
    $links .= "      <a href=\"index.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_log_linktitle'] . "\">" . $_lang['dive_log'] . "</a> | \n";
    $links .= "      <a href=\"equipment.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_equip_linktitle'] . "\">" . $_lang['dive_equip'] . "</a> | \n";
    $links .= "      <a href=\"divestats.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_stats_linktitle'] . "\">" . $_lang['dive_stats'] . "</a>\n";
    $links .= "     </div>\n";
    $links .= "    </td>\n";

    // Next, Last
    $links .= "    <td width=\"25%\" align=\"right\">";
    if (($start + $max) >= $end) {
        $links .= "&nbsp;";
    } else {
        $next = $start + $max;
        $startlast = ((floor($end / $max) * $max) + 1);
        $links .= "\n     <div class=\"divelogcrumbs\">\n";
        $links .= "      <a href=\"divesite.php?start=" . $next . "&amp;max=" . $max . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['next_group_linktitle'] . "\">" . $_lang['next'] . "</a>";
        $links .= "&nbsp;|&nbsp;";
        $links .= "<a\n";
        $links .= "       href=\"divesite.php?start=" . $startlast . "&amp;max=" . $max . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['last_group_linktitle'] . "\">" . $_lang['last'] . "</a>\n";
        $links .= "     </div>\n    ";
    } 
    $links .= "</td>\n";

    $links .= "   </tr></table>\n";
    $links .= "  </td>\n";
    $links .= " </tr>\n";

    // Get the page header
    $pagetitle = $_lang['dive_sites'];
    include($_config['header']);

    echo "<table class=\"divetable\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\">\n";

    // Show the links
    echo $links;

    // Get the details of the locations to be listed
    $locationlist = parse_mysql_query('locationlist.sql');
    $count = count($locationlist);

    echo " <tr>\n  <td colspan=\"4\" class=\"spacing\">&nbsp;</td>\n </tr>\n";

    if ($count == 0) {
        echo " <tr class=\"divecontent\">\n";
        echo "  <td colspan=\"4\">" . $_lang['dsite_none'] . "</td>\n";
        echo " </tr>\n";
    } else {
        echo " <tr class=\"divelogtitle\">\n";
        echo "  <td valign=\"bottom\">" . $_lang['dsite_title_place'] . "</td>\n";
        echo "  <td valign=\"bottom\">" . $_lang['dsite_title_city'] . "</td>\n";
        echo "  <td valign=\"bottom\">" . $_lang['dsite_title_country'] . "</td>\n";
        echo "  <td valign=\"bottom\">" . $_lang['dsite_title_maxdepth'] . "</td>\n";
        echo " </tr>\n";

        for ($i = 0; $i < $count; $i++) {
            // Get the Country and City
            $globals['placeid'] = $locationlist[$i]['ID'];
            $countrycity = parse_mysql_query('countrycity.sql');
            $country = "-";
            $city = "-";
            if (count($countrycity) != 0) {
                if ($countrycity[0]['City'] != "") {
                    $city = $countrycity[0]['City'];
                } 
                if ($countrycity[0]['Country'] != "") {
                    $country = $countrycity[0]['Country'];
                } 
            } 

            echo " <tr class=\"divecontent\">\n";
            echo "  <td><a href=\"divesite.php?loc=" . $locationlist[$i]['ID'];
            echo "\"\n";
            echo "   title=\"" . $locationlist[$i]['Place'];
            echo " " . $_lang['logbook_place_linktitle'] . "\">";
            echo $locationlist[$i]['Place'] . "</a></td>\n";
            echo "  <td>" . $city . "</td>\n";
            echo "  <td>" . $country . "</td>\n";
            if ($locationlist[$i]['MaxDepth'] == "") {
                echo "  <td>-</td>\n";
            } else {
                if ($_config['length']) {
                    echo "  <td>" . MetreToFeet($locationlist[$i]['MaxDepth'], 0) . "&nbsp;" . $_lang['unit_length_short_imp'] . "</td>\n";
                } else {
                    echo "  <td>" . $locationlist[$i]['MaxDepth'] . "&nbsp;" . $_lang['unit_length_short'] . "</td>\n";
                } 
            } 
            echo " </tr>\n";
        } 
    } 
    // Show the links
    echo " <tr>\n  <td colspan=\"4\" class=\"spacing\">&nbsp;</td>\n </tr>\n";
    echo $links;

    echo "</table>\n";
} 

// Get the page footer
include ($_config['footer_divesite']);

?>
