<?php
/**
 * Implements hook_block_info().
 */
function facebook_boxes_block_info() {
  $blocks['fb_like'] = array(
    'info'   => t('Facebook Like Box'),
    'cache'  => DRUPAL_CACHE_GLOBAL,
  );

  $blocks['fb_rec'] = array(
    'info'   => t('Facebook Recommended Box'),
    'cache'  => DRUPAL_CACHE_GLOBAL,
  );

  return $blocks;
}

/**
 * Implements hook_block_configure().
 */
function facebook_boxes_block_configure($delta = '') {

  global $base_root;

  $form = array();
  if ($delta == 'fb_like') {
    $form['fb_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Facebook Page URL'),
      '#description' => t('The full URL of your Facebook page, e.g. http://www.facebook.com/platform'),
      '#default_value' => variable_get('fb_like_url', 'http://www.facebook.com/platform'),
    );

    $form['fb_width'] = array(
      '#type' => 'textfield',
      '#size' => 6,
      '#maxlength' => 4,
      '#title' => t('Width'),
      '#description' => t('Width, in pixels, of the Facebook box iframe.'),
      '#default_value' => variable_get('fb_like_width', 292),
    );

    $form['fb_height'] = array(
      '#type' => 'textfield',
      '#size' => 6,
      '#maxlength' => 5,
      '#title' => t('Height'),
      '#description' => t('Height, in pixels, of the Facebook box iframe.'),
      '#default_value' => variable_get('fb_like_height', 300),
    );

    $form['fb_border'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#maxlength' => 7,
      '#title' => t("Border color"),
      '#description' => t('Color of 1px border around iframe, including leading "#" such as #ff0000.'),
      '#default_value' => variable_get('fb_like_border', ''),
    );

    $form['fb_toggles'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Configuration'),
      '#options' => array(
        'fb_faces' => 'Show Faces',
        'fb_stream' => 'Show Stream',
        'fb_header' => 'Show FB Header',
      ),
      '#default_value' => variable_get('fb_like_toggles', array('fb_faces', 'fb_header')),
    );
  }
  elseif ($delta == 'fb_rec') {
  /**
   * recommendations box documentation is at https://developers.facebook.com/docs/reference/plugins/recommendations/
   * It includes options that aren't included here, such as app id, actions, and font
   */
    global $base_path;
    $form['fb_domain'] = array(
      '#type' => 'textfield',
      '#title' => t('Site Domain'),
      '#description' => t('The domain to track activity on, e.g. ' . parse_url($base_root, PHP_URL_HOST)),
      '#default_value' => variable_get('fb_rec_domain', parse_url($base_root, PHP_URL_HOST)),
    );

    $form['fb_width'] = array(
      '#type' => 'textfield',
      '#size' => 6,
      '#maxlength' => 4,
      '#title' => t('Width'),
      '#description' => t('Width, in pixels, of the Facebook box iframe.'),
      '#default_value' => variable_get('fb_rec_width', 292),
    );

    $form['fb_height'] = array(
      '#type' => 'textfield',
      '#size' => 6,
      '#maxlength' => 5,
      '#title' => t('Height'),
      '#description' => t('Height, in pixels, of the Facebook box iframe.'),
      '#default_value' => variable_get('fb_rec_height', 300),
    );

    $form['fb_border'] = array(
      '#type' => 'textfield',
      '#size' => 10,
      '#maxlength' => 7,
      '#title' => t("Border color"),
      '#description' => t('Color of 1px border around iframe, including leading "#" such as #ff0000.'),
      '#default_value' => variable_get('fb_rec_border', ''),
    );

    $form['fb_toggles'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Configuration'),
      '#options' => array(
        'fb_blank' => 'Open links in new window/tab',
        'fb_header' => 'Show Facebook Header',
      ),
      '#default_value' => variable_get('fb_like_toggles', array('fb_blank', 'fb_header')),
    );
  }

  return $form;
}

/**
 * Implements hook_block_save().
 */
