Skip to content
buddylist_ui.module 17 KiB
Newer Older
nodestroy's avatar
nodestroy committed
<?php


/**
 * Implementation of hook_help
 * @return string, help text
 */
function buddylist_ui_help($section) {
nodestroy's avatar
nodestroy committed
  switch ($section) {
    case 'admin/help#buddylist_ui' :
      $output = t("buddylist_ui help text");
      return $output;
  }
nodestroy's avatar
nodestroy committed
}

/**
 * returns an array of common translation placeholders
 * @return array, translations
 */
function buddylist_ui_translation() {
nodestroy's avatar
nodestroy committed
  $translations = array (
    '@buddy' => t('buddy'), 
    '@Buddy' => t('Buddy'), 
    '@buddylist' => t('buddylist'), 
    '@Buddylist' => t('Buddylist'), 
    '@buddies' => t('buddies'), 
    '@Buddies' => t('Buddies'),);
  return variable_get('buddylist_ui_translation', $translations);
nodestroy's avatar
nodestroy committed
}

/**
 * Implementation of hook_perm
 * @return array, perms
 */
function buddylist_ui_perm() {
nodestroy's avatar
nodestroy committed
  return array ('view buddy lists', 'maintain buddy list');
nodestroy's avatar
nodestroy committed
}

/**
 * Implementation of hook_menu
 */
