<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<?php
  // Global-ish variables, functions and such.

  // Replace special characters with HTML entities in a string
  function myent( $x )
  {
    return htmlentities( $x, ENT_COMPAT | ENT_XHTML, "UTF-8" );
  }


  // Emit HTML to find and show all images in a row of a result set with a
  //   specified type.
  function show_images( $connection, $row, $type )
  {
    $images = pg_execute( $connection, "images", array( $row['key'], $type ) );
    if( !$images )
    {
      echo "Not showing images because there was an error executing the query.";
      return;
    }
    while( $row2 = pg_fetch_assoc( $images ) )
    {
      echo "<a href=\"images/" . myent( $row2['filename'] ) . ".jpg\"><img src=\"images/thumbs/" . myent( $row2['filename'] ) . "_thumb.jpg\" alt = \"" . myent( $row2['description'] ) . "\" title = \"" . myent( $row2['description'] ) . "\"/></a>";
    }
  }


  // Create a string containing notes for a record.
  function create_notes( $acquisition_date, $provenance, $purchase_price, $notes )
  {
    $result = "";
    if( !is_null( $acquisition_date ) || !is_null( $provenance ) || !is_null( $purchase_price ) )
    {
      $result .= "Acquired";
      if( !is_null( $acquisition_date ) )
      {
        $result .= " " . $acquisition_date;
      }
      else
      {
        $result .= " on unknown date";
      }
      if( !is_null( $provenance ) )
      {
        $result .= " from " . $provenance;
      }
      else
      {
        $result .= " from unknown vendor";
      }
      if( !is_null( $purchase_price ) )
      {
        $result .= " for $" . $purchase_price;
      }
      $result .= ".";
    }
    else
    {
      $result .= "Unknown provenance.";
    }
    if( !is_null( $notes ) )
    {
      $result .= " " . $notes;
    }
    return $result;
  }


  // Emit HTML to find and show all records that I own.
  function show_owned( $connection )
  {
    $owned = pg_query( $connection, "SELECT * FROM owned ORDER BY artist, year, album" );
    if( !$owned )
    {
      echo "<p style=\"font-weight: bold\">Could not show record collection because query for owned records failed.</p>\n";
      return;
    }

    $query = pg_prepare( $connection, "images", "SELECT * FROM owned_image WHERE key = $1 AND type = $2" );
    if( !$query )
    {
      echo "<p style=\"font-weight: bold\">Could not show record collection because preparing images statement failed.</p>\n";
      return;
    }

    if( pg_num_rows( $owned ) < 1 )
    {
      echo "<p style=\"font-weight: bold\">Not showing record collection because it appears to be empty.</p>\n";
      return;
    }

    echo "   <table>\n";
    echo "    <caption>Albums that I currently own.</caption>\n";
    echo "    <tr>\n";
    echo "     <th class=\"border_top\">Artist</th>\n";
    echo "     <th class=\"border_top\">Album</th>\n";
    echo "     <th class=\"border_top\">Label: Catalog</th>\n";
    echo "     <th class=\"border_top\">Condition (Record/Packaging)</th>\n";
    echo "    </tr>\n";
    echo "    <tr>\n";
    echo "     <th class=\"border_bottom\" colspan=\"3\">Images</th>\n";
    echo "     <th class=\"border_bottom\">Notes</th>\n";
    echo "    </tr>\n";

    while( $row = pg_fetch_assoc( $owned ) )
    {
      echo "    <tr>\n";
      echo "     <td class=\"border_top\">" . myent( $row['artist'] ) . "</td>\n";
      echo "     <td class=\"border_top\">" . myent( $row['album'] ) . " (" . myent( $row['year'] ) . ")</td>\n";
      echo "     <td class=\"border_top\">" . myent( $row['label'] ) . ": " . myent( $row['catalog'] ) . "</td>\n";
      echo "     <td class=\"border_top\">" . myent( $row['record_grade'] ) . " / " . myent( $row['cover_grade'] ) . "</td>\n";

      echo "    </tr>\n";
      echo "    <tr>\n";

      echo "     <td class=\"border_bottom\" colspan=\"3\">";
      show_images( $connection, $row, "F" );
      show_images( $connection, $row, "B" );
      show_images( $connection, $row, "O" );
      echo "     </td>\n";

      echo "     <td class=\"border_bottom\">";
/*
      if( !is_null( $row['acquisition_date'] ) || !is_null( $row['provenance'] ) || !is_null( $row['purchase_price'] ) )
      {
        echo "Acquired";
        if( !is_null( $row['acquisition_date'] ) )
        {
          echo " " . $row['acquisition_date'];
        }
        else
        {
          echo " on unknown date";
        }
        if( !is_null( $row['provenance'] ) )
        {
          echo " from " . $row['provenance'];
        }
        else
        {
          echo " from unknown vendor";
        }
        if( !is_null( $row['purchase_price'] ) )
        {
          echo " for $" . $row['purchase_price'];
        }
        echo ".";
      }
      else
      {
        echo "Unknown provenance.";
      }
      if( !is_null( $row['notes'] ) )
      {
        echo " " . $row['notes'];
      }
*/
      echo myent( create_notes( $row['acquisition_date'], $row['provenance'], $row['purchase_price'], $row['notes'] ) );
      echo "</td>\n";

      echo "    </tr>\n";
    }
    echo "   </table>\n";
  }


  // Emit HTML to find and show all packaging that I own.
  function show_packaging( $connection )
  {
    $packaging = pg_query( $connection, "SELECT * FROM packaging ORDER BY artist, year, album" );
    if( !$packaging )
    {
      echo "<p style=\"font-weight: bold\">Could not show packaging collection because query for owned packaging failed.</p>\n";
      return;
    }

    if( pg_num_rows( $packaging ) < 1 )
    {
      return;
    }

    echo "   <p>There are also a few cases in which I have managed to acquire the packaging for an album but not the record itself.</p>\n";
    echo "   <table>\n";
    echo "    <caption>Albums for which I own only packaging.</caption>\n";
    echo "    <tr>\n";
    echo "     <th class=\"border_both\">Artist</th>\n";
    echo "     <th class=\"border_both\">Album</th>\n";
    echo "     <th class=\"border_both\">Label: Catalog</th>\n";
    echo "     <th class=\"border_both\">Packaging Condition</th>\n";
    echo "     <th class=\"border_both\">Notes</th>\n";
    echo "    </tr>\n";

    while( $row = pg_fetch_assoc( $packaging ) )
    {
      echo "    <tr>\n";
      echo "     <td class=\"border_both\">" . myent( $row['artist'] ) . "</td>\n";
      echo "     <td class=\"border_both\">" . myent( $row['album'] ) . " (" . myent( $row['year'] ) . ")</td>\n";
      echo "     <td class=\"border_both\">" . myent( $row['label'] ) . ": " . myent( $row['catalog'] ) . "</td>\n";
      echo "     <td class=\"border_both\">" . myent( $row['cover_grade'] ) . "</td>\n";
      echo "     <td class=\"border_both\">" . myent( create_notes( $row['acquisition_date'], $row['provenance'], $row['purchase_price'], $row['notes'] ) ) . "</td>\n";
      echo "    </tr>\n";
    }

    echo "   </table>\n";
  }


  // Emit HTML to find and show my wishlist.
  function show_wishlist( $connection )
  {
    $wishlist = pg_query( $connection, "SELECT * FROM wishlist ORDER BY artist, year, album" );
    if( !$wishlist )
    {
      echo "<p style=\"font-weight: bold\">Could not show wishlist because query for wishlist failed.</p>\n";
      return;
    }

    if( pg_num_rows( $wishlist ) < 1 )
    {
      echo "<p style=\"font-weight: bold\">Not showing wishlist because it appears to be empty.</p>\n";
      return;
    }

    echo "   <table>\n";
    echo "    <tr>\n";
    echo "     <th class=\"border_both\">Artist</th>\n";
    echo "     <th class=\"border_both\">Album</th>\n";
    echo "    </tr>\n";

    while( $row = pg_fetch_assoc( $wishlist ) )
    {
      echo "    <tr>\n";
      echo "     <td class=\"border_both\">" . myent( $row['artist'] ) . "</td>\n";
      echo "     <td class=\"border_both\">" . myent( $row['album'] ) . " (" . myent( $row['year'] ) . ")</td>\n";
      echo "    </tr>\n";
    }

    echo "   </table>\n";
  }


  $connection = pg_connect( "dbname=records" );
