Custom code when user enters group

Permalink
Hello, im trying to add user to newsletter list(mailchimp, mail monkey block) when he enters certain group.

I try to achive this by c5 events (on_user_enter_group), so i hooked an event
Events::extend('on_user_enter_group', 'DodanieDoNewslettera', 'NewsletterDodanie', 'models/newsletter_dodanie.php');


but then i have no idea how can I:
1. get currently editing user's attributes ( i need email and name)
2. how can i check if user entered certain group and THEN do my code ?

Looking forward for any advices and tips which i could follow !
greetings

grosik
 
jordanlev replied on at Permalink Reply
jordanlev
User events generally pass the UserInfo object to the event handler function. So in your DodanieDoNewslettera class, you should be able to do this:
public function NewsletterDodanie($ui) {
    $u = $ui->getUserObject();
    $g = Group::getByName('Your Group Name');
    //check if user is in group
    if ($u->inGroup($g)) {
        //get user data
        $email = $ui->getUserEmail();
        $username = $ui->getUserName();
        $some_attribute = $ui->getAttribute('your_attribute_handle');
        //do whatever....
    }
}
grosik replied on at Permalink Reply
grosik
Hello jordan thanks for your reply ! that was the same way i was trying to achieve !

anyway... working on this for some hours and im still having an issue...

my code for adding an user to newsletter is working perfectly, but i'm still having problems with events.

it seems that on_user_enter_group event isn't working. When i hook whole function to other event (f.e on_user_add), whole userinfo and so on passes and newly created user is signed into mailing list.

When i try to do the same, with on_user_enter_group (even without "if" statement checking if user is in group), NOTHING happens.

where is the bug ? maybe this event is bugged ? any ideas ?

Any help apprecieated, because i run out of ideas...
jordanlev replied on at Permalink Reply
jordanlev
try adding this to your config/site.php file:
define('ENABLE_APPLICATION_EVENTS', true);


Depending on which version of C5 you're running, you need to include that to "turn on" the event system. Note that the documentation says you don't need it, but I've found that the docs are wrong and you actually do.
grosik replied on at Permalink Reply
grosik
Hi Jordan,

i'm using concrete 5.6.0.2 and also i added this code before.

below i post my code, which isn't actually big.

site_events:
<?
Events::extend('on_user_update', 'MailDoAdmina', 'MailAdmin', 'models/mail_do_admina.php');
Events::extend('on_user_enter_group', 'DodanieDoNewslettera', 'NewsletterDodanie', 'models/newsletter_dodanie.php');


and code of [root]/models/newsletter_dodanie.php
<?php
class DodanieDoNewslettera {
    public function NewsletterDodanie($ui) {
        $ui->getUserObject();
        $g = Group::getByName('odbiorcy hurtowi');
        if ($u->inGroup($g)) {
            //get user data
            $email = $ui->getUserEmail();
            $listId = 'xxx';
            $chimp_key = 'xxx-xxxxx-sadasda';
            $name = $ui->getAttribute('osoba_odpowiedzialna');
            loader::model('monkeysee','mail_monkey');  // loading the mailchimp api (from mail_monkey package), but it's the original api        
            $api = new MCAPI($chimp_key);
            $retval = $api->listSubscribe( $listId, $email, $name ); // signup to newsletter with obtained data
        }


maybe this can help with figuring out
jordanlev replied on at Permalink Reply
jordanlev
I don't know why that isn't working. Is your event handler even being called int he first place? You're going to have to do some basic debugging here -- break things down into smaller pieces and try to isolate where the problem is. So if you put a die() statement in the event handler, you can see if it is being called at all. That will let you know if the problem is in how the event handler is set up, or if it's a problem with the code you have in the event handler.

Good luck.
FatTony1952 replied on at Permalink Reply
FatTony1952
I've been working on a similar function for the on_user_exit_group event, but it isn't triggering either. I've installed the Events Trigger addon to log when an event triggers, but it doesn't log the on_user_exit_group event. I got it to trigger the on_user_update event though.

I reported it as a bug, but got a reply that, "This is not a but". Great explanation, eh?
naumeK replied on at Permalink Reply
naumeK
You have to write

$u = $ui->getUserObject();

This should help!!