<?php
// $Id$

/**
 *  Implementation of hook_mapi_presenters()
 */
function mapi_jwplayer_mapi_presenters($op, $presenter, $filename, $data) {
  $presenter = drupal_get_path('module', 'mapi_jwplayer') .'/flvplayer.swf';
  switch ($op) {
    // list of presenters that the module can handle
    case 'list':
      // presenter must exist before it can be turned "on"
      if (is_file($presenter)) {
        return array('jw_player');
      }
      break;

    // list of available extensions for this presenter
    case 'extensions':
      return array('mp3', 'mp4', 'flv');

    //
    case 'configure':
      break;

    //
    case 'validate':
      break;

    //
    case 'save':
      break;

    // this displays the presenter using a playlist
    case 'display playlist':
      // handle the fact that a playlist requires an extended height/width
      $height = (!empty($data['height']) ? $data['height'] + 20 : 20);
      if (!isset($data['displayheight'])) {
        $data['displayheight'] = $height + 65;
      }
      // deliberate fall through here, to allow the presenter to be displayed

    // This displays the presenter utilizing the data
    case 'display':
      // only display the presenter
      if (!is_file($presenter)) {
        return;
      }

      $height = (!empty($data['height']) ? $data['height'] + 20 : 20);
      $width  = (!empty($data['width']) ? $data['width'] : 250);

      // this contradicts the way jw_player sees both displayheight and height.
      // but to keep it in common with the question of height/displayheight
      // since height references how high the file is to be displayed, we
      // adjust to the appropriate proportions.
      if (isset($data['displayheight'])) {
        $dh = $data['displayheight'];
        $data['displayheight'] = $height;
        $height = $dh;
      }

      // the extra settings which are valid for the jw_player
      $variables = array('displayheight');

      $base_url = url('', null, null, true);

      $filename = $base_url . $filename;
      $link = $base_url . $presenter;
      $param = array('wmode' => 'opaque');
      $var = array();
      $flashvar = 'file='. $filename;
      foreach ($variables as $key) {
        if (isset($data[$key])) {
          $flashvar .= "&{$key}=". $data[$key];
        }
      }

      $general = array(
        'param' => array('movie' => $link, 'wmode' => 'opaque', 'flashvars' => $flashvar),
        'var' => array('type' => 'application/x-shockwave-flash'),
      );
      $microsoft = array(
        'param' => array('src' => $link, 'wmode' => 'opaque', 'flashvars' => $flashvar),
        'var' => array('classid' => 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'),
      );

      return mapi_render_object($link, $width, $height, $general, $microsoft);

    // This outputs a playlist the jw_player understands
    case 'playlist':
      // XPSF playlist
      $items = array();

      foreach ($data as $info) {
        // check the filename, and the existance of the file
        $filename = (isset($info['filename']) ? $info['filename'] : '');
        if (!$filename || !in_array(mapi_file_ext($filename), mapi_presenter_extensions('jw_player')) || !mapi_file_exists($filename)) {
          continue;
        }

        // make sure some basic metadata is filled out
        $meta          = (isset($info['meta']) ? $info['meta'] : array());
        $meta['title'] = (isset($meta['title']) ? $meta['title'] : t('Untitled'));

        // generate the link the player will use
        $link = url($filename, null, null, true);

        // NEEDS WORK: missing thumbnails
        $items[] = array(
          'key' => 'track',
          'value' => array(
            'title' => $meta['title'],
            'location' => $link,
          )
        );
      }
      $playlist[] = array(
        'key' => 'playlist',
        'attributes' => array('version' => 1, 'xmlns' => 'http://xspf.org/ns/0/'),
        'value' => array('trackList' => $items)
      );

      $output = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
      $output .= format_xml_elements($playlist);

      // return output and headers
      return array($output, array('Content-Type: text/xml; charset=utf-8'));
  }
}