<?php

/**
 * @file
 * Adds a handler for Facebook videos to Video Embed Field.
 *
 * @see video_embed_field.api.php for more documentation
 */

/**
 * Implements hook_video_embed_handler_info().
 */
function video_embed_facebook_video_embed_handler_info() {
  $handlers = array();
  $handlers['facebook'] = array(
    'title' => 'Facebook Video',
    'function' => 'video_embed_facebook_handle_video',
    'thumbnail_function' => 'video_embed_facebook_handle_thumbnail',
    'thumbnail_default' => drupal_get_path('module', 'video_embed_facebook') . '/img/facebook.jpg',
    'form' => 'video_embed_facebook_form',
    'form_validate' => 'video_embed_field_handler_youtube_form_validate',
    'domains' => array(
      'facebook.com',
    ),
    'defaults' => array(
      'width' => 640,
      'height' => 360,
      'class' => '',
    ),
  );

  return $handlers;
}

/**
 * Defines the form elements for the Facebook videos configuration form.
 *
 * @param array $defaults
 *   The form default values.
 *
 * @return array
 *   The provider settings form array.
 */
function video_embed_facebook_form($defaults) {
  $form = array();

  $form['width'] = array(
    '#type' => 'textfield',
    '#title' => t('Player Width'),
    '#description' => t('The width of the player.'),
    '#default_value' => $defaults['width'],
  );

  $form['height'] = array(
    '#type' => 'textfield',
    '#title' => t('Player Height'),
    '#description' => t('The height of the player.'),
    '#default_value' => $defaults['height'],
  );

  $form['class'] = array(
    '#type' => 'textfield',
    '#title' => t('Player CSS class'),
    '#description' => t('CSS class to add to the player'),
    '#default_value' => $defaults['class'],
  );

  return $form;
}

/**
 * Validates the form elements for the Facebook video configuration form.
 *
 * @param array $element
 *   The form element to validate.
 * @param array $form_state
 *   The form to validate state.
 * @param array $form
 *   The form to validate structure.
 */
function video_embed_field_handler_facebook_form_validate($element, &$form_state, $form) {
  video_embed_field_validate_dimensions($element);
}

/**
 * Handler for Facebook videos.
 *
 * @param string $url
 *   The video URL.
 * @param array $settings
 *   The settings array.
 *
 * @return string|bool
 *   The video iframe, or FALSE in case the ID can't be retrieved from the URL.
 */
function video_embed_facebook_handle_video($url, $settings) {
  $id = _video_embed_facebook_get_video_id($url);

  if ($id) {
    // Our embed code.
    $embed='<iframe class="@class" src="//www.facebook.com/video/embed?video_id=!id" width="@width" height="@height"></iframe> ';
    // Use format_string to replace our placeholders with the settings values.
    $embed = format_string($embed, array(
      '!id' => $id,
      '@width' => $settings['width'],
      '@height' => $settings['height'],
      '@class' => $settings['class'],
    ));

    $video = array(
      '#markup' => $embed,
    );
    return $video;
  }

  return FALSE;
}

/**
 * Gets the thumbnail url for Facebook videos.
 *
 * @param string $url
 *   The video URL.
 *
 * @return array
 *   The video thumbnail information.
 */
function video_embed_facebook_handle_thumbnail($url) {
  $id = _video_embed_facebook_get_video_id($url);

  return array(
    'id' => $id,
    'url' => 'https://graph.facebook.com/' . $id . '/picture',
  );
}

/**
 * Helper function to get the Facebook video's id.
 *
 * @param string $url
 *   The video URL.
 *
 * @return string|bool
 *   The video ID, or FALSE in case the ID can't be retrieved from the URL.
 */
function _video_embed_facebook_get_video_id($url) {
  // Parse_url is an easy way to break a url into its components.
  $matches = array();
  preg_match('/(?:.*)(?:v=|video_id=|videos\/|videos\/v.\.\d+\/)(\d+).*/', $url, $matches);
  // If the v or video_id get parameters are set, return it.
  if ($matches && !empty($matches[1])) {
    return check_plain($matches[1]);
  }
  // Otherwise return false.
  return FALSE;
}
