<?php
// $Id: eminline.module,v 1.1.4.6 2010/07/19 17:00:20 aaron Exp $

/**
 * Implemenation of hook_filter().
 */
function eminline_filter($op, $delta = 0, $format = -1, $text = '', $langcode = '', $cache_id = 0) {
  switch ($op) {
    case 'list':
      return array(0 => t('Embedded Inline Media'));

    case 'description':
      switch ($delta) {
        case 0:
          return t('Embed videos from specified URL.');
        default:
          return;
      }

    case "process":
      switch ($delta) {
        case 0:
          return _eminline_url($text, $format);
        default:
          return;
      }
    case 'settings':
      switch ($delta) {
        case 0:
          return _eminline_url_settings($format);
        default:
          return;
      }
    default:
      return $text;
  }
}

/**
 * Implementation of hook_filter_tips().
 */
function eminline_filter_tips($delta, $format, $long = FALSE) {
  $output = '';
  switch ($delta) {
    case '0':
      $providers = variable_get('eminline_providers_' . $format, array_keys(emfield_system_list('emvideo', NULL, FALSE)));
      $output .= t('You may embed videos from the following providers %providers. Just add the video URL to your textarea in the place where you would like the video to appear, i.e. http://www.youtube.com/watch?v=pw0jmvdh.', array('%providers' => implode(', ', array_filter($providers))));
  }
  return $output;
}

/**
 * Match all http(s) URLs and pass to link checker.
 *
 * @param string $text
 *  The contents of the text area.
 * @param int $format
 *  The format id.
 *
 * @return string
 */
function _eminline_url($text, $format) {
  $text = ' '. $text .' ';

  // Need to attach the variables to the callback after the regex.
  $callback = _eminline_curry('_eminline_url_parse_full_links', 2);

  // Match absolute URLs.
  $text = preg_replace_callback("`(<p>|<li>|<br\s*/?>|[ \n\r\t\(])((http://|https://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?!]*?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))`i", $callback($format), $text);

  return $text;
}

/**
 * If one of our allowed providers knows what to do with the url,
 * then let it embed the video.
 *
 * @param int $format
 *  The format id.
 * @param array $match
 *  The matched text from our regex.
 *
 * @return string
 *  The replacement text for the url.
 */
function _eminline_url_parse_full_links($format, $match) {
  // Get just the URL.
  $match[2] = check_url(decode_entities($match[2]));

  // Get our filter settings.
  $video_default = variable_get('eminline_video_' . $format, _eminline_default_filter_settings('video'));
  $preview_default = variable_get('eminline_preview_' . $format, _eminline_default_filter_settings('preview'));
  $tn_default = variable_get('eminline_tn_' . $format, _eminline_default_filter_settings('tn'));

  // Build a fake field, remember that emfield is well a field.
  $field = array(
    'field_name' => 'eminline',
    'type' => 'emvideo',
    'video_width' => $video_default['video_width'],
    'video_height' => $video_default['video_height'],
    'video_autoplay' => $video_default['video_autoplay'],
    'preview_width' => $preview_default['preview_width'],
    'preview_height' => $preview_default['preview_height'],
    'preview_autoplay' => $preview_default['preview_autoplay'],
    'thumbnail_width' => $tn_default['thumbnail_width'],
    'thumbnail_height' => $tn_default['thumbnail_height'],
    'thumbnail_default_path' => $tn_default['thumbnail_default_path'],
    'providers' =>  variable_get('eminline_providers_' . $format, array_keys(emfield_system_list('emvideo', NULL, FALSE))),
  );

  // Build our embed item.
  $item = emfield_parse_embed($field, $match[2], 'emvideo');

  // Check to make sure the provider that was found is an allowed provider.
  if ($field['providers'][$item['provider']]) {
    $item['data'] = (array)emfield_include_invoke('emvideo', $item['provider'], 'data', $field, $item);
  }
  else {
    return $match[0];
  }

  // Get how the video should be displayed.
  $display_format = variable_get('eminline_full_node_' . $format, 'video_video');

  // Change the options based on how the video should be displayed.
  if ($display_format == 'video_thumbnail') {
    $options = array(
      'width' => $field['thumbnail_width'],
      'height' => $field['thumbnail_height'],
      'default_path' => $field['thumbnail_default_path'],
    );
  }
  else if ($display_format == 'video_preview') {
    $options = array(
      'width' => $field['preview_width'],
      'height' => $field['preview_height'],
      'autoplay' => $field['preview_autoplay'],
    );
  }
  else {
    $options = array(
      'width' => $field['video_width'],
      'height' => $field['video_height'],
      'autoplay' => $field['video_autoplay'],
    );
  }

  // Theme the video w00t!
  return theme('eminline_url_wrapper', emfield_emfield_field_formatter($field, $item, $display_format, NULL, 'emvideo', $options));
}

