Automatic Buddy Add

apt94jesse - October 21, 2006 - 01:56
Project:Buddylist
Version:5.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed
Description

I posted this in the post installation forum (http://drupal.org/node/90424), however this is probably a more appropriate location. I'm trying to add the functionality of automatically adding a buddy into the buddylist for every new user registration. I'm assuming that adding the following code, or something like it, to a user registration function, would do the trick.

db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid , '1', time());

Question 1: is this the correct line, syntax and all, for this functionality?

Question 2: what function, or where, can I add this to achieve this functionality?

Any help is greatly appreciated.

-Jesse

#1

rkn - October 25, 2006 - 16:35

Have you seen this:

http://drupal.org/node/75884

#2

apt94jesse - October 25, 2006 - 19:35

Thanks for the response, I had seen that, but that seems to be more of a "how they want it done" rather than "how it can be done."

I got my functionality to work like this. I added these lines in the page.tpl.php:

$buddycount = db_query('SELECT COUNT(*) FROM {buddylist} WHERE uid = %d', $GLOBALS['user']->uid);
  $buddyobject=db_result($buddycount, $row=0);
  if ($buddyobject=='0') {
  db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $GLOBALS['user']->uid , '1' , time());
  }

As you can see, I query the buddylist table, count the entries where the current user has buddies. If that count is 0, then it executes the insert query adding the default buddy. This way, whenever a new user logs in for the first time, their buddy count is of course 0, so it adds the buddy into the table.

#3

VeryMisunderstood - November 10, 2006 - 05:39

Thanks for the snippet.

scenario. user logs in , snippet sees no buddies, snippets adds default buddy. user deletes default buddy. logs out, logs back in, default buddy added again ?

#4

apt94jesse - November 15, 2006 - 04:07

Good point. Honestly I'm not worried about it because for my site I don't really want users to be able to delete the admin. Your scenario forgot to say that "user deletes default buddy and has no other buddies." Feel free to come up with a better function to add this sql code to, mine was chosen as the first one to work, not the best one.

Thanks for the interest, though, I hope someone wiser than me comes up with a better solution.

#5

apt94jesse - December 3, 2006 - 20:33

In case anyone is interested, I fixed the above scenario by adding these two lines to the user.module. This actually executes two queries, one is adding a default buddy, the other is sending an opening private message from said buddy.

db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid , '1' , time());


db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES ('1', '%d', 'MESSAGE SUBJECT', 'MESSAGE CONTENT', '%d', '%d', '%s', '0')", $user->uid, time(), 1, getenv("REMOTE_ADDR"));

The first query adds the buddy. Just change the '1' near the end to change the default buddy. I used 1 because I want the admin to be the default.

The second query sends the pm. Change the MESSAGE SUBJECT and MESSAGE CONTENT sections to suit your needs.

And now for placement. This is works in 4.7.4. You need to open your user.module file and navigate to the user_save function.

Find the following lines of code:

// Build the finished user object.
    $user = user_load(array('uid' => $array['uid']));

The commented line should start at line 212.

Then just insert the two queries directly after those lines.

Mine looks as follows:

// Build the finished user object.
    $user = user_load(array('uid' => $array['uid']));
    db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid , '1' , time());
    db_query("INSERT INTO {privatemsg} (author, recipient, subject, message, timestamp, newmsg, hostname, format) VALUES ('1', '%d', 'Welcome', 'Thank you for joining WrestleSpace.com.  As this
    is currently a Beta Service, we are constantly updating our features.  Look around and enjoy, and we will send you updates on things we change.', '%d', '%d', '%s', '0')", $user->uid, time(), 1, getenv("REMOTE_ADDR"));

As I just implemented this, there may be some bugs. I have determined that the user_save function is used both to update information and to save a new user. I tested to see if the queries were executed when a user updates their account info, and they don't appear to.

Should anyone know of a better way to do this, let me know, or tell me what I'm doing wrong.

-Jesse

#6

fago - December 25, 2006 - 20:21

please do it properly and provide a patch for it.
you can react on user-creation by using hook_user()

#7

apt94jesse - December 25, 2006 - 23:14

I'm honestly not quite sure how to do that. If someone with more knowledge than me would like to do so, go right ahead. I'm just happy that a solution exists.

#8

rkn - April 1, 2007 - 17:57

subscribe

#9

kbahey - April 24, 2007 - 21:03
Version:4.7.x-1.x-dev» 5.x-1.x-dev

Fago

I wrote a contrib module for 5.x that adds a predefined user to the buddy list for new users.

I can commit it to the contrib directory directly. Are you Ok with that?

#10

robertDouglass - April 24, 2007 - 21:07

Khalid, please feel free to give buddylist any loving treatment that you feel it needs. It is not getting much attention from me at the moment, so your help is very welcome.

#11

kbahey - April 25, 2007 - 01:39
Status:active» fixed

There is now a module called buddylistautoadd under the contrib directory that does this.

#12

droople - May 9, 2007 - 20:53

Please supply the download link

thank you

#13

kbahey - May 9, 2007 - 23:31

There is no download link needed.

It is included already in the download of buddylist.

#14

Anonymous - May 23, 2007 - 23:33
Status:fixed» closed
 
 

Drupal is a registered trademark of Dries Buytaert.