You are here

function vimeo_video_uploader_node_presave in Vimeo Video Uploader 7

Same name and namespace in other branches
  1. 8.4 vimeo_video_uploader.module \vimeo_video_uploader_node_presave()
  2. 8 vimeo_video_uploader.module \vimeo_video_uploader_node_presave()
  3. 7.2 vimeo_video_uploader.module \vimeo_video_uploader_node_presave()

Implements hook_node_presave().

File

./vimeo_video_uploader.module, line 49
Module file for the Vimeo Video Uploader.

Code

function vimeo_video_uploader_node_presave($node) {
  require_once 'vimeo-lib-api/vimeo.php';
  $consumer_key = variable_get('vimeo_api_key');
  $consumer_secret = variable_get('vimeo_api_secret');
  $oauth_access_token = variable_get('vimeo_oauth_access_token');
  $oauth_request_token_secret = variable_get('vimeo_oauth_request_token_secret');
  $vimeo_user_id = variable_get('vimeo_user_id');
  if ($node->type == variable_get('vimeo_content_type_select') && empty($node->nid)) {
    try {
      $file = file_load($node->field_vimeo_file_browse["und"][0]["fid"]);
      $vimeo = new phpVimeo($consumer_key, $consumer_secret);
      $vimeo
        ->setToken($oauth_access_token, $oauth_request_token_secret);
      $vimeo->user_id = $vimeo_user_id;
      $video_id = $vimeo
        ->upload(drupal_realpath($file->uri));
      if ($video_id) {
        $videotitle = $node->title;
        $videodesc = $node->body["und"][0]["value"];
        $vimeo
          ->call('vimeo.videos.setTitle', array(
          'title' => $videotitle,
          'video_id' => $video_id,
        ));
        $vimeo
          ->call('vimeo.videos.setDescription', array(
          'description' => $videodesc,
          'video_id' => $video_id,
        ));
        $videourl = 'http://vimeo.com/' . $video_id;
        $node->field_embeddedvideo["und"][0]["video_url"] = $videourl;
        $node->field_embeddedvideo["und"][0]["description"] = $videodesc;
        $video_info = $vimeo
          ->call('vimeo.videos.getInfo', array(
          'video_id' => $video_id,
        ));
        $node->field_embeddedvideo["und"][0]["thumbnail_path"] = $video_info->video[0]->thumbnails->thumbnail[0]->_content;
        file_delete($file);
      }
    } catch (Exception $e) {
      drupal_set_message(t('Not Authorized...! Please check your Auth information and login to your vimeo account.'), 'error');
      drupal_goto('node/add');
      drupal_exit();
    }
  }
  elseif ($node->type == variable_get('vimeo_content_type_select') && !empty($node->nid)) {
    $video_id_ = substr($node->original->field_embeddedvideo["und"][0]["video_url"], 17);
    try {
      $fid = $node->field_vimeo_file_browse["und"][0]["fid"];
      if ($node->field_vimeo_file_browse["und"][0]["fid"] != $node->original->field_vimeo_file_browse["und"][0]["fid"] && !empty($fid)) {
        $file = file_load($node->field_vimeo_file_browse["und"][0]["fid"]);
        $vimeo_ = new phpVimeo($consumer_key, $consumer_secret);
        $vimeo_
          ->setToken($oauth_access_token, $oauth_request_token_secret);
        $vimeo_->user_id = $vimeo_user_id;
        $vimeo_
          ->call('vimeo.videos.delete', array(
          'video_id' => $video_id_,
        ));
        unset($vimeo_);
        $vimeo = new phpVimeo($consumer_key, $consumer_secret);
        $vimeo
          ->setToken($oauth_access_token, $oauth_request_token_secret);
        $vimeo->user_id = $vimeo_user_id;
        $video_id = $vimeo
          ->upload(drupal_realpath($file->uri));
        if ($video_id) {
          $videotitle = $node->title;
          $videodesc = $node->body["und"][0]["value"];
          $vimeo
            ->call('vimeo.videos.setTitle', array(
            'title' => $videotitle,
            'video_id' => $video_id,
          ));
          $vimeo
            ->call('vimeo.videos.setDescription', array(
            'description' => $videodesc,
            'video_id' => $video_id,
          ));
          $videourl = 'http://vimeo.com/' . $video_id;
          $node->field_embeddedvideo["und"][0]["video_url"] = $videourl;
          $node->field_embeddedvideo["und"][0]["description"] = $videodesc;
          $video_info = $vimeo
            ->call('vimeo.videos.getInfo', array(
            'video_id' => $video_id,
          ));
          $node->field_embeddedvideo["und"][0]["thumbnail_path"] = $video_info->video[0]->thumbnails->thumbnail[0]->_content;
          file_delete($file);
        }
      }
    } catch (Exception $e) {
      drupal_set_message(t('Not Authorized...! Please check your Auth information and login to your vimeo account.'), 'error');
      drupal_goto('node/add');
      drupal_exit();
    }
  }
}