<?php
  // --------------------------------------------------------------------------
  // view_by_permalink.php
  // this presents a post via permalink passed on the URL.  post is show along 
  // with comments and a form for caputuring additional comments.  
  // --------------------------------------------------------------------------

  // define the vars + include the header
  $page_title = "<a href=http://rebecca.hallqvist.se/schooner/wheatblog/index.php title=tillbaks>schooner</a> :: inlägg";
  $prefix = "../";
  $template = 3;
  include("$prefix" . "whp_header.inc"); 
?>

<?php
    // read in the connection settings
    require("wheatblog_settings.inc");

    // parse the passed vars into something useful 
    // also check for nulls
    $the_id = $_GET['the_id'];

    // connect to the RDBMS
    $db = mysql_connect("$site","$user","$pass") 
        or die("<h2>Could not connect to database server</h2><p>Check passwords and sockets</p>");

    // select the database
    mysql_select_db("$database",$db) 
        or die("<h2>Could not select database $database</h2><p>Check database name</p>");

    // ---------------------------
    // post
    // ---------------------------

    // select post
    $result = mysql_query("select * 
        from $database_table
        where id = '$the_id' 
        order by year, month, day 
        limit $front_page_max",$db) 
        or die("<h2>Could not select posts</h2>");

    // output post        
    echo("<!-- generated by wheatblog: start -->\n\n");

    while($row = mysql_fetch_array($result)) {
        $the_id = $row["id"];
        $the_day = $row["day"];
        $the_month = $row["month"];
        $the_date = $row["date"];
        $the_year = $row["year"];
        $the_category = $row["category"];

            // parse out the category id into its string value
            $result2 = mysql_query("select *   
                from $database_table_categories   
                where id = '$the_category'",$db)
                or die("<h2>Could not select category</h2>"); 

            while($row2 = mysql_fetch_array($result2)) {
                $the_category_name = $row2["category"];
            }

        $the_showpref = $row["showpref"];
        $the_title = $row["title"];
        $the_body = $row["body"];

        echo("<div class='indent'>\n");
        echo("<span class='desc_header'>");
        echo("$the_day" . ", " . "$the_date" . "/" . "$the_month" . " " . "$the_year" . " :: " . "$the_title");
        echo("</span><br>\n");
        echo("$the_body" . "<br>\n");
        echo("[<a href='view_by_category.php?the_category=$the_category'" . "'>" . "$the_category_name" . "</a>] ");
        echo("</div>\n\n");
    }

    // ---------------------------
    // comments
    // ---------------------------

    // set vars to null
    $comment_month = "";
    $comment_date = "";
    $comment_year = "";
    $comment_title = "";
    $comment_body = "";
    $comment_author_name = "";
    $comment_author_email = "";
    $comment_author_url = "";   
    $the_post_id = ""; 

    // select comments for this post
    $result_comments = mysql_query("select * 
        from $database_table_comments  
        where post_id = '$the_id'  
        order by comment_year, comment_month, comment_date",$db) 
        or die("<h2><span class='desc_header'>Could not select comments</span></h2>");

    // print a comments header
    echo(
      "<div class='indent2'><span class='desc_header'>" . 
      "kommentarer</span></div>" 
    );

    while($row = mysql_fetch_array($result_comments)) {
        $comment_month = $row["comment_month"];
        $comment_date = $row["comment_date"];
        $comment_year = $row["comment_year"];
        $comment_body = $row["comment_body"];
        $comment_author_name = $row["comment_author_name"];
        $comment_author_email = $row["comment_author_email"];
        $comment_author_url = $row["comment_author_url"];

        // print comments
        echo("<div class='indent'> " .  
          "  <span class='desc_header'>" . 
          "datum: $comment_date" . "/" . "$comment_month" . " " . "$comment_year" .  
          "</span><br>\n" .  
          "$comment_body" . "<br />\n" . 
          "[<a href='mailto:" . "$comment_author_email" .  "'>" . "$comment_author_name" . "</a>" . " | " .
          "<a href='" . "$comment_author_url" . "'>" . "www</a>]" . 
          "</div>\n\n"
        );

        // or print some feedback NOT WORKING!!!
        //if ($row == "") {
        //  echo("<div class='indent'>There are no comments on this post</div>\n");
        //}

    }

    echo("<!-- generated by wheatblog: stop -->\n\n");

    // new comment form
    echo("<div class='indent'> " .  
      "<form method='post' action='add_comment.php'>" . 
      "<span class='desc_header'>" .
      "kommentera!<br />\n". 
      "datum: " . date("d") . "/" . date("m") . " " . date("Y") . 
      "</span><br>\n" . 
      "  <input name='comment_month' type='hidden' value='" . date("m") . "'></input>\n" . 
      "  <input name='comment_date' type='hidden' value='" . date("d") . "'></input>\n" .  
      "  <input name='comment_year' type='hidden' value='" . date("Y") . "'></input>\n" . 
      "  <input name='the_id' type='hidden' value='" . "$the_id" . "'></input>\n" .  
      "  namn:<br />\n" . 
      "  <input name='comment_author_name' class='wheatblog_input' type='text' /><br />\n" . 
      "  email:<br />\n" . 
      "  <input name='comment_author_email' class='wheatblog_input' type='text' /><br />\n" . 
      "  hemsida:<br />\n" . 
      "  <input name='comment_author_url' class='wheatblog_input' type='text' /><br />\n" . 
      "  kommentar:<br />\n" . 
      "  <textarea name='comment_body' class='wheatblog_textarea'></textarea><br />\n" .
      "<br />\n" .  
      "  <input type='submit' value='skicka'></input><br />\n" . 
      "</form>\n" . 
      "</div>\n\n" 
    );
?>

<?php
  // include the footer
  include("$prefix" . "whp_footer.inc");
?>