You are here

function fb_connect_block in Drupal for Facebook 5.2

Same name and namespace in other branches
  1. 6.3 fb_connect.module \fb_connect_block()
  2. 6.2 fb_connect.module \fb_connect_block()

Implementation of hook_block.

File

./fb_connect.module, line 254
Support for Facebook Connect features

Code

function fb_connect_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $items = array();
    foreach (fb_connect_enabled_apps() as $fb_app) {
      $d = 'login_' . $fb_app->label;
      $items[$d] = array(
        'info' => t('Facebook Connect Login to !app', array(
          '!app' => $fb_app->title,
        )),
      );
    }
    return $items;
  }
  else {
    if ($op == 'configure') {
      $defaults = variable_get('fb_connect_block_' . $delta, _fb_connect_block_login_defaults());
      $form['config'] = array(
        '#tree' => TRUE,
      );
      foreach (array(
        'anon_not_connected',
        'user_not_connected',
        'connected',
      ) as $key) {
        $form['config'][$key] = array(
          '#type' => 'fieldset',
          // title and description below
          '#collapsible' => TRUE,
          '#collapsed' => FALSE,
        );
        $form['config'][$key]['title'] = array(
          '#type' => 'textfield',
          '#title' => t('Default title'),
          //'#description' => t('Default title.'),
          '#default_value' => $defaults[$key]['title'],
        );
        $form['config'][$key]['body'] = array(
          '#type' => 'textarea',
          '#title' => t('Body'),
          //'#description' => t('Block body'),
          '#default_value' => $defaults[$key]['body'],
        );
      }
      $form['config']['anon_not_connected']['#title'] = t('Anonymous user, not connected');
      $form['config']['anon_not_connected']['#description'] = t('Settings when local user is Anonymous, and not connected to Facebook.  Typically a new account will be created when the user clicks the connect button.');
      $form['config']['user_not_connected']['#title'] = t('Registered user, not connected');
      $form['config']['user_not_connected']['#description'] = t('Settings when local user is registered, and not connected to Facebook.  Typically the facebook id will be linked to the local id after the user clicks the connect button.');
      $form['config']['connected']['#title'] = t('Connected user');
      $form['config']['connected']['#description'] = t('Settings when local user is connected to Facebook.  You may render facebook\'s logout button, and/or information about the user.');
      $form['config']['connected']['body']['#description'] .= t('Note that <em>!fbu</em> will be replaced with the user\'s facebook id.');
      $form['config']['filter'] = filter_form($defaults['filter']);
      $form['config']['filter']['#description'] .= t('Format selected will apply to all body fields above.  Be sure to select a format which allows FBML tags!');
      $form['config']['filter']['#collapsed'] = FALSE;
      return $form;
    }
    else {
      if ($op == 'save') {
        variable_set('fb_connect_block_' . $delta, $edit['config']);
      }
      else {
        if ($op == 'view') {
          if (strpos($delta, 'login_') === 0) {

            // Login block
            $label = substr($delta, 6);

            // length of 'login_'
            $fb_app = fb_get_app(array(
              'label' => $label,
            ));
            $fb = fb_connect_app_init($fb_app);
            $fbu = $fb
              ->get_loggedin_user();
            fb_connect_require_feature('XFBML', $fb_app);

            //fb_connect_init_option('reloadIfSessionStateChanged', TRUE, $fb_app);

            //fb_connect_init_option('doNotUseCachedConnectState', TRUE, $fb_app);
            $base = drupal_get_path('module', 'fb_connect');
            drupal_add_js(array(
              'fb_connect' => array(
                'fbu' => $fbu ? $fbu : 0,
                // XXX
                'logout_url' => url('logout'),
                // XXX
                'front_url' => url('<front>'),
                'enable_login' => TRUE,
              ),
            ), 'setting');
            drupal_add_js($base . '/fb_connect.js');
            $defaults = variable_get('fb_connect_block_' . $delta, _fb_connect_block_login_defaults());
            if ($fbu) {
              $subject = $defaults['connected']['title'];
              $content = $defaults['connected']['body'];

              // substitute %fbu
              $content = str_replace('!fbu', $fbu, $content);
            }
            else {
              if ($GLOBALS['user']->uid) {
                $subject = $defaults['user_not_connected']['title'];
                $content = $defaults['user_not_connected']['body'];
              }
              else {
                $subject = $defaults['anon_not_connected']['title'];
                $content = $defaults['anon_not_connected']['body'];
              }
            }

            // If user has changed defaults, run filter
            if ($defaults['filter']) {
              $subject = check_plain($subject);
              $content = check_markup($content, $default['filter'], FALSE);
            }
            $block = array(
              'subject' => $subject,
              'content' => $content,
            );
            return $block;
          }
        }
      }
    }
  }
}