<?php
// $Id$

/**
 * Creates the HTML to present the filename to the user.
 *
 * @param $filename
 *   A filename
 * @param $options
 *   An associative array which can override the standard settings for the presenter.
 * @return
 *   Returns the HTML generated code.
 */
function mapi_display($filename, $options = array()) {
  // must be a filename
  if (!$filename || !is_string($filename) || !mapi_file_exists($filename)) {
    return '';
  }

  // profile
  $profile = (isset($options['profile']) ? $options['profile'] : null);

  // use the contextual filename (which could simply be the original)
  $filename = ($profile ? mapi_invoke_profile('filename', $profile, $filename) : $filename);

  // get the extension
  $ext = mapi_file_ext($filename);

  // we're missing an extension
  if (!$ext) {
    // see if we can determine the extension from mime type.
    $mime = mapi_file_mimetype($filename);
    $ext = mapi_extension_from_mime($mime);
    if (!$ext) {
      return theme('mapi_display_failed', $filename);
    }
  }

  // get the settings for the file
  $settings = ($profile ? mapi_invoke_profile('settings', $profile, $filename) : null);
  $settings = (array_key_exists('settings', $options) ? $options['settings'] : $settings);
  if (!$settings || !is_array($settings)) {
    $settings = mapi_file_dimensions($filename);
  }

  // override the settings, etc.
  $default = ($profile ? mapi_invoke_profile('presenter', $profile, $filename) : variable_get("mapi_ext_{$ext}_presenter", ''));
  $default = (array_key_exists('presenter', $options) && is_string($options['presenter']) ? $options['presenter'] : $default);

  $presenters = mapi_presenter_by_ext($ext);

  // attempt with possible overridden presenter
  if (in_array($default, $presenters)) {
    return mapi_invoke_presenters('display', $default, $filename, $settings);
  }

  // fall back to try again on the defaults
  $default = variable_get("mapi_ext_{$ext}_presenter", '');
  if (in_array($default, $presenters)) {
    return mapi_invoke_presenters('display', $default, $filename, $settings);
  }

  // otherwise, simply execute the generic function.
  return mapi_presenter_generic($filename, $settings);
}

/**
 *  Allows a playlist to be given to a presenter
 */
function mapi_display_playlist($filename, $presenter, $settings = array()) {
  // must have a filename, and presenter
  if (!$filename || !in_array($presenter, mapi_presenter_list())) {
    return '';
  }

  return mapi_invoke_presenters('display playlist', $presenter, $filename, $settings);
}

/**
 * Generates the output for unable to display media file.
 *
 * @param $filename
 *   The failed filename.
 * @return
 *   Returns HTML.
 */
function theme_mapi_display_failed($filename) {
  return t('Could not locate %file.', array('%file' => $filename));
}

/**
 * This allows plug-ins to provide links to files which normally would be without Clean
 * URLs enabled, be transferred via drupal.
 *
 * @param $filename
 *   The filename to present
 * @param $settings
 *   The settings provided for the presenter
 * @return
 *   HTML code representing the media file.
 */
function mapi_presenter_generic($filename, $settings) {
  // some generic things we handle, such as images.
  // otherwise provide simply provide a link to the file
  $ext = mapi_file_ext($filename);
  switch ($ext) {
    case 'jpg':
    case 'png':
    case 'gif':
      $output = theme('image', $filename, '', '', $settings, (empty($settings['width']) || empty($settings['height'])));
      break;
    default:
      // simply provide a link to the file
      $output = l($filename, $filename, array('class' => 'ext-'. $ext));
      break;
  }

  return $output;
}

/**
 * This creates the HTML for an XHTML-valid object.
 * Uses the technique from http://www.alistapart.com/articles/byebyeembed
 *
 * With swfobject.js installed in the mapi/misc directory, javascript will be used
 * to insert the object, thereby skipping the Internet Explorer click on object to
 * activate problem.
 *
 * @param $url
 *   A full URL
 * @param $width
 *   The width to be displayed within the HTML.
 * @param $height
 *   The height to be displayed within the HTML.
 * @param $general
 *   An array containing 'param' and 'var' wich themselves are associative arrays,
 *   which provides the generally used parameters and variables for an object.
 * @param $microsoft
 *   Similar to general but providing the details to the Microsoft aware object.
 * @return
 *   HTML code representing the media object.
 */
function mapi_render_object($url, $width, $height, $general = array(), $microsoft = array()) {
  static $sobject;
  $uses_js = false;

  $output = '';

  // define the main ones
  $param = (!empty($general['param']) ? $general['param'] : array());
  $var = (!empty($general['var']) ? $general['var'] : array());

  $var['width'] = $width;
  $var['height'] = $height;

  $path = drupal_get_path('module', 'mapi') . '/misc/swfobject.js';
  if (is_file($path)) {
    if (!is_numeric($sobject))  {
      $sobject = 0;
    }
    $uses_js = true;
    drupal_add_js($path);

    $sobj = ' s'. ++$sobject;
    $output .= '<div id="player-'. md5($url) .'-'. $sobject .'">'."\n";
    $output .= '<script type="text/javascript">'."\n";
    $output .= '  var'. $sobj .' = new SWFObject("'. url($url, null, null, true) .'","single","'. $width .'","'. $height .'","7");'."\n";
    foreach ($param as $k => $v) {
      $output .= $sobj .'.addParam("'. $k .'", "'. $v .'");'."\n";
    }
    foreach ($var as $k => $v) {
      $output .= $sobj .'.addVariable("'. $k .'", "'. $v .'");'."\n";
    }
    $output .= $sobj .'.write("player-'. md5($url) .'-'. $sobject .'");'."\n";
    $output .= '</script><noscript>';
  }

  // process microsoft problematic param and variables
  $ms_param = (!empty($microsoft['param']) ? $microsoft['param'] : array());
  $ms_var = (!empty($microsoft['var']) ? $microsoft['var'] : array());

  // begin the object Microsoft sees
  $output .= '<object width="'. $width .'" height="'. $height .'" '. drupal_attributes($ms_var) .'>';
  foreach ($ms_param as $k => $v) {
    $output .= '<param name="'. $k .'" value="'. $v .'" />';
  }

  // begin the object that everybody else processes correctly, and that we tell IE to ignore
  // nested second IE hidden object
  $output .= '<!--[if !IE]>-->';
  $output .= '<object data="'. $url .'" '. drupal_attributes($var) .'>';
  foreach ($param as $k => $v) {
    $output .= '<param name="'. $k .'" value="'. $v .'" />';
  }
  $output .= '</object>';
  $output .= '<!--<![endif]-->';

  if ($uses_js) {
    $output .= '</noscript></div>';
  }

  return $output;
}
