Getting Reports To User Hierarchy in Sugar CRM as Array

Create File custom_utils.php in custom/include directory and add the following functions in to it

The getReportsToUserArray Function has two parameters
$userid is the id of user
$teamArray returns the array of private team of reporting user hierarchy



function getReportsToUserArray($userId, $teamArray = array()) {
    if (!empty($userId) && $userId != 1) {
        $reportingUser = new User();
        $reportingUser->retrieve($userId);
        if (!empty($reportingUser->reports_to_id) && $reportingUser->reports_to_id != 1) {
            $teamArray[] = User::staticGetPrivateTeamID($reportingUser->reports_to_id);
            $nextuserId = $reportingUser->reports_to_id;
            return getReportsToUserArray($nextuserId, $teamArray);
        }
        return $teamArray;
    }
}

The getUserIdByPrivateTeam Function has two parameters
$teamArray is the array of private team of reporting user which will be return by above function
This Function returns the Array of userid by private team of users

function getUserIdByPrivateTeam($teamArray = array()) {
    global $db;
    if (!empty($teamArray)) {
        $privateteamstring = implode("','", $teamArray);
        $result = $db->Query("SELECT associated_user_id  from teams WHERE id in ('$privateteamstring')");
        while ($row = $db->fetchByAssoc($result)) {
            $UserIDArray[] = $row['associated_user_id'];
        }
    }
    return $UserIDArray;
}

After Getting this array we can check the selected assigned user id in this array
if the id is available we can restrict the save action by javascript or jquery in view.edit.php of case module

Get Reports To User in Array Format – Sugar CRM

The Following Function will return the Users Reports To one by one
like category and its parent category concept

/*
$userId is the id of user
$teamArray is the result which will return array of reports to hierarchy
*/

function getReportsToUserArray($userId, $teamArray = array()) {
    if (!empty($userId) && $userId != 1) {
        $reportingUser = new User();
        $reportingUser->retrieve($userId);
        if (!empty($reportingUser->reports_to_id) && $reportingUser->reports_to_id != 1) {
            $teamArray[] = User::staticGetPrivateTeamID($reportingUser->reports_to_id);
            $nextuserId = $reportingUser->reports_to_id;            
            return getReportsToUserArray($nextuserId, $teamArray);
        }
        return $teamArray;
    }
}