<?php
#
#   FILE:  SPT--SelectEditUserList.php
#
#   FUNCTIONS PROVIDED:
#
#   FUNCTIONS EXPECTED:
#
#   Part of the Scout Portal Toolkit
#   Copyright 2003 Internet Scout Project
#   http://scout.cs.wisc.edu
#

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

# function to print the index value
function PrintIndex()
{
    global $Index;

    print $Index;
}

# functon to print Edit User Link
function PrintEditUserLink()
{
    global $FoundUserArray;

    $UserName = $FoundUserArray["UserName"];
    print "index.php?P=EditUser&amp;F_UserName=".$UserName;
}

# functon to print the User Description
function PrintUserName()
{
    global $FoundUserArray;

    $UserName = $FoundUserArray["UserName"];
    print htmlentities(stripslashes($UserName));
}

function PrintRealName()
{
    global $FoundUserArray;

    $RealName = $FoundUserArray["RealName"];
    print htmlentities(stripslashes($RealName));
}

function PrintEMail()
{
    global $FoundUserArray;

    $EMail = $FoundUserArray["EMail"];
    print htmlentities(stripslashes($EMail));
}

function PrintLastLoginDate()
{
    global $FoundUserArray;

    $LastLoginDate = $FoundUserArray["LastLoginDate"];
    print htmlentities(stripslashes($LastLoginDate));
}

function PrintAddress()
{
    global $FoundUserArray;

    $Address = $FoundUserArray["AddressLineOne"];
    print htmlentities(stripslashes($Address));
}

function PrintCity()
{
    global $FoundUserArray;

    $City = $FoundUserArray["City"];
    print htmlentities(stripslashes($City));
}

function PrintState()
{
    global $FoundUserArray;

    $State = $FoundUserArray["State"];
    print htmlentities(stripslashes($State));
}

function PrintZipCode()
{
    global $FoundUserArray;

    $ZipCode = $FoundUserArray["ZipCode"];
    print htmlentities(stripslashes($ZipCode));
}

function PrintCountry()
{
    global $FoundUserArray;

    $Country = $FoundUserArray["Country"];
    print htmlentities(stripslashes($Country));
}

# function to print the UserId
function PrintUserId()
{
    global $FoundUserArray;

    print $FoundUserArray["UserId"];
}

# function to return if any enabled option list fields exist
function UsersExist()
{
    global $UserArray;

    return count($UserArray);
}

# function to print number of users found
function PrintUsersFound()
{
    global $UserArray;

    print count($UserArray);
}

# display controlled name lists for editing
function DisplayUserList()
{
    global $Session;
    global $UFactory;
    global $UserArray;
    global $Index;
    global $FoundUserArray;
    global $NumberOfResults;
    global $StartingResult;
    global $ResultsPerPage;
    global $DB;

    PrintBegin();

    $NumberOfResults = $UFactory->GetMatchingUserCount();

    foreach ($UserArray as $UserId => $FoundUserArray)
    {
        DisplayUser();
    }
}

function PreviousResultsAvailable()
{
    global $StartingResult;

    return ($StartingResult > 0) ? TRUE : FALSE;
}

function NextResultsAvailable()
{
    global $StartingResult;
    global $ResultsPerPage;
    global $NumberOfResults;

    return (($StartingResult + $ResultsPerPage) < $NumberOfResults) ? TRUE : FALSE;
}

function PrintPreviousResultsLink()
{
    global $StartingResult;
    global $ResultsPerPage;

    $NewStartingResult = max(($StartingResult - $ResultsPerPage), 0);

    $PreviousLink = "index.php?P=SelectEditUserList"
        ."&amp;ResultsPerPage=".$ResultsPerPage
        ."&amp;StartingResult=". $NewStartingResult
        ."&amp;Text=";

    # search text from POST or GET?
    if (!empty($_POST['F_SearchText']))
        $PreviousLink .= $_POST['F_SearchText'];
    elseif (!empty($_GET['Text']))
        $PreviousLink .= $_GET['Text'];

    # search field from POST, GET or not at all?
    if (!empty($_POST['F_SearchField']))
        $PreviousLink .= "&Field=". $_POST['F_SearchField'];
    elseif(!empty($_GET['Field']))
        $PreviousLink .= "&Field=". $_GET['Field'];

    print($PreviousLink);
}

function PrintNextResultsLink()
{
    global $StartingResult;
    global $ResultsPerPage;

    $NewStartingResult = $StartingResult + $ResultsPerPage;

    $NextLink = "index.php?P=SelectEditUserList"
        ."&amp;ResultsPerPage=".$ResultsPerPage
        ."&amp;StartingResult=". $NewStartingResult
        ."&amp;Text=";

    # search text from POST or GET?
    if (!empty($_POST['F_SearchText']))
        $NextLink .= $_POST['F_SearchText'];
    elseif (!empty($_GET['Text']))
        $NextLink .= $_GET['Text'];

    # search field from POST, GET or not at all?
    if (!empty($_POST['F_SearchField']))
        $NextLink .= "&Field=". $_POST['F_SearchField'];
    elseif(!empty($_GET['Field']))
        $NextLink .= "&Field=". $_GET['Field'];

    print($NextLink);
}

function PrintNumberOfPreviousResults()
{
    global $ResultsPerPage;

    print($ResultsPerPage);
}

function PrintNumberOfNextResults()
{
    global $ResultsPerPage;
    global $StartingResult;
    global $NumberOfResults;

    print(min($ResultsPerPage, ($NumberOfResults - ($StartingResult + $ResultsPerPage))));
}

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

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

# non-standard global variables
global $FoundUserArray;
global $Index;
global $NumberOfResults;
global $ResultsPerPage;
global $StartingResult;
global $UFactory;
global $UserArray;

if (!CheckAuthorization(PRIV_SYSADMIN, PRIV_USERADMIN)) {  return;  }

$UFactory = new UserFactory($Session);
$UserArray = NULL;

if (isset($_GET["StartingResult"]))
    $StartingResult = $_GET["StartingResult"];
else
    $StartingResult = 0;

# hard code results per page for 20 so it is easily changed
if (isset($_GET["ResultsPerPage"]))
    $ResultsPerPage = $_GET["ResultsPerPage"];
else
    $ResultsPerPage = 20;

if (isset($_POST["Submit"]) &&
        $_POST["Submit"] == "Search for User(s)")
{
    if (!empty($_POST["F_SearchText"]))
    {
        $UserArray = $UFactory->GetMatchingUsers(
            $_POST["F_SearchText"],
            $_POST["F_SearchField"],
            $_POST["F_SearchField"],
            $StartingResult,
            $ResultsPerPage);
    }
    else
    {
        $AF->SetJumpToPage("SelectEditUser");
        return;
    }
}
# return all users
else if (isset($_POST["Submit"]) &&
        $_POST["Submit"] == "Return All Users")
{
    $UserArray = $UFactory->GetMatchingUsers(
        ".*.",
        NULL,
        "UserName",
        $StartingResult,
        $ResultsPerPage);
}
# came here from a next/previous link
else
{
    if (!empty($_GET["Text"]))
    {
        $UserArray = $UFactory->GetMatchingUsers(
            $_GET["Text"],
            $_GET["Field"],
            $_GET["Field"],
            $StartingResult,
            $ResultsPerPage);
    }
    else
    {
       $AF->SetJumpToPage("SelectEditUser");
       return;
    }
}

PageTitle("Edit/Delete Selected User(s)");

?>
