You are here

function _brightcove_field_video_browse in Brightcove Video Connect 7.3

Same name and namespace in other branches
  1. 7.7 brightcove_field.video.inc \_brightcove_field_video_browse()
  2. 7.4 brightcove_field/brightcove_field.video.inc \_brightcove_field_video_browse()
  3. 7.5 brightcove_field/brightcove_field.video.inc \_brightcove_field_video_browse()
  4. 7.6 brightcove_field.video.inc \_brightcove_field_video_browse()
1 call to _brightcove_field_video_browse()
brightcove_field_browse in brightcove_field/brightcove_field.browse.inc
This function is a callback for modalframe window, providing an access to browse videos.

File

brightcove_field/brightcove_field.video.inc, line 183
Holds all of the video field related methods.

Code

function _brightcove_field_video_browse() {
  global $pager_from_array, $pager_total, $pager_page_array;
  $items_per_page = 20;
  $page = isset($_GET['page']) ? intval($_GET['page']) : '0';
  $params = array();
  if (isset($_SESSION['brightcove_field_filter'])) {
    switch ($_SESSION['brightcove_field_filter']['search']) {
      case 'tags':
        $method = 'find_videos_by_tags';
        $params = array(
          'or_tags' => $_SESSION['brightcove_field_filter']['keywords'],
        );
        break;
      case 'and_tags':
        $method = 'find_videos_by_tags';
        $params = array(
          'and_tags' => $_SESSION['brightcove_field_filter']['keywords'],
        );
        break;
      case 'name':
      default:
        $method = 'find_videos_by_text';
        $params = array(
          'text' => join(' ', explode(',', $_SESSION['brightcove_field_filter']['keywords'])),
        );
        break;
    }
  }
  else {
    $method = 'find_all_videos';
  }
  $params['page_size'] = $items_per_page;
  $params['page_number'] = $page;
  $params['get_item_count'] = 'TRUE';
  $result = NULL;

  // Try to load the data from cache.
  $cid = 'brightcove:video:list';
  foreach ($params as $key => $param) {
    $cid .= ":{$key}:{$param}";
  }
  $content = brightcove_cache_get($cid);

  // If no cached data is available.
  if (!$content) {
    $bc = brightcove_initialize();
    try {
      $result = $bc
        ->find($method, $params);
    } catch (Exception $error) {
      watchdog('brightcove', 'Finding videos in browse failed.', array(), WATCHDOG_ERROR);
    }
    $pager_page_array = explode(',', $page);
    $pager_total[0] = ceil($bc->total_count / $items_per_page);
    $pager_page_array[0] = max(0, min((int) $pager_page_array[0], (int) $pager_total[0] - 1));
    $items = array();
    if (count($result)) {
      foreach ($result as $video) {
        $item = array();
        $item['title'] = check_plain($video->name);
        $item['brightcove_id'] = $video->id;
        if (!empty($video->thumbnailURL)) {
          $image_vars = array(
            'path' => $video->thumbnailURL,
            'alt' => '',
            'title' => '',
            'attributes' => '',
          );
          $item['thumbnail'] = theme('image', $image_vars);
        }
        else {
          $image_vars = array(
            'path' => brightcove_get_default_image(),
            'alt' => '',
            'title' => '',
            'attributes' => '',
            'width' => '120',
            'height' => '90',
          );
          $item['thumbnail'] = theme('image', $image_vars);
        }
        $items[] = $item;
      }
    }
    $pager_vars = array(
      'tags' => NULL,
      'element' => 0,
      'parameters' => array(),
      'quantity' => $items_per_page,
    );
    $content['items'] = $items;
    $content['pager'] = theme('pager', $pager_vars);
    $content['filter_form'] = drupal_get_form('brightcove_field_filter_form');

    // Save the output to the cache record(not only the video objects),
    // because we also need data for pagination.
    brightcove_cache_set($cid, $content);
  }
  return $content;
}