You are here

fbconnect_invite.module in Facebook Connect 6.2

@todo.

File

fbconnect_invite/fbconnect_invite.module
View source
<?php

/**
 * @file
 * @todo.
 */

/**
 * Implements hook_menu().
 */
function fbconnect_invite_menu() {
  $items['fbconnect/invite/friends'] = array(
    'title' => 'Invite my Facebook friends',
    'page callback' => 'fbconnect_invite_friends_page',
    'access callback' => 'user_access',
    'access arguments' => array(
      'invite facebook friends',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['fbconnect/invite/accept/%user'] = array(
    'title' => 'Accept invite',
    'page callback' => 'fbconnect_invite_accept',
    'page arguments' => array(
      1,
    ),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['admin/settings/fbconnect/invite'] = array(
    'title' => 'Friends Invite',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'fbconnect_invite_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
}

/**
 * Implements hook_perm().
 */
function fbconnect_invite_perm() {
  return array(
    'invite facebook friends',
  );
}

/**
 * Implements hook_theme().
 */
function fbconnect_invite_theme() {
  return array(
    'fbconnect_invite_form' => array(
      'arguments' => array(
        'request_form_attributes' => array(),
        'friends_selector_attributes' => array(),
      ),
    ),
  );
}

/**
 * Implements hook_block().
 */
function fbconnect_invite_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0]['info'] = t('Facebook Friends');
      $blocks[0]['cache'] = BLOCK_NO_CACHE;
      return $blocks;
    case 'view':
      $fbuid = fbconnect_get_fbuid(TRUE);
      if (user_is_logged_in() && $fbuid) {
        $cid = 'fbconnect:' . $fbuid;
        $cached = cache_get($cid, 'cache_block');
        if ($cached->data) {
          $content = $cached->data;
        }
        else {
          $friends = fbconnect_get_connected_friends($fbuid);
          if ($friends) {
            foreach ($friends as $friend) {
              $row[] = theme('username', $friend);
            }
            $content = theme('item_list', $row);
            cache_set($cid, $content, 'cache_block', time() + 3600);
          }
        }
        $link_options = array();
        if (!drupal_is_front_page()) {
          $link_options = array(
            'query' => drupal_get_destination(),
          );
        }
        $invite_link = l(t('Invite Friends'), 'fbconnect/invite/friends', $link_options);
        $blocks['subject'] = t('Facebook Friends');
        $blocks['content'] = $invite_link . $content;
        return $blocks;
      }
      break;
  }
}

/**
 * Render the facebook friends invite form.
 */
function fbconnect_invite_friends_page() {
  global $base_url;
  global $user;
  $site = variable_get('site_name', $base_url);
  $type = check_plain(variable_get('fbconnect_invite_name', $site));
  $join_btn = check_plain(variable_get('fbconnect_invite_label', t('Become a Member!')));
  $invite_msg = check_plain(variable_get('fbconnect_invite_msg', t('Enjoy!')));
  $action_txt = check_plain(t('Please select the Facebook friends you want to invite'));
  $invite_dest = isset($_REQUEST['destination']) ? $_REQUEST['destination'] : '';
  $accept_url = url('fbconnect/invite/accept/' . $user->uid, array(
    'absolute' => TRUE,
    'query' => 'destionation=' . urlencode($invite_dest),
  ));
  $content = "{$invite_msg} <fb:req-choice url=\"{$accept_url}\" label=\"{$join_btn}\" />";
  $request_form_attributes = array(
    'method' => 'POST',
    'action' => url($invite_dest, array(
      'absolute' => TRUE,
    )),
    'type' => $type,
    'content' => $content,
    'invite' => TRUE,
  );
  $friends_selector_attributes = array(
    'cols' => "6",
    'rows' => "6",
    'max' => "35",
    'showborder' => "false",
    'actiontext' => $action_txt,
    'bypass' => "cancel",
  );
  return theme('fbconnect_invite_form', $request_form_attributes, $friends_selector_attributes);
}

/**
 * @todo.
 */
function theme_fbconnect_invite_form($request_form_attributes, $friends_selector_attributes) {
  $request_form_attr = drupal_attributes($request_form_attributes);
  $friend_selector_attr = drupal_attributes($friends_selector_attributes);
  return <<<FBML
    <fb:serverfbml>
      <script type="text/fbml">
        <fb:fbml>
          <fb:request-form {<span class="php-variable">$request_form_attr</span>} >
            <fb:multi-friend-selector {<span class="php-variable">$friend_selector_attr</span>} />
          </fb:request-form>
        </fb:fbml>
      </script>
    </fb:serverfbml>
FBML;
}

/**
 * Get facebook friend who has_added_app.
 */
function fbconnect_get_connected_friends($fbuid) {
  if (fbconnect_facebook_client()) {
    $query = "SELECT uid, has_added_app FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = {$fbuid})";
    try {
      try {
        $rows = fbconnect_facebook_client()
          ->api(array(
          'method' => 'fql.query',
          'query' => $query,
        ));
      } catch (FacebookApiException $e) {
        drupal_set_message($e
          ->getMessage(), 'error');
        throw $e;
      }
    } catch (Exception $e) {
      $msg = 'Exception thrown while using fbconnect_get_connected_friends: @code';
      $args = array(
        '@code' => $e
          ->getMessage(),
      );
      watchdog('fbconnect', $msg, $args, WATCHDOG_ERROR);
    }
    if ($rows) {
      foreach ($rows as $row) {
        if ($row['has_added_app']) {
          $fbid[] = $row['uid'];
        }
      }
      if ($fbid) {
        $res = db_query('SELECT uid FROM {fbconnect_users} WHERE fbuid IN (' . implode($fbid, ',') . ')');
        while ($uid = db_result($res)) {
          $user = user_load($uid);
          if ($user->fb_visible) {
            $friends[] = $user;
          }
        }
        return $friends;
      }
    }
  }
}

/**
 * @todo.
 */
function fbconnect_invite_settings() {
  $form['fbconnect_invite_msg'] = array(
    '#type' => 'textfield',
    '#title' => t('Invite message'),
    '#default_value' => variable_get('fbconnect_invite_msg', t('Enjoy facebook connect')),
  );
  $form['fbconnect_invite_label'] = array(
    '#type' => 'textfield',
    '#title' => t('Invite button label'),
    '#default_value' => variable_get('fbconnect_invite_label', t('Become a Member!')),
  );
  $form['fbconnect_invite_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Your site name'),
    '#default_value' => variable_get('fbconnect_invite_name', variable_get('site_name', NULL)),
  );
  return system_settings_form($form);
}

/**
 * @todo.
 */
function fbconnect_invite_accept($inviter) {
  module_invoke('rules', 'invoke_event', 'fbconnect_invite_accepted', $inviter);
  drupal_goto();
}

Functions

Namesort descending Description
fbconnect_get_connected_friends Get facebook friend who has_added_app.
fbconnect_invite_accept @todo.
fbconnect_invite_block Implements hook_block().
fbconnect_invite_friends_page Render the facebook friends invite form.
fbconnect_invite_menu Implements hook_menu().
fbconnect_invite_perm Implements hook_perm().
fbconnect_invite_settings @todo.
fbconnect_invite_theme Implements hook_theme().
theme_fbconnect_invite_form @todo.