<?php
// $Id$

/**
 * Makes sure that the correct routines are called for media errors
 *
 * @param $message
 *   The message to record for the watchdog and drupal_set_message
 * @param $type
 *   The type of error that is being created.
 */
function mapi_error($message, $type = 'media') {
  watchdog($type, $message, WATCHDOG_ERROR);
  drupal_set_message($message, 'error');
}

/**
 * Get all the types handled by the system.
 *
 * @return
 *   An array with the types as values.
 */
function mapi_type_list() {
  static $cache;
  if (!isset($cache)) {
    $types = mapi_invoke_types('list');
    $types = (is_array($types) ? $types : array());
    sort($types);
    $cache = $types;
  }
  return $cache;
}

/**
 * Returns the list of extensions for a given type.
 *
 * @param $type
 *   The type for which we seek the extensions
 * @return
 *   A sorted array of extensions that are handled as part of this type.
 */
function mapi_type_extensions($type) {
  static $cache;
  if (!isset($cache[$type])) {
    $exts = mapi_invoke_types('extensions', $type);
    $exts = (is_array($exts) ? $exts : array());
    sort($exts);
    $cache[$type] = $exts;
  }
  return $cache[$type];
}

/**
 * Gets type information
 *
 * @param $type
 *   The type for which we seek the extensions
 * @return
 *   A sorted array of extensions that are handled as part of this type.
 */
function mapi_type_info($type) {
  $info = mapi_invoke_types('info', $type);

  // standard info block for types
  $details = array(
    'name' => $type,
    'title' => (isset($info['title']) ? $info['title'] : $type),
    'description' => (isset($info['description']) ? $info['description'] : '')
  );

  return $details;
}

/**
 * Returns the list of extensions.
 *
 * @return
 *   A sorted array of list of all extensions.
 */
function mapi_extension_list() {
  static $cache;
  if (!isset($cache)) {
    $exts = mapi_invoke_extensions('list');
    $exts = (is_array($exts) ? $exts : array());
    sort($exts);
    $cache = $exts;
  }
  return $cache;
}

/**
 * Locates the type for the extension provided
 *
 * @param $ext
 *   The type for which we seek the type
 * @return
 *   The type of the extension, generic if unsupported.
 */
function mapi_extension_type($ext) {
  // extension has to exist
  if (!$ext) {
    return;
  }

  // cycle through the types, looking for the first which contains the extension
  foreach (mapi_type_list() as $type) {
    $exts = mapi_type_extensions($type);
    if (in_array($ext, $exts)) {
      return $type;
    }
  }

  return;
}

/**
 * Provides the mime type for the given extension.
 *
 * @param $ext
 *   The type for which we seek the mime type.
 * @return
 *   The mime type of the extension, 'application/octet-stream' if unsupported.
 */
function mapi_extension_mime($ext) {
  if (!$ext) {
    return 'application/octet-stream';
  }

  $mime = mapi_invoke_extensions('mime', $ext);
  if ($mime) {
    return $mime;
  }

  return 'application/octet-stream';
}

/**
 * Finds the extension from the mime type.
 *
 * @param $ext
 *   The type for which we seek the mime type.
 * @return
 *   The extension for the mime type, '' if unsupported.
 */
function mapi_extension_from_mime($mime) {
  if (!$mime) {
    return '';
  }

  // cycle through the extension list, looking for matching mime type
  foreach (mapi_extension_list() as $ext) {
    if (mapi_extension_mime($ext) == $mime) {
      return $ext;
    }
  }

  return '';
}

/**
 * Provides dimensional details for a filename.
 *
 * @param $filename
 *   The filename.
 * @return
 *   An associative array with dimensional details.
 *   i.e. array('width'=>200, 'height'=>100)
 */
function mapi_file_dimensions($filename, $refresh = false) {
  static $dimensions;

  if (!is_string($filename) || !mapi_file_exists($filename)) {
    return array();
  }

  if ($refresh || !is_array($dimensions)) {
    $dimensions = array();
  }

  if (array_key_exists($filename, $dimensions)) {
    return $dimensions[$filename];
  }

  $dims = mapi_invoke_extensions('dimensions', $filename);

  // cache the dimensions
  $dimensions[$filename] = $dims;

  return $dims;
}

/**
 * Provides metadata information for a filename.
 *
 * @param $filename
 *   The filename.
 * @return
 *   An array of metadata information
 */
