--- trunk/Nos.pm 2005/05/15 21:52:56 22 +++ trunk/Nos.pm 2005/05/15 22:12:31 23 @@ -73,6 +73,63 @@ $self ? return $self : return undef; } +=head2 add_member_to_list + +Add new member to list + + $nos->add_member_to_list( + list => "My list", + email => "john.doe@example.com", + name => "John A. Doe", + ); + +C parametar is optional. + +Return true if user is added. + +=cut + +sub add_member_to_list { + my $self = shift; + + my $arg = {@_}; + + my $email = $arg->{'email'} || confess "can't add user without e-mail"; + my $name = $arg->{'name'} || ''; + confess "need list name" unless ($arg->{'list'}); + + if (! Email::Valid->address($email)) { + warn "SKIPPING $name <$email>"; + return 0; + } + + print "# $name <$email>\n"; + + my $lists = $self->{'loader'}->find_class('lists'); + my $users = $self->{'loader'}->find_class('users'); + my $user_list = $self->{'loader'}->find_class('user_list'); + + my $list = $lists->find_or_create({ + name => $arg->{'list'}, + }) || croak "can't add list ",$arg->{'list'},"\n"; + + my $this_user = $users->find_or_create({ + email => $email, + full_name => $name, + }) || croak "can't find or create member\n"; + + my $user_on_list = $user_list->find_or_create({ + user_id => $this_user->id, + list_id => $list->id, + }) || croak "can't add user to list"; + + $list->dbi_commit; + $this_user->dbi_commit; + $user_on_list->dbi_commit; + + return 1; +} + =head2 send_queued_messages Send queued messages or just ones for selected list