XF 2.2 Add user to group and/or remove via PHP

RisteDimitrievski

Active member
I'm trying to set visitor to a group specified in the form, or to remove them. I'm still unsure how.
Any idea how to access

Code:
\XF::visitor()->addUserInGroup($groupid);
 
Solution
I've fixed my problem by setting data as array and then set them in secondary_group_ids.
PHP:
$data = [];
$data[0] = $mygroupid;
foreach($user->secondary_group_ids as $key => $value){
    ++$key;
    $data[$key] = $value;
}
$user->set('secondary_group_ids',$data);
$user->save();

And for removal:
PHP:
$data = array();
foreach($user->secondary_group_ids as $this->key => $this->group){
    $data[$this->key] = $this->group;
}
$neededKey = array_search($mygroupid,$data,false);
if($neededKey === false){
    return $this->message(\XF::phrase('no_members_found'));
}
unset($data[$neededKey]);
$user->set('secondary_group_ids', $data);
$user->save();
// Other code...
I've fixed my problem by setting data as array and then set them in secondary_group_ids.
PHP:
$data = [];
$data[0] = $mygroupid;
foreach($user->secondary_group_ids as $key => $value){
    ++$key;
    $data[$key] = $value;
}
$user->set('secondary_group_ids',$data);
$user->save();

And for removal:
PHP:
$data = array();
foreach($user->secondary_group_ids as $this->key => $this->group){
    $data[$this->key] = $this->group;
}
$neededKey = array_search($mygroupid,$data,false);
if($neededKey === false){
    return $this->message(\XF::phrase('no_members_found'));
}
unset($data[$neededKey]);
$user->set('secondary_group_ids', $data);
$user->save();
// Other code...
 
Solution
What you mean with "php"?

Just a script outside of XF? Then you have to manipulate xf_user.secondary_group_ids.
You select the old value, check if the group is inside, add it or not, update, finish.
I have a lot of scripts done, but never used xf itself.

If so, it is done as an addon or done with the api.

There are some scripts with the addons from XON, i have never used or watched them, but maybe you find there an idea, how to use xf as a library from outside.

BTW: Where you have found addUserInGroup?


Do you need something for using always or just one time? For one time, i would do it with a small script in php. (Or with a promotion?)
For a regulary use, there is an addon somewhere. And there was an addon from nobita.me; but i am not shure, if this was for xf1 or 2.
 
Top Bottom