You are here

kaltura_playlist.module in Kaltura 5

File

plugins/kaltura_playlist/kaltura_playlist.module
View source
<?php

/**
 * @file
 * kaltura integration module - functions to provide kaltura playlist node
 *
 * A playlist node is a node that has title + reference to View(RSS type)
 * And is rendered as a div of media list + a div of player
 * Each media in the list has a onClick event to InsertMedia into Kaltura KDP
 * 
 */
drupal_add_js(drupal_get_path('module', 'kaltura') . '/js/kaltura_player_controller.js');
drupal_add_js(drupal_get_path('module', 'kaltura_playlist') . '/jquery.mousewheel.js');
drupal_add_js(drupal_get_path('module', 'kaltura_playlist') . '/jquery.scrollable-0.13.js');

/*
 * Implementation of hook_perm().
 */
function kaltura_playlist_perm() {
  return array(
    'create kaltura playlist',
    'edit kaltura playlist',
    'view kaltura playlist',
  );
}

/*
 * Implementation of hook_access().
 */
function kaltura_playlist_access($op, $node) {
  switch ($op) {
    case 'create':
      return user_access('create kaltura playlist', $account);
    case 'update':
      return user_access('edit kaltura playlist', $account);
    case 'delete':
      return user_access('delete kaltura playlist', $account);
  }
}

/*
 * Implementation of hook_menu().
 */
function kaltura_playlist_menu() {
  $items = array();
  $items[] = array(
    'path' => 'admin/settings/kaltura/kaltura_playlist_settings',
    'title' => 'Kaltura Playlist Node Settings',
    'description' => 'Administer the kaltura playlist module.',
    'callback' => 'drupal_get_form',
    'callback arguments' => array(
      'kaltura_playlist_settings_form',
    ),
    'access' => user_access('administer kaltura'),
    'type' => MENU_NORMAL_ITEM,
    'weight' => -5,
  );
  return $items;
}
function kaltura_playlist_settings_form() {
  $players = kaltura_choose_player(0, 'video', 'entry');
  $saved_player = variable_get('kaltura_viewplaylist_entry_widget', KalturaSettings_DEFAULT_VIEWPLAYLIST_PLAYER_UICONF);
  $form['kaltura_viewplaylist_entry_widget'] = $players['default_widget'];
  $form['kaltura_viewplaylist_entry_widget']['#default_value'] = $saved_player;
  $form['clear_block_item'] = array(
    '#type' => 'item',
    '#value' => '<div class="clear-block"></div>',
  );
  $form['kaltura_viewplaylist_entry_width'] = array(
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => t('Player Width'),
    '#default_value' => variable_get('kaltura_viewplaylist_entry_width', '410'),
  );
  $form['kaltura_viewplaylist_entry_height'] = array(
    '#type' => 'textfield',
    '#size' => 5,
    '#title' => t('Player Height'),
    '#default_value' => variable_get('kaltura_viewplaylist_entry_height', '364'),
  );
  $form['kaltura_viewplaylist_autonext'] = array(
    '#type' => 'radios',
    '#options' => array(
      1 => t('When the player finish playing an item, it moves automatically to the next'),
      0 => t('When the player finish playing an item, it just stops'),
    ),
    '#title' => t('Player AutoNext behavior'),
    '#default_value' => variable_get('kaltura_viewplaylist_autonext', 1),
  );
  return system_settings_form($form);
}

/*
 * Implementation of hook_node_info().
 */
function kaltura_playlist_node_info() {
  return array(
    'kaltura_playlist' => array(
      'name' => t('Kaltura Playlist'),
      'module' => 'kaltura_playlist',
      'description' => t('A Kaltura Playlist is a simple method for displaying Kaltura Views as a playlist.'),
    ),
  );
}

/*
 * Implementation of hook_form().
 */
function kaltura_playlist_form(&$node, &$param) {
  $type = node_get_types('type', $node);
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => check_plain($type->title_label),
    '#required' => TRUE,
    '#default_value' => $node->title,
    '#weight' => -5,
  );
  $available_views = kaltura_get_relevant_views();
  $form['playlist_view'] = array(
    '#type' => 'select',
    '#title' => t('View'),
    '#description' => t('The list includes only views that has a feed display'),
    '#options' => $available_views,
    '#required' => TRUE,
    '#default_value' => $node->playlist_view,
  );
  return $form;
}
function kaltura_views_get_all_views() {
  $result = db_query("SELECT vid FROM {view_view} ORDER BY name");
  while ($view = db_fetch_object($result)) {
    $views[$view->vid] = _views_load_view($view->vid);
  }
  return $views;
}
function kaltura_get_relevant_views() {
  $views = kaltura_views_get_all_views();
  if (module_exists('views')) {
    require_once drupal_get_path('module', 'views') . '/views_cache.inc';
    $default_views = _views_get_default_views();
    if (is_array($views) && count($views)) {
      $views = array_merge($views, $default_views);
    }
    else {
      $views = $default_views;
    }
  }
  $relevant_views = array();
  if (!is_array($views) || !count($views)) {
    return $relevant_views;
  }
  foreach ($views as $view_id => $view) {
    foreach ($view->field as $field) {
      if ($field['field'] == 'kaltura_entryId') {
        $relevant_views[$view->name . '|' . $view->vid] = $view->name;
        break;
      }
    }
  }
  return $relevant_views;
}

