summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Douglass2006-11-29 23:15:38 (GMT)
committerRobert Douglass2006-11-29 23:15:38 (GMT)
commit6172d5d43a9e67c7c15341b559ad8f5a9358dc43 (patch)
tree03ca01100e678bdbacab3893daad327e942a911f
parent8039b59215ca859820565e38fe09075f1b3d759c (diff)
reinstating HEAD
-rw-r--r--README.txt118
-rw-r--r--buddylist.info4
-rwxr-xr-xbuddylist.install52
-rw-r--r--buddylist.module1079
-rw-r--r--buddylist_views.inc146
5 files changed, 1385 insertions, 14 deletions
diff --git a/README.txt b/README.txt
index 9b2f042..725d0e2 100644
--- a/README.txt
+++ b/README.txt
@@ -1,27 +1,117 @@
Readme
------
+This module allows users to put each other on a personal 'Buddy List'.
-Where is the module???
+Features include buddy groups and the ability to track your buddies' recent posts.
-You're looking at the CVS version of the buddylist module. It doesn't exist.
+Send comments to Robert Douglass at: http://drupal.org/user/5449/contact
-Available versions are:
-DRUPAL-4-5
-DRUPAL-4-6
-DRUPAL-4-7
-DRUPAL-5-0
+Requirements
+------------
+This module requires Drupal 4.7.
-As of this writing, all of the 4-* versions can be downloaded from the modules page:
-http://drupal.org/project/Modules
+This module does not yet offer PostgreSQL support. If you would like to contribute to this module by creating the appropriate PostgreSQL schema, please submit your code at http://drupal.org/project/issues/buddylist
-The DRUPAL-5-0 modules aren't being tarballed yet (October 25, 2006), so you have to get that version straight from CVS:
-cvs -z6 -d:pserver:anonymous:anonymous@cvs.drupal.org:/cvs/drupal-contrib co -d buddylist -r DRUPAL-5-0 contributions/modules/buddylist
+Upgrading from 4.6
+------------------
+WARNING: There is currently no migration path from 4.6 to 4.7. There are significant database schema changes and the 4.7 module will not work with the 4.6 schema. A migration path is forthcoming. If you need one sooner, contact me: http://drupal.org/user/5449/contact
-Why no CVS version?
+Installation
+------------
+1. Copy the buddylist.module to the Drupal modules/ directory.
-It is the least useful version that Drupal has. If you are actually honestly maintaining your module against the CVS development branch of Drupal, fine, have a CVS version of your module. The typical case is much uglier, however, and has led to our cvs repository being horribly disorganized. The typical case is that a module author wants to check in a new module and learns just enough CVS to get it into the repository, without a tag. Thus you find modules in the CVS repository, without tags, that are intended to be used with every imaginable flavor of Drupal. It is a mess.
+2. Enable buddy list in the "site settings | modules" administration screen.
-On the other hand, a tagged version of a module is clear; use this version with DRUPAL-*-*. Furthermore, at this time, the development branch of Drupal core IS DRUPAL-5-0. When the development branch is opened up again after the 5-0 release, if someone is interested in chasing it with the buddylist, we'll reinstate the untagged "CVS" version of buddylist.
+ Enabling the buddylist module will trigger the creation of the database schema. If you are shown error messages you may have to create the schema by hand. Please see the database definition at the end of this file.
+
+3. Enable buddy list blocks you want in the "blocks" administration screen.
+
+4. Optionally add the following theme function to your PHPTemplate's template.php file:
+
+function phptemplate_username($object) {
+ global $user;
+ /* Use the default theme_username for anonymous users, nodes by this user */
+ if ($user->uid == 0 || $object->uid == $user->uid || $object->uid == 0) {
+ return theme_username($object);
+ }
+ if (!user_access('maintain buddy list')) {
+ return theme_username($object);
+ }
+
+ /* an array, keyed on buddy uids */
+ $buddies = buddylist_get_buddies($user->uid);
+ /* Find out if this buddy is in the user's buddy list */
+ foreach ($buddies as $buddyuid => $buddystructure) {
+ if ($buddyuid == $object->uid) {
+ $output .= theme_username($object);
+ $output .= ' (';
+ $output .= theme('remove_from_buddylist_link', $object);
+ $output .= ')';
+ return $output;
+ }
+ }
+ /* The user is not in the buddylist, give a link to add */
+ $output .= theme_username($object);
+ $output .= ' (';
+ $output .= theme('add_to_buddylist_link', $object);
+ $output .= ')';
+ return $output;
+}
+
+
+Database Schema
+---------------
+If the automatic creation of the database tables was unsuccessful you can try creating the tables by hand using the following SQL:
+
+CREATE TABLE `buddylist` (
+ `uid` int(10) unsigned NOT NULL default '0',
+ `buddy` int(10) unsigned NOT NULL default '0',
+ `timestamp` int(11) NOT NULL default '0',
+ `received` tinyint(1) NOT NULL default '0',
+ UNIQUE KEY `uid-buddy-label` (`uid`,`buddy`),
+ KEY `uid` (`uid`)
+) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;
+
+--
+-- Table structure for table `buddylist_buddy_group`
+--
+
+CREATE TABLE `buddylist_buddy_group` (
+ `uid` int(11) NOT NULL default '0',
+ `buddy` int(11) NOT NULL default '0',
+ `label_id` int(11) NOT NULL default '0',
+ PRIMARY KEY (`uid`,`buddy`,`label_id`)
+) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;
+
+--
+-- Table structure for table `buddylist_groups`
+--
+
+CREATE TABLE `buddylist_groups` (
+ `uid` int(11) NOT NULL default '0',
+ `label_id` int(11) NOT NULL default '0',
+ `label` varchar(255) NOT NULL default '',
+ `visible` tinyint(1) NOT NULL default '0',
+ PRIMARY KEY (`uid`,`label_id`)
+) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;
+
+
+Credits
+-------
+Written by Adrian Rossouw.
+Thanks to Ratko Kovacina for the comments/debugging info
+Browsing improvements by Doug Sikora
+
+Maintainer: Robert Douglass
+Status: maintained (Feb. 2006)
+
+TODO
+----
+1. PGSQL schema (see buddylist.install, buddylist_install())
+2. Rework texts so that one is not stuck with "buddy" but could choose "contact", for example
+3. Make a workflow whereby a buddy request is sent to the buddy for confirmation which is required before the buddy can be added.
+4. Make the notification that someone added you to their buddylist use the privatemsg module, if available.
+5. Consider possible Views module integration. \ No newline at end of file
diff --git a/buddylist.info b/buddylist.info
new file mode 100644
index 0000000..25884f6
--- /dev/null
+++ b/buddylist.info
@@ -0,0 +1,4 @@
+; $Id$
+name = Buddylist
+description = Users add other users to their buddylists.
+package = Social networking \ No newline at end of file
diff --git a/buddylist.install b/buddylist.install
new file mode 100755
index 0000000..0ff955d
--- /dev/null
+++ b/buddylist.install
@@ -0,0 +1,52 @@
+<?php
+// $Id$
+
+/**
+ * Install the initial schema.
+ */
+function buddylist_install() {
+ switch ($GLOBALS['db_type']) {
+ case 'mysql':
+ case 'mysqli':
+ $query1 = db_query("
+ CREATE TABLE {buddylist} (
+ uid int(10) unsigned NOT NULL default '0',
+ buddy int(10) unsigned NOT NULL default '0',
+ timestamp int(11) NOT NULL default '0',
+ received tinyint(1) NOT NULL default '0',
+ PRIMARY KEY (uid,buddy)
+ ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
+
+ $query2 = db_query("
+ CREATE TABLE {buddylist_buddy_group} (
+ uid int(11) NOT NULL default '0',
+ buddy int(11) NOT NULL default '0',
+ label_id int(11) NOT NULL default '0',
+ PRIMARY KEY (uid, buddy, label_id)
+ ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
+
+ $query3 = db_query("
+ CREATE TABLE {buddylist_groups} (
+ uid int(11) NOT NULL default '0',
+ label_id int(11) NOT NULL default '0',
+ label varchar(255) NOT NULL default '',
+ visible tinyint(1) NOT NULL default '0',
+ PRIMARY KEY (uid, label_id)
+ ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
+
+ if ($query1 && $query2 && $query3) {
+ $created = TRUE;
+ }
+ break;
+ }
+ if ($created) {
+ drupal_set_message(t('Buddylist module installed successfully.'));
+ }
+ else {
+ drupal_set_message(t('Table installation for the Buddylist module was unsuccessful. The tables may need to be installed by hand. See the README.txt file for a list of the installation queries.'), 'error');
+ }
+}
+
+function buddylist_update_1() {
+ return _system_update_utf8(array('buddylist'));
+}
diff --git a/buddylist.module b/buddylist.module
new file mode 100644
index 0000000..6b6f481
--- /dev/null
+++ b/buddylist.module
@@ -0,0 +1,1079 @@
+<?php
+// $Id$
+
+if (module_exists('views')) {
+ include_once('buddylist_views.inc');
+}
+
+
+define('BUDDYLIST_ADD_SUBJECT', variable_get('buddylist_add_subject', "[@site] You are @adder_name's newest @buddy"));
+define('BUDDYLIST_REMOVE_SUBJECT', variable_get('buddylist_remove_subject', "[@site] You have been removed from @adder_name's @buddy list"));
+
+/**
+ * returns an array of common translation placeholders
+ */
+function buddylist_translation() {
+ $translations = array(
+ '@buddy' => t('buddy'),
+ '@Buddy' => t('Buddy'),
+
+ '@buddylist' => t('buddylist'),
+ '@Buddylist' => t('Buddylist'),
+
+ '@buddies' => t('buddies'),
+ '@Buddies' => t('Buddies'),
+
+ '@buddyof' => t('buddy of'),
+ '@Buddyof' => t('Buddy of'),
+ );
+ return $translations;
+}
+
+
+/**
+ * Implementation of hook_help
+ */
+function buddylist_help($section) {
+ switch ($section) {
+ case 'admin/settings/modules#description':
+ return t('Enable @buddy list functionality.', buddlist_translation());
+ case 'admin/help#buddylist':
+ $output = t("
+<p>@Buddy list enables users to keep a list of @buddies from their social network in their user account.
+Users can also track what their @buddies are posting to the site.
+Furthermore, they can track their <i>@buddies'</i> @buddies and thereby explore their social network.</p>
+<p>If the administrator has enabled the profile module, users can add @buddies via their @buddies' user profiles.
+On the \"View\" tab of each user's profile, there is a \"@Buddy list\" section. Select the 'add @buddy' action to add the user to your @buddy list.
+If a user is already in your @buddy list, the 'delete' action will remove the @buddy. Administrators can also enable the @buddylist block.
+This block allows you to see a list of your @buddies. If the <a href=\"http://drupal.org/project/foaf\">Friends Of A Friend (FOAF)</a> module is enabled, it will be possible to share @buddy lists with other FOAF-aware social networking applications.</p>
+<p>You can:</p>
+<ul>
+<li>add a @buddy by looking at their profile: <a href=\"@userprofiles\" title=\"View user profiles\">view user profiles</a></li>
+<li>allow users to view profiles in <a href=\"@setaccesspermissions\" title=\"set access permissions\">administer &raquo; access control</a></li>
+<li>enable the @buddy list block at <a href=\"@blockadministration\" title=\"block administration\">administer &raquo; block</a></li>
+<li>administer the @buddy list block at <a href=\"@buddylistsettings\" title=\"@buddylist settings\">administer &raquo; settings &raquo; @buddylist</a></li>
+</ul>
+
+<p>For more information, read the configuration and customization handbook <a href=\"http://drupal.org/handbook/modules/Buddylist\" title=\"Buddylist page\">Buddylist page</a></p>",
+array('@userprofiles' => url('profile'),
+ '@setaccesspermissions' => url('admin/user/access'),
+ '@blockadministration' => url('admin/build/block'),
+ '@buddylistsettings' => url('admin/settings/buddylist')
+ ) + buddylist_translation());
+ return $output;
+ }
+}
+
+
+/**
+ * Implementation of hook_perm
+ */
+function buddylist_perm() {
+ return array('maintain buddy list', 'view buddy lists');
+}
+
+/**
+ * Implementation of hook_menu
+ */
+function buddylist_menu($may_cache) {
+ global $user;
+
+ $items = array();
+ $id = is_numeric(arg(1)) ? arg(1) : $user->uid;
+
+ if ($may_cache) {
+ // buddylist settings page
+ $items[] = array(
+ 'path' => 'admin/settings/buddylist',
+ 'title' => t('Buddylist'), // Note that this isn't translated on purpose since it is for the admin
+ 'description' => t('Buddylist configuration options for blocks, email, etc.'),
+ 'callback' => 'drupal_get_form',
+ 'callback arguments' => 'buddylist_admin_settings',
+ 'access' => user_access('administer site configuration'),
+ );
+ // my buddylist menu item
+ $items[] = array(
+ 'path' => 'buddylist',
+ 'title' => t('My @buddylist', buddylist_translation()),
+ 'access' => (user_access('maintain buddy list') && $id),
+ 'callback' => 'buddylist_buddylisting_page',
+ 'callback arguments' => array($user->uid,'buddies'),
+ );
+ }
+ else {
+ // 'edit access' only granted to user's own buddy list or to administrative users
+ $editAccess = (
+ ($id == $user->uid && user_access('maintain buddy list') && $user->uid)
+ || user_access('administer users'));
+
+ $items[] = array(
+ 'path' => 'buddylist/'. $id .'/buddies/groups/edit',
+ 'title' => t('Edit groups'),
+ 'access' => $editAccess,
+ 'callback' => 'buddylist_buddiesgroups_edit',
+ 'type' => MENU_LOCAL_TASK,
+ 'weight' => 3,
+ 'callback arguments' => array($id),
+ );
+ $items[] = array(
+ 'path' => 'buddy/add',
+ 'title' => t('Add to @buddylist', buddylist_translation()),
+ 'access' => $editAccess,
+ 'callback' => 'drupal_get_form',
+ 'callback arguments' => array('buddylist_addbuddy', arg(2)),
+ 'type' => MENU_CALLBACK,
+ );
+ $items[] = array(
+ 'path' => 'buddy/delete',
+ 'title' => t('Delete from @buddylist', buddylist_translation()),
+ 'access' => $editAccess,
+ 'callback' => 'drupal_get_form',
+ 'callback arguments' => array('buddylist_deletebuddy', arg(2)),
+ 'type' => MENU_CALLBACK,
+ );
+
+ // 'view only' tabs
+ $viewAccess = (($id == $user->uid) || user_access('view buddy lists'));
+ $items[] = array(
+ 'path' => 'buddylist/'. $id .'/buddies',
+ 'title' => t('@Buddies', buddylist_translation()),
+ 'access' => $viewAccess,
+ 'callback' => 'buddylist_buddylisting_page',
+ 'type' => MENU_DEFAULT_LOCAL_TASK,
+ 'weight' => -1,
+ 'callback arguments' => array($id)
+ );
+ $items[] = array(
+ 'path' => 'buddylist/'. $id .'/buddyof',
+ 'title' => t('@Buddyof', buddylist_translation()),
+ 'access' => $viewAccess,
+ 'callback' => 'buddylist_buddylisting_page',
+ 'type' => MENU_LOCAL_TASK,
+ 'weight' => 1,
+ 'callback arguments' => array($id, 'buddyof')
+ );
+
+ // subtabs
+ $items[] = array(
+ 'path' => 'buddylist/'. $id .'/buddies/list',
+ 'title' => t('List'),
+ 'access' => $viewAccess,
+ 'callback' => 'buddylist_buddylisting_page',
+ 'type' => MENU_DEFAULT_LOCAL_TASK,
+ 'weight' => -1,
+ 'callback arguments' => array($id),
+ );
+ $items[] = array(
+ 'path' => 'buddylist/'. $id .'/buddies/recent',
+ 'title' => t('Recent posts'),
+ 'access' => ($viewAccess && module_exists('tracker')),
+ 'callback' => 'buddylist_buddiesrecent_page',
+ 'type' => MENU_LOCAL_TASK,
+ 'weight' => 1,
+ 'callback arguments' => array($id),
+ );
+ $items[] = array(
+ 'path' => 'buddylist/'. $id .'/buddies/groups/view',
+ 'title' => t('View groups'),
+ 'access' => $viewAccess,
+ 'callback' => 'buddylist_buddiesgroups_page',
+ 'type' => MENU_LOCAL_TASK,
+ 'weight' => 2,
+ 'callback arguments' => array($id),
+ );
+
+ // other callbacks
+ if ($id != $user->uid) {
+ // This callback can interfere with the 'my buddylist' menu item,
+ // so we only load it if the user is viewing another user's list.
+ $items[] = array(
+ 'path' => 'buddylist/'. $id,
+ 'title' => t('@Buddylist', buddylist_translation()),
+ 'access' => (($viewAccess || $editAccess) && $id),
+ 'callback' => 'buddylist_buddylisting_page',
+ 'type' => MENU_CALLBACK,
+ 'callback arguments' => array($id),
+ );
+ }
+ $items[] = array(
+ 'path' => 'buddylist/'. $id .'/buddies/recent/feed',
+ 'title' => t('Xml feed'),
+ 'access' => $viewAccess,
+ 'callback' => 'buddylist_buddyfeed',
+ 'type' => MENU_CALLBACK,
+ 'callback arguments' => array($id),
+ );
+ }
+
+ return $items;
+}
+
+/**
+ * Buddylist administration settings page
+ */
+function buddylist_admin_settings() {
+
+ // TODO: move these to block settings
+ $form['block_settings'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('@Buddylist block options', buddylist_translation()),
+ );
+ $form['block_settings']['buddylist_blocklisting_size'] = array(
+ '#type' => 'select',
+ '#title' => t("Number of @buddies to list in the user's @buddy block", buddylist_translation()),
+ '#default_value' => variable_get('buddylist_blocklisting_size', 5),
+ '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
+ '#description' => t('This setting controls the maximum number of @buddies displayed in a user\'s "@buddylist block" given that the "@buddylist block" is enabled in the @link.', array('@link' => l(t('block settings'), 'admin/build/block')) + buddylist_translation()),
+ );
+ $form['block_settings']['buddylist_posts_block'] = array(
+ '#type' => 'select',
+ '#title' => t("Number of posts to list in the @buddies' recent posts block", buddylist_translation()),
+ '#default_value' => variable_get('buddylist_posts_block', 7),
+ '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30)),
+ '#description' => t('This setting controls the maximum number of posts to display in a user\'s "@buddy recent posts" block given that the "@buddies\' recent posts" block is enabled in the @link.', array('@link' => l(t('block settings'), 'admin/build/block')) + buddylist_translation()),
+ );
+ $form['block_settings']['buddylist_list_block_title'] = array(
+ '#type' => 'textfield',
+ '#title' => t('"My @buddies list" block title', buddylist_translation()),
+ '#default_value' => variable_get('buddylist_list_block_title', t('My @buddy list', buddylist_translation())),
+ '#size' => 70,
+ '#maxlength' => 128,
+ '#description' => t('This will be the title for the "My @buddy list" block. If none is specified, "My @buddy list" will be used.', buddylist_translation()),
+ );
+ $form['block_settings']['buddylist_block_title'] = array(
+ '#type' => 'textfield',
+ '#title' => t('"My @buddies\' recent posts" block title', buddylist_translation()),
+ '#default_value' => variable_get('buddylist_block_title', t("My @buddies' recent posts", buddylist_translation())),
+ '#size' => 70,
+ '#maxlength' => 128,
+ '#description' => t('This will be the title for the recent @buddies post block. If none is specified, "My @buddies\' recent posts" will be used.', buddylist_translation()),
+ );
+
+ // User profile page settings
+ $form['profile_settings'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('Profile page options'),
+ );
+ $form['profile_settings']['buddylist_prof_buddies'] = array(
+ '#type' => 'select',
+ '#title' => t('Number of @buddies and users who\'ve added me', buddylist_translation()),
+ '#default_value' => variable_get('buddylist_prof_buddies', 5),
+ '#options' => drupal_map_assoc(range(0, 10)),
+ '#description' => t('The default maximum number of @buddies and users who\'ve added me as a @buddy to display on a user\'s profile page.', buddylist_translation()),
+ );
+
+ $form['mail'] = array(
+ '#type' => 'fieldset',
+ '#title' => t('email'),
+ );
+
+ $macros = implode(', ', array_keys(buddylist_mail_replacements() + buddylist_translation()));
+
+ $form['mail']['buddylist_send_add'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Send add messages'),
+ '#default_value' => variable_get('buddylist_send_add', FALSE),
+ );
+
+ $form['mail']['buddylist_add_subject'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Added @buddy email subject', buddylist_translation()),
+ '#default_value' => BUDDYLIST_ADD_SUBJECT,
+ );
+
+ $add_default = buddylist_mail_add_default();
+
+ $form['mail']['buddylist_add_text'] = array(
+ '#type' => 'textarea',
+ '#title' => t('Added @buddy email message', buddylist_translation()),
+ '#default_value' => variable_get('buddylist_add_text', $add_default),
+ '#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
+ );
+
+ $form['mail']['buddylist_send_remove'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Send remove messages'),
+ '#default_value' => variable_get('buddylist_send_remove', FALSE),
+ );
+
+ $form['mail']['buddylist_remove_subject'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Removed @buddy email subject', buddylist_translation()),
+ '#default_value' => BUDDYLIST_REMOVE_SUBJECT,
+ );
+
+ $remove_default = buddylist_mail_remove_default();
+
+ $form['mail']['buddylist_remove_text'] = array(
+ '#type' => 'textarea',
+ '#title' => t('Removed @buddy email message'),
+ '#default_value' => variable_get('buddylist_remove_text', $remove_default),
+ '#description' => t('Replacement strings are: %macros', array('%macros' => $macros)),
+ );
+
+ return system_settings_form($form);
+}
+
+/**
+ * Implementation of hook_user
+ */
+function buddylist_user($type, &$edit, &$thisuser, $category = NULL) {
+ global $user;
+
+ $output = array();
+ // show any buddylist notifications upon login and upon viewing own profile
+ if (user_access('maintain buddy list') && (($type == 'login') || ($type == 'view') && ($thisuser->uid == $user->uid))) {
+ buddylist_setmsg_received($thisuser);
+ }
+
+ if ($type == 'view' && user_access('view buddy lists')) {
+ // if thisuser has friends, show friends
+ $cnt = variable_get('buddylist_prof_buddies', 5);
+ $i = 0;
+ if ($buddies = buddylist_get_buddies($thisuser->uid)) {
+ foreach(array_keys($buddies) as $buddy) {
+ $account = user_load(array('uid' => $buddy));
+ $listbuddies[] = $account;
+ $i++;
+ if ($i > $cnt) {
+ break;
+ }
+ }
+ $output[] = array('title' => t('@buddies', buddylist_translation()), 'value' => theme('user_list', $listbuddies), 'class' => 'buddylist',);
+ }
+
+ // This portion of code is used to see if this $thisuser is a buddy of others and, if s/he is, returns a list
+ // of people s/he is a buddy of.
+ // Note the distinction between having a buddy and being someone else's buddy (i.e., 'buddyof')
+ $sql = 'SELECT b.uid, u.name FROM {buddylist} b INNER JOIN {users} u ON b.uid = u.uid WHERE b.buddy = %d ORDER BY u.access DESC';
+ $result = db_query_range($sql, $thisuser->uid, 0, $cnt);
+ while ($row = db_fetch_object($result)) {
+ $listbuddyof[$row->uid] = $row;
+ }
+ if ($listbuddyof) {
+ $output[] = array('title' => t('@buddy of', buddylist_translation()), 'value' => theme('user_list', $listbuddyof));
+ }
+ }
+
+ if ($type == 'view' && user_access('maintain buddy list')) {
+ // Check to see whether or not $thisuser is in global $user's buddy list
+ // If $thisuser is already in $user's buddy list, a link offering to delete $thisuser from $user's buddy list is generated
+ // If $thisuser is not on $user's buddy list, and $thisuser != $user, then a link offering to add $thisuser to $user's buddy list
+ // is generated.
+ if (@in_array($thisuser->uid, array_keys(buddylist_get_buddies($user->uid))) && user_access('maintain buddy list')) {
+ $actions[] = theme('remove_from_buddylist_link', $thisuser);
+ }
+ else {
+ if ($user->uid != $thisuser->uid && user_access('maintain buddy list')) {
+ $actions[] = theme('add_to_buddylist_link', $thisuser);
+ }
+ }
+ if ($actions) {
+ $output[] = array('title' => t('@buddy actions', buddylist_translation()), 'value' => theme('item_list', $actions), 'class' => 'buddylist');
+ }
+ if(count($output) > 0) {
+ return array(t('@buddy List', buddylist_translation()) => $output);
+ }
+ }
+}
+
+
+/**
+ * Implementation for hook_block
+ */
+function buddylist_block($op = 'list', $delta = 0) {
+ global $user;
+
+ if ($op == 'list') {
+ $block[0]['info'] = variable_get('buddylist_list_block_title', t('My @buddy list', buddylist_translation()));
+ $block[1]['info'] = variable_get('buddylist_block_title', t('My @buddies\' recent posts', buddylist_translation()));
+ return $block;
+ }
+ else if ($op == 'view' && user_access('access content') && $user->uid > 0) {
+ switch ($delta) {
+ case 0 : // Shows buddylist block
+ if ($buddies = buddylist_get_buddies()) {
+ $i = 0;
+ foreach (array_keys($buddies) as $buddy) {
+ $users[] = user_load(array('uid' => $buddy));
+ $i++;
+ if ($i == variable_get('buddylist_blocklisting_size', 5)) {
+ break;
+ }
+ }
+
+
+ $block['content'] = theme('user_list', $users);
+ $block['subject'] = variable_get('buddylist_list_block_title', t('My @buddy list', buddylist_translation()));
+
+ // check if a "more" link should generated by seeing if there are more buddies than the specified $upperlimit
+ if (count($buddies) > variable_get('buddylist_blocklisting_size', 5)) {
+ $block['content'] .= '<div class="more-link">' . l(t('more'), 'buddylist', array('title' => t('View more.'))) . '</div>';
+ }
+ return $block;
+ }
+ break;
+
+ case 1: // Shows my buddies recent posts block
+ $buddies = buddylist_get_buddies();
+ $keys = array_keys($buddies);
+ if (count($keys) > 0) {
+ $str_buddies = implode(',', $keys);
+ $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.status, n.type, u.uid, u.name, n.created, n.title FROM {node} n INNER JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND n.uid IN ($str_buddies) ORDER BY n.nid DESC"), 0, variable_get('buddylist_posts_block', 7));
+
+ if (db_num_rows($result)) {
+ $block['subject'] = variable_get('buddylist_block_title', t('My @buddies\' recent posts', buddylist_translation()));
+ $block['content'] = node_title_list($result);
+
+ // check if a "more" link should generated by seeing if there are more buddies than the specified $upperlimit
+ $result = db_query(db_rewrite_sql('SELECT COUNT(n.nid) AS node_count FROM {buddylist} b LEFT JOIN {node} n ON n.uid=b.buddy LEFT JOIN {users} u ON n.uid = u.uid WHERE n.status = 1 AND b.uid=%d'), $user->uid);
+ $countresult = db_fetch_object($result);
+
+ if (variable_get('buddylist_posts_block', 7) < $countresult->node_count) {
+ $block['content'] .= '<div class="more-link">'. l(t('more'), 'buddylist/'. $user->uid .'/buddies/recent', array('title' => t('View more.'))) .'</div>';
+ }
+ return $block;
+ }
+ }
+ break;
+ }
+ }
+}
+
+/**
+ * Public API for retrieving buddies. Feel free to use this from other
+ * modules.
+ * $key can be 'uid' or 'label'.
+*/
+function buddylist_get_buddies($uid = NULL, $key = 'uid') {
+ static $buddies;
+
+ if (!$uid) {
+ global $user;
+ $uid = $user->uid;
+ }
+ if (!isset($buddies[$key][$uid])) {
+ $buddies[$key][$uid] = array();
+ $sql = 'SELECT b.buddy, u.name, u.mail FROM {buddylist} b
+ INNER JOIN {users} u ON b.buddy = u.uid
+ WHERE b.uid = %d';
+ $result = db_query($sql, $uid);
+ while ($row = db_fetch_object($result)) {
+ $buddies[$key][$uid][$row->buddy]['name'] = $row->name;
+ $buddies[$key][$uid][$row->buddy]['mail'] = $row->mail;
+ $buddies[$key][$uid][$row->buddy]['groups'] = buddylist_get_buddy_groups($uid, $row->buddy);
+ $buddies[$key][$uid][$row->buddy]['online'] = 0;
+ $selectlist .= $row->buddy.",";
+ }
+ // Add the online flag
+ if (db_num_rows($result)) {
+ $sql = 'SELECT uid FROM {sessions} WHERE uid IN (%s) AND timestamp > %d';
+ $result = db_query($sql, substr($selectlist,0,-1), time()-1800);
+ while ($row = db_fetch_object($result)) {
+ $buddies[$key][$uid][$row->uid]['online'] = 1;
+ }
+ }
+ }
+
+ return $buddies[$key][$uid];
+}
+
+function buddylist_setmsg_received($thisuser) {
+ global $user;
+
+ $check_received = db_query('SELECT received, b.uid as uid, u.name FROM {buddylist} b LEFT JOIN {users} u ON u.uid = b.uid WHERE buddy = %d AND received = 1', $thisuser->uid);
+ while ($rec = db_fetch_object($check_received)) {
+ if (($rec->received) and ($thisuser->uid == $user->uid)) {
+ // TODO: This is where integration with Privatemsg could happen. If enabled, send a private message instead.
+ $link = (user_access('access user profiles')) ? l($rec->name, 'user/'. $rec->uid) : $rec->name;
+ drupal_set_message(t('!linktouser has added you to his/her @buddylist.', array('!linktouser' => $link) + buddylist_translation()));
+ db_query('UPDATE {buddylist} SET received = 0 WHERE buddy = %d', $user->uid);
+ }
+ }
+}
+
+/**
+ * expose add and remove links to theming.
+ */
+function theme_remove_from_buddylist_link($buddyuser) {
+ return l(t('Remove %name from my @buddy list', array('%name' => $buddyuser->name) + buddylist_translation()), 'buddy/delete/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
+}
+
+function theme_add_to_buddylist_link($buddyuser) {
+ return l(t('Add %name to my @buddy list', array('%name' => $buddyuser->name) + buddylist_translation()), 'buddy/add/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
+}
+
+
+/**
+ * Displays a list of a given user's buddies.
+ */
+function buddylist_buddylisting_page($uid = NULL, $mode = 'buddies') {
+ global $user;
+
+ if (empty($uid)) {
+ $uid = $user->uid;
+ }
+ // Check that the uid is valid, not the anonymous user, and the user exists
+ if (!(is_numeric($uid) && ($uid > 0) && $thisuser = user_load(array('uid' => $uid)))) {
+ drupal_not_found();
+ exit();
+ }
+
+ drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name) + buddylist_translation()));
+
+ $buddies_per_page = 20;
+
+ //TODO: use the get_buddies function instead
+ if ($mode == 'buddies') {
+ $sql = "SELECT DISTINCT(b.buddy), u.access FROM {buddylist} b INNER JOIN {users} u ON b.buddy = u.uid WHERE b.uid = %d ORDER BY u.access DESC";
+ }
+ else {
+ $sql = "SELECT DISTINCT(u.uid) as buddy, u.access FROM {buddylist} b INNER JOIN {users} u ON b.uid = u.uid WHERE b.buddy = %d ORDER BY u.access DESC";
+ }
+ $result = pager_query($sql, $buddies_per_page, 0 , NULL, $uid);
+
+ $header = array(t('@buddy', buddylist_translation()), t('online'));
+ $online_interval = time() - variable_get('user_block_seconds_online', 180);
+
+ if (db_num_rows($result)) {
+ while ($account = db_fetch_object($result)) {
+ $online = $account->access > $online_interval;
+ $rows[] = array(theme('username', user_load(array('uid' => $account->buddy))), theme('buddylist_online', $online));
+ }
+ $output .= theme('table', $header, $rows);
+ }
+ else {
+ $output .= t('No @buddies found.', buddylist_translation());
+ }
+
+ $output .= theme('pager', NULL, $buddies_per_page);
+
+ return $output;
+}
+
+function buddylist_form_buddiesrecent_page($buddies) {
+ foreach ($buddies as $user_id => $buddy) {
+ $form[] = array('#type' => 'fieldset',
+ '#title' => $buddy['name'],
+ '#collapsible' => 'true',
+ '#value' => tracker_page($user_id),
+ );
+ }
+ return $form;
+}
+
+function buddylist_buddiesrecent_page($uid) {
+ global $user;
+
+ $thisuser = user_load(array('uid' => $uid));
+ drupal_set_title(t('%username\'s @buddylist', array('%username' => $thisuser->name) + buddylist_translation()));
+
+ $buddies = buddylist_get_buddies($uid);
+ $output .= drupal_get_form('buddylist_form_buddiesrecent_page', $buddies);
+ $output .= theme('xml_icon', url('buddylist/'. $uid .'/buddies/recent/feed'));
+ drupal_set_html_head('<link rel="alternate" type="application/rss+xml" title="'. t('RSS - @buddies posts', buddylist_translation()). '" href="'. url('buddylist/'. $user->uid .'/buddies/recent/feed') .'" />');
+
+ return $output;
+}
+
+function buddylist_buddiesgroups_page($uid) {
+ global $user;
+ $thisuser = user_load(array('uid' => $uid));
+ drupal_set_title(t('%username\'s @buddy groups', array('%username' => $thisuser->name) + buddylist_translation()));
+
+ $headers = array(t('@buddy', buddylist_translation()), t('online'), t('# of @buddies', buddylist_translation()), t("@buddy's posts", buddylist_translation()));
+
+ $result = db_query('SELECT DISTINCT(label), label_id FROM {buddylist_groups} WHERE uid = %d ORDER BY label ASC', $thisuser->uid);
+ $groups = array();
+ while ($row = db_fetch_object($result)) {
+ $groups[$row->label_id] = $row->label;
+ }
+
+ if (count($groups) == 0) {
+ if ($thisuser->uid == $user->uid) {
+ drupal_set_message(
+ t("To organize your @buddies into groups, visit the !edit_groups page",
+ array('!edit_groups' =>
+ l(t('edit groups'), "buddylist/$uid/buddies/groups/edit")) + buddylist_translation())
+ );
+ }
+ return t('No groups found.');
+ }
+
+ $online_interval = time() - variable_get('user_block_seconds_online', 180);
+ $buddies = buddylist_get_buddies($thisuser->uid);
+
+ foreach ($groups as $label_id => $label) {
+ $result = pager_query('SELECT bg.buddy, u.access FROM {buddylist_buddy_group} bg INNER JOIN {users} u ON bg.buddy = u.uid WHERE bg.uid = %d and bg.label_id = %d', 10, 0, NULL, $thisuser->uid, $label_id);
+ $rows = array();
+ while ($row = db_fetch_object($result)) {
+ $online = $row->access > $online_interval;
+ $rows[] = array(
+ l($buddies[$row->buddy]['name'], 'user/'.$row->buddy),
+ theme('buddylist_online', $online),
+ buddylist_count_buddies($row->buddy),
+ l(t('view posts'), 'user/'. $row->buddy. '/track')
+ );
+ }
+ return theme('table', $headers, $rows);
+ }
+}
+
+function buddylist_count_buddies($uid) {
+ $result = db_query("SELECT count(DISTINCT buddy) AS buddies FROM {buddylist} WHERE uid = %d", $uid);
+ return db_result($result);
+}
+
+
+function buddylist_get_buddy_groups($uid, $buddy = NULL) {
+ if (isset($buddy)) {
+ $and = " AND bbg.buddy = %d ";
+ }
+ $result = db_query("SELECT bg.label_id, bg.label, bg.visible FROM {buddylist_groups} bg INNER JOIN {buddylist_buddy_group} bbg ON bbg.uid = bg.uid WHERE bbg.uid = %d AND bg.label_id = bbg.label_id $and", $uid, $buddy);
+ $buddy_groups = array();
+ while ($row = db_fetch_array($result)) {
+ $buddy_groups[] = $row;
+ }
+ return $buddy_groups;
+}
+
+
+function buddylist_form_edit_groups_add() {
+ // Add group form
+ $form['add_group'] = array(
+ '#type' => 'textfield',
+ '#title' => t('Add new group'),
+ '#description' => t('Groups are a way to keep your @buddies organized. Groups can be named whatever you like.', buddylist_translation()),
+ );
+
+ $form['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Add'),
+ );
+
+ return $form;
+}
+
+function buddylist_form_edit_groups_remove($all_groups) {
+ // Make a form to remove groups
+ $form['remove']['groups'] = array(
+ '#type' => 'checkboxes',
+ '#return_value' => 1,
+ '#title' => '',
+ '#default_value' => null,
+ '#options' => $all_groups,
+ );
+
+ $form['remove']['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Remove'),
+ );
+
+ return $form;
+}
+
+function buddylist_form_edit_groups_table($buddies, $all_groups, $thisuser) {
+ // Build the table with buddies and their groups
+ $form['table']['groups'] = array ('#tree' => true);
+ foreach ($buddies as $uid => $buddy) {
+ $items = array();
+ foreach ($buddy['groups'] as $group) {
+ $items[] = $group['label_id'];
+ }
+
+ if (count($all_groups) > 0) {
+ $form['table']['groups'][$uid] = array(
+ '#type' => 'checkboxes',
+ '#title' => '',
+ '#return_value' => '1',
+ '#default_value' => $items,
+ '#options' => $all_groups,
+ );
+ }
+ }
+
+ $form['table']['user'] = array(
+ '#type' => 'value',
+ '#value' => $thisuser->uid,
+ );
+
+ if (count($form['table']['groups']) > 0) {
+ $form['table']['submit'] = array(
+ '#type' => 'submit',
+ '#value' => t('Submit'),
+ );
+ }
+
+ return $form;
+}
+
+/**
+ * Callback for the buddygroup editing page
+ */
+function buddylist_buddiesgroups_edit($uid) {
+ $thisuser = user_load(array('uid' => $uid));
+ drupal_set_title(t('%username\'s @buddy groups', array('%username' => $thisuser->name) + buddylist_translation()));
+
+ if ($buddies = buddylist_get_buddies($thisuser->uid)) {
+ $output['add'] = drupal_get_form('buddylist_form_edit_groups_add');
+
+ // Get all groups
+ $result = db_query("SELECT label_id, label FROM {buddylist_groups} WHERE uid = %d ORDER BY label ASC", $uid);
+ $all_groups = array();
+ while ($row = db_fetch_object($result)) {
+ if ($row->label != '') {
+ $all_groups[$row->label_id] = $row->label;
+ }
+ }
+
+ $other_groups = buddylist_get_buddy_groups($uid);
+
+ if (count($all_groups) > 0) {
+ $output['remove'] = drupal_get_form('buddylist_form_edit_groups_remove', $all_groups);
+ $output['table'] = drupal_get_form('buddylist_form_edit_groups_table', $buddies, $all_groups, $thisuser);
+ }
+ else {
+ drupal_set_message(t("You don't have any groups defined."));
+ }
+
+ return theme('buddylist_edit_groups', $output);
+ }
+ else {
+ drupal_set_message(t('Unable to edit @buddy groups. Add @buddies to your @buddylist before making groups.', buddylist_translation()));
+ return t('No @buddies found.', buddylist_translation());
+ }
+}
+
+function theme_buddylist_edit_groups($forms) {
+ $output = '<span id="buddylist-group-add-form">'.
+ $forms['add'].
+ '</span>'.
+ '<span id="buddylist-group-remove-form">'.
+ $forms['remove'].
+ '</span>'.
+ '<span id="buddlist-groups-form">'.
+ $forms['table'].
+ '</span>';
+ return $output;
+}
+
+function theme_buddylist_form_edit_groups_table($form) {
+ $rows = array();
+
+ foreach ($form['table']['groups'] as $key => $value) {
+ if(is_numeric($key)) {
+ $rows[] = array(theme('username', user_load(array('uid' => $key))), drupal_render($form['table']['groups'][$key]));
+ }
+ }
+
+ $headers = array(t('buddy'), t('@buddy groups', buddylist_translation()));
+ $output .= theme('table', $headers, $rows);
+
+ $output .= drupal_render($form);
+
+ return $output;
+}
+
+function buddylist_form_edit_groups_add_submit($form_id, $form_values) {
+ global $user;
+ $label_id = buddylist_buddygroup_new($user->uid, $form_values['add_group']);
+}
+
+function buddylist_form_edit_groups_remove_submit($form_id, $form_values) {
+ global $user;
+ foreach ($form_values['groups'] as $label_id => $remove) {
+ if ($remove > 0) {
+ buddylist_buddygroup_remove($user->uid, $label_id);
+ }
+ }
+}
+
+function buddylist_form_edit_groups_table_submit($form_id, $form_values) {
+ $userid = $form_values['user'];
+ foreach ($form_values['groups'] as $buddy => $groups) {
+ foreach ($groups as $label_id => $checked) {
+ if ($checked == 0) {
+ buddylist_buddygroup_remove_buddy($userid, $buddy, $label_id);
+ }
+ else {
+ buddylist_buddygroup_add_buddy($userid, $buddy, $label_id);
+ }
+ }
+ }
+
+ drupal_set_message(t('@buddy groups saved.', buddylist_translation()));
+}
+
+/**
+ * Creates a new buddy group for a user
+ *
+ * @param $uid user id of the user to whom the group will belong.
+ * @param $group string; name of the group
+ * @param $visible determines whether the user's buddies can see which groups they've been put in.
+ *
+ * @return $label_id the existing or newly created id for the name of this group.
+ */
+function buddylist_buddygroup_new($uid, $group, $visible = FALSE) {
+ $label_id = db_result(db_query("SELECT label_id FROM {buddylist_groups} WHERE uid = %d AND label = '%s'", $uid, $group));
+ if (is_null($label_id)) {
+ $new_label_id = db_next_id('buddygroup');
+ db_query("INSERT INTO {buddylist_groups} VALUES (%d, %d, '%s', %d)", $uid, $new_label_id, $group, $visible);
+ return $new_label_id;
+ }
+ else {
+ return $label_id;
+ }
+}
+
+/**
+ * Removes a buddy group for a user
+ *
+ * @param $uid user id of the user to whom the group belongs.
+ * @param $label_id id of the group
+ */
+function buddylist_buddygroup_remove($uid, $label_id) {
+ db_query("DELETE FROM {buddylist_groups} WHERE uid = %d AND label_id = %d", $uid, $label_id);
+ db_query("DELETE FROM {buddylist_buddy_group} WHERE uid = %d AND label_id = %d", $uid, $label_id);
+}
+
+function buddylist_buddygroup_remove_buddy($uid, $buddy, $label_id) {
+ db_query("DELETE FROM {buddylist_buddy_group} WHERE uid = %d AND buddy = %d AND label_id = %d", $uid, $buddy, $label_id);
+}
+
+function buddylist_buddygroup_add_buddy($uid, $buddy, $label_id) {
+ db_lock_table('buddylist_buddy_group');
+ buddylist_buddygroup_remove_buddy($uid, $buddy, $label_id);
+ db_query('INSERT INTO {buddylist_buddy_group} VALUES (%d, %d, %d)', $uid, $buddy, $label_id);
+ db_unlock_tables();
+}
+
+/**
+ * Feed for buddies recent posts
+ */
+function buddylist_buddyfeed($uid) {
+ if (!(is_numeric($uid) && $uid > 0)) {
+ return drupal_not_found();
+ exit();
+ }
+
+ $buddy_ids = array_keys(buddylist_get_buddies($uid));
+
+ // false query to be used if no posts from buddies are available (as in this user has no buddies).
+ $result = db_query('SELECT nid FROM {node} WHERE 0');
+ if (count($buddy_ids)) {
+ $buddy_ids_str = '('. implode(',', $buddy_ids). ')';
+ $result = db_query(db_rewrite_sql('SELECT nid FROM {node} WHERE status = 1 AND uid IN %s ORDER BY nid DESC'), $buddy_ids_str);
+ }
+ $channel['title'] = t('@buddies recent posts on %site', array('%site' => variable_get('site_name', 'drupal')) + buddylist_translation());
+ $channel['link'] = url('buddylist/'. $uid .'/buddies/recent', NULL, NULL, TRUE);
+
+ node_feed($result, $channel);
+}
+
+function buddylist_addbuddy($uid) {
+ global $user;
+ $buddy = user_load(array('uid' => $uid));
+
+ if (empty($buddy->name)) {
+ drupal_set_message(t('This user does not exist'));
+ }
+ elseif (in_array($uid, array_keys(buddylist_get_buddies($user->uid)))) {
+ drupal_set_message(t('This user is already on your @buddy list', buddylist_translation()));
+ }
+ elseif ($user->uid == $uid) {
+ drupal_set_message(t('Cannot add yourself to @buddy list', buddylist_translation()));
+ }
+ else {
+ $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
+ $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name);
+ $output = confirm_form(
+ $form,
+ t('Add user %name to your @buddy list?', array('%name' => $buddy->name) + buddylist_translation()),
+ $_GET['destination'],
+ '',
+ t('Add'), t('Cancel'),
+ 'buddylist_addbuddy_confirm');
+ return $output;
+ }
+ drupal_goto();
+}
+//function buddylist_form_alter($a, $b) {dprint_r($a); dprint_r($b);}
+
+/**
+ * Confirm and add a buddy.
+ */
+function buddylist_addbuddy_submit($form_id, $form_values) {
+ buddylist_add($form_values['uid']);
+ drupal_set_message(t('%name will be be notified the next time s/he logs in.', array('%name' => $form_values['name'])));
+ return 'user';
+};
+
+/**
+ * Removes the user $uid from the global user's account.
+ * TODO: generalize this so that two uids can be given
+ */
+function buddylist_deletebuddy($uid) {
+ global $user;
+ $buddy = user_load(array('uid' => $uid));
+
+ if (empty($buddy->name)) {
+ drupal_set_message('This user does not exist');
+ }
+ else if (!in_array($uid, array_keys(buddylist_get_buddies($user->uid)))) {
+ drupal_set_message('This user is not on your @buddy list', buddylist_translation());
+ }
+ else {
+ $form['uid'] = array('#type' => 'hidden', '#value' => $uid);
+ $form['name'] = array('#type' => 'hidden', '#value' => $buddy->name);
+ $output = confirm_form(
+ $form,
+ t('Remove user %name from your @buddy list?', array('%name' => $buddy->name) + buddylist_translation()),
+ $_GET['destination'],
+ '',
+ t('Remove'), t('Cancel'),
+ 'buddylist_deletebuddy_confirm');
+ return $output;
+ }
+ drupal_goto();
+}
+
+/**
+ * Confirm and add a buddy.
+ */
+function buddylist_deletebuddy_submit($form_id, $form_values) {
+ buddylist_remove($form_values['uid']);
+ drupal_set_message(t('@name will be be notified of being removed.', array('@name' => $form_values['name'])));
+ return 'user';
+};
+
+function buddylist_add($id) {
+ global $user;
+ $user_to_add = user_load(array('uid' => $id));
+
+ if (!in_array($id, array_keys(buddylist_get_buddies($user->uid)))) {
+ db_query('INSERT INTO {buddylist} (received, uid, buddy, timestamp) VALUES (1, %d, %d, %d)' , $user->uid , $id , time());
+ // DB value buddylist.received set to 1, meaning buddy has a message waiting
+ // letting them know you added them as a buddy
+ // buddylist.received set back to 0 when user logs in along with being informed of new buddy
+ if (variable_get('buddylist_send_add', FALSE)) {
+ buddylist_mail_user('add', $user_to_add);
+ }
+ drupal_set_message(t('%username has been added to your @buddy list', array('%username' => $user_to_add->name) + buddylist_translation()));
+ }
+ else {
+ drupal_set_message(t('%username is already on your @buddylist', array('%username' => $user_to_add->name) + buddylist_translation()));
+ }
+}
+
+function buddylist_remove($id) {
+ global $user;
+ db_query('DELETE FROM {buddylist} WHERE uid = %d AND buddy = %d' , $user->uid , $id);
+ $thisuser = user_load(array('uid' => $id));
+ if (variable_get('buddylist_send_remove', FALSE)) {
+ buddylist_mail_user('remove', $thisuser);
+ }
+ drupal_set_message(t('%username has been removed from your @buddylist', array('%username' => $thisuser->name) + buddylist_translation()));
+}
+
+function buddylist_cancel_add($id) {
+ $thisuser = user_load(array('uid' => $id));
+ drupal_set_message(t('User %name was NOT added to your @buddylist.', array('%name' => $thisuser->name) + buddylist_translation()));
+}
+
+function buddylist_cancel_remove($id) {
+ $thisuser = user_load(array('uid' => $id));
+ drupal_set_message(t('User %name was NOT removed from your @buddylist.', array('%name' => $thisuser->name) + buddylist_translation()));
+}
+
+function theme_buddylist_online($online) {
+ return $online ? t('yes') : t('no');
+}
+
+function buddylist_mail_user($op, $account, $user = NULL) {
+ if (is_null($user)){
+ global $user;
+ }
+ switch($op) {
+ case 'add':
+ $subject = BUDDYLIST_ADD_SUBJECT;
+ $message = variable_get('buddylist_add_message', buddylist_mail_add_default());
+ break;
+ case 'remove':
+ $subject = BUDDYLIST_REMOVE_SUBJECT;
+ $message = variable_get('buddylist_remove_message', buddylist_mail_remove_default());
+ break;
+ }
+
+ // eval the replacements
+ $replacements = array();
+ foreach(buddylist_mail_replacements() as $key => $code) {
+ eval('$replacements["$key"] = '. $code .';');
+ }
+ $replacements += buddylist_translation();
+
+ // replace the macros
+ $subject = t($subject, $replacements);
+ $message = t($message, $replacements);
+
+ $site_mail = variable_get('site_mail', "");
+ if (!strlen($site_mail)) {
+ if (user_access('administer nodes')){
+ drupal_set_message(t('You should create an administrator mail address for your site! <a href="%url">Do it here</a>.', array('%url' => url('admin/settings/site-information'))), 'error');
+ }
+ $site_mail = 'nobody@localhost';
+ }
+
+ // send the email
+ if (! drupal_mail("buddylist_mail_user_$op", $account->mail, $subject, $message, $site_mail)) {
+ $message = t('%type message was sent to %username', array('%type' => $op, '%username' => $account->name));
+ watchdog('buddylist', $message);
+ }
+ else {
+ $message = t('There was a problem sending the %type message to %username', array('%type' => $op, '%username' => $account->name));
+ watchdog('buddylist', $message, WATCHDOG_WARNING);
+ }
+}
+
+function buddylist_mail_add_default() {
+ return <<<MESSAGE
+Hi @addee_name,
+
+You are @adder_name's newest @buddy.
+
+Here's a link to @adder_name's profile. If you'd like, you can add them as one of your @buddies:
+
+ @adder_link
+
+Regards,
+The @site team
+MESSAGE;
+}
+
+function buddylist_mail_remove_default() {
+ return <<<MESSAGE
+Hi @addee_name,
+
+You have been removed from @adder_name's @buddy list.
+
+Here's a link to @adder_name's profile:
+
+ @adder_link
+
+Enjoy your new freedom!
+
+Regards,
+The @site team
+MESSAGE;
+}
+
+function buddylist_mail_replacements(){
+ return array(
+ '@adder_name' => '$user->name',
+ '@adder_link' => 'url("user/". $user->uid, NULL, NULL, TRUE)',
+ '@adder_uid' => '$user->uid',
+ '@addee_name' => '$account->name',
+ '@addee_link' => 'url("user/". $account->uid, NULL, NULL, TRUE)',
+ '@addee_uid' => '$account->uid',
+ '@site' => 'variable_get("site_name", "Drupal")',
+ );
+}
diff --git a/buddylist_views.inc b/buddylist_views.inc
new file mode 100644
index 0000000..65584b2
--- /dev/null
+++ b/buddylist_views.inc
@@ -0,0 +1,146 @@
+<?php
+
+// $Id$
+/**
+ * This include file implements views functionality on behalf of the
+ * buddylist.module
+ */
+
+function buddylist_views_tables() {
+ $tables['buddylist'] = array(
+ "name" => "buddylist",
+ "join" => array(
+ "left" => array(
+ "table" => "users",
+ "field" => "uid",
+ ),
+ "right" => array(
+ "field" => "buddy",
+ ),
+ ),
+ );
+
+ $tables['buddylist_users'] = array(
+ 'name' => 'users',
+ 'join' => array(
+ 'left' => array(
+ 'table' => 'buddylist',
+ 'field' => 'uid'
+ ),
+ 'right' => array(
+ 'field' => 'uid'
+ ),
+ ),
+ 'fields' => array(
+ 'name' => array(
+ 'name' => t('Buddylist: User Name'),
+ 'handler' => 'views_handler_field_username',
+ 'sortable' => true,
+ 'uid' => 'uid',
+ 'addlfields' => array('uid'),
+ 'help' => t('This will display the username of the owner of the buddylist.'),
+ ),
+ 'uid' => array(
+ 'name' => t('Buddylist: User Picture'),
+ 'handler' => 'views_handler_field_userpic',
+ 'sortable' => false,
+ 'help' => t('This will display the user picture of the owner of the buddylist.'),
+ ),
+ ),
+ 'sorts' => array(
+ 'name' => array(
+ 'name' => t('Buddylist: Buddy Name'),
+ 'help' => t('This allows you to sort alphabetically by buddy name.'),
+ )
+ ),
+ 'filters' => array(
+ 'uid' => array(
+ 'name' => t('Buddylist: Author Is A Buddy Of Username'),
+ 'operator' => 'views_handler_operator_or',
+ 'list' => 'views_handler_filter_username',
+ 'value-type' => 'array',
+ 'help' => t('This allows you to filter by buddies of a particular user.'),
+ ),
+ 'currentuid' => array(
+ 'field' => 'uid',
+ 'name' => t('Buddylist: Author Is A Buddy Of Current User'),
+ 'operator' => 'views_handler_operator_eqneq',
+ 'list' => 'views_handler_filter_usercurrent',
+ 'help' => t('This allows you to filter by buddies of the current user.'),
+ 'cacheable' => 'no',
+ ),
+ ),
+ );
+
+ return($tables);
+}
+
+function buddylist_views_default_views() {
+ $view = new stdClass();
+ $view->name = 'buddylist';
+ $view->description = 'A list of nodes created by buddies of current user';
+ $view->access = array ();
+ $view->view_args_php = '';
+ $view->page = TRUE;
+ $view->page_title = 'Buddylist Content';
+ $view->page_header = 'Nodes authored by buddies of current user<BR />';
+ $view->page_header_format = '4';
+ $view->page_footer = '';
+ $view->page_footer_format = '4';
+ $view->page_empty = '';
+ $view->page_empty_format = '4';
+ $view->page_type = 'table';
+ $view->url = 'buddies';
+ $view->use_pager = TRUE;
+ $view->nodes_per_page = '10';
+ $view->sort = array (
+ array (
+ 'tablename' => 'node',
+ 'field' => 'created',
+ 'sortorder' => 'DESC',
+ 'options' => '',
+ ),
+ );
+ $view->argument = array (
+ );
+ $view->field = array (
+ array (
+ 'tablename' => 'users',
+ 'field' => 'name',
+ 'label' => 'Buddy',
+ ),
+ array (
+ 'tablename' => 'node',
+ 'field' => 'title',
+ 'label' => 'Node Title',
+ 'handler' => 'views_handler_field_nodelink',
+ ),
+ array (
+ 'tablename' => 'node',
+ 'field' => 'type',
+ 'label' => 'Node Type',
+ ),
+ );
+ $view->filter = array (
+ array (
+ 'tablename' => 'node',
+ 'field' => 'status',
+ 'operator' => '=',
+ 'options' => '',
+ 'value' => '1',
+ ),
+ array (
+ 'tablename' => 'buddylist_users',
+ 'field' => 'currentuid',
+ 'operator' => '=',
+ 'options' => '',
+ 'value' => '***CURRENT_USER***',
+ ),
+ );
+ $view->exposed_filter = array (
+ );
+ $view->requires = array(node, users, buddylist_users);
+ $views[$view->name] = $view;
+
+ return($views);
+}