<?PHP
#
#   FILE:  AddForum.php
#
#   Part of the Collection Workflow Integration System (CWIS)
#   Copyright 2012-2013 Edward Almasy and Internet Scout Research Group
#   http://scout.wisc.edu/cwis/
#

# ----- EXPORTED FUNCTIONS ---------------------------------------------------

/**
* Print the forum heading
*/
function PrintForumHeading()
{
    $Edit = isset($_SESSION["Edit"]) ?
        $_SESSION["Edit"] : 0 ;

    if ($Edit > 0)
    {
        print "Edit Forum";
    }
    else
    {
        print "Add New Forum";
    }
}

/**
* Print the forum update or add button
*/
function PrintForumButton()
{
    $Edit = isset($_SESSION["Edit"]) ?
        $_SESSION["Edit"] : 0 ;

    if ($Edit > 0) {
        print "Update Forum"; }
    else {
        print "Add New Forum"; }
}

/**
* Print the forum name
*/
function PrintForumName()
{
    $F_ForumName = isset($_SESSION["F_ForumName"]) ?
        $_SESSION["F_ForumName"] : "";

    print defaulthtmlentities($F_ForumName);
}

/**
* Print the forum description
*/
function PrintForumDescription()
{
    $ForumDescription = isset($_SESSION["F_ForumDescription"])?
        $_SESSION["F_ForumDescription"] : "";

    print defaulthtmlentities($ForumDescription);
}

/**
* Display list of forums from a query
* @param OptionList $FieldName Optionlist containing the
* forum option list
*/
function DisplayForums($FieldName)
{
    $DB = new Database();

    $F_ForumId = isset($_SESSION["F_ForumId"]) ?
        $_SESSION["F_ForumId"] : NULL ;

    $Query = "SELECT * FROM Forums ORDER BY ForumName";
    $DB->Query($Query);

    $Selected=0;
    $Items = array();
    while ($Entry = $DB->FetchRow())
    {
        $Forum = new Forum($Entry["ForumId"]);
        if ($Selected == 0 && is_null($F_ForumId)) {
            $F_ForumId = $Entry["ForumId"]; }

        $Items[$Entry["ForumId"]] = $Forum->ForumName();
        $Selected++;
    }
    PrintOptionList($FieldName, $Items, $F_ForumId, 0, 4, NULL, FALSE);
}

/**
* Display a list of users
* @param OptionList $Moderator OptionList Containing the user options
*/
function DisplayUserList($Moderator)
{
    $Selected = NULL;
    $F_ForumId = isset($_SESSION["F_ForumId"]) ?
        $_SESSION["F_ForumId"] : NULL ;

    $UFactory = new CWUserFactory();
    $Items = $UFactory->GetUsersWithPrivileges(PRIV_FORUMADMIN);

    # display selected moderator
    if (isset($F_ForumId))
    {
        $Forum = new Forum($F_ForumId);
        $Selected = $Forum->ModeratorId();
    }
    if (count($Items))
    {
        PrintOptionList($Moderator, $Items, $Selected, NULL, 1, NULL, FALSE);
    }
    else
    {
        print("(NO USERS AVAILABLE WITH FORUM ADMIN PRIVILEGES!)");
    }
}

/**
* Print error messages, if error messages exist
*/
function PrintErrorMessages()
{
    $ErrorMessages = array();
    if (isset($_SESSION["ErrorMessages"]))
    {
        $ErrorMessages = $_SESSION["ErrorMessages"];
        unset($_SESSION["ErrorMessages"]);
    }

    # if error messages were passed
    if (isset($ErrorMessages) && is_array($ErrorMessages) &&
        count($ErrorMessages) > 0)
    {
        # print error messages
        print("<ul>\n");
        foreach ($ErrorMessages as $Message)
        {
            printf("<li style='color: red;'>%s</li>\n", $Message);
        }
        print("</ul>\n");
    }
}

/**
* Fetch the forums as forum objects
* @return an array of forums indexed by forum id
*/
function GetForums()
{
    $Database = new Database();

    $Query = "SELECT * FROM Forums ORDER BY ForumName";
    $Database->Query($Query);

    $Forums = array();

    while (FALSE !== ($Row = $Database->FetchRow()))
    {
        $ForumId = $Row["ForumId"];
        $Forums[$ForumId] = new Forum($ForumId);
    }

    return $Forums;
}

/**
* Fetch the number of forums in this CWIS install
* @return int $Count the number of forums
*/
function GetForumCount()
{
    $Database = new Database();

    $Query = "SELECT * FROM Forums ORDER BY ForumName";
    $Database->Query($Query);

    $Count = $Database->NumRowsSelected();

    return $Count;
}

# ----- LOCAL FUNCTIONS ------------------------------------------------------

# ----- MAIN -----------------------------------------------------------------

PageTitle("Add/Remove Forums");

CheckAuthorization(PRIV_SYSADMIN, PRIV_FORUMADMIN);

include_once("lib/AxisPHP/Axis--StandardLibrary.php");
