<?php

/*  Alternate WebKit
(C) 2011 by Alternate Tools
-----------------------------------------------------

File:         modify_user.php
Description:  Administrative module to unlock/lock/delete an user/customer

*/

    include "../modules/class_user.php";
    include "../modules/mod_admin.php";

    $bUserLoadFailed = false;
    $usrId = "";
    $errorString = "";
    
    // Check privileges
    check_login();
    if (user_is_reporter() == true || user_is_customer() == true)
    {
        log_out();
        return;
    }

    // Get action
    $usrModAction = "unlock";
    if (isset ($_POST['usr_action']) == true)
    {
        if ($_POST['usr_action'] == "lock")
            $usrModAction = "lock";
        else if ($_POST['usr_action'] == "delete")
            $usrModAction = "delete";
    }
    else if (isset ($_GET['action']) == true)
    {
        if ($_GET['action'] == "lock")
            $usrModAction = "lock";
        else if ($_GET['action'] == "delete")
            $usrModAction = "delete";
    }

    $usrUser = new cUser;
    $usrUser->SetFolder($USER_FOLDER);

    if (isset ($_POST['usr_id']) == true)
        $usrId = $_POST['usr_id'];
    else if (isset ($_GET['id']) == true)
        $usrId = $_GET['id'];

    if ($usrUser->LoadUserFromId($usrId) == false)
    {
        print_error_message(get_translation('admin_usrmod_errorload') . ', ID: ' . $usrId . '!!');
        $bUserLoadFailed = true;
    }

    // Cancel modification?
    if (isset ($_POST['usr_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_USERS_SITE);
        return;
    }
    // Modify user?
    if (isset ($_POST['usr_DoAction']) && $bUserLoadFailed == false)
    {
        if ($usrModAction == "lock")
        {
            if ($usrUser->SetLocked(true) == false)
                $errorString = get_translation('admin_usrmod_errorlock') . ', ID: ' . $usrId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' locked user ' . $usrUser->GetId());
                header('location: ' . $ADMIN_LIST_USERS_SITE . '?mode=' . $usrMode);
                return;
            }
        }
        else if ($usrModAction == "delete")
        {
            $bDone = false;
            $bDone = $usrUser->DeleteUserFinalX();  // Users can be unlinked
            if ($bDone == false)
                $errorString = get_translation('admin_usrmod_errordelete') . ', ID: ' . $usrId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' deleted user ' . $usrUser->GetId());
                header('location: ' . $ADMIN_LIST_USERS_SITE . '?mode=' . $usrMode);
                return;
            }
        }
        else
        {
            if ($usrUser->SetLocked(false) == false)
                $errorString = get_translation('admin_usrmod_errorunlock') . ', ID: ' . $usrId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' unlocked user ' . $usrUser->GetId());
                header('location: ' . $ADMIN_LIST_USERS_SITE . '?mode=' . $usrMode);
                return;
            }
        }
    }

    // Display
    $titleString = get_translation('admin_usrmod_unlockhead');
    if ($usrModAction == "lock")
        $titleString = get_translation('admin_usrmod_lockhead');
    else if ($usrModAction == "delete")
        $titleString = get_translation('admin_usrmod_deletehead');
    print_admin_header($titleString, true, get_translation('admin_login_logout'), "", true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_USERS_SITE, get_translation('admin_overview'));

    if ($errorString != "")
        print_error_message($errorString);
        
    if ($usrModAction == "lock")
        echo_r('<p>' . get_translation('admin_usrmod_questlock') . ':</p><br>');
    else if ($usrModAction == "delete")
        echo_r('<p>' . get_translation('admin_usrmod_questdelete') . ':</p><br>');
    else
        echo_r('<p>' . get_translation('admin_usrmod_questunlock') . ':</p><br>');
    echo_r('<table width="100%" border="0">');
    echo_r('  <tr>');
    echo_r('    <td width="10%">');
        if ($usrUser->GetType() == 0)
            echo_r('       <img src="' . $CUSTOMER_IMAGE . '">');
        else if ($usrUser->GetType() == 1)
            echo_r('       <img src="' . $REPORTER_IMAGE . '">');
        else if ($usrUser->GetType() == 2)
            echo_r('       <img src="' . $OWNER_IMAGE . '">');
        else if ($usrUser->GetType() == 3)
            echo_r('       <img src="' . $ADMIN_IMAGE . '">');
    echo_r('    </td>');
    echo_r('    <td width="90%">');
    if ($usrUser->GetAlias() != "") // If no alias, use name
        echo_r('       <b>' . get_translation('admin_usr_alias') . ': ' . $usrUser->GetAlias() . '</b></br>');
    else
        echo_r('       <b>' . get_translation('admin_usr_name') . ': ' . $usrUser->GetFirstName() . ' ' . $usrUser->GetLastName() . '</b></br>');
    echo_r('       ' . get_translation('admin_usr_email') . ': ' . $usrUser->GetEmail() . '</br>');
    echo_r('       ' . get_translation('admin_usr_id') . ': ' . $usrUser->GetId() . '</br>');
    echo_r('       ' . get_translation('admin_usr_date') . ': ' . $usrUser->GetDate() . '</br>');
        echo_r('       ' . get_translation('admin_usr_type') . ': ');
        if ($usrUser->GetType() == 0)
            echo_r(get_translation('admin_usr_typecustomer') . '</br>');
        else if ($usrUser->GetType() == 1)
            echo_r(get_translation('admin_usr_typereporter') . '</br>');
        else if ($usrUser->GetType() == 2)
            echo_r(get_translation('admin_usr_typeowner') . '</br>');
        else if ($usrUser->GetType() == 3)
            echo_r(get_translation('admin_usr_typeadmin') . '</br>');
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');
    
    echo_r('<form method="post">');
    print_bottom_links("usr_DoAction", get_translation('admin_yes'),
                       "usr_Cancel", get_translation('admin_cancel'));
    echo_r('  <input type="hidden" value="' .  $usrId . '" name="usr_id">  ');
    echo_r('  <input type="hidden" value="' .  $usrModAction . '" name="usr_action">  ');
    echo_r('</form>');

    print_admin_footer();
?>