AnsweredAssumed Answered

Create a custom filter with dynamic users and teams

Question asked by Pierre-Nicolas Lemoine on May 3, 2018
Latest reply on May 4, 2018 by Pierre-Nicolas Lemoine

Dear Sugar Community,

I'm have to implement a custom filter for the opportunity module. I would like to add a filter called My Teams elements that is able to filter the records owned by the teams of the user. 

For now I created a custom filter and with static ID I'm able to filter my Opportunity list view : 

$teams = ['ID_1','ID_2'];

$viewdefs['Opportunities']['base']['filter']['basic']['filters'][] = array (

    'id' => 'filterOpportunityByTeamElements',

    'name' => 'LBL_FILTER_OPPORTUNITY_BY_TEAM_ELEMENTS',

    'filter_definition' => array (

        array (

            'team_id' => array (

                '$in' => $teams,

            ),

        ),

    ),

    'editable' => false,

    'is_template' => false,

);

 

Then I tried to retried the current user teams but it's not working well. I have the team of the admin user and the current_user is always admin. 

I added at the beginning of the code below this code : 

global $current_user;

$id = $current_user->id;

$query = 'SELECT team_id FROM `team_memberships` where user_id = ? and team_id != "1"';

$conn = $GLOBALS['db']->getConnection();

$stmt = $conn->executeQuery($query, array($id));

$teams = array();

foreach($stmt->fetchAll() as $row){

    array_push($teams, $row['team_id']);

}

 

I think it's because of the Extension framework. It's building everything one time during the rebuild process. I looking now with Visibility framework but I'm not sure that it can help me. 

 

Is someone have an idea of how to achieve to do that ? 

 

Kind regards,

 

Pierre-Nicolas

Outcomes