Location: PHPKode > projects > SQL Familytree > document/document.php
<?php

//////////////////////////////////////////////////////////////////////
//
// $Id: document.php,v 1.15 2002/02/10 00:35:00 carney Exp $
//
// Copyright (C) 2002 Michael Carney
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

//////////////////////////////////////////////////////////////////////

include_once ("util/misc.php");

// First, some utility routines

function emailForm($id,$member)
{
    global $theme,$t;
    $default = 1;
    $label   = "Yes"; 
    $result  = db_query("select * from doc_email where ((member='$member') && (document='$id'))");
    if (mysql_num_rows($result) > 0)
    {
        $checked = "checked"; 
    }
    global $HTTP_GET_VARS, $PHP_SELF;
    $var   = format_get_args($HTTP_GET_VARS); 
    $rval  = "<form border=0 action=\"$PHP_SELF$var\" method=\"post\">";
    $rval .= "<input type=\"hidden\" name=\"mode\" value=\"email_doc_responses\">\n"; 
    $rval .= "<input type=\"hidden\" value=\"$member\" name=\"email_doc_responses_member\">";
    $rval .= "<input type=\"hidden\" value=\"$id\" name=\"email_doc_responses_doc\">";
    $rval .= "<input border=0 type=\"checkbox\" value=\"1\" name=\"email_doc_responses_on\" $checked>\n"; 
    $rval .= "Notify with comments on this document?<br>";
    $rval .= "<input type=\"submit\" border=0 name=\"Submit\" title=\"Submit\" value=\"Submit\">\n"; 
    $rval .= "</form>\n";
    return $rval;
}

function updateActivity($id,$when)
{
    while ($id != 0)
    {
        db_query("update document set last_activity='$when' where id='$id'");
        $result = db_query("select document from document where id='$id'");
        list ($id) = mysql_fetch_row($result);
    }
}

function emailNotification($id)
{
    $to       = array();
    $email_id = $id; 
    $result   = db_query("select member,title,bodytext from document where id='$id'");
    list ($poster, $email_subject, $email_body) = mysql_fetch_row($result);
    while ($id != 0)
    {
        $result = db_query("select member from doc_email where document='$id'");
        while (list($member) = mysql_fetch_row($result))
        {
            if ($member != $poster)
            {
                $member_result = db_query("select email from member where id='$member'");
                if (mysql_num_rows($member_result) == 1)
                {
                    list($member_email) = mysql_fetch_row($member_result);
                    array_push($to, $member_email);
                }
            }
        }
        $result = db_query("select document from document where id='$id'");
        list ($id) = mysql_fetch_row($result);
    }
    if (count($to) > 0)
        // We got some people to send it to, so send it off.
    {
        $result = db_query("select name,email from member where id='$poster'");
        if ($vars = mysql_fetch_assoc($result))
        {
            $member_name  = $vars["name"];
            $member_email = $vars["email"]; 
            global $SERVER_NAME;
            $url = "\r\n\r\nDocument: <a href=\"http://$SERVER_NAME/document.php?did=$email_id\">$email_subject</a>\r\n\n";
            $email_body = "<pre>$url$email_body</pre>"; 
            if ($member_name != "")
                $headers = "From: $member_name <$member_email>\r\n";
            else
                $headers = "From: $member_email\r\n";
            $headers .= "MIME-Version: 1.0\r\n";
            $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
            
            $tolist   = join(",", $to);
            $sent     =  mail($tolist,$email_subject, $email_body, $headers);
        }
    }
}

function incResponseCount($id)
{
    while ($id != 0)
    {
        db_query("update document set responses=responses+1 where id='$id'");
        $result = db_query("select document from document where id='$id'");
        list ($id) = mysql_fetch_row($result);
    }
}

function getTopDocument($id)
{
    $top = 0; 
    while ($id != 0)
    {
        $top = $id; 
        $result = db_query("select document from document where id='$id'");
        list ($id) = mysql_fetch_row($result);
    }
    return $top;
}

