You are here

function fb_connect_block in Drupal for Facebook 6.3

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

Implements hook_block().

File

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

Code

function fb_connect_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $items = array();
    foreach (fb_get_all_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=loggedinuser&gt;&lt;/fb:name&gt; or &lt;fb:profile-pic uid=loggedinuser&gt;&lt;/fb:profile-pic&gt;', array(
      'xfbml_url' => 'http://wiki.developers.facebook.com/index.php/XFBML',
    ));
    $form['config']['connected']['body']['#description'] .= '<br/>' . t('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' && !fb_is_tab()) {

    // Hide block on tabs, where the $fbu is actually the page, not the user.
    if (strpos($delta, 'login_') === 0) {

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

      // length of 'login_'
      $fb_app = fb_get_app(array(
        'label' => $label,
      ));
      if ($fb_app && ($fb = fb_connect_app_init($fb_app))) {
        $fbu = $fb
          ->getUser();

        //_fb_connect_add_js($fb_app); moved to fb_connect_app_init()
        $base = drupal_get_path('module', 'fb_connect');
        $defaults = variable_get('fb_connect_block_' . $delta, _fb_connect_block_login_defaults());
        if (!$fbu && $GLOBALS['user']->uid >= 1) {

          // Render only logged in user markup.
          $subject = $defaults['user_not_connected']['title'];
          $content = $defaults['user_not_connected']['body'];
          $subject_connected = NULL;
          $content_connected = NULL;
        }
        else {

          // Render both connected and not connected markup.
          $subject = $defaults['anon_not_connected']['title'];
          $content = $defaults['anon_not_connected']['body'];
          $subject_connected = $defaults['connected']['title'];
          $content_connected = $defaults['connected']['body'];
          $content_connected = str_replace('!fbu', 'loggedinuser', $content_connected);

          // support deprecated !fbu
        }

        // substitute perms
        $perms = array();
        drupal_alter('fb_required_perms', $perms);
        $content = str_replace('!perms', implode(',', $perms), $content);

        // If user has changed defaults, run filter
        if (isset($defaults['format'])) {
          $subject = check_plain($subject);
          $content = check_markup($content, $defaults['format'], FALSE);
          if ($content_connected) {
            $subject_connected = check_plain($subject_connected);
            $content_connected = check_markup($content_connected, $defaults['format'], FALSE);
          }
        }
        $block = array(
          // Theme fb_markup uses javascript to show/hide the right markup.
          'subject' => theme('fb_markup', $subject, $subject_connected),
          'content' => theme('fb_markup', $content, $content_connected),
        );
        return $block;
      }
    }
  }
}