You are here

function _brightcove_field_video_browse in Brightcove Video Connect 7.7

Same name and namespace in other branches
  1. 7.3 brightcove_field/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()

Video browser form.

Parameters

Entity $client: The brightcove client entity object.

Return value

array An array rendered by brightcove_field_browse().

1 call to _brightcove_field_video_browse()
brightcove_field_browse in ./brightcove_field.browse.inc
This function is a callback for modalframe window, providing an access to browse videos.

File

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

Code

function _brightcove_field_video_browse($client) {
  global $pager_total, $pager_page_array;
  $items_per_page = 21;
  $page = isset($_GET['page']) ? intval($_GET['page']) : '0';
  $params = [];
  $search = '';
  if (isset($_SESSION['brightcove_field_filter']['search'])) {
    switch ($_SESSION['brightcove_field_filter']['search']) {
      case 'tags':
        $search = "tags:{$_SESSION['brightcove_field_filter']['keywords']}";
        break;
      case 'everything':
      default:
        $search = "text:{$_SESSION['brightcove_field_filter']['keywords']}";
        break;
    }
  }
  $params['page_size'] = $items_per_page;
  $params['page_number'] = $page;

  /** @var \Brightcove\Object\Video\Video[] $result */
  $result = [];

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

  // If no cached data is available.
  if (!$content) {
    brightcove_try(function () use (&$result, $client, $search, $items_per_page, $page, &$pager_total) {

      /** @var \Brightcove\API\CMS $cms */
      list($cms, ) = brightcove_create_classes($client);
      $result = $cms
        ->listVideos($search, NULL, $items_per_page, $items_per_page * $page);
      $pager_total[0] = ceil($cms
        ->countVideos($search) / $items_per_page);
    });
    $pager_page_array = explode(',', $page);
    $pager_page_array[0] = max(0, min((int) $pager_page_array[0], (int) $pager_total[0] - 1));
    $items = [];
    if (count($result)) {
      foreach ($result as $video) {
        $item = [];
        $item['title'] = check_plain($video
          ->getName());
        $item['brightcove_id'] = $video
          ->getId();
        $video_images = $video
          ->getImages();
        if (!empty($video_images) && !empty($video_images['thumbnail'])) {
          $image_vars = [
            'path' => $video_images['thumbnail']
              ->getSrc(),
            'alt' => '',
            'title' => '',
            'attributes' => [],
          ];
          $item['thumbnail'] = theme('image', $image_vars);
        }
        else {
          $image_vars = [
            'path' => brightcove_get_default_image(),
            'alt' => '',
            'title' => '',
            'attributes' => [],
            'width' => '120',
            'height' => '90',
          ];
          $item['thumbnail'] = theme('image', $image_vars);
        }
        $items[] = $item;
      }
    }
    $pager_vars = [
      'tags' => NULL,
      'element' => 0,
      'parameters' => [],
      'quantity' => $items_per_page,
    ];
    $content['pager'] = theme('pager', $pager_vars);
    $content['items'] = $items;

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

  // The filter form should not be cached,
  // because it wouldn't invoke the submit handler in this case.
  $content['filter_form'] = drupal_get_form('brightcove_field_filter_form', $client);
  return $content;
}