<?php
/**
 * Filename: equipment.php
 * Function: This file shows a list of dive equipment, or the details for a
 * piece of equipment, 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);

$get = check_number($_GET['gear']);
if ($get != "") {

    // Get the equipment details from database
    $globals['gear'] = $get;
    $result = parse_mysql_query('oneequipment.sql');
    if (count($result) == 0) {
        header("Location: equipment.php");
    } 

    // Get the list of gear from database sorted by manufacturer and object
    $gearlist = parse_mysql_query('gearlist.sql');

    // Get the page header
    $pagetitle = $_lang['equip_details_pagetitle'] . $result[0]['Object'];
    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($gearlist) - 1;
    $position = -1;
    for ($i = 0; $i < count($gearlist); $i++) {
        if ($gearlist[$i]['ID'] == $globals['gear']) {
            $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=\"equipment.php?gear=" . $gearlist[0]['ID'] . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['first_equip_linktitle'] . "\">" . $_lang['first'] . "</a>";
        $links .= "&nbsp;|&nbsp;";
        $links .= "<a\n";
        $links .= "       href=\"equipment.php?gear=" . $gearlist[$position - 1]['ID'] . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['previous_equip_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=\"equipment.php?gear=" . $gearlist[$position + 1]['ID'] . "\" class=\"divelogcrumbs\"\n";
        $links .= "       title=\"" . $_lang['next_equip_linktitle'] . "\">" . $_lang['next'] . "</a>";
        $links .= "&nbsp;|&nbsp;";
        $links .= "<a\n";
        $links .= "       href=\"equipment.php?gear=" . $gearlist[$last]['ID'] . "\"\n";
        $links .= "       title=\"" . $_lang['last_equip_linktitle'] . "\" class=\"divelogcrumbs\">" . $_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 equipment details
    echo " <tr class=\"divetitle\">\n";
    echo "  <td colspan=\"2\">" . $_lang['equip_object'] . "</td>\n";
    echo "  <td colspan=\"2\">" . $_lang['equip_manufacturer'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    if ($result[0]['Object'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td colspan=\"2\">" . $result[0]['Object'] . "</td>\n";
    } 
    if ($result[0]['Manufacturer'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td colspan=\"2\">" . $result[0]['Manufacturer'] . "</td>\n";
    } 
    echo " </tr>\n";

    // Show equipment purchase details
    echo " <tr class=\"divetitle\">\n";
    echo "  <td colspan=\"2\">" . $_lang['equip_shop'] . "</td>\n";
    echo "  <td>" . $_lang['equip_datep'] . "</td>\n";
    echo "  <td>" . $_lang['equip_price'] . "</td>\n";
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    if ($result[0]['Shop'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td colspan=\"2\">" . $result[0]['Shop'] . "</td>\n";
    } 
    if ($result[0]['DateP'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . date($_lang['equip_date_format'], strtotime($result[0]['DateP'])) . "</td>\n";
    } 
    if ($result[0]['Price'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $_lang['currency_prefix'] . number_format($result[0]['Price'], 2) . $_lang['currency_suffix'] . "</td>\n";
    } 
    echo " </tr>\n";

    // Show the rest of the details
    echo " <tr class=\"divetitle\">\n";
    echo "  <td>" . $_lang['equip_serial'] . "</td>\n";
    echo "  <td>" . $_lang['equip_warranty'] . "</td>\n";
    echo "  <td>" . $_lang['equip_dater'] . "</td>\n";
    if ($result[0]['PhotoPath'] == "") {
        echo "  <td>&nbsp;</td>\n";
    } else {
        echo "  <td>" . $_lang['equip_photo'] . "</td>\n";
    } 
    echo " </tr>\n";

    echo " <tr class=\"divecontent\">\n";
    if ($result[0]['Serial'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Serial'] . "</td>\n";
    } 
    if ($result[0]['Warranty'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . $result[0]['Warranty'] . "</td>\n";
    } 
    if ($result[0]['DateR'] == "") {
        echo "  <td>-</td>\n";
    } else {
        echo "  <td>" . date($_lang['equip_date_format'], strtotime($result[0]['DateR'])) . "</td>\n";
    } 
    if ($result[0]['PhotoPath'] == "") {
        echo "  <td>&nbsp;</td>\n";
    } else {
        echo "  <td><a href=\"" . $_config['equippath_web'] . $result[0]['PhotoPath'];
        echo "\" rel=\"lightbox[others]\"\n";
        echo "   title=\"" . $_lang['equip_photo_linktitle'] . $result[0]['Object'];
        echo "\">" . $_lang['equip_photo_link'] . "</a></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['equip_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);

        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 Equipment 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 equipment items
    $count = parse_mysql_query('equipcount.sql');
    $end = $count[0]['COUNT(*)'];

    // First, Previous, Next, Last links
    $links = " <tr>\n";
    $links .= "  <td colspan=\"2\">\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=\"equipment.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=\"equipment.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=\"divesite.php\" class=\"divelogcrumbs\"\n";
    $links .= "       title=\"" . $_lang['dive_sites_linktitle'] . "\">" . $_lang['dive_sites'] . "</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=\"equipment.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=\"equipment.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_equip'];
    include($_config['header']);

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

    // Show the links
    echo $links;

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

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

    if ($count == 0) {
        echo " <tr class=\"divecontent\">\n";
        echo "  <td colspan=\"2\">" . $_lang['equip_none'] . "</td>\n";
        echo " </tr>\n";
    } else {
        echo " <tr class=\"divelogtitle\">\n";
        echo "  <td valign=\"bottom\">" . $_lang['equip_title_object'] . "</td>\n";
        echo "  <td valign=\"bottom\">" . $_lang['equip_title_manufacturer'] . "</td>\n";
        echo " </tr>\n";

        for ($i = 0; $i < $count; $i++) {
            echo " <tr class=\"divecontent\">\n";
            echo "  <td><a href=\"equipment.php?gear=" . $equiplist[$i]['ID'];
            echo "\"\n";
            echo "   title=\"" . $equiplist[$i]['Object'];
            echo " " . $_lang['logbook_place_linktitle'] . "\">";
            echo $equiplist[$i]['Object'] . "</a></td>\n";
            if ($equiplist[$i]['Manufacturer'] == "") {
                echo "  <td>-</td>\n";
            } else {
                echo "  <td>" . $equiplist[$i]['Manufacturer'] . "</td>\n";
            } 
            echo " </tr>\n";
        } 
    } 
    // Show the links
    echo " <tr>\n  <td colspan=\"2\" class=\"spacing\">&nbsp;</td>\n </tr>\n";
    echo $links;

    echo "</table>\n";
} 

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

?>