/*
 * Implementation of hook_theme().
 */
function kaltura_playlist_theme() {
  $themes = array(
    'kaltura_playlist_view' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
  return $themes;
}

/**
 * Implementation of hook_insert().
 */
function kaltura_playlist_insert(&$node) {
  watchdog('playlist', 'node created ' . print_r($node, true));
  $sql = 'INSERT INTO {node_kaltura_playlist} (vid, nid, playlist_view) VALUES(%d, %d, \'%s\')';
  db_query($sql, $node->vid, $node->nid, $node->playlist_view);
}

/**
 * Implementation of hook_update().
 */
function kaltura_playlist_update($node) {
  if ($node->revision) {
    node_example_insert($node);
  }
  else {
    $sql = 'UPDATE {node_kaltura_playlist} SET playlist_view = \'%s\' WHERE vid = %d';
    db_query($sql, $node->playlist_view, $node->vid);
  }
}

/*
 * Implementation of hook_delete().
 */
function kaltura_playlist_delete($node) {
  $sql = 'DELETE FROM {node_kaltura_playlist} where nid = %d';
  db_query($sql, $node->nid);
}

/**
 * Implementation of hook_load().
 */
function kaltura_playlist_load($node) {
  $sql = 'SELECT playlist_view FROM {node_kaltura_playlist} WHERE vid = %d';
  $result = db_query($sql, $node->vid);
  $additions = db_fetch_object($result);
  return $additions;
}

/**
 * Implementation of hook_view().
 */
function kaltura_playlist_view($node, $teaser = FALSE, $page = FALSE) {
  $node = node_prepare($node, $teaser);
  $node->content['playlist_view'] = array(
    '#value' => theme('kaltura_playlist_view', $node),
    '#weight' => 0,
  );
  return $node;
}
function theme_kaltura_playlist_view($node) {
  list($view_name, $view_display) = kaltura_get_view_details($node->playlist_view);
  list($view_raw, $view_fields) = kaltura_get_view_raw_data($node->playlist_view);
  $first_entry = $view_raw[0];
  $view_html = kaltura_nodes2html($view_raw, $view_fields);
  $autonext = variable_get('kaltura_viewplaylist_autonext', 1);
  $playlist_js = kaltura_prepare_js_code('kaltura_playlist_' . $view_name, $first_entry, $autonext, count($view_raw));
  $kdp = kaltura_get_kaltura_kdp($first_entry);
  return '<div class="kaltura_playlist"><div class="playlist-player">' . $kdp . '</div><div id="kaltura_playlist_' . $view_name . '" class="play-list">' . '<a class="go up"></a>' . $view_html . '<a class="go down"></a></div><div class="clear-block"></div></div>' . $playlist_js;
}
function kaltura_get_kaltura_kdp($first_entry) {
  $width = variable_get('kaltura_viewplaylist_entry_width', 410);
  $height = variable_get('kaltura_viewplaylist_entry_height', 364);
  $size = 'width="' . $width . '" height="' . $height . '"';
  return '[kaltura-widget media_type="viewplaylist" entry="' . $first_entry->node_kaltura_kaltura_entryId . '" id="kaltura_playlist_module" finishF="auto_scroll_playlist" ' . $size . ' /]';
}
function kaltura_get_view_details($view_details) {
  $arr_view_details = explode('|', $view_details);
  $view_name = $arr_view_details[0];
  $view_display = $arr_view_details[1];
  return array(
    $view_name,
    $view_display,
  );
}
function kaltura_extract_fields_from_view($view) {
  $all_fields = $view->field;
  foreach ($all_fields as $field_key => $data) {
    $fields[$data['queryname']] = $data['queryname'];
  }
  return $fields;
}
function kaltura_get_view_raw_data($view_details) {
  global $user;
  list($view_name, $view_display) = kaltura_get_view_details($view_details);
  $view = views_get_view($view_name);
  if ($view) {
    foreach ($view->filter as $field_key => $filter_field) {
      if ($filter_field['field'] == 'node_kaltura.kaltura_media_type') {

        // remove media_type(2) - images
        foreach ($filter_field['value'] as $key => $value) {
          if ($value == 2) {
            unset($filter_field['value'][$key]);
          }
        }
      }
    }
    $view_fields = kaltura_extract_fields_from_view($view);

    //    require_once(drupal_get_path('module', 'views') .'/includes/admin.inc');
    $result = views_build_view('result', $view);
    $query = $result['query'];
    $query = str_replace('***CURRENT_USER***', $user->uid, $query);
    $query = str_replace('***ADMINISTER_NODES***', intval(user_access('administer nodes')), $query);
    $results = db_query($query);
    while ($node = db_fetch_object($results)) {
      $nodes[] = $node;
    }
    return array(
      $nodes,
      $view_fields,
    );
  }
  return array(
    $view_name,
    $view_fields,
  );
}
function kaltura_wrap_value($attr, $val) {
  return '<div class="' . $attr . '">' . $val . '</div>';
}
function kaltura_format_known_fields($attr, $value) {
  switch ($attr) {
    case 'node_kaltura_kaltura_thumbnail_url':
      $return = '<img src="' . $value . '/width/74/height/55" />';
      break;
    case 'node_kaltura_kaltura_entryId':
      return FALSE;
      break;
    case 'node_changed':
      $return = format_date($value, 'small');
      break;
    case 'node_kaltura_kaltura_duration':
      $secs = $value;
      if ($secs > 60 * 60) {
        $return = (int) ($secs / 60) . ':' . (int) ($secs / 60 / 60) . ':' . (int) ($secs / 60 % 60);
      }
      else {
        $return = (int) ($secs / 60) . ':' . (int) ($secs % 60);
      }
      break;
    default:
      $return = $value;
  }
  return $return;
}
function kaltura_playlist_format_value($attribute, $value) {
  $user_val = kaltura_playlist_invoke('format_value', $attribute, $value);
  if ($user_val && $user_val[0]) {
    return kaltura_wrap_value($attribute, $user_val[0]);
  }
  if ($user_val && $user_val[0] === FALSE) {
    return '';
  }
  $value = kaltura_format_known_fields($attribute, $value);
  if ($value === FALSE) {
    return '';
  }
  return kaltura_wrap_value($attribute, $value);
}
function kaltura_playlist_invoke($hook) {
  $args = func_get_args();
  array_shift($args);

  // remove $hook
  $hook = 'kaltura_playlist_' . $hook;
  $return = module_invoke_all($hook, $data);
  return $return;
}
function kaltura_nodes2html($nodes, $fields) {
  $node_div = array();
  if (is_array($nodes) && count($nodes)) {
    foreach ($nodes as $node_result) {
      $node_div[$node_result->nid] = '<div id="' . $node_result->node_kaltura_kaltura_entryId . '" class="playlist-item" onclick="replace_media(\'' . $node_result->node_kaltura_kaltura_entryId . '\');">';
      foreach ($node_result as $attr => $val) {
        if (!$fields[$attr]) {
          continue;
        }
        $node_div[$node_result->nid] .= kaltura_playlist_format_value($attr, $val);
      }
      $node_div[$node_result->nid] .= '</div>';
    }
  }
  $html = implode('', $node_div);
  return '<div class="clips">' . $html . '</div>';
}
function kaltura_prepare_js_code($div_id, $first_entry, $autonext, $node_count) {
  $javascript = '<script type="text/javascript">
  var timeoutId = 0;
  var mykdp = new KalturaPlayerController(\'kaltura_playlist_module\');
  mykdp.currentEntryId = "' . $first_entry->node_kaltura_kaltura_entryId . '";
  $("#' . $first_entry->node_kaltura_kaltura_entryId . '").addClass("playing");

  var myplaylist = Array();
  function test() { alert("test"); }
  function fill_playlist(div_id) {
    myplaylist = $("div.clips").children(".playlist-item");
  }
  fill_playlist("' . $div_id . '");
  ';
  if ($node_count >= 4) {
    $javascript .= '
    $("div#' . $div_id . '").scrollable({
      items:\'div.clips\',
      vertical:true,
      size:4,
      next:\'a.down\',
      prev:\'a.up\'
    });
    ';
  }
  $javascript .= '
  $("div.clips").height($("div.clips").height()+2);

  function replace_media(entry_id){
    $("#"+mykdp.currentEntryId).removeClass("playing");
    mykdp.insertEntry(entry_id, true);
    $("#"+mykdp.currentEntryId).addClass("playing");
  }
  ';
  if ($autonext == 1) {
    $javascript .= 'function auto_scroll_playlist() {
      current_entry = mykdp.currentEntryId;
      next_entries = $("#"+current_entry).next();
      if (next_entries.length) {
        next_id = $(next_entries[0]).attr("id");
      } else {
        next_id = $(myplaylist[0]).attr("id");
      }
      $("#"+next_id).click();
    }';
  }
  $javascript .= '</script>';
  return $javascript;
}