function getDiscussionThread($id, $mode)
{
    function i_getMemberName($member)
    {
        $result = db_query("select * from member where id='$member'");
        if (mysql_num_rows($result) == 0)
            return "";

        $vars = mysql_fetch_assoc($result);
        if ($vars["name"] != "")
            return $vars["name"];
        else
            return $vars["email"];
    }
    function i_AddDiscussionThread($vars, $mode, $level=0)
    {
        $id = $vars["id"]; 
        if ($level > 0)
        {
            global $p;
            $base_indent = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
            $indent      = "";
            for ($i = 0; $i < $level; $i++)
            {
                $indent .= $base_indent;
            }
            
            if ($mode == "thread")
            {
                $row["discuss_indent"] = $indent; 
                $row["discuss_url"]    = "document.php?did=".$vars["id"]."&discuss=thread"; 
                $row["discuss_author"]     = i_getMemberName($vars["member"]);
                $row["discuss_subtext"]    = $vars["subtext"];
                $row["discuss_title"]      = $vars["title"];
                $p->t->assign($row);
                $p->t->parse("discussItem", ".discussItem");
            }
            /*
        else
        {
            if ($level != 0)
            {
                include_once("util/manila.php");
                $bodytext = stripslashes(manilaDisplayProcess($vars["bodytext"]));
                
                $htmltext .= "<hr size=2 noshade>\n";
                $htmltext .= "<a name=\"$id\">\n" ;
                $htmltext .= "<h2>$title</h2>\n";
                $htmltext .= "<small>By $author</small>\n"; 
                $htmltext .= "<p>$subtext\n";
                $htmltext .= "<p>$bodytext\n";
            }
        }
            */
        }
        $result = db_query("select * from document where document='$id'"); 
        while ($vars = mysql_fetch_assoc($result))
        {
            i_AddDiscussionThread($vars, $mode, $level+1);
        }
    }

    if ($id != 0)
    {
        $result = db_query("select * from document where id='$id'");
        $vars   = mysql_fetch_assoc($result); 
        i_AddDiscussionThread($vars, $mode);
    }
}

function getEditLink($id, $member=0)
{
    global $s,$p;
    $rval = ""; 
    if ($id != 0)
    {
        if ((($member != 0)
             && (($s->getVar("id") == $member))
             || ($s->getVar("member_level") > 1)))
            // A member is logged in, and it's the same member that wrote
            // the article, or it's an admin
        {
            include_once("util/manila.php");
            $href = "document.php?did=$id&mode=edit";
            $rval = $href; 
        }
    }
    return $rval;
}

function getCommentLink($id)
{
    $rval = "" ;
    if ($id != 0)
    {
        global $discuss;
        include_once("util/manila.php");
        $image = "<img src=\"qbullets/note.gif\" border=0 hspace=4>";
        if ($discuss == 0)
            $rval = "document.php?did=$id&discuss=thread#discussion";
        else
            $rval = "document.php?did=$id";
    }
    return $rval;
}

// 
// Now the main routines that are needed to support the base document
// 
function document_submit_handler(&$vars,&$errors)
{
    global $s;

    include_once ("util/manila.php");

    if (trim($vars["title"]) == "")
    {
        $errors["title"] = "blankTitle";
        return;
    }
        
    global $auto_form_mode;
    if ($auto_form_mode == "new")
    {
        $vars["member"] = $s->getVar("id"); 
    }
    return 0;
}

function document_post_submit_handler(&$vars, $id)
{
    include_once("util/misc.php");
    include_once ("util/concordance.php");
    $link   = "<a href=\"document.php?did=$id\">".$vars["title"]."</a>"; 
    $handle = flushConcordance($link,"Document","document",$id,$vars["subtext"]);
    updateConcordance($vars["bodytext"],$handle);
    updateConcordance($vars["subtext"],$handle);
    updateConcordance($vars["title"],$handle);
    
    updateActivity($id,$vars["updated"]);
    global $auto_form_mode;
    if ($auto_form_mode == "new")
        incResponseCount($vars["document"]);
    emailNotification($id);
    return "document.php?did=$vars[id]";
}

