<?php
    //-----------------------------------------------------------------------------------------------
    // content.php
    //
    // implements a simple article publishing system (let's call it SCS -- simple content system)
    // similar to the blog in its internals, but the layout and a few things are different.  This
    // is where a user puts the larger things they don't want in a blog (reviews, articles, etc)
    //
    //
    //
    //
    //
    //----------------------------------------------------------------------------------------------
   
    global $siteSettings;
     
    // Get permissions *once*
    if (isInGroup($CURRENTUSER, "admin"))
    {
        $verifyEditDelete = true;
    }
    else
    {
        $verifyEditDelete = false;
    }
     
     
     
    switch ($action):
     
     
    //-------------------------------------------------------------------------------------
    // g_default -- default display of blog
    //-------------------------------------------------------------------------------------
    case "g_default":
     
     
    $content = mysql_query("SELECT ID,author, title,summary,category,date FROM content ORDER BY date DESC LIMIT 0,20") or DIE(mysql_error());
    while ($row = mysql_fetch_array($content) )
    {
        $formattedBody = format_post($row['summary']);
        drawMainTop();
         
         
        print("<div class=\"contenttitle\">");
         
        if ($siteSettings['slickurls'] == TRUE )
            //print("<a class=\"contentlinkfont\" href='". $siteSettings['siteurl'] . "/content/" . $row['ID'] . "'>" . $row['title'] . "</a><br />");
        print("<a class=\"contentlinkfont\" href='". $siteSettings['siteurl'] . "/content/" . $row['ID'] . "/" . make_url_friendly($row['title']) . "'>" . $row['title'] . "</a><br />");
        else
            print("<a class=\"contentlinkfont\" href='index.php?shard=" . $shard . "&amp;action=g_viewContent&amp;ID=" . $row['ID'] . "'>" . $row['title'] . "</a><br />");
        print("<small>Author: " . $row['author'] . "</small><br />");
        print("<small>Category: " . $row['category'] . "</small><br />");
        print("<small>Written: " . date("F dS Y", $row['date']) . "</small><br />");
        //at " . date("g:ia l, F dS, Y", $row['date']) . "</small><br />");
         
        print("</div>"); //content title
        //draw_pre_post( );
        print("<div class=\"contentbox\">");
        print($formattedBody . '<br />' );
         
         
         
        if ($siteSettings['slickurls'] == TRUE )
        {
            print("<small><span class=\"floatright\"><a href='". $siteSettings['siteurl'] . "/content/" . $row['ID'] . "/" . make_url_friendly($row['title']) . "'>[full article]</a>
                </span></small><br />" );
             
        }
        else
            print("<small><span class=\"floatright\"><a href='index.php?shard=content&amp;action=g_viewContent&amp;ID=" . $row['ID'] ."'>[full article]</a>
            </span></small><br />" );
         
         
         
        print("</div>"); //contentbox
        //draw_post_post( );
         
         
        drawMainBottom();
         
    }
     
     
    break;
     
    //-------------------------------------------------------------------------------------
    // g_viewPost - view a specific post
    //-------------------------------------------------------------------------------------
    case "g_viewContent":
     
    $content = mysql_query("SELECT * FROM content WHERE ID=$_REQUEST[ID]") or DIE(mysql_error() );
     
    while ($row = mysql_fetch_array($content) )
    {
        drawMainTop();
        $formatted = format_post($row['body'] );
        print("<div class=\"contenttitle\">");
        print("<h2>" . $row['title'] . "</h2>");
         
        print("<small>Author: " . $row['author'] . "</small><br />");
        print("<small>Category: " . $row['category'] . "</small><br />");
        print("<small>Written: " . date("F dS Y", $row['date']) . "</small><br />");
         
        print("</div>");//contenttitle
        draw_pre_post();
        print($formatted );
        draw_post_post();
         
         
        if ($verifyEditDelete == true )
        {
            if ($siteSettings['slickurls'] == TRUE )
            {
                print "[<a href='" . $siteSettings['siteurl'] . "/index.php?shard=content&amp;action=g_edit&amp;ID=" . $row['ID']. "'>Edit</a>]
                    [<a href='" . $siteSettings['siteurl'] . "/index.php?shard=content&amp;action=delete&amp;ID=" . $row['ID'] . "'>Delete</a>]";
            }
            else
            {
                print "[<a href='index.php?shard=content&amp;action=g_edit&amp;ID=" . $row['ID']. "'>Edit</a>]
                    [<a href='index.php?shard=content&amp;action=delete&amp;ID=" . $row['ID'] . "'>Delete</a>]";
            }
        }
         
         
         
        drawMainBottom();
    }
     
    break;
     
    //-------------------------------------------------------------------------------------
    // g_crt_new - create a new blog entry (show the form)
    //-------------------------------------------------------------------------------------
    case "g_crt_new":
     
     
    if (isInGroup($CURRENTUSER, "admin"))
    {
        drawMainTop();
         
         
        if ($siteSettings['slickurls'] == TRUE )
            $formaction = $siteSettings['siteurl'] . "/index.php?shard=content&amp;action=proc_new";
        else
            $formaction = 'index.php?shard=content&amp;action=proc_new';
         
         
        print("
            Create New Content<br /><br />
            <form action=" . $formaction . " method=POST>
             
             
            Title:
            <br />
            <input class='titleinput' type='text' name='title' />
            <br />
             
            Category:
            <br />
            <input class='titleinput' type='text' name='category' />
            <br />
             
            Summary:
            <br />
            <input class='titleinput' type='text' name='summary' />
            <br />
             
            Content Body:
            <br />
            <textarea class='bodyinput' name='body' rows='20' cols='40'></textarea>
            <br />
             
             
            Click Submit to Publish Content<br />
            <input type='Submit' class='button' name='Submit' value='Submit'></form>");
         
         
        drawMainBottom();
    }
    break;
    //-------------------------------------------------------------------------------------
    // proc_new - process a new blog post (insert the blog)
    //-------------------------------------------------------------------------------------
    case "proc_new":
     
    $cleanbody = htmlentities($_REQUEST['body'] );
     
    $inTime = time();
    $result = mysql_query("insert into content
        (author, title, summary, category, body, date)
        VALUES
        ('$CURRENTUSER', '$_REQUEST[title]', '$_REQUEST[summary]', '$_REQUEST[category]', '$cleanbody', '$inTime')")
    or DIE(mysql_error() );
     
     
    generate_content_rss();
     
     
    if ($siteSettings['slickurls'] == TRUE )
        header("Location: " . $siteSettings['siteurl'] . "/content");
    else
        header("Location: index.php?shard=content");
    exit();
    break;
     
     
     
     
    //-------------------------------------------------------------------------------------
    // g_edit - create a new blog entry (show the form)
    //-------------------------------------------------------------------------------------
    case "g_edit":
     
    if (isInGroup($CURRENTUSER, "admin"))
    {
        $editcontent = mysql_query("SELECT title, category,summary,body FROM content WHERE ID=" . $_REQUEST['ID'] ) or DIE(mysql_error());
        $editdata = mysql_fetch_array($editcontent );
         
         
        drawMainTop();
         
        if ($siteSettings['slickurls'] == TRUE )
            $formaction = $siteSettings['siteurl'] . "/index.php?shard=content&amp;action=proc_edit";
        else
            $formaction = 'index.php?shard=content&amp;action=proc_edit';
         
         
        print("
            Create New Content<br /><br />
            <form action=". $formaction . " method=POST>
             
             
            Title:
            <br />
            <input class='titleinput' type='text' name='title' value='". $editdata['title']. "' />
            <br />
             
            Category:
            <br />
            <input class='titleinput' type='text' name='category' value='" . $editdata['category']. "' />
            <br />
             
            Summary:
            <br />
            <input class='titleinput' type='text' name='summary' value='" . $editdata['summary'] . "' />
            <br />
             
            Content Body:
            <br />
            <textarea class='bodyinput' name='body' rows='20' cols='40'>" . $editdata['body']. "</textarea>
            <br />
             
             
            Click Submit to Publish Content<br />
            <input type='hidden' name='ID' value='$_REQUEST[ID]' />
            <input type='Submit' class='button' name='Submit' value='Submit'></form>");
         
         
        drawMainBottom();
    }
    break;
     
     
     
     
    //-------------------------------------------------------------------------------------
    // proc_edit - process the edited blog (submit into database)
    //-------------------------------------------------------------------------------------
    case "proc_edit":
     
    if (isInGroup($CURRENTUSER, "admin"))
    {
        $cleanmsg = htmlentities($_REQUEST['body'] );
         
        $result = mysql_query("update content set title='$_REQUEST[title]', body='$cleanmsg' where ID=$_REQUEST[ID]") or DIE(mysql_error());
         
         
        if ($siteSettings['slickurls'] == TRUE )
            header("Location: " . $siteSettings['siteurl'] . "/content");
        else
            header("Location: index.php?shard=content");
        exit();
    }
     
    break;
     
     
     
     
    break;
    //-------------------------------------------------------------------------------------
    // delete - delete a blog entry
    //-------------------------------------------------------------------------------------
    case "delete":
    if (isInGroup($CURRENTUSER, "admin"))
    {
        $result = mysql_query("delete from content where ID=".$_REQUEST['ID']  );
        if ($siteSettings['slickurls'] == TRUE )
            header("Location: " . $siteSettings['siteurl'] . "/content");
        else
            header("Location: index.php?shard=content");
         
         
    }
    break;
    endswitch;
     
     
     
?>