/**
 * Settings form for filter.
 *
 * @param int format
 *  The format id.
 *
 * @return array
 */
function _eminline_url_settings($format) {
  $form['format'] = array(
    '#type' => 'value',
    '#value' => $format,
  );

  // Get the settings that are avaliable to the cck widget.
  $form['eminline_'. $format] = emvideo_widget_settings('form', array('type' => 'emvideo_textfields'));

  $form['eminline_'. $format]['provider_list']['providers']['#default_value'] = variable_get('eminline_providers_' . $format, array_keys($form['eminline_'. $format]['provider_list']['providers']['#options']));
  $video_default = variable_get('eminline_video_' . $format, _eminline_default_filter_settings('video'));
  foreach ($video_default as $key => $value) {
    $form['eminline_'. $format]['video'][$key]['#default_value'] = $value;
  }
  $preview_default = variable_get('eminline_preview_' . $format, _eminline_default_filter_settings('preview'));
  foreach ($preview_default as $key => $value) {
    $form['eminline_'. $format]['preview'][$key]['#default_value'] = $value;
  }
  $tn_default = variable_get('eminline_tn_' . $format, _eminline_default_filter_settings('tn'));
  foreach ($tn_default as $key => $value) {
    $form['eminline_'. $format]['tn'][$key]['#default_value'] = $value;
  }

  // Add select for choosing display format.
  $form['eminline_'. $format]['node_display'] = array(
    '#type' => 'select',
    '#title' => t('Video display in node view'),
    '#default_value' =>  variable_get('eminline_full_node_' . $format, 'video_video'),
    '#options' => array(
      'video_video' => t('Video'),
      'video_preview' => t('Preview'),
      'video_thumbnail' => t('Thumbnail'),
    ),
    '#weight' => -999
  );

  $form['eminline_'. $format]['#type'] = 'fieldset';
  $form['eminline_'. $format]['#title'] = t('Embed inline');
  $form['eminline_'. $format]['#collapsible'] = TRUE;
  $form['eminline_'. $format]['#tree'] = TRUE;

  return $form;
}

/**
 * Implementation of hook_form_FORM_ID_alter().
 *
 * Because filter format forms are merged, any common keys need to be added in
 * a form_alter. Otherwise, e.g. submit handlers added by one module would be
 * overwritten by those of another module.
 */
function eminline_form_filter_admin_configure_alter(&$form, $form_state) {
  if (isset($form['format']) && isset($form['eminline_' . $form['format']['#value']])) {
    $form['#validate'][] = '_eminline_url_settings_form_validate';

    // Add the submit callback to the beginning of the array because we need
    // to prepare data for system_settings_form_submit().
    array_unshift($form['#submit'], '_eminline_url_settings_form_submit');
  }
}

/*
  TODO Write validation function...
*/