function document_display_handler($id)
    // Go off and munge the display content as needed
{
    include_once("util/manila.php");
    
    global $p,$category,$search,$display_mode,$discuss;
    if ($id != 0)
    {

        $result                   = db_query("select * from document where id='$id'");
        if (mysql_num_rows($result) == 0)
        {
            $p->setSectionVars(array("title"=>"No Such Document!",
                                     "page_body"=>"Please check somewhere else!"));
            return;
        }

        $vars           = mysql_fetch_assoc($result);
        if ($vars["doc_type"] != 0)
        {
            $doc_type_id     = $vars["doc_type"];
            $doc_result      = db_query("select * from doc_type where id='$doc_type_id'");
            $doc_vars        = mysql_fetch_assoc($doc_result);
            $display_handler = $doc_vars["subtable"]."_display_handler";
            if (file_exists("document/$doc_vars[subtable].php"))
            {
                include_once("document/$doc_vars[subtable].php");
                $p->t->define(array("doc_body"=>$doc_vars["template"]));
                if (function_exists($display_handler))
                    $vars = array_merge($vars, $display_handler($id));
            }
        }
        else
            $p->t->define(array("doc_body"=>"article.thtml"));
        $p->t->define(array("page_body"=>"document.thtml"));

        $vars["mailto_href"]       = "";
        $vars["comment_href"]      = getCommentLink($id);
        $vars["edit_href"]         = getEditLink($id); 
        $vars["email_form"]        = "";
        $vars["discussion_thread"] = ""; 
        global $discuss;
        global $s;
        if (isset($discuss))
        {
            $p->t->define(array("discussion_thread"=>"discussion_thread.thtml"));
            getDiscussionThread($id,$discuss);
            if ($s->getVar("id") != 0)
            {
                include_once("util/autoform.php");
                include_once("util/misc.php");
                $title = $vars["title"];
                if (ereg("^[Rr][Ee]:", $title) == 0)
                    $title = "Re: $title";
                global $form_input;
                global $HTTP_GET_VARS,$PHP_SELF;
                $form_input["document"]["title"]    = $title;
                $form_input["document"]["document"] = $id;
                $discuss_vars["response_form"]              = buildForm("document","",0,1);
            }
            else
                $discuss_vars["response_form"] = ""; 
            $p->t->assign($discuss_vars);
            $p->t->parse("discussion_thread", "discussion_thread");
            $p->t->parse("discussionArea", ".discussionArea");
        }
        if ($s->getVar("id") != 0)
        {
            $vars["email_form"]    = emailForm($id,$s->getVar("id")); 
        }
        if ($vars["member"] != 0)
        {
            $member_id = $vars["member"]; 
            $member    = $vars["member"]; 
            $result    = db_query("select name,email,avatar,member_level from member where id='$member'");
            list($name,$email,$avatar,$member_level) = mysql_fetch_row($result);
            if ($name != "")
                $vars["member"] = $name;
            else
                $vars["member"]  = $email;
            $result              = db_query("select item from member_level where id='$member_level'");
            list($vars["member_title"]) = mysql_fetch_row($result); 
            $vars["mailto_href"] = "mailto:$email";
            $vars["avatar"]      = ""; 
            if ($avatar != "")
                $vars["avatar"] = imageRef($avatar,array("show_thumbnail"=>"yes",
                                                         "hspace"=>"4")); 
        }
        if (($s->getVar("id") == $vars["member"])
            || ($s->getVar("member_level") > 2))
        {
            $editDocument = true;
        }

        $topDocument           = getTopDocument($id);
        $vars["updated"]       = fix_time($vars["updated"],true);
        $vars["created"]       = fix_time($vars["created"],true);
        $vars["last_activity"] = fix_time($vars["last_activity"],true);
        $vars["bodytext"]      = manilaDisplayProcess($vars["bodytext"]);
        $vars["subtext"]       = manilaDisplayProcess($vars["subtext"]); 
        $vars["parent_href"]   = "document.php?did=$vars[document]&discuss=thread"; 
        $vars["top_href"]      = "document.php?did=$topDocument&discuss=thread"; 
        $p->t->assign($vars);
        if ($editDocument)
            $p->t->parse("editDocument", ".editDocument");
        if ($vars["document"] != 0)
        {
            $p->t->parse("parentDocument", ".parentDocument");
            if ($topDocument != 0)
                $p->t->parse("topDocument", ".topDocument");
        }
        if ($s->getVar("member_level") >= 2)
            // Add in the editors tools
        {
            if ($s->getVar("member_level") >= 3)
                // Add in the managing editor stuff
            {
                $managing_editors = array("homepage_button"=>"",
                                          "delete_button"=>"Delete Document");
                $managing_editors["homepage_button"]   = "<input border=0 type=\"radio\" name=\"admin_action\" value=\"makeHomepage\">";
                $managing_editors["delete_button"] = "<input border=0 type=\"radio\" name=\"admin_action\" value=\"deleteDocument\">";
                $managing_editors["delete_comments_button"] = "<input border=0 type=\"radio\" name=\"admin_action\" value=\"deleteDocumentAndComments\">";
                $p->t->assign($managing_editors);
                $p->t->parse("managingEditorsOnly", ".managingEditorsOnly");
            }
            $p->t->parse("editorsOnly", ".editorsOnly");
        }
        $p->t->parse("doc_body", "doc_body");
        $p->t->parse("page_body", "page_body");

        /*
        if ($discuss == "topic")
        {
            $orig_id = $id; 
            $parent  = $row["document"]; 
            while ($parent != 0)
            {
                $result = mysql_query("select document from document where id='$id'");
                if ($result != 0)
                {
                    if (mysql_num_rows($result) == 1)
                    {
                        $id     = $parent;
                        list($parent) = mysql_fetch_row($result);
                    }
                    else
                        $parent = 0; 
                }
                else
                    $parent = 0; 
            }
            if ($orig_id != $id)
                // We walked up the parent tree to the top, now we need to
                // redirect to display that one
            {
                redirect("document.php?did=$id&discuss=topic#$orig_id");
            }
        }
        */
        
        db_query("update document set reads=reads+1 where id='$id'");
        // first update all the variables for the base class

        /*
        $vars                     = $row; 
        $category                 = $vars["category"];

        $member_id            = 0; 
        if ($vars["revision"] > 1)
            $vars["revision"]      = "<b><a href=\"document.php?did=$id&mode=rev_history\">".$vars["revision"]."</a></b>"; 
        // Go mark up any words that match what search was done (if there was
        // one). 
        if ($search != "")
        {
            $vars["bodytext"]      = showSearchResults($vars["bodytext"], $search); 
        }
        $p->openPageSection('document', $vars);
        getEditLink($id,$member_id);
        $p->closePageSection('document');
        $p->clearPageSubSection("editDocument");
        $doc_footer["discussionThread"] = "";
        $doc_footer["response_form"]    = "";
        $doc_footer["email_form"]       = "";
        global $discuss;
        global $s;
        if (isset($discuss))
        {
            $doc_footer["discussionThread"] = getDiscussionThread($id,$discuss);
            if ($s->getVar("id") != 0)
            {
                include_once("util/autoform.php");
                include_once("util/misc.php");
                $title = $row["title"];
                if (ereg("^[Rr][Ee]:", $title) == 0)
                    $title = "Re: $title";
                global $form_input;
                global $HTTP_GET_VARS,$PHP_SELF;
                $form_input["document"]["title"]           = $title;
                $form_input["document"]["document"]        = $id;
                $doc_footer["response_form"] = "<hr>".buildForm("document","",0,1);
            }
        }
        if ($s->getVar("id") != 0)
        {
            $doc_footer["email_form"]    = emailForm($id,$s->getVar("id")); 
        }
        $p->openPageSection('doc_footer', $doc_footer);
        $p->closePageSection('doc_footer');
        */
    }
}

?>
Return current item: SQL Familytree