<?php

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

File:         list_users.php
Description:  Administrative overview of all users/customers

*/

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


    // Check privileges
    check_login();
    if (user_is_reporter() == true || user_is_customer() == true)
    {
        log_out();
    }
    if (isset ($_POST['usrl_Cancel']))
    {
        header ("location: " . $ADMIN_MAIN_SITE);
        return;
    }

    // Create new user?
    if (isset ($_POST['usrl_CreateUser']))
    {
        header ('Location: ' . $ADMIN_EDIT_USERS_SITE . '?action=new');
        return;
    }
    // Edit user?
    if (isset ($_POST['usrl_EditUser']))
    {
        header ('Location: ' . $ADMIN_EDIT_USERS_SITE . '?id=' . $_POST['usrl_UserId'] . '&action=edit');
        return;
    }
    // Delete user?
    if (isset ($_POST['usrl_DeleteUser']))
    {
        header ('Location: ' . $ADMIN_MODIFY_USERS_SITE . '?id=' . $_POST['usrl_UserId'] . '&action=delete');
        return;
    }
    // Lock user?
    if (isset ($_POST['usrl_LockUser']))
    {
        header ('Location: ' . $ADMIN_MODIFY_USERS_SITE . '?id=' . $_POST['usrl_UserId'] . '&action=lock');
        return;
    }
    // Unlock user?
    if (isset ($_POST['usrl_UnlockUser']))
    {
        header ('Location: ' . $ADMIN_MODIFY_USERS_SITE . '?id=' . $_POST['usrl_UserId'] . '&action=unlock');
        return;
    }

    // Display
    print_admin_header(get_translation('admin_usrl_head'), true, get_translation('admin_login_logout'), $ADMIN_HELP_FILE . '#n1-10', true, get_translation('admin_backtomain'));
    $usr = new cUser;
    $usrList = new cUserList;
    $usrFilter = 0;
    if (isset ($_GET['filter']) && $_GET['filter'] != "")
        $usrFilter = $_GET['filter'];
    else if (isset ($_POST['usrl_filter']) && $_POST['usrl_filter'] != "")
        $usrFilter = $_POST['usrl_filter'];

    $usr->SetFolder($USER_FOLDER);
    $usrList->SetFolder($USER_FOLDER);

    $usrList->LoadUserList($usrFilter);
    $list = $usrList->GetUserList();
    rsort($list);

    echo_r ('<form method="post">');
    echo_r ('<table border="0" width="100%">');
    echo_r ('<tr>');
    echo_r ('  <td width="20%"><input type="submit" class="bottom_btn" value="' . get_translation('admin_usrl_createnew') . '" name="usrl_CreateUser"></td>');
    echo_r ('  <td width="10%">' . get_translation('admin_filter') . '</td>');
    echo_r ('  <td>');
    echo_r ('  <select name="usrl_filter" onChange="submit();">');
    echo   ('  <option value="' . $FILTER_ALL . '"');
    if ($usrFilter == $FILTER_ALL)
        echo (' selected');
    echo_r ('>' . get_translation('admin_all') . '</option>');
    echo   ('  <option value="' . $FILTER_UNLOCKED . '"');
    if ($usrFilter == $FILTER_UNLOCKED)
        echo (' selected');
    echo_r ('>' . get_translation('admin_unlocked') . '</option>');
    echo   ('  <option value="' . $FILTER_LOCKED . '"');
    if ($usrFilter == $FILTER_LOCKED)
        echo (' selected');
    echo_r ('>' . get_translation('admin_locked') . '</option>');
    echo_r ('  </select>');
    echo_r ('  </td>');
    echo_r ('</tr>');
    echo_r ('</table>');
    echo_r ('</form>');

    foreach ($list as $value)
    {
        $usr->LoadUserFromId($value);
        
        echo_r('<hr>');
        echo_r('<table width="100%" border="0">');
        echo_r('  <tr>');
        echo_r('    <td width="20%">');
        echo  ('      <a href="' . $ADMIN_EDIT_USERS_SITE . '?id=' . $usr->GetId() . '&action=edit">');
        if ($usr->GetType() == 0)
            echo_r('<img src="' . $CUSTOMER_IMAGE . '" title="' . get_translation('admin_edit') . '"></a>');
        else if ($usr->GetType() == 1)
            echo_r('<img src="' . $REPORTER_IMAGE . '" title="' . get_translation('admin_edit') . '"></a>');
        else if ($usr->GetType() == 2)
            echo_r('<img src="' . $OWNER_IMAGE . '" title="' . get_translation('admin_edit') . '"></a>');
        else if ($usr->GetType() == 3)
            echo_r('<img src="' . $ADMIN_IMAGE . '" title="' . get_translation('admin_edit') . '"></a>');
        if ($usr->IsLocked() == true)
            echo_r('       <img src="' . $LOCKED_IMAGE . '" title="' . get_translation('admin_locked') . '">');
        if ($usr->WantsNewsletter() == true)
            echo_r('       <img src="' . $NEWSLETTER_IMAGE . '" title="' . get_translation('admin_usr_wantsnewsletter') . '" alt="">');
        echo_r('    </td>');
        echo_r('    <td width="25%">');
        if ($usr->GetAlias() != "") // If no alias, use name
            echo_r('       <b>' . get_translation('admin_usr_alias') . ': ' . $usr->GetAlias() . '</b></br>');
        else
            echo_r('       <b>' . get_translation('admin_usr_name') . ': ' . $usr->GetFirstName() . ' ' . $usr->GetLastName() . '</b></br>');
        echo_r('       ' . get_translation('admin_usr_email') . ': ' . $usr->GetEmail());
        echo_r('    </td>');
        echo_r('    <td width="25%">');
        echo_r('       ' . get_translation('admin_usr_id') . ': ' . $usr->GetId() . '</br>');
        echo_r('       ' . get_translation('admin_usr_type') . ': ');
        if ($usr->GetType() == 0)
            echo_r(get_translation('admin_usr_typecustomer'));
        else if ($usr->GetType() == 1)
            echo_r(get_translation('admin_usr_typereporter'));
        else if ($usr->GetType() == 2)
            echo_r(get_translation('admin_usr_typeowner'));
        else if ($usr->GetType() == 3)
            echo_r(get_translation('admin_usr_typeadmin'));
        echo_r('    </td>');
        echo_r('    <td width="30%">');
        echo_r('    <form method="post">');
        echo_r('    <input type="hidden" value="' . $usr->GetId() . '" name="usrl_UserId">  ');
        if ($usr->GetType() != 3 || user_is_admin() == true)
        {
            echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_edit') . '" name="usrl_EditUser">  ');
            if ($usr->IsLocked() == true)
            {
                echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_unlock') . '" name="usrl_UnlockUser">  ');
            }
            else
            {
                echo_r('    <input type="submit" class="side_btn" value="' . get_translation('admin_lock') . '" name="usrl_LockUser">  ');
            }
        }
        if (user_is_admin() == true)
            echo_r('    <input type="submit"  class="side_btn" value="' . get_translation('admin_delete') . '" name="usrl_DeleteUser">  ');
        echo_r('    </form>');
        echo_r('    </td>');
        echo_r('  </tr>');
        echo_r('</table>');
    }
    print_bottom_links("usrl_Cancel", get_translation('admin_ok'));

    print_admin_footer();
?>