function mapi_file_metadata($filename, $refresh = false) {
  static $metadata;

  if ($refresh || !is_array($metadata)) {
    $metadata = array();
  }

  if (array_key_exists($filename, $metadata)) {
    return $metadata[$filename];
  }

  $meta = mapi_invoke_extensions('metadata', $filename);

  // cache the dimensions
  $metadata[$filename] = $meta;

  return $meta;
}

/**
 * Gets list of the presenters which are installed on the system.
 *
 * @return
 *    Simple list of presenters installed
 */
function mapi_presenter_list() {
  static $cache;

  if (!isset($cache)) {
    $presenters = mapi_invoke_presenters('list');
    $presenters = (is_array($presenters) ? $presenters : array());
    sort($presenters);
    $cache = $presenters;
  }
  return $cache;
}

/**
 * Returns a list of the extensions a presenter can handle.
 *
 * @return
 *    Simple list of presenters installed
 */
function mapi_presenter_extensions($presenter) {
  static $cache;

  if (!isset($cache[$presenter])) {
    $exts = mapi_invoke_presenters('extensions', $presenter);
    $exts = (is_array($exts) ? $exts : array());
    sort($exts);
    $cache[$presenter] = $exts;
  }
  return $cache[$presenter];
}

/**
 * Provides list of presenters for a given extension.
 *
 * @param $ext
 *   The extension.
 * @return
 *   An associative array with presenter name as the key, and the include
 *   path as the value.
 */
function mapi_presenter_by_ext($ext) {
  $presenters = array();
  foreach (mapi_presenter_list() as $presenter) {
    $exts = mapi_invoke_presenters('extensions', $presenter);
    if (is_array($exts) && in_array($ext, $exts)) {
      $presenters[] = $presenter;
    }
  }
  return $presenters;
}

/**
 * Outputs the playlist for the presenter.
 *
 * @param $name
 *   The name of the presenter
 * @param $items
 *   An array where each item is of the form array('thumbnail' => '', 'filename' => '', 'title' => 'Title of Item').
 *   The 'thumbnail' option can be left out, the filename is required.
 * @return
 *   An array with the output and the necessary HTML headers to identify the type of the output.
 */
function mapi_presenter_playlist($presenter, $items) {
  // gets the details
  list($output, $headers) = mapi_invoke_presenters('playlist', $presenter, null, $items);

  return array('output' => $output, 'headers' => $headers);
}

/**
 * Provides default presenter for a given extension.
 *
 * @param $ext
 *   The extension.
 * @return
 *   Name of the presenter currently installed as default for the given extension.
 */
function mapi_presenter_default($ext) {
  $default = variable_get("mapi_ext_{$ext}_presenter", '');
  $presenters = mapi_presenter_by_ext($ext);

  // does the default exist?
  if (in_array($default, $presenters)) {
    return $default;
  }

  return false;
}


/**
 *
 */
function mapi_presenter_form($presenter, $form_values = null) {
  $form = mapi_invoke_presenters('configure', $presenter, $form_values);
  if (isset($form)) {
    return $form;
  }
  return array();
}

/**
 *
 */
function mapi_presenter_validate($presenter, $form_values) {
  mapi_invoke_presenters('validate', $presenter, $form_values);
}


/**
 *
 */
function mapi_presenter_exists($presenter) {
  $presenters = mapi_presenter_list();
  if (in_array($presenter, $presenters)) {
    return true;
  }
  return false;
}


/**
 * Gets the details of a persistant media variable.
 *
 * @param $key
 *   The reference key.
 * @return
 *   Returns FALSE on failure, value on success.
 */
function mapi_persist_get($key) {
  if (!isset($_SESSION['mapi'][$key])) {
    return FALSE;
  }

  return $_SESSION['mapi'][$key];
}

/**
 * Sets the details of a persistant media variable.
 *
 * @param $key
 *   The reference key.
 * @param $value
 *   The value to be placed in the session variable.
 */
function mapi_persist_set($key, $value) {
  if ($key === null) {
    unset($_SESSION['mapi']);
  }
  // wipe variable completely from memory if NULL
  elseif ($value === null && isset($_SESSION['mapi'][$key])) {
    unset($_SESSION['mapi'][$key]);
  }
  // otherwise set it to the value
  else {
    $_SESSION['mapi'][$key] = $value;
  }
}
