. group_ids; $this->add_groups_user( $user_id, $user_groups_csv ); } function import_single_user_by_name( $headers, $row, $user_id ){ $pos = array_search( 'group_name', $headers ); if( $pos === FALSE ) return; // groups that appears in the CSV $user_groups_name_csv = explode( ',', $row[ $pos ] ); $user_groups_name_csv = array_filter( $user_groups_name_csv, function( $value ){ return $value !== ''; } ); $user_groups_csv = array(); foreach ( $user_groups_name_csv as $user_group_name_csv ) { $group = Groups_Group::read_by_name( $user_group_name_csv ); $user_groups_csv[] = $group->group_id; } $this->add_groups_user( $user_id, $user_groups_csv ); } function add_groups_user( $user_id, $user_groups_csv ){ // groups that user belongs to $groups_user = new Groups_User( $user_id ); $user_group_ids = empty( $groups_user->group_ids ) ? array() : $groups_user->group_ids; // first we look into all current user groups, if they do not appear in CSV it will be removed foreach ( $user_group_ids as $user_group_id ) { if( !in_array( $user_group_id, $user_groups_csv ) ) Groups_User_Group::delete( $user_id, $user_group_id ); } // finally we loop into groups that are present in CSV data, if they already exists, we do nothing, if not, we add it foreach ( $user_groups_csv as $user_group_csv ) { if( !in_array( $user_group_csv, $user_group_ids ) ) Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $user_group_csv ) ); } } } new ACUI_Groups();