You are here

kaltura_playlist.module in Kaltura 6.2

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, $account) {
  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['admin/settings/kaltura/kaltura_playlist_settings'] = array(
    'title' => 'Playlist Node Settings',
    'description' => 'Administer the kaltura playlist module.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'kaltura_playlist_settings_form',
    ),
    'access arguments' => array(
      'administer kaltura',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => -5,
  );
  return $items;
}
function kaltura_playlist_settings_form() {
  $saved_player = variable_get('kaltura_viewplaylist_entry_widget', KalturaSettings_DEFAULT_VIEWPLAYLIST_PLAYER_UICONF);
  $players = kaltura_choose_player(0, 'viewplaylist', 'entry', FALSE, $saved_player);
  $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_delivery_type'] = array(
    '#type' => 'select',
    '#title' => t('Delivery Type'),
    '#options' => array(
      'HTTP' => 'Progressive Download (HTTP)',
      'RTMP' => 'Adaptive Streaming (RTMP)',
    ),
    '#default_value' => variable_get('kaltura_viewplaylist_entry_delivery_type', 'HTTP'),
  );
  $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();
  $tags_views = kaltura_get_tag_filter_views();
  $js_script = '<script type="text/javascript">
  function change_view(val)
  {
	switch(val)
	{';
  foreach ($tags_views as $name => $val) {
    $js_script .= 'case "' . $name . '":
	';
  }
  $js_script .= 'document.getElementById("edit-tag-filter").style.display="inline"; break;
	default: document.getElementById("edit-tag-filter").style.display="none"; break;
	}
  }
  </script>';
  $ops = array(
    "=" => "Is equal to",
    "!=" => "Is not equal to",
    "contains" => "Contains",
    "word" => "Contains any word",
    "allwords" => "Contains all words",
    "starts" => "Starts with",
    "not_starts" => "Does not start with",
    "ends" => "Ends with",
    "not_ends" => "Does not end with",
    "not" => "Does not contain",
  );
  $form['playlist_view'] = array(
    '#type' => 'select',
    '#title' => t('View'),
    '#description' => t('The list includes only views that selects the "Kaltura media ID" field in the default display.'),
    '#options' => $available_views,
    '#required' => TRUE,
    '#prefix' => $js_script,
    '#attributes' => array(
      'onChange' => 'change_view(this.options[this.selectedIndex].value)',
    ),
    '#default_value' => $node->playlist_view,
  );
  $form['tag_filter_operator'] = array(
    '#type' => 'select',
    '#title' => t('Tag operator'),
    '#description' => t('* "Is equal to" operator works only on a single tag, if using multiple tags on the same node should use "Contains"'),
    '#default_value' => empty($node->tag_filter_operator) ? "contains" : $node->tag_filter_operator,
    '#options' => $ops,
  );
  $form['tag_filter'] = array(
    '#type' => 'textfield',
    '#title' => t('Tag value'),
    '#description' => t('Tag specified for all entries'),
    '#default_value' => $node->tag_filter,
    '#size' => '30',
  );
  return $form;
}
function kaltura_get_relevant_views() {
  $views = views_get_all_views();

  //var_dump($views);
  $relevant_views = array();
  if (!is_array($views) || !count($views)) {
    return $relevant_views;
  }
  foreach ($views as $view_name => $view) {
    if ($view->disabled) {
      continue;
    }
    foreach ($view->display as $display) {
      if ($display->display_plugin == 'default') {

        // has a feed output, let's see if entryId is a field
        if (isset($display->display_options['fields']['kaltura_entryId'])) {
          $relevant_views[$view->name . '|' . $display->id] = $view_name;
        }
      }
    }
  }
  return $relevant_views;
}
function kaltura_get_tag_filter_views() {
  $views = views_get_all_views();
  $relevant_views = array();
  if (!is_array($views) || !count($views)) {
    return $relevant_views;
  }
  foreach ($views as $view_name => $view) {
    if ($view->disabled) {
      continue;
    }
    foreach ($view->display as $display) {
      if ($display->display_plugin == 'default') {
        if (!empty($display->display_options['filters']['kaltura_tags']) && $display->display_options['filters']['kaltura_tags']['exposed'] == 1) {
          $relevant_views[$view->name . '|' . $display->id] = $view_name;
        }
      }
    }
  }
  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, tag_filter_operator, tag_filter)
			VALUES(%d, %d, \'%s\', \'%s\', \'%s\' )';
  db_query($sql, $node->vid, $node->nid, $node->playlist_view, $node->tag_filter_operator, $node->tag_filter);
}

/**
 * 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\',tag_filter_operator = \'%s\',tag_filter = \'%s\' WHERE vid = %d';
    db_query($sql, $node->playlist_view, $node->tag_filter_operator, $node->tag_filter, $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,tag_filter_operator,tag_filter 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, $node->tag_filter, $node->tag_filter_operator);
  $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->display['default']->display_options['fields'];
  foreach ($all_fields as $field_name => $data) {
    if ($data['exclude'] == 0) {
      $fields[$data['table'] . '_' . $field_name] = $data['table'] . '_' . $field_name;
    }
  }
  return $fields;
}
function kaltura_get_view_raw_data($view_details, $tag_filter = '', $tag_filter_op = 'contains') {
  global $user;
  list($view_name, $view_display) = kaltura_get_view_details($view_details);
  $view = views_get_view($view_name);
  if ($view) {
    $view->display['default']->display_options['fields']['kaltura_media_type'] = array(
      'label' => 'Kaltura Media Type',
      'link_to_node' => 0,
      'exclude' => 1,
      'id' => 'kaltura_media_type',
      'table' => 'node_kaltura',
      'field' => 'kaltura_media_type',
      'relationship' => 'none',
    );
    if (!key_exists('kaltura_entryId', $view->display['default']->display_options['fields'])) {
      $view->display['default']->display_options['fields']['kaltura_entryId'] = array(
        'label' => '',
        'exclude' => 1,
        'id' => 'kaltura_entryId',
        'table' => 'node_kaltura',
        'field' => 'kaltura_entryId',
        'relationship' => 'none',
      );
    }
    if (!empty($view->display['default']->display_options['filters']['kaltura_tags'])) {
      $view->display['default']->display_options['filters']['kaltura_tags']['value'] = $tag_filter;
      $view->display['default']->display_options['filters']['kaltura_tags']['operator'] = $tag_filter_op;
    }
    $view->display['default']->display_options['filters']['kaltura_media_type_1'] = array(
      'operator' => 'not in',
      'value' => array(
        '2' => '2',
      ),
      'group' => '0',
      'exposed' => FALSE,
      'expose' => array(
        'operator' => FALSE,
        'label' => '',
      ),
      'id' => 'kaltura_media_type_1',
      'table' => 'node_kaltura',
      'field' => 'kaltura_media_type',
      'relationship' => 'none',
    );
    $view_fields = kaltura_extract_fields_from_view($view);
    require_once drupal_get_path('module', 'views') . '/includes/admin.inc';
    @$view
      ->preview('default', $args);
    $query = db_prefix_tables($view->build_info['query']);
    $query = str_replace('***CURRENT_USER***', $user->uid, $query);
    $query = str_replace('***ADMINISTER_NODES***', intval(user_access('administer nodes')), $query);
    if ($view->build_info['query_args']) {
      _db_query_callback($view->build_info['query_args'], TRUE);
      $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
    }
    if ($view->pager['items_per_page']) {
      $query .= ' LIMIT 0,' . $view->pager['items_per_page'];
    }
    $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) {
  if ($attr == 'node_kaltura_kaltura_thumbnail_url') {
    return '<div class="' . $attr . '">' . $val . '</div>';
  }
  else {
    return '<div class="' . 'playlist-item-line' . '">' . $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_revisions_timestamp':
      $return = format_date($value, 'small');
      break;
    case 'node_changed':
      $return = format_date($value, 'small');
      break;
    case 'node_kaltura_kaltura_duration':
      $secs = $value;
      if ($secs > 60 * 60) {
        $hr = (int) ($secs / 60);
        $hr = $hr < 10 ? '0' . $hr : $hr;
        $min = (int) ($secs / 60 / 60);
        $min = $min < 10 ? '0' . $min : $min;
        $sec = (int) ($secs / 60 % 60);
        $sec = $sec < 10 ? '0' . $sec : $sec;
        $return = $hr . ':' . $min . ':' . $sec;
      }
      else {
        $min = (int) ($secs / 60);
        $min = $min < 10 ? '0' . $min : $min;
        $sec = (int) ($secs % 60);
        $sec = $sec < 10 ? '0' . $sec : $sec;
        $return = $min . ':' . $sec;
      }
      break;
    case 'node_kaltura_kaltura_plays':
      $return = "Plays: " . $value;
      break;
    case 'node_kaltura_kaltura_tags':
      $return = "Tags: " . $value;
      break;
    case 'node_kaltura_kaltura_rank':
      $return = "Rank: " . $value;
      break;
    case 'node_kaltura_kaltura_admin_tags':
      $return = "Categories: " . $value;
      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, $args);
  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");
  }
  function next_playlist_item()
  {
	auto_scroll_playlist();
  }
  function prev_playlist_item()
  {
      current_entry = mykdp.currentEntryId;
      prev_entries = $("#"+current_entry).prev();
      if (prev_entries.length) {
        next_id = $(prev_entries[prev_entries.length-1]).attr("id");
      } else {
        next_id = $(myplaylist[0]).attr("id");
      }
      $("#"+next_id).click();
  }

  ';
  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;
}