?>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

 <head>
  <title>Chad Hogg's Record Collection</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="description" content="Chad Hogg' Record Collection" />
  <link href="../main.css" rel="stylesheet" type="text/css" />
  <style type="text/css">
   img.album_thumb
     { width: 100px; 
       height: auto }

   table
     { border-style: solid; 
       border-collapse: collapse }

   td
     { text-align: center }

   td.border_top, 
     th.border_top
     { border-top: medium solid;
       border-left: thin dotted;
       border-right: thin dotted;
       border-bottom: thin dotted }

   td.border_bottom,
     th.border_bottom
     { border-top: thin dotted;
       border-left: thin dotted;
       border-right: thin dotted;
       border-bottom: medium solid }

   td.border_both,
     th.border_both
     { border-top: medium solid;
       border-left: thin dotted;
       border-right: thin dotted;
       border-bottom: medium solid }

   img
     { padding:5px }
  </style>
 </head>

 <body>

  <div id="main">

   <h1>Chad Hogg's Record Collection</h1>

   <p>I have been a collector of music since whatever age that usually starts, but I never cared much about format.  I bought cassette tapes when that was what was available, then compact discs, and now sometimes digital downloads -- whatever was in a convenient format for playing.  And I still do buy most of my music that way.</p>

   <p>But I became interested in collecting specifically long play vinyl records in 2004, when my parents decided that a good Christmas gift would be several selections from my father's old collection that had been collecting dust and display cases to decorate the walls of my first apartment away from home.  I had not known at the time that I would have any interest in acquiring music in an obsolete format, but it turned out to be a great idea.</p>

   <p>Currently, I do not actually own a turntable with which to play records, but I hope to change that soon.  In the meantime, my collection is not about the ability to play the music; virtually everything that I own on vinyl I also have digital copies of, and I am quite happy to listen to the digital copies.  But there is something special about having a large, physical object, with beautiful, detailed artwork -- especially when that object has a history spanning multiple decades.</p>

   <p>Since that first gift that started my collection I have been steadily adding to it from other gifts, record stores, online auctions, and yard sales.  Unfortunately what I would most want is often rare, but I have found a few especially desirable treasures.</p>

   <h3>My Current Collection</h3>

   <p>My grading scale goes M (Mint), NM (Near Mint), VG++, VG+, VG (Very Good), VG-, VG--, G (Good), P (Poor), to match that of the store where I have done the most shopping.  The descriptions can be a bit misleading: G is not good in any usual sense of the word.  Since I currently lack the ability to play any of these records, my grades are from visual inspection only.</p>

   <p>I have included images of all of the packaging and artwork in each release that I own.  I created these by scanning parts of the object at a time and stitching them together with <a href="http://hugin.sourceforge.net/">Hugin</a>, so there are some visual anomalies if you look closely enough, especially around the edges.  Click any image for a much larger, higher-resolution version.</p>

