You are here

function brightcove_media_node_presave in Brightcove Video Connect 7.2

Same name and namespace in other branches
  1. 7.3 brightcove_media/brightcove_media.module \brightcove_media_node_presave()
  2. 7.4 brightcove_media/brightcove_media.module \brightcove_media_node_presave()

Implements hook_node_presave(). (needed for media gallery)

File

brightcove_media/brightcove_media.module, line 523

Code

function brightcove_media_node_presave($node) {
  global $user;
  if (isset($node->media_gallery_media['und'])) {
    foreach ($node->media_gallery_media['und'] as &$file) {
      if (isset($file['fid']) && strpos($file['fid'], 'v') === 0) {
        $video_id = substr($file['fid'], 1);
        $candidates = file_load_multiple(array(), array(
          'uri' => "brightcove://{$video_id}",
        ));
        if (count($candidates)) {
          $file['fid'] = array_shift($candidates)->fid;
        }
        else {
          $handler = new MediaInternetBrightcoveHandler($video_id);
          if (!isset($_SESSION['brightcove']["video_just_uploaded_{$user->uid}"])) {
            $bc = brightcove_initialize();
            try {
              $result = $bc
                ->find('find_video_by_id', $video_id);
              if (count($result)) {
                $_SESSION['brightcove']["selected_video_{$user->uid}"] = (object) $result;
              }
            } catch (Exception $e) {
              watchdog('brightcove', 'Retrieving media list failed.', array(), WATCHDOG_ERROR);
            }
          }
          $file['fid'] = $handler
            ->save()->fid;
        }
      }
    }
  }
}