<?php

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

File:         edit_user.php
Description:  Administrative module to edit users/customers or create new ones

*/

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

    $b_NotFinished = true;
    $b_ErrorOccured = false;
    $usrId = "";
    $errorString = "";

    // Check privileges
    check_login();
    if (user_is_reporter() == true || user_is_customer() == true)
    {
        log_out();
        return;
    }

    // Check action
    $usrEdtAction = "new";
    if (isset ($_POST['usr_EdtAction']) == true)
    {
        if ($_POST['usr_EdtAction'] == "edit")
            $usrEdtAction = "edit";
    }
    else if (isset ($_GET['action']) == true)
    {
        if ($_GET['action'] == "edit")
            $usrEdtAction = "edit";
    }

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

    if ($usrEdtAction == "edit")  // Edit -> load it
    {
        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_usredt_errorload') . ', ID: ' . $usrId);
            return;
        }
    }

    // User edit cancelled?
    if (isset ($_POST['usr_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_USERS_SITE );
        return;
    }

    // Check for errors and if none, store the user values
    if (isset ($_POST['usr_Save']))
    {

        // Customers don't need an alias
        if ($_POST['usr_type'] != 0 || $_POST['usr_alias'] != "")
        {
            $usrl_UserList = new cUserList;
            $usrl_UserList->SetFolder($USER_FOLDER);
            $usrId = "";
            if (isset($_POST['usr_id']))
                $usrId = $_POST['usr_id'];
            if ($_POST['usr_alias'] == "")
            {
                $errorString = 'Alias fehlt!';
                $b_ErrorOccured = true;
            }
            else if ($usrl_UserList->CheckAlias($_POST['usr_alias'], $usrId) == false)
            {
                $errorString = get_translation('admin_usr_erroraliasexits') . '!';
                $b_ErrorOccured = true;
            }
        }
        // Customers don't need a password
        if ($_POST['usr_type'] != 0 || $_POST['usr_password'] != "")
        {
            if ($_POST['usr_password'] == "" || $_POST['usr_passwordconf'] == "")
            {
                $errorString = get_translation('admin_usr_errorpassword') . '!';
                $b_ErrorOccured = true;
            }
            if ($_POST['usr_password'] != $_POST['usr_passwordconf'])
            {
                $errorString = get_translation('admin_usr_errorpassconf') . '!';
                $b_ErrorOccured = true;
            }
        }
        // Customer MUST have an address
        if ($_POST['usr_type'] == "0")
        {
            if (!ereg("^.+@.+\\..+$", $_POST['usr_email']))
            {
                $errorString = get_translation('admin_usr_erroremail') . '!';
                $b_ErrorOccured = true;
            }
            else
            {
                $missingElement = "";
                if ($_POST['usr_firstname'] == "")
                    $missingElement = $missingElement . '<br> -> ' . get_translation('admin_usr_firstname');
                if ($_POST['usr_lastname'] == "")
                    $missingElement = $missingElement . '<br> -> ' . get_translation('admin_usr_lastname');
                if ($_POST['usr_street'] == "")
                    $missingElement = $missingElement . '<br> -> ' . get_translation('admin_usr_street');
                if ($_POST['usr_streetno'] == "")
                    $missingElement = $missingElement . '<br> -> ' . get_translation('admin_usr_streetno');
                if ($_POST['usr_city'] == "")
                    $missingElement = $missingElement . '<br> -> ' . get_translation('admin_usr_city');
                if ($_POST['usr_zipcode'] == "")
                    $missingElement = $missingElement . '<br> -> ' . get_translation('admin_usr_zipcode');
                if ($_POST['usr_land'] == "")
                    $missingElement = $missingElement . '<br> -> ' . get_translation('admin_usr_land');
                if ($_POST['usr_phoneno'] == "")
                    $missingElement = $missingElement . '<br> -> ' . get_translation('admin_usr_phoneno');
                if ($_POST['usr_email'] == "")
                    $missingElement = $missingElement . '<br> -> ' . get_translation('admin_usr_email');

                if ($missingElement != "")
                {
                    $errorString = get_translation('admin_usr_erroraddress') . ': ' . $missingElement;
                    $b_ErrorOccured = true;
                }
            }
        }

        if ($b_ErrorOccured == false)
        {
            // If done, fill the rest of the parameters
            if ($usrEdtAction == "new")    // New -> create Id first
            {
                $usrUser->CreateUser();
                $usrUser->SetFolder($USER_FOLDER);
            }
            $usrUser->SetAlias($_POST['usr_alias']);
            if ($_POST['usr_password'] != $usrUser->GetPassword())  // Only if changed, cause of MD5
                $usrUser->SetPassword($_POST['usr_password']);
            $usrUser->SetType($_POST['usr_type']);
            $usrUser->SetFirstName($_POST['usr_firstname']);
            $usrUser->SetLastName($_POST['usr_lastname']);
            $usrUser->SetStreet($_POST['usr_street']);
            $usrUser->SetStreetNo($_POST['usr_streetno']);
            $usrUser->SetCity($_POST['usr_city']);
            $usrUser->SetZipCode($_POST['usr_zipcode']);
            $usrUser->SetLand($_POST['usr_land']);
            $usrUser->SetTelephoneNo($_POST['usr_phoneno']);
            $usrUser->SetEmail($_POST['usr_email']);
            if (isset($_POST['usr_newsletter']))
                $usrUser->SetWantsNewsletter($_POST['usr_newsletter'] == "1");
            else
                $usrUser->SetWantsNewsletter(false);
            if ($usrUser->SaveUser() == false)
            {
                if ($usrEdtAction == "new")
                    $errorString = get_translation('admin_usredt_errorcreate') . '!';
                else
                    $errorString = get_translation('admin_usredt_errorsave') . '!';
                $b_ErrorOccured = true;
            }
            else
            {
                if ($usrEdtAction == "edit")
                {
                    // User is saved.
                    log_message ('User ' . get_user_name() . ' saved user ' . $usrUser->GetId());
                    header ('Location: ' . $ADMIN_LIST_USERS_SITE . '?mode=' . $usrMode);
                    return;
                }
                else
                    log_message ('User ' . get_user_name() . ' created user ' . $usrUser->GetId());

                $b_NotFinished = false;
            }
        }
    }

    // Display the input dialog for edit user
    $titleString = get_translation('admin_usredt_newhead');
    if ($usrEdtAction == "edit")  // Edit
        $titleString = get_translation('admin_usredt_edithead');
    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 ($b_NotFinished == false)
    {
        // User is created. Display form to create another or go back to user list
        echo_r('<p>' . get_translation('admin_usredt_usercreated') . ' : ' . $usrUser->GetId() . '.<br></p>');
        print_bottom_links("usr_New", get_translation('admin_usredt_createanother'),
                           "usr_Cancel", get_translation('admin_ok'), true);
        return;
    }

    echo_r('<form action="' . $_SERVER['PHP_SELF'] . '" method="post" enctype="multipart/form-data">');
    if ($b_NotFinished == true)
    {
        echo_r('<table width="100%">');
        if ($usrEdtAction == "edit")  // Edit
        {
            echo_r('  <tr>');
            echo_r('    <td>' . get_translation('admin_usr_id') . ':</td>');
            echo_r('    <td><b>' . $usrUser->GetId() . '</b></td>');
            echo_r('  <input type="hidden" value="' .  $usrUser->GetId() . '" name="usr_id">  ');
            echo_r('  </tr>');
        }
        echo_r('  <tr>');
        echo_r('    <td width="30%">' . get_translation('admin_usr_alias') . ':</td>');
        if (isset($_POST['usr_alias']))
            echo_r('    <td><input type="text" name="usr_alias" value="' . $_POST['usr_alias'] . '"></td>');
        else
            echo_r('    <td><input type="text" name="usr_alias" value="' .  $usrUser->GetAlias() . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_password') . ':</td>');
        if (isset($_POST['usr_password']))
            echo_r('    <td><input type="password" name="usr_password" value="' . $_POST['usr_password'] . '"></td>');
        else
            echo_r('    <td><input type="password" name="usr_password" value="' . $usrUser->GetPassword() . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_passwordconf') . ':</td>');
        if (isset($_POST['usr_passwordconf']))
            echo_r('    <td><input type="password" name="usr_passwordconf" value="' . $_POST['usr_passwordconf'] . '"></td>');
        else
            echo_r('    <td><input type="password" name="usr_passwordconf" value="' . $usrUser->GetPassword() . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_type') . ':</td>');
        $usrType = $usrUser->GetType();
        if (isset($_POST['usr_type']))
            $usrType = $_POST['usr_type'];
        echo_r('    <td><select name="usr_type">');
        echo('        <option value="0"');
        if ($usrType == "0")
            echo(' selected');
        echo_r ('>' . get_translation('admin_usr_typecustomer') . '</option>');
        echo('        <option value="1"');
        if ($usrType == "1")
            echo(' selected');
        echo_r ('>' . get_translation('admin_usr_typereporter') . '</option>');
        echo('        <option value="2"');
        if ($usrType == "2")
            echo(' selected');
        echo_r ('>' . get_translation('admin_usr_typeowner') . '</option>');
        if (user_is_admin() == true)
        {
            echo('        <option value="3"');
            if ($usrType == "3")
                echo(' selected');
        }
        echo_r ('>' . get_translation('admin_usr_typeadmin') . '</option>');
        echo_r('   </select></td>');
        echo_r('  </tr>');
        echo_r('  <tr><td>' . get_translation('admin_usr_address') . ':</td><td><br></td></tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_firstname') . ':</td>');
        if (isset($_POST['usr_firstname']) == false)
            $_POST['usr_firstname'] = $usrUser->GetFirstName();
        echo_r('    <td><input type="text" name="usr_firstname" value="' . $_POST['usr_firstname'] . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_lastname') . ':</td>');
        if (isset($_POST['usr_lastname']) == false)
            $_POST['usr_lastname'] = $usrUser->GetLastName();
        echo_r('    <td><input type="text" name="usr_lastname" value="' . $_POST['usr_lastname'] . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_street') . ':</td>');
        if (isset($_POST['usr_street']) == false)
            $_POST['usr_street'] = $usrUser->GetStreet();
        echo_r('    <td><input type="text" name="usr_street" value="' . $_POST['usr_street'] . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_streetno') . ':</td>');
        if (isset($_POST['usr_streetno']) == false)
            $_POST['usr_streetno'] = $usrUser->GetStreetNo();
        echo_r('    <td><input type="text" name="usr_streetno" value="' . $_POST['usr_streetno'] . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_zipcode') . ':</td>');
        if (isset($_POST['usr_zipcode']) == false)
            $_POST['usr_zipcode'] = $usrUser->GetZipCode();
        echo_r('    <td><input type="text" name="usr_zipcode" value="' . $_POST['usr_zipcode'] . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_city') . ':</td>');
        if (isset($_POST['usr_city']) == false)
            $_POST['usr_city'] = $usrUser->GetCity();
        echo_r('    <td><input type="text" name="usr_city" value="' . $_POST['usr_city'] . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_land') . ':</td>');
        if (isset($_POST['usr_land']) == false)
            $_POST['usr_land'] = $usrUser->GetLand();
        echo_r('    <td><input type="text" name="usr_land" value="' . $_POST['usr_land'] . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_phoneno') . ':</td>');
        if (isset($_POST['usr_phoneno']) == false)
            $_POST['usr_phoneno'] = $usrUser->GetTelephoneNo();
        echo_r('    <td><input type="text" name="usr_phoneno" value="' . $_POST['usr_phoneno'] . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td>' . get_translation('admin_usr_email') . ':</td>');
        if (isset($_POST['usr_email']) == false)
            $_POST['usr_email'] = $usrUser->GetEmail();
        echo_r('    <td><input type="text" name="usr_email" value="' . $_POST['usr_email'] . '"></td>');
        echo_r('  </tr>');
        echo_r('  <tr>');
        echo_r('    <td></td>');
        $wantsNewsletter = 0;
        if (isset($_POST['usr_newsletter']) && $_POST['usr_newsletter'] == "1")
            $wantsNewsletter = 1;
        else if ($usrUser->WantsNewsletter() == true)
            $wantsNewsletter = 1;
        echo ('    <td><input type="checkbox" name="usr_newsletter" value="1"');
        if ($wantsNewsletter == 1)
            echo (' checked');
        echo_r ('>' . get_translation('admin_usr_wantsnewsletter') . '</td>');
        echo_r('  </tr>');
        echo_r('</table>');

        if ($usrEdtAction == "edit")  // Edit
            print_bottom_links("usr_Save", get_translation('admin_save'),
                               "usr_Cancel", get_translation('admin_cancel'), false);
        else  // New
            print_bottom_links("usr_Save", get_translation('admin_create'),
                               "usr_Cancel", get_translation('admin_cancel'), false);

    }
    echo_r('<input type="hidden" name="usr_EdtAction" value="' . $usrEdtAction . '">');
    echo_r('</form>');

    print_admin_footer();
?>