You are here

function fb_feed_template_page in Drupal for Facebook 5.2

1 string reference to 'fb_feed_template_page'
fb_feed_menu in ./fb_feed.module

File

./fb_feed.module, line 115
Helpers for Facebook feeds (http://wiki.developers.facebook.com/index.php/New_Design_Feed_Wall)

Code

function fb_feed_template_page($fb_app_nid) {
  $registered = array();
  $unregistered = array();

  // Query all known templates
  $result = db_query("SELECT n.nid, n.title, n.status, fft.bundle_id FROM {node} n LEFT JOIN {fb_feed_template} fft ON fft.nid = n.nid WHERE fft.nid IS NOT NULL");
  while ($data = db_fetch_object($result)) {
    if ($data->bundle_id) {
      $registered[$data->bundle_id] = $data;
    }
    else {
      $unregistered[] = $data;
    }
  }

  // Query facebook for a list of registered templates we know nothing about.
  $fb_app = fb_get_app(array(
    'nid' => $fb_app_nid,
  ));
  $fb = fb_api_init($fb_app, FB_FBU_ANY);
  if ($fb) {
    try {
      $all = $fb->api_client
        ->feed_getRegisteredTemplateBundles();
      if (is_array($all)) {
        foreach ($all as $data) {
          if (isset($registered[$data['template_bundle_id']])) {
            $registered[$data['template_bundle_id']]->confirmed = $data;
          }
          else {
            $registered[$data['template_bundle_id']] = $data;
          }
        }
      }
    } catch (Exception $e) {
      fb_log_exception($e, t('Failed to get template bundles.'));
      drupal_set_message(t('This page may show more information if you configure an infinite session or Facebook Connect.'));
    }
  }
  else {
    drupal_set_message("Facebook API not available.  Some options will not be shown.  Possible infinite session configuration problem.", 'error');
  }

  // Display one table of registered templates and another of unregistered.
  if (count($unregistered)) {
    $rows = array();
    foreach ($unregistered as $data) {
      $ops = array(
        l(t('register'), 'fb_feed/register/' . $data->nid),
      );
      $row = array(
        l($data->title, 'node/' . $data->nid),
        implode(' | ', $ops),
      );
      $rows[] = $row;
    }
    $header = array(
      t('Unregistered Template'),
      t('Operations'),
    );
    $output .= theme('table', $header, $rows);
  }
  if (count($registered)) {
    $rows = array();
    foreach ($registered as $data) {
      $ops = array();
      if ($data->confirmed) {
        $ops[] = l(t('deactivate'), 'fb_feed/deactivate/' . $fb_app->nid . '/' . $data->bundle_id);
      }
      else {
        if ($data->nid && $fb && is_array($all)) {
          $ops = array(
            l(t('re-register'), 'fb_feed/register/' . $data->nid),
          );
        }
        else {
          if ($data->template_bundle_id) {
            $ops[] = l(t('deactivate'), 'fb_feed/deactivate/' . $fb_app->nid . '/' . $data['template_bundle_id']);
          }
        }
      }
      if ($data->nid) {
        $title = l($data->title, 'node/' . $data->nid);
      }
      else {

        // Use a template learned from facebook.
        $title = $data['one_line_story_templates'][0];
      }
      if ($data->bundle_id) {
        $bundle_id = $data->bundle_id;
      }
      else {
        $bundle_id = $data['template_bundle_id'];
      }
      $row = array(
        $title,
        $bundle_id,
        implode(' | ', $ops),
      );
      $rows[] = $row;
    }
    $header = array(
      t('Registered Template'),
      t('Bundle ID'),
      t('Operations'),
    );
    $output .= theme('table', $header, $rows);
  }
  return $output;
}