function _eminline_url_settings_form_validate($form, &$form_state) {
  $format = $form_state['values']['format'];
  if (!is_numeric($form_state['values']['eminline_'. $format]['video']['video_width'])
    || intval($form_state['values']['eminline_'. $format]['video']['video_width']) !=  $form_state['values']['eminline_'. $format]['video']['video_width']
    || $form_state['values']['eminline_'. $format]['video']['video_width'] < 1) {
      form_set_error('eminline_'. $format .'][video][video_width', t('"Video width" must be a positive integer.'));
  }
  if (!is_numeric($form_state['values']['eminline_'. $format]['video']['video_height'])
    || intval($form_state['values']['eminline_'. $format]['video']['video_height']) !=  $form_state['values']['eminline_'. $format]['video']['video_height']
    || $form_state['values']['eminline_'. $format]['video']['video_height'] < 1) {
      form_set_error('eminline_'. $format .'][video][video_height', t('"Video height" must be a positive integer.'));
  }
  if (!is_numeric($form_state['values']['eminline_'. $format]['preview']['preview_width'])
    || intval($form_state['values']['eminline_'. $format]['preview']['preview_width']) !=  $form_state['values']['eminline_'. $format]['preview']['preview_width']
    || $form_state['values']['eminline_'. $format]['preview']['preview_width'] < 1) {
      form_set_error('eminline_'. $format .'][preview][preview_width', t('"Preview width" must be a positive integer.'));
  }
  if (!is_numeric(  $form_state['values']['eminline_'. $format]['preview']['preview_height'])
    || intval(  $form_state['values']['eminline_'. $format]['preview']['preview_height']) !=    $form_state['values']['eminline_'. $format]['preview']['preview_height']
    || $form_state['values']['eminline_'. $format]['preview']['preview_height'] < 1) {
      form_set_error('eminline_'. $format .'][preview][preview_height', t('"Preview height" must be a positive integer.'));
  }
  if (!is_numeric($form_state['values']['eminline_'. $format]['tn']['thumbnail_height'])
    || intval($form_state['values']['eminline_'. $format]['tn']['thumbnail_height']) !=  $form_state['values']['eminline_'. $format]['tn']['thumbnail_height']
    || $form_state['values']['eminline_'. $format]['tn']['thumbnail_height'] < 1) {
      form_set_error('eminline_'. $format .'][tn][thumbnail_height', t('"Thumbnail height" must be a positive integer.'));
  }
  if (!is_numeric($form_state['values']['eminline_'. $format]['tn']['thumbnail_width'])
    || intval($form_state['values']['eminline_'. $format]['tn']['thumbnail_width']) !=  $form_state['values']['eminline_'. $format]['tn']['thumbnail_width']
    || $form_state['values']['eminline_'. $format]['tn']['thumbnail_width'] < 1) {
      form_set_error('eminline_'. $format .'][tn][thumbnail_width', t('"Thumbnail width" must be a positive integer.'));
  }
}

function _eminline_url_settings_form_submit($form, &$form_state) {
  $format = $form_state['values']['format'];
  variable_set('eminline_providers_' . $format, $form_state['values']['eminline_'. $format]['provider_list']['providers']);
  variable_set('eminline_video_' . $format, $form_state['values']['eminline_'. $format]['video']);
  variable_set('eminline_preview_' . $format, $form_state['values']['eminline_'. $format]['preview']);
  variable_set('eminline_tn_' . $format, $form_state['values']['eminline_'. $format]['tn']);
  variable_set('eminline_full_node_' . $format, $form_state['values']['eminline_'. $format]['node_display']);
}

function _eminline_curry($func, $arity) {
  return create_function('', "
    \$args = func_get_args();
    if(count(\$args) >= $arity)
        return call_user_func_array('$func', \$args);
    \$args = var_export(\$args, 1);
    return create_function('','
        \$a = func_get_args();
        \$z = ' . \$args . ';
        \$a = array_merge(\$z,\$a);
        return call_user_func_array(\'$func\', \$a);
    ');
  ");
}

function _eminline_default_filter_settings($type = 'video') {
  $return = array();
  $widget = emvideo_widget_settings('form', array('type' => 'emvideo_textfields'));
  if (is_array($widget[$type])) {
    foreach(element_children($widget[$type]) as $key) {
      $return[$key] = $widget[$type][$key][$default_value];
    }
  }
  return $return;
}

/**
 *  Implement hook_theme().
 */
function eminline_theme($existing, $type, $theme, $path) {
  return array(
    'eminline_url_wrapper' => array(
      'arguments' => array('content' => NULL),
    ),
  );
}

/**
 * Create additional theme layer.
 *
 * @param string $content
 *
 * @return string
 */
function theme_eminline_url_wrapper($content) {
  $output = '';
  $output .= '<div class="eminline-wrapper">';
  $output .= $content;
  $output .= '</div>';
  return $output;
}
