You are here

vimeo_video_uploader.module in Vimeo Video Uploader 7

Module file for the Vimeo Video Uploader.

File

vimeo_video_uploader.module
View source
<?php

/**
 * @file
 * Module file for the Vimeo Video Uploader.
 */

/**
 * Implements hook_help().
 */
function vimeo_video_uploader_help($path, $arg) {
  switch ($path) {
    case 'admin/help#vimeo_video_uploader':
      $output = '<p>' . t('Visit (DRUPAL_ROOT/sites/all/modules/vimeo_video_uploader/README.txt) for instructions.') . '</p>';
      return $output;
  }
}

/**
 * Implements hook_permission().
 */
function vimeo_video_uploader_permission() {
  return array(
    'access vimeo uploader' => array(
      'title' => t('Administer Vimeo Video Uploader'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function vimeo_video_uploader_menu() {
  $items = array();
  $items['admin/config/media/vimeo_video_uploader'] = array(
    'title' => 'Vimeo Auth Configuration',
    'description' => 'vimeo authorization',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'vimeo_video_uploader_admin_form',
    ),
    'access arguments' => array(
      'access vimeo uploader',
    ),
    'file' => 'vimeo_video_uploader.inc',
  );
  return $items;
}

/**
 * Implements hook_node_presave().
 */
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();
    }
  }
}

/**
 * Implements hook_node_delete($node).
 */
function vimeo_video_uploader_node_delete($node) {
  $video_id_val = substr($node->field_embeddedvideo["und"][0]["video_url"], 17);
  if (!empty($video_id_val)) {
    if ($node->type == variable_get('vimeo_content_type_select') && !empty($node->nid)) {
      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');
      $video_id_ = substr($node->field_embeddedvideo["und"][0]["video_url"], 17);
      $vimeo_ = new phpVimeo($consumer_key, $consumer_secret);
      $vimeo_
        ->setToken($oauth_access_token, $oauth_request_token_secret);
      $vimeo_->user_id = $vimeo_user_id;
      try {
        $vimeo_
          ->call('vimeo.videos.delete', array(
          'video_id' => $video_id_,
        ));
        unset($vimeo_);
      } catch (Exception $e) {
        drupal_goto('admin/content');
        drupal_exit();
      }
    }
  }
}

/**
 * Implements hook_form_alter().
 */
function vimeo_video_uploader_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == variable_get('vimeo_content_type_select') . "_node_form") {
    $form["field_embeddedvideo"]["und"][0]["video_url"]["#default_value"] = "https://vimeo.com/1214";
    $css = "#edit-field-embeddedvideo, #edit-field-vimeo-file-browse #edit-field-vimeo-file-browse-und-0-upload-button { display: none; }";
    drupal_add_css($css, 'inline');
    $form["#validate"][] = "vimeo_video_uploader_form_validate";
  }
}

/**
 * Implements hook_form_validate().
 */
function vimeo_video_uploader_form_validate($form_id, $form_state) {
  if ($form_state["values"]["field_vimeo_file_browse"]["und"][0]["fid"] == 0) {
    $form_state["input"]["field_embeddedvideo"]["und"] = array();
    $form_state["values"]["field_embeddedvideo"]["und"] = array();
    $form_state["complete form"]["field_embeddedvideo"]["und"][0]["video_url"]["#default_value"] = array();
    $form_state["complete form"]["field_embeddedvideo"]["und"][0]["video_url"]["#value"] = array();
    unset($form_state["input"]["field_embeddedvideo"]);
    unset($form_state["values"]["field_embeddedvideo"]);
    unset($form_state["complete form"]["field_embeddedvideo"]);
    unset($form_state["field"]["field_embeddedvideo"]);
  }
}