function facebook_boxes_block_save($delta = '', $edit = array()) {

  if ($delta == 'fb_like') {

    variable_set('fb_like_url', $edit['fb_url']);
    variable_set('fb_like_width', $edit['fb_width']);
    variable_set('fb_like_height', $edit['fb_height']);
    variable_set('fb_like_border', $edit['fb_border']);
    $toggles = array();
    foreach ($edit['fb_toggles'] as $key => $val) {
      if ($val) $toggles[] = $key;
    }
    variable_set('fb_like_toggles', $toggles);
  }
  elseif ($delta == 'fb_rec') {
    variable_set('fb_rec_domain', $edit['fb_domain']);
    variable_set('fb_rec_width', $edit['fb_width']);
    variable_set('fb_rec_height', $edit['fb_height']);
    variable_set('fb_rec_border', $edit['fb_border']);
    $toggles = array();
    foreach ($edit['fb_toggles'] as $key => $val) {
      if ($val) $toggles[] = $key;
    }
    variable_set('fb_rec_toggles', $toggles);
  }
}

/**
 * Implements hook_block_view().
 */
function facebook_boxes_block_view($delta = '') {

  $block = array();
  global $base_root;

  if ($delta == 'fb_like') {
    $url = urlencode( variable_get('fb_like_url', 'http://www.facebook.com/platform'));
    $width = variable_get('fb_like_width', 292);
    $height = variable_get('fb_like_height', 300);
    $border = urlencode(variable_get('fb_like_border', ''));
    $opts = variable_get('fb_like_toggles', array('fb_faces', 'fb_header'));

    $block['subject'] = "On Facebook";
    $block['content'] = array(
        '#markup' => sprintf('<iframe name="facebook" title="FacebookIframe" src="//www.facebook.com/plugins/likebox.php?href=%s&amp;width=%u&amp;height=%u&amp;colorscheme=light&amp;show_faces=%s&amp;border_color=%s&amp;stream=%s&amp;header=%s" style="border:none; overflow:hidden; width:%spx; height:%spx;"></iframe>',
          $url,
          $width,
          $height,
          (in_array('fb_faces', $opts)) ? 'true' : 'false',
          $border,
          (in_array('fb_stream', $opts)) ? 'true' : 'false',
          (in_array('fb_header', $opts)) ? 'true' : 'false',
          $width,
          $height
        ),
      );
    }
    elseif ($delta == 'fb_rec') {
    $opts = variable_get('fb_rec_toggles', array('fb_blank', 'fb_header'));

    $block['subject'] = "Facebook Recommendations";
    $block['content'] = array(
        '#markup' => sprintf('<iframe name="facebook" title="FacebookIframe" src="//www.facebook.com/plugins/recommendations.php?site=%s&amp;action&amp;width=%u&amp;height=%u&amp;colorscheme=light&amp;&amp;border_color=%s&amp;header=%s&amp;font&amp;linktarget=%s" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:%upx; height:%upx;" allowTransparency="true"></iframe>',
          urlencode( variable_get('fb_rec_domain', parse_url($base_root, PHP_URL_HOST))),
          variable_get('fb_rec_width', 292),
          variable_get('fb_rec_height', 300),
          urlencode(variable_get('fb_rec_border', '')),
          (in_array('fb_header', $opts)) ? 'true' : 'false',
          (in_array('fb_target', $opts)) ? '_blank' : '_top',
          variable_get('fb_rec_width', 292),
          variable_get('fb_rec_height', 300)
        ),
      );
  }
  return $block;
}

/**
 * Implements hook_help().
 */
function facebook_boxes_help($path, $arg) {
  if ($path == 'admin/help#facebook_boxes') {
    return '<p>' . t('This module creates two new blocks, a !recommendations and a !like, which you can see and configure on your !blockadmin.',
      array('!recommendations' => l('Facebook Recommended Box', 'admin/structure/block/manage/facebook_boxes/fb_rec/configure'), '!like' => l(t('Facebook Like Box'), 'admin/structure/block/manage/facebook_boxes/fb_like/configure'), '!blockadmin' => l( t('block administration page'), 'admin/structure/block/list'))) . '</p><p>' .
    t("For details on how these boxes work, visit !fbdocs", array('!fbdocs' => l(t('Facebook\'s documentation'), 'https://developers.facebook.com/docs/plugins/'))) . '</p>' .
    '<p>' . t('There are no global configuration options.') . '</p>';
  }
}