function buddylist_ui_menu($may_cache) {
nodestroy's avatar
nodestroy committed
  global $user;
  $items = array ();
  $id = is_numeric(arg(1)) ? arg(1) : $user->uid;
  $editAccess = (($id == $user->uid && user_access('maintain buddy list') && $user->uid) || user_access('administer users'));

nodestroy's avatar
nodestroy committed
  // Menu structure loosely based on user.module
  if ($may_cache) {
    $items[] = array (
      'path' => 'buddylist',
      'title' => t('My @buddylist', buddylist_ui_translation()), 
      'callback' => 'theme',
      'callback arguments' => array('buddylist_ui_page_buddylist', $id),
      'type' => MENU_NORMAL_ITEM,
      'weight' => 0,
      'access' => $editAccess,
      );

    // user page: buddylist
    $items[] = array (
      'path' => 'buddylist/confirmed',
      'title' => t('My @Buddies', buddylist_ui_translation()),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => 1,
      );
      
    // user page: pending requests
    $items[] = array(
      'path' => 'buddylist/requests',
      'title' => t('My @Buddy Requests', buddylist_ui_translation()),
      'access' => $editAccess,
      'callback' => 'theme',
      'type' => MENU_LOCAL_TASK,
      'weight' => 2,
      'callback arguments' => array('buddylist_ui_page_pending_requests', $id)
    );      

    // admin backend
    $items[] = array(
      'path' => 'admin/settings/buddylist_ui',
      'title' => t('Buddylist UI Settings'), 
      'description' => t('Buddylist UI Admin Settings'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'buddylist_ui_admin_settings',
      'access' => user_access('administer site configuration'),
    );
  }
  else {
nodestroy's avatar
nodestroy committed
    $items[] = array (
      'path' => 'buddy/add',
      'title' => t('Add to @buddylist', buddylist_ui_translation()), 
      'access' => $editAccess, 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array ('buddylist_ui_addbuddy', arg(2)), 'type' => MENU_CALLBACK,
      );

    $items[] = array (
      'path' => 'buddy/delete',
      'title' => t('Delete from @buddylist', buddylist_ui_translation()), 
      'access' => $editAccess, 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array ('buddylist_ui_deletebuddy', arg(2)), 'type' => MENU_CALLBACK,
      );
      
    $items[] = array (
      'path' => 'buddylist/' . $id . '/buddies/requested/accept',
      'title' => t('Accept Request'), 
      'access' => $editAccess, 
      'type' => MENU_CALLBACK, 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array ('buddylist_ui_pending_requested_accept', $id),
      );

    $items[] = array (
      'path' => 'buddylist/' . $id . '/buddies/requested/deny',
      'title' => t('Deny Request'), 
      'access' => $editAccess, 
      'type' => MENU_CALLBACK, 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array ('buddylist_ui_pending_requested_deny', $id),
      );

    $items[] = array (
      'path' => 'buddylist/' . $id . '/buddies/request/cancel',
      'title' => t('Cancel Request'), 
      'access' => $editAccess, 
      'type' => MENU_CALLBACK, 
      'callback' => 'drupal_get_form', 
      'callback arguments' => array ('buddylist_ui_cancel_request', $id),
      );
  }
  return $items;
nodestroy's avatar
nodestroy committed
/**
 * budbuddylist_ui_admin_settings administration settings page
 */
function buddylist_ui_admin_settings() {
  $form['general'] = array(
    '#type' => 'fieldset',
    '#title' => t('General settings'),
  );

  $form['general']['buddylist_ui_oneway'] = array(    
    '#type' => 'checkbox',
    '#default_value' => variable_get('buddylist_ui_oneway', 0),
    '#title' => t('One way connection mode'),
    '#description' => t("oneway means, that userA can add userB without approval. in the case of twoway a request to userB is sent, which can be denied or approved."),
  
  );

  return system_settings_form($form);
}

nodestroy's avatar
nodestroy committed
/**
 * @param $requestee_uid, user id 
 * @param $requester_uid, user id
 * @return
 */
function buddylist_ui_pending_requested_deny($requestee_uid, $requester_uid) {
nodestroy's avatar
nodestroy committed
  $requestee_account = user_load(array (
    'uid' => $requestee_uid
  ));
  $requester_account = user_load(array (
    'uid' => $requester_uid
  ));
nodestroy's avatar
nodestroy committed
  $output = confirm_form(buddylist_ui_confirm_form($requester_account, $requestee_account), t('Deny Request'), 'buddylist/requests', 
nodestroy's avatar
nodestroy committed
  t("Are you sure you want to deny the request from !name?", array (
    '!name' => theme('username',
    $requester_account
  ))), t('Yes'), t('No'), 'buddylist_ui_request_deny_confirm');

  return $output;
nodestroy's avatar
nodestroy committed
}

/**
 * @param $form_id
 * @param $form_values
 * @return
 */
function buddylist_ui_pending_requested_deny_submit($form_id, $form_values) {
nodestroy's avatar
nodestroy committed
  buddy_api_deny_request($form_values['requestee_account'], $form_values['requester_account']);
nodestroy's avatar
nodestroy committed
  return 'user/' . $form_values['requester_account']->uid;
nodestroy's avatar
nodestroy committed
}

/**
 * @param $requestee_uid, user id 
 * @param $requester_uid, user id
 * @return
 */
function buddylist_ui_pending_requested_accept($requestee_uid, $requester_uid) {
nodestroy's avatar
nodestroy committed
  $requestee_account = user_load(array (
    'uid' => $requestee_uid
  ));
  $requester_account = user_load(array (
    'uid' => $requester_uid
  ));
nodestroy's avatar
nodestroy committed
  $output = confirm_form(buddylist_ui_confirm_form($requester_account, $requestee_account), t('Accept Request'), 'buddylist/requests', 
nodestroy's avatar
nodestroy committed
  t("Are you sure you want to accept the request from !name?", array (
    '!name' => theme('username',
    $requester_account
  ))), t('Yes'), t('No'), 'buddylist_ui_request_accept_confirm');

  return $output;
nodestroy's avatar
nodestroy committed
}

/**
 * @param $form_id
 * @param $form_values
 * @return
 */
function buddylist_ui_pending_requested_accept_submit($form_id, $form_values) {
nodestroy's avatar
nodestroy committed
  buddy_api_accept_request($form_values['requestee_account'], $form_values['requester_account']);
nodestroy's avatar
nodestroy committed
  return 'user/' . $form_values['requester_account']->uid;
nodestroy's avatar
nodestroy committed
}

/**
 * @param $requestee_uid, user id 
 * @param $requester_uid, user id
 * @return
 */
function buddylist_ui_confirm_form($requester_account, $requestee_account) {
nodestroy's avatar
nodestroy committed
  $form = array ();
nodestroy's avatar
nodestroy committed
  $form['requester_account'] = array (
    '#type' => 'value',
    '#value' => $requester_account
  );
nodestroy's avatar
nodestroy committed
  $form['requestee_account'] = array (
    '#type' => 'value',
    '#value' => $requestee_account
  );
nodestroy's avatar
nodestroy committed
  return $form;
nodestroy's avatar
nodestroy committed
}
/**
 * @param $requestee_uid, user id 
 * @param $requester_uid, user id
 * @return
 */
function buddylist_ui_cancel_request($requester_uid, $requestee_uid) {
nodestroy's avatar
nodestroy committed
  $requester_account = user_load(array (
    'uid' => $requester_uid
  ));
  $requestee_account = user_load(array (
    'uid' => $requestee_uid
  ));
nodestroy's avatar
nodestroy committed
  $output = confirm_form(buddylist_ui_cancel_request_form($requestee_account, $requester_account), t('Cancel Request'), 'buddylist/requests', 
nodestroy's avatar
nodestroy committed
  t("Are you sure you want to cancel the request to !name?", array (
    '!name' => theme('username',
    $requestee_account
  ))), t('Yes'), t('No'), 'buddylist_ui_cancel_request_confirm');

  return $output;
nodestroy's avatar
nodestroy committed
}

/**
 * @param $form_id
 * @param $form_values
 * @return
 */
function buddylist_ui_cancel_request_submit($form_id, $form_values) {
nodestroy's avatar
nodestroy committed
  buddy_api_cancel_request($form_values['requester_account'], $form_values['requestee_account']);
  return 'user/' . $form_values['requestee_account']->uid;
nodestroy's avatar
nodestroy committed
}

/**
 * @param $requestee_uid, user id 
 * @param $requester_uid, user id
 * @return
 */
function buddylist_ui_cancel_request_form($requestee_account, $requester_account) {
nodestroy's avatar
nodestroy committed
  $form['requestee_account'] = array (
    '#type' => 'value',
    '#value' => $requestee_account,
  );
nodestroy's avatar
nodestroy committed
  $form['requester_account'] = array (
    '#type' => 'value',
    '#value' => $requester_account
  );
nodestroy's avatar
nodestroy committed
  return $form;
nodestroy's avatar
nodestroy committed
}

/**
 * Returns an array of posible actions (html) for the viewing user,
 * e.g. a link to make the viewed user a buddy
 * @param $viewing_user
 * @param $viewed_user
 * @return
 */
function buddylist_ui_get_buddy_actions(& $viewing_user, & $viewed_user) {

nodestroy's avatar
nodestroy committed
  $actions = array ();
  if (!user_access('maintain buddy list') || $viewing_user->uid == $viewed_user->uid) {
    return $actions;
  }

nodestroy's avatar
nodestroy committed
  if (in_array($viewed_user->uid, array_keys(buddy_api_get_requestees($viewing_user->uid)))) {
nodestroy's avatar
nodestroy committed
    $actions['buddy_requested'] = theme('buddylist_ui_action_sent_request', $viewed_user);
nodestroy's avatar
nodestroy committed
  } 
  else
nodestroy's avatar
nodestroy committed
    if (in_array($viewed_user->uid, array_keys(buddy_api_get_buddies($viewing_user->uid)))) {
nodestroy's avatar
nodestroy committed
      $actions['buddy_remove'] = theme('buddylist_ui_action_remove_buddy', $viewed_user);
nodestroy's avatar
nodestroy committed
    } 
    else
nodestroy's avatar
nodestroy committed
      if (in_array($viewing_user->uid, array_keys(buddy_api_get_requestees($viewed_user->uid)))) {
nodestroy's avatar
nodestroy committed
        $actions['buddy_confirm'] = theme('buddylist_ui_action_confirm_request', $viewed_user);
nodestroy's avatar
nodestroy committed
      } 
      else {
nodestroy's avatar
nodestroy committed
        $actions['buddy_add'] = theme('buddylist_ui_action_add_buddy', $viewed_user);
nodestroy's avatar
nodestroy committed
      }

  return $actions;
nodestroy's avatar
nodestroy committed
}

/**
 * Implementation of hook_user
 * @return
 */
function buddylist_ui_user($type, & $edit, & $thisuser, $category = NULL) {
nodestroy's avatar
nodestroy committed
  
  if ($type == 'view') {
nodestroy's avatar
nodestroy committed
    $output = array();
    $output[] = theme('buddylist_ui_user_buddies', $thisuser);
    $output[] = theme('buddylist_ui_user_actions', $thisuser);    
    if (count($output) > 0)
      return array (t('@Buddylist', buddylist_ui_translation()) => $output);
nodestroy's avatar
nodestroy committed
  } 
  else
    if ($type == 'delete') {
      db_query("DELETE FROM {buddylist} WHERE uid = %d OR buddy = %d", $thisuser->uid, $thisuser->uid);
      db_query("DELETE FROM {buddylist_pending_requests} WHERE requester_uid = %d OR requestee_uid = %d", $thisuser->uid, $thisuser->uid);
    } 
    else
      if ($type == 'load') {
nodestroy's avatar
nodestroy committed
        // Do not load buddies as objects to avoid recursion
        $thisuser->buddies = buddy_api_get_buddies($thisuser->uid);
nodestroy's avatar
nodestroy committed
      }
nodestroy's avatar
nodestroy committed

nodestroy's avatar
nodestroy committed
/**
 * @param $uid
 * @return
 */
function buddylist_ui_addbuddy($uid) {
nodestroy's avatar
nodestroy committed
  global $user;
  $buddy = user_load(array (
    'uid' => $uid
  ));

  if (empty ($buddy->name)) {
    drupal_set_message(t('This user does not exist'));
  }
nodestroy's avatar
nodestroy committed
  elseif (in_array($uid, array_keys(buddy_api_get_buddies($user->uid)))) {
nodestroy's avatar
nodestroy committed
    drupal_set_message(t('This user is already on your @buddylist', buddylist_ui_translation()));
  }
  elseif ($user->uid == $uid) {
    drupal_set_message(t('Cannot add yourself to @buddylist', buddylist_ui_translation()));
  } 
  else {
    $form['requester_account'] = array (
      '#type' => 'value',
      '#value' => $user
    );
    $form['requestee_account'] = array (
      '#type' => 'value',
      '#value' => $buddy
    );
    $form['message'] = array (
      '#type' => 'textfield',
         '#title' => t('Message'),
         '#description' => t('Message, which is sent by mail and shown at pending requests.'),
nodestroy's avatar
nodestroy committed
    );
    $output = confirm_form($form, t('Add user %name to your @buddylist?', array (
      '%name' => $buddy->name
    ) + buddylist_ui_translation()), $_GET['destination'], '', t('Add'), t('Cancel'), 'buddylist_ui_addbuddy_confirm');
    return $output;
  }
  drupal_goto();
nodestroy's avatar
nodestroy committed
}

/**
 * Confirm and add a buddy.
 * @param $form_id
 * @param $form_values
 * @return
 */
function buddylist_ui_addbuddy_submit($form_id, $form_values) {
nodestroy's avatar
nodestroy committed
  $buddyinfo = array();
  $buddyinfo['message'] = $form_values['message'];
nodestroy's avatar
nodestroy committed
  $buddyinfo['oneway'] = variable_get('buddylist_ui_oneway', 0);
nodestroy's avatar
nodestroy committed
  buddy_api_add($form_values['requester_account'], $form_values['requestee_account'], $buddyinfo);
  return 'user';
nodestroy's avatar
nodestroy committed
};

/**
 * Removes the user $uid from the global user's account.
 * TODO: generalize this so that two uids can be given
 * @param $uid
 * @return
 */
function buddylist_ui_deletebuddy($uid) {
nodestroy's avatar
nodestroy committed
  global $user;
  $buddy = user_load(array (
    'uid' => $uid
  ));

  if (empty ($buddy->name)) {
    drupal_set_message('This user does not exist');
  } 
  else
nodestroy's avatar
nodestroy committed
    if (!in_array($uid, array_keys(buddy_api_get_buddies($user->uid)))) {
nodestroy's avatar
nodestroy committed
      drupal_set_message('This user is not on your @buddylist', buddylist_ui_translation());
    } 
    else {
      $form['requester_account'] = array (
        '#type' => 'value',
        '#value' => $user
      );
      $form['requestee_account'] = array (
        '#type' => 'value',
        '#value' => $buddy
      );
      $output = confirm_form($form, t('Remove user %name from your @buddylist?', array (
        '%name' => $buddy->name
      ) + buddylist_ui_translation()), $_GET['destination'], '', t('Remove'), t('Cancel'), 'buddylist_ui_deletebuddy_confirm');
      return $output;
    }
  drupal_goto();
nodestroy's avatar
nodestroy committed
}

/**
 * @param $form_id
 * @param $form_values
 */
function buddylist_ui_deletebuddy_submit($form_id, $form_values) {
nodestroy's avatar
nodestroy committed
  buddy_api_remove($form_values['requester_account'], $form_values['requestee_account']);
nodestroy's avatar
nodestroy committed
};

/**
 * @param $buddyuser
 * @return
 */
nodestroy's avatar
nodestroy committed
function theme_buddylist_ui_action_add_buddy($buddyuser) {
nodestroy's avatar
nodestroy committed
  return l(t('Add %name to my @buddylist', array (
    '%name' => $buddyuser->name
  ) + buddylist_ui_translation()), 'buddy/add/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
nodestroy's avatar
nodestroy committed
}

/**
 * @param $buddyuser
 * @return
 */
nodestroy's avatar
nodestroy committed
function theme_buddylist_ui_action_remove_buddy($buddyuser) {
nodestroy's avatar
nodestroy committed
  return l(t('Remove %name from my @buddylist', array (
    '%name' => $buddyuser->name
  ) + buddylist_ui_translation()), 'buddy/delete/' . $buddyuser->uid, NULL, drupal_get_destination(), NULL, FALSE, TRUE);
nodestroy's avatar
nodestroy committed
/**
 * @param $buddyuser
 * @return
 */
function theme_buddylist_ui_action_sent_request($buddyuser) {
  return t('You have requested to add %name to your @buddylist. (See !your_pending_requests)', array (
    '%name' => $buddyuser->name,
    '!your_pending_requests' => l(t('your pending requests'
  ), 'buddylist/requests')) + buddylist_ui_translation());
}

/**
 * @param $buddyuser
 * @return
 */
function theme_buddylist_ui_action_confirm_request($buddyuser) {
  return t('%name has requested to add you to your @buddylist. (See !your_pending_requests)', array (
    '%name' => $buddyuser->name,
    '!your_pending_requests' => l(t('your pending requests'
  ), 'buddylist/requests')) + buddylist_ui_translation());
}

/**
 * Main page for displaying confirmed buddylist
 * 
 * @return string, html formatted output
 */
function theme_buddylist_ui_page_buddylist() {
  drupal_set_title(t('@Buddylist', buddylist_ui_translation()));
  $view = views_get_view('buddy_api_buddylist'); 
  $title = views_get_title($view);
  $view_buddylist = views_build_view('embed', $view, array(), FALSE);
  if (!strlen($view_buddylist)){
    $output .= '<p>'. t('NONE') .'</p>';
  }
  $output .= $view_buddylist;
  
  return theme_box($title, $output, $region = 'main');
}

/**
 * Main page for displaying pending buddy requests (sent & received)
 * 
 * @return string, html formatted output
 */
function theme_buddylist_ui_page_pending_requests($id) {
  drupal_set_title(t('Pending Requests'));
nodestroy's avatar
nodestroy committed
  
  $view = views_get_view('buddy_api_received_requests'); 
  $title = views_get_title($view);
  $view_received_requests = views_build_view('embed', $view, array(), FALSE);
  if (!strlen($view_received_requests)){
  	$view_received_requests .= '<p>'. t('NONE') .'</p>';
  }
  $themed_received_requests = theme_box($title, $view_received_requests, $region = 'main');
  
  $view = views_get_view('buddy_api_sent_requests'); 
  $title = views_get_title($view);
  $view_sent_requests = views_build_view('embed', $view, array(), FALSE); 
  if (!strlen($view_sent_requests)){
  	$view_sent_requests .= '<p>'. t('NONE') .'</p>';
  }
  $themed_sent_requests = theme_box($title, $view_sent_requests, $region = 'main');
  
  return $themed_received_requests . $themed_sent_requests;     
}
nodestroy's avatar
nodestroy committed
/**
nodestroy's avatar
nodestroy committed
 * Themes default display of buddies on user profile
 * 
 * @return string, html formatted output
nodestroy's avatar
nodestroy committed
 */
nodestroy's avatar
nodestroy committed
function theme_buddylist_ui_user_buddies($buddyuser) {

  // Permission check first
  global $user;
  if ( !user_access('view buddy lists') && $buddyuser->uid != $user->uid) return;

  // Display entire buddylist page
  $view = views_get_view('buddy_api_buddylist');
  $view_buddylist = views_build_view('embed', $view, array(), FALSE);
  if (!strlen($view_buddylist)){
    $output .= '<p>'. t('NONE') .'</p>';
  }
  $output .= $view_buddylist;

  return array ('title' => t('@Buddies', buddylist_ui_translation()), 'value' => theme_box('', $output, $region = 'main'));
nodestroy's avatar
nodestroy committed

nodestroy's avatar
nodestroy committed
/**
 * Themes default display of buddy actions on user profile
 * 
 * @return string, html formatted output
 */
function theme_buddylist_ui_user_actions($buddyuser) {

  // Retrieve all actions
  global $user;
  $actions = buddylist_ui_get_buddy_actions($user, $buddyuser);
nodestroy's avatar
nodestroy committed

nodestroy's avatar
nodestroy committed
  // Return actions as a list, if there are any
  if ($actions)
    return array ('title' => t('@Buddy actions', buddylist_ui_translation()), 'value' => theme('item_list', $actions), 'class' => 'buddylist_actions');
}