<?php

  if( $connection )
  {
    show_owned( $connection );
  }
  else
  {
    echo "<p style=\"font-weight: bold\">Could not show record collection because connection to database failed.</p>\n";
  }
?>

   <p>Some of these I have chosen because they contain some of my very favorite music and have at least somewhat interesting artwork (Secret Treaties, The Chicago Transit Authority, D&eacute;j&agrave; Vu).  Others are in my collection because they have iconic artwork and at least pretty good music (News Of The World, Don't Look Back, Rocka Rolla).  Some have both outstanding music and artwork (Hotel California, Rumours, Aqualung).  Some were given to me (Double Vision, Naturally).  Some are the result of not having found much I wanted in a particular search but still feeling like I needed to buy something (An Innocent Man, Building The Perfect Beast).</p>

   <p>Almost everything is studio albums, though I have a few live albums that were the best-known releases by their bands or are by one of my very favorite bands.  There are no singles, bootlegs, compilations, or anthologies, by design.</p>

<?php
  if( $connection )
  {
    show_packaging( $connection );
  }
  else
  {
    echo "<p style=\"font-weight: bold\">Could not show packaging collection because connection to database failed.</p>\n";
  }
?>

   <!--
   <p><img style="display:block; margin-left:auto; margin-right:auto" src="records-years.png" alt="Graph of number of records I own grouped by release year"/></p>

   <p>There is a remarkable pattern here: 2% of my collection was released in the 1950s, 3% in the 1960s, 64% in the 1970s, 30% in the 1980s, and 2% in the 1990s.  Before some recent purchases in was even more strongly centered, with 82% of the collection falling in the 12-year span 1969-1980.  There are three partial explanations for this.  First, I enjoy much more of the music of the '70s than I do of any other decade.  Second, the 70's are the last decade before cassettes and compact discs became the main audio format, and thus many more vinyl copies of music from that era were printed than in more modern eras.  Third, much of the music that I like from the '70s was widely popular and purchased by casual fans, then casually discarded, while much of the music that I like from the '80s, '90s, and '00s is more of a niche market and is only purchased by hardcore fans that will not be turning around and selling it a few years later.</p>
   -->

   <h3>My Wishlist</h3>

   <p>There are lots of other albums that I would be interested in acquiring, but most only if I could get them very cheaply.  These, on the other hand, are the few that I specifically want enough to spend more than a dollar or two on them.  Some I simply have not run into yet, but others are extremely rare and only sell at market prices that are quadruple what I would be willing to spend.</p>

<?php
  if( $connection )
  {
    show_wishlist( $connection );
  }
  else
  {
    echo "<p style=\"font-weight: bold\">Could not show wishlist because connection to database failed.</p>\n";
  }
?>

  </div>
 </body>
</html>

<?php
  // Cleanup and such
  pg_close( $connection );
?>
