<?php
ob_start();
/************************************************************************/
/*     PROGRAMMER     :  SMA                                            */
/*     SCRIPT NAME    :  adminuser_edit.php                             */
/*     CREATED ON     :  10/JULY/2006                                   */
/*     MODERNIZED     :  20/AUG/2025                                    */
/*     PURPOSE        :  Page to set privileges to admin user           */
/************************************************************************/

include_once '../includes/session.php';
include '../includes/constants.php';
include '../includes/functions.php';
include_once '../includes/function1.php';
include_once '../includes/allstripslashes.php';

$partners = new partners();
$partners->connection($host, $user, $pass, $db);

$adminobj = new adminuser();

$id = isset($_POST['id']) ? intval($_POST['id']) : 0;

$adminobj->GetAllAdminLinks();

for ($i = 0; $i < count($adminobj->linkid); $i++) {
    $linkid = intval($adminobj->linkid[$i]);
    $users = $adminobj->linkusers[$i];
    $userLink = isset($_POST['chk_' . $linkid]);

    $newusers = "";

    if ($userLink) {
        if ($users !== '') {
            $userlist = explode(",", $users);
            if (!in_array($id, $userlist)) {
                $userlist[] = $id;
            }
            $newusers = implode(",", $userlist);
        } else {
            $newusers = $id;
        }
    } else {
        if ($users !== '') {
            $userlist = explode(",", $users);
            $userlist = array_filter($userlist, function ($uid) use ($id) {
                return intval($uid) !== $id;
            });
            $newusers = implode(",", $userlist);
        } else {
            $newusers = "";
        }
    }

    $adminobj->UpdateAdminUserLinks($linkid, $newusers);
}

$msg = urlencode("Link Privilege Updated");
header("Location: index.php?Act=adminuser_privilege&id=$id&msg=$msg");
exit;
?>
