<?php

require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'uvConst.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'databaseapi.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'userprojectapi.php';
auth_reauthenticate();
access_ensure_global_level(config_get('UserProjectAccessLevel', ADMINISTRATOR));
form_security_validate('plugin_UserProjectView_config_update');
$option_reset = gpc_get_bool('reset', false);
$option_change = gpc_get_bool('change', false);
if ($option_reset) {
    $databaseapi = new databaseapi();
    $databaseapi->reset_plugin();
}
if ($option_change) {
    update_single_value('UserProjectAccessLevel', ADMINISTRATOR);
    userprojectapi::editPluginInWhiteboardMenu('plugin_access_level', gpc_get_int('UserProjectAccessLevel', ADMINISTRATOR));
    update_button('ShowMenu');
    userprojectapi::editPluginInWhiteboardMenu('plugin_show_menu', gpc_get_int('ShowMenu'));
    update_button('ShowInFooter');
    update_button('ShowAvatar');
    update_button('IAUHighlighting');
    update_color('IAUHBGColor', '#E67C7C');
    update_button('URIUHighlighting');
    update_color('URIUHBGColor', '#E67C7C');
    update_button('NUIHighlighting');
    update_color('NUIHBGColor', '#FCBDBD');
    update_button('ShowZIU');
    update_button('ZIHighlighting');
    update_color('ZIHBGColor', '#F8FFCC');
    update_button('layer_one_name');
/**
 * information if user in group one is just assigned to subprojects
 *
 * @param $user_id
 */
function remark_assigned_subprojects($user_id)
{
    $databaseapi = new databaseapi();
    $user_is_assigned_to_project = $databaseapi->check_user_project_assignment($user_id, helper_get_current_project());
    if (is_null($user_is_assigned_to_project) && helper_get_current_project() > 0) {
        echo plugin_lang_get('remark_noprojectassignment');
    }
}
function print_tbody()
{
    $databaseapi = new databaseapi();
    $selected_values = null;
    if (isset($_POST['dataRow'])) {
        $selected_values = $_POST['dataRow'];
    }
    $select = strtolower($_POST['option']);
    /** prepare user groups */
    $user_group = userprojectapi::prepare_user_project_remove_group($selected_values);
    echo '<tbody><form action="' . plugin_page('UserProject_RemoveSubmit') . '" method="post">';
    foreach ($user_group as $user) {
        $user_id = $user[0];
        $project_ids = explode(',', $user[1]);
        print_option_user_row($user_id);
        for ($project_index = 0; $project_index < count($project_ids); $project_index++) {
            $project_id = $project_ids[$project_index];
            if ($project_index > 0) {
                $project_id_spec_sub_projects = project_hierarchy_get_all_subprojects($project_id);
                $old_project_id = $project_ids[$project_index - 1];
                $old_project_id_spec_sub_projects = project_hierarchy_get_all_subprojects($old_project_id);
                if (in_array($old_project_id, $project_id_spec_sub_projects)) {
                    /** alte löschen */
                    $project_ids[$project_index - 1] = null;
                } elseif (in_array($project_id, $old_project_id_spec_sub_projects)) {
                    continue;
                }
            }
            switch ($select) {
                case 'removesingle':
                    $user_is_assigned_to_project = $databaseapi->check_user_project_assignment($user_id, $project_id);
                    if (!is_null($user_is_assigned_to_project)) {
                        print_option_project_row($user_id, $project_id);
                    } else {
                        echo '<tr class="info" data-level="1" data-status="0">';
                        echo '<td width="20px"></td>';
                        echo '<td class="user_row_bg" style="text-align: left">' . project_get_name($project_id) . '</td>';
                        echo '<td class="user_row_bg" style="text-align: left" colspan="2">';
                        if (user_is_administrator($user_id)) {
                            echo plugin_lang_get('remove_administrator');
                        } else {
                            echo plugin_lang_get('remove_noassignment');
                        }
                        echo '</td>';
                        echo '</tr>';
                    }
                    break;
                case 'removeall':
                    $sub_project_ids = array();
                    array_push($sub_project_ids, $project_id);
                    $t_sub_project_ids = project_hierarchy_get_all_subprojects($project_id);
                    foreach ($t_sub_project_ids as $t_sub_project_id) {
                        if (!in_array($t_sub_project_id, $sub_project_ids, true)) {
                            array_push($sub_project_ids, $t_sub_project_id);
                        }
                    }
                    foreach ($sub_project_ids as $sub_project_id) {
                        $user_is_assigned_to_project = $databaseapi->check_user_project_assignment($user_id, $sub_project_id);
                        if (!is_null($user_is_assigned_to_project)) {
                            print_option_project_row($user_id, $sub_project_id);
                        } else {
                            echo '<tr class="info" data-level="1" data-status="0">';
                            echo '<td width="20px"></td>';
                            echo '<td class="user_row_bg" style="text-align: left">' . project_get_name($sub_project_id) . '</td>';
                            echo '<td class="user_row_bg" style="text-align: left" colspan="2">';
                            if (user_is_administrator($user_id)) {
                                echo plugin_lang_get('remove_administrator');
                            } else {
                                echo plugin_lang_get('remove_noassignment');
                            }
                            echo '</td>';
                            echo '</tr>';
                        }
                    }
                    break;
            }
        }
    }
    print_option_submit_button();
    echo '</form></tbody>';
}