<?php

/**
 * @file
 * Youtube field module adds a field for YouTube videos.
 */
require_once (dirname(__FILE__) . '/youtube.inc');
 
/**
 * Implements hook_menu().
 */
function youtube_menu() {
  $items['admin/config/media/youtube'] = array(
    'title' => 'YouTube settings', 
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('youtube_settings_form'),
    'access arguments' => array('administer youtube'), 
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}

/**
 * Implements hook_permission().
 */
function youtube_permission() {
  return array(
    'administer youtube' => array(
      'title' => t('Administer YouTube field'), 
      'description' => t('Set default configurations for YouTube field settings.'),
    ),
  );
}

/**
 * Settings form for YouTube field module.
 */
function youtube_settings_form($form) {
  $form = array();
  $form['text'] = array(
    '#type' => 'markup',
    '#markup' => '<p>' . t('The following settings will be used as default values 
      on all YouTube video fields.  Many of these settings can be overridden
      on a per-field basis.') . '</p>',
  );
  $form['youtube_privacy'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable privacy-enhanced mode'),
    '#default_value' => variable_get('youtube_privacy', FALSE),
    '#description' => t('Checking this box will prevent YouTube from setting cookies in your site visitors browser.'),
  );
  $form['youtube_old'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use old embed code (Flash)'),
    '#default_value' => variable_get('youtube_old', FALSE),
    '#description' => t('Checking this box will load your video using Flash object embed code, potentially slowing down page load speed. Leaving this box unchecked loads the video in an iFrame instead (recommended).'),
  );
  $form['youtube_suggest'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show suggested videos when the video finishes'),
    '#default_value' => variable_get('youtube_suggest', TRUE),
  );
  $form['youtube_thumb_dir'] = array(
    '#type' => 'textfield',
    '#title' => t('YouTube thumbnail directory'),
    '#field_prefix' => variable_get('file_public_path', conf_path() . '/files') . '/',
    '#field_suffix' => '/thumbnail.png',
    '#description' => t('Location, within the files directory, where you would like the YouTube thumbnails stored.'),
    '#default_value' => variable_get('youtube_thumb_dir', 'youtube'),
  );

  return system_settings_form($form);
}

/**
 * Implements hook_field_info().
 */
function youtube_field_info() {
  return array(
    // We name our field as the associative name of the array.
    'youtube' => array(
      'label' => t('YouTube video'),
      'description' => t('A video hosted on YouTube.'),
      'default_widget' => 'youtube',
      'default_formatter' => 'youtube_video',
    ),
  );
}

/**
 * Implements hook_field_validate().
 */
function youtube_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  foreach ($items as $delta => $item) {
    if (!empty($item['input'])) {

      $valid = FALSE;
      // Video URL in the format http://www.youtube.com/watch?v=1SqBdS0XkV4.
      if (strstr($item['input'], 'youtube.com/watch?v=')) {
        $valid = TRUE;
      }
      // Video URL in the format http://youtu.be/1SqBdS0XkV4.
      elseif (strstr($item['input'], 'youtu.be/')) {
        $valid = TRUE;
      }

      if (!$valid) {
        $errors[$field['field_name']][$langcode][$delta][] = array(
          'error' => 'youtube_invalid',
          'message' => t('Please provide a valid YouTube URL.'),
        );
      }
    }
  }
}

/**
 * Implements hook_field_is_empty().
 */
function youtube_field_is_empty($item, $field) {
  return empty($item['input']);
}

/**
 * Implements hook_field_formatter_info().
 */
function youtube_field_formatter_info() {
  $formatters =  array(
    // This formatter displays your youtube video.
    'youtube_video' => array(
      'label' => t('YouTube video'),
      'field types' => array('youtube'),
      'settings' => array('youtube_size' => '420x315'), // TODO
    ),
    // This formatter just displays a thumbnail for your video.
    'youtube_thumbnail' => array(
      'label' => t('YouTube thumbnail'),
      'field types' => array('youtube'),
      'settings' => array('image_style' => 'thumbnail'),
    ),
  );

  return $formatters;
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function youtube_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  
  if ($display['type'] == 'youtube_video') {
    $element['youtube_size'] = array(
      '#type' => 'select',
      '#title' => t('YouTube video size'),
      '#options' => youtube_size_options(),
      '#default_value' => $settings['youtube_size'],
    );
  }

  if ($display['type'] == 'youtube_thumbnail') {
    $element['image_style'] = array(
      '#type' => 'select',
      '#title' => t('Image style'),
      '#options' => image_style_options(FALSE),
      '#default_value' => $settings['image_style'],
      '#empty_option' => t('None (original image)'),
    );
  }

  return $element;
}

/**
 * Implements hook_field_formatter_settings_summary().
 */
function youtube_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];

  // Summary for the video style.
  if ($display['type'] == 'youtube_video') {
    $video_sizes = youtube_size_options();
    if (isset($video_sizes[$settings['youtube_size']])) {
      $summary = t('YouTube video: @size', array('@size' => $video_sizes[$settings['youtube_size']]));
    }
    else {
      $summary = t('YouTube video: 450px by 315px');
    }
    return $summary;
  }

  // Summary for the thumbnail style.
  if ($display['type'] == 'youtube_thumbnail') {
    $image_styles = image_style_options(FALSE);
    // Unset possible 'No defined styles' option.
    unset($image_styles['']);
    if (isset($image_styles[$settings['image_style']])) {
      $summary = t('Image style: @style', array('@style' => $image_styles[$settings['image_style']]));
    }
    else {
      $summary = t('Original image');
    }
    return $summary;
  }
}

/**
 * Implements hook_field_formatter_view().
 */
function youtube_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();

  switch ($display['type']) {
    // This formatter outputs the youtube embed code.
    case 'youtube_video':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#theme' => 'youtube_video',
          '#video_id' => $item['video_id'],
          '#youtube_size' => $display['settings']['youtube_size'],
        );
      }
      break;
    
    // This formatter uses an imagecache preset to generate a thumbnail.
    case 'youtube_thumbnail':
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#theme' => 'youtube_thumbnail',
          '#video_id' => $item['video_id'],
          '#image_style' => $display['settings']['image_style'],
        );
      }
      break;
  }

  return $element;
}

/**
 * Implements hook_field_widget_info().
 */
function youtube_field_widget_info() {
  return array(
    'youtube' => array(
      'label' => t('YouTube'),
      'field types' => array('youtube'),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 */
function youtube_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $value = isset($items[$delta]['input']) ? $items[$delta]['input'] : '';

  $element += array(
    '#type' => 'textfield',
    '#default_value' => $value,
    '#size' => 50,
    '#maxlength' => 1024,
    '#description' => t('Enter the YouTube URL (or Embed code). Valid URL formats include: http://www.youtube.com/watch?v=1SqBdS0XkV4, http://youtu.be/1SqBdS0XkV4'),
    '#element_validate' => array('youtube_input_validate'),
    //'#value_callback' => 'youtube_widget_value',
  );

  $video_id = isset($items[$delta]['video_id']) ? $items[$delta]['video_id'] : '';
  $video_id_element = array(
    '#markup' => '<div class="youtube-video-id">' . t('This is your YouTube ID: ') . $video_id . '<div>',
  );
  return array(
    'input' => $element,
    'video_id' => $video_id_element,
  );
}

/**
 * Validation for the youtube field itself.
 */
function youtube_input_validate($element, &$form_state, $form) {
  $input = $element['#value'];
  $valid = FALSE;

  // The video URL in the format http://youtube.com/watch?v=1SqBdS0XkV4.
  if (strstr($input, 'youtube.com/watch?v=')) {
    $valid = TRUE;
    $anchor = '?v=';
    $position = strpos($input, $anchor);
    $video_id = trim(substr($input, $position + strlen($anchor)));
    // Remove additional query parameters.
    if ($amp = strpos($video_id, '&')) {
      $video_id = substr($video_id, 0, $amp);
    }
  }
  
  // The video URL in the format http://youtu.be/1SqBdS0XkV4.
  elseif (strstr($input, 'youtu.be/')) {
    $valid = TRUE;
    $anchor = 'be/';
    $position = strpos($input, $anchor);
    $video_id = trim(substr($input, $position + strlen($anchor)));
    // Remove additional query parameters.
    if ($amp = strpos($video_id, '&')) {
      $video_id = substr($video_id, 0, $amp);
    }
  }
  
  if ($valid) {
    $video_id_element = array(
      '#parents' => $element['#parents'],
    );
    array_pop($video_id_element['#parents']);
    $video_id_element['#parents'][] = 'video_id';
    form_set_value($video_id_element, $video_id, $form_state);
  }
}

/**
 * Implements hook_field_widget_error().
 */
function youtube_field_widget_error($element, $error, $form, &$form_state) {
  switch ($error['error']) {
    case 'youtube_invalid':
      form_error($element, $error['message']);
      break;
  }
}

/**
 * Implements of hook_theme().
 */
function youtube_theme($existing, $type, $theme, $path) {
  return array(
    'youtube_thumbnail' => array(
      'variables' => array('video_id' => NULL, 'image_style' => NULL),
      'file' => 'youtube.theme.inc',
    ),
    'youtube_video' => array(
      'variables' => array('video_id' => NULL, 'youtube_size' => NULL),
      'file' => 'youtube.theme.inc',
    ),
  );
}