You are here

function fb_connect_block in Drupal for Facebook 6.2

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

Implementation of hook_block.

File

./fb_connect.module, line 529
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;
  }
  elseif ($op == 'configure') {
    $orig_defaults = _fb_connect_block_login_defaults();
    $defaults = variable_get('fb_connect_block_' . $delta, $orig_defaults);
    $form['config'] = array(
      '#tree' => TRUE,
    );

    // Settings for each user status that we can detect.
    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']['anon_not_connected']['body']['#description'] = t('Suggestion: %default .', array(
      '%default' => $orig_defaults['anon_not_connected']['body'],
    ));
    $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']['user_not_connected']['body']['#description'] = t('Suggestion: %default .', array(
      '%default' => $orig_defaults['user_not_connected']['body'],
    ));
    $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.  Consider using <a target="_blank" href="!xfbml_url">XFBML</a> such as &lt;fb:name uid=!fbu&gt;&lt;/fb:name&gt; or &lt;fb:profile-pic uid=!fbu&gt;&lt;/fb:profile-pic&gt;', array(
      'xfbml_url' => 'http://wiki.developers.facebook.com/index.php/XFBML',
    ));
    $form['config']['connected']['body']['#description'] .= t('Note that <strong>!fbu</strong> will be replaced with the user\'s facebook id.<br/>Suggestion: %default .', array(
      '%default' => $orig_defaults['connected']['body'],
    ));
    $form['config']['format'] = filter_form($defaults['format']);
    $form['config']['format']['#description'] .= t('<p><strong>Be sure to select a format which allows XFBML tags!</strong> (That is, use <em>Full HTML</em> or <em>PHP code</em>, rather than  <em>Filtered HTML</em>.)</p><p>Format selected will apply to all body fields above.</p>');
    $form['config']['format']['#collapsed'] = FALSE;
    return $form;
  }
  elseif ($op == 'save') {
    $edit['config']['format'] = $edit['format'];
    variable_set('fb_connect_block_' . $delta, $edit['config']);
  }
  elseif ($op == 'view') {
    if (strpos($delta, 'login_') === 0) {

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

      // length of 'login_'
      $fb_app = fb_get_app(array(
        'label' => $label,
      ));
      if ($fb = fb_connect_app_init($fb_app)) {
        $fbu = $fb
          ->get_loggedin_user();
        fb_connect_require_feature('XFBML', $fb_app);
        fb_connect_init_option('ifUserConnected', "{FB_Connect.on_connected}", $fb_app);
        fb_connect_init_option('ifUserNotConnected', "{FB_Connect.on_not_connected}", $fb_app);
        $base = drupal_get_path('module', 'fb_connect');
        $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);
        }
        elseif ($GLOBALS['user']->uid > 1) {
          $subject = $defaults['user_not_connected']['title'];
          $content = $defaults['user_not_connected']['body'];
        }
        elseif ($GLOBALS['user']->uid == 1) {
          $subject = $defaults['user_not_connected']['title'];
          $content = '<em>' . t('Facebook Connect login disabled for user #1.') . '</em>';
        }
        else {
          $subject = $defaults['anon_not_connected']['title'];
          $content = $defaults['anon_not_connected']['body'];
        }

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