You are here

function brightcove_media_list in Brightcove Video Connect 7.4

Same name and namespace in other branches
  1. 7.7 brightcove_media/brightcove_media.module \brightcove_media_list()
  2. 7.2 brightcove_media/brightcove_media.module \brightcove_media_list()
  3. 7.3 brightcove_media/brightcove_media.module \brightcove_media_list()
  4. 7.6 brightcove_media/brightcove_media.module \brightcove_media_list()

Get the avaliable videos from brightcove.

Return value

JSON list of available brightcove vieos.

1 string reference to 'brightcove_media_list'
brightcove_media_menu in brightcove_media/brightcove_media.module
Implements hook_menu().

File

brightcove_media/brightcove_media.module, line 574
This module provide the hook implementations for the integration with Media module.

Code

function brightcove_media_list() {
  global $user;
  module_load_include('browser.inc', 'media', 'includes/media');
  $default_items_per_page = 20;
  $params = drupal_get_query_parameters();
  $limit = isset($params['limit']) ? $params['limit'] : $default_items_per_page;
  $start = isset($params['start']) ? ceil($params['start'] / $limit) : 0;
  $media = array();
  $bc_find_params = array();
  if (isset($params['filter'])) {
    switch ($params['filter']['search']) {
      case 'tags':
        $method = 'find_videos_by_tags';
        $bc_find_params = array(
          'or_tags' => $params['filter']['keywords'],
        );
        break;
      case 'and_tags':
        $method = 'find_videos_by_tags';
        $bc_find_params = array(
          'and_tags' => $params['filter']['keywords'],
        );
        break;
      case 'name':
      default:
        $method = 'find_videos_by_text';
        $bc_find_params = array(
          'text' => join(' ', explode(',', $params['filter']['keywords'])),
        );
        break;
    }
  }
  else {
    $method = 'find_all_videos';
  }
  $bc_find_params['page_size'] = $limit;
  $bc_find_params['page_number'] = $start;

  // Try to load the data from cache. It uses different key than the cache of field browse results,
  // because it has a different output.
  $cid = 'brightcove:video:list:media';
  foreach ($bc_find_params as $key => $param) {
    $cid .= ":{$key}:{$param}";
  }
  $result = brightcove_cache_get($cid);
  if (!$result) {
    try {
      $bc = brightcove_initialize();
      $result = $bc
        ->find($method, $bc_find_params);
      brightcove_cache_set($cid, $result);
    } catch (Exception $e) {
      watchdog('brightcove', 'Retrieving media list failed.', array(), WATCHDOG_ERROR);
    }
  }
  if (count($result)) {
    foreach ($result as $video) {
      $uri = file_stream_wrapper_uri_normalize("brightcove://{$video->id}");
      $file = file_uri_to_object($uri);
      $file->filename = $video->name;
      $file->type = 'video';
      if (!isset($file->fid)) {
        $file->fid = 0;
      }
      media_browser_build_media_item($file);
      $file->preview = l($file->preview, 'media/browser', array(
        'html' => TRUE,
        'attributes' => array(
          'data-uri' => $uri,
        ),
        'query' => array(
          'render' => 'media-popup',
          'uri' => $uri,
        ),
      ));
      $files[$uri] = $file;
      $media[] = $file;
    }
  }
  drupal_add_js(array(
    'brightcove' => array(
      'files' => $files,
    ),
  ), 'setting');
  drupal_json_output($media);
  exit;
}