You are here

vimeo_video_uploader.inc in Vimeo Video Uploader 7

Same filename and directory in other branches
  1. 7.2 vimeo_video_uploader.inc

Include file for the Vimeo Video Uploader.

File

vimeo_video_uploader.inc
View source
<?php

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

/**
 * Implements hook_form().
 * 
 * Function provides form for Vimeo Video Uploader Configuration page.
 */
function vimeo_video_uploader_admin_form($form, &$form_state) {
  $form['vimeo_auth'] = array(
    '#type' => 'fieldset',
    '#title' => t('Vimeo Video Upload Configuration'),
    '#description' => "Before Configuring, Visit (DRUPAL_ROOT/sites/all/modules/vimeo_video_uploader/README.txt) for instructions.",
  );
  $form['vimeo_auth']['user_id'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('vimeo_user_id'),
    '#title' => t('Enter Vimeo User Id'),
    '#required' => TRUE,
  );
  $form['vimeo_auth']['api_key'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('vimeo_api_key'),
    '#title' => t('Enter API Key (Client ID)'),
    '#required' => TRUE,
  );
  $form['vimeo_auth']['api_secret'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('vimeo_api_secret'),
    '#title' => t('Enter API Secret (Client Secret)'),
    '#required' => TRUE,
  );
  $form['vimeo_auth']['oauth_access_token'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('vimeo_oauth_access_token'),
    '#title' => t('Enter Oauth Access Token (Access token)'),
    '#required' => TRUE,
  );
  $form['vimeo_auth']['oauth_request_token_secret'] = array(
    '#type' => 'textfield',
    '#default_value' => variable_get('vimeo_oauth_request_token_secret'),
    '#title' => t('Enter Oauth Access Token Secret (Access token secret)'),
    '#required' => TRUE,
  );
  $contenttypes = array_keys(node_type_get_types());
  $contenttypes = node_type_get_types();
  $contenttypes_val = array(
    '' => "-Select-",
  );
  foreach ($contenttypes as $type) {
    $contenttypes_val[$type->type] = $type->name;
  }
  $form['vimeo_auth']['content_type_select'] = array(
    '#type' => 'select',
    '#title' => t('Select the Content Types from which you have to upload video to Vimeo'),
    '#options' => $contenttypes_val,
    '#default_value' => variable_get('vimeo_content_type_select'),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

/**
 * Implements hook_form_validate().
 */
function vimeo_video_uploader_admin_form_validate($form_id, $form_state) {
  if (file_exists('sites/all/libraries/vimeo-lib-api/vimeo.php')) {
    require_once 'sites/all/libraries/vimeo-lib-api/vimeo.php';
    $consumer_key = $form_state["input"]["api_key"];
    $consumer_secret = $form_state["input"]["api_secret"];
    $oauth_access_token = $form_state["input"]["oauth_access_token"];
    $oauth_request_token_secret = $form_state["input"]["oauth_request_token_secret"];
    $vimeo_user_id = $form_state["input"]["user_id"];
    $vimeo = new phpVimeo($consumer_key, $consumer_secret);
    $vimeo
      ->setToken($oauth_access_token, $oauth_request_token_secret);
    $vimeo->user_id = $vimeo_user_id;
    $result = $vimeo
      ->call('vimeo.test.echo', array(
      'AUTH' => 'SUCCESS',
    ));
    if (!($result->stat == "ok" && $result->AUTH[0] == "SUCCESS")) {
      drupal_set_message(t('Vimeo App was not authenticated successfully, please visit (DRUPAL_ROOT/sites/all/modules/vimeo_video_uploader/README.txt) for instructions..'), 'error');
      drupal_goto('admin/config/media/vimeo_video_uploader');
      drupal_exit();
    }
  }
  else {
    drupal_set_message(t('Vimeo library not added, please visit (DRUPAL_ROOT/sites/all/modules/vimeo_video_uploader/README.txt) for instructions..'), 'error');
    drupal_goto('admin/config/media/vimeo_video_uploader');
    drupal_exit();
  }
}

/**
 * Implements hook_form_submit().
 */
function vimeo_video_uploader_admin_form_submit($form, $form_state) {
  variable_set('vimeo_user_id', $form["vimeo_auth"]["user_id"]["#value"]);
  variable_set('vimeo_api_key', $form["vimeo_auth"]["api_key"]["#value"]);
  variable_set('vimeo_api_secret', $form["vimeo_auth"]["api_secret"]["#value"]);
  variable_set('vimeo_oauth_access_token', $form["vimeo_auth"]["oauth_access_token"]["#value"]);
  variable_set('vimeo_oauth_request_token_secret', $form["vimeo_auth"]["oauth_request_token_secret"]["#value"]);
  $message = t("Saved the Vimeo configuration.");
  $sel_con_type = $form["vimeo_auth"]["content_type_select"]["#value"];
  $exist_set_con_type = variable_get("vimeo_content_type_select");
  if ($sel_con_type != $exist_set_con_type) {
    field_delete_field("field_vimeo_file_browse");
    field_delete_field("field_embeddedvideo");
  }
  variable_set('vimeo_content_type_select', $sel_con_type);
  $node_type = node_type_load($sel_con_type);
  $instances = field_info_instances('node', $sel_con_type);
  $fields = array();
  foreach (array_keys($instances) as $field_instance) {
    $fields[$field_instance] = $field_instance;
  }
  if (!in_array("field_vimeo_file_browse", $fields)) {
    $field_name = 'field_vimeo_file_browse';
    $field = array(
      'field_name' => $field_name,
      'type' => 'file',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    );
    field_create_field($field);
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => 'node',
      'bundle' => $sel_con_type,
      'settings' => array(
        'file_extensions' => 'txt mp4',
      ),
      'display' => array(
        'default' => array(
          'label' => 'hidden',
          'type' => 'hidden',
        ),
      ),
      'label' => 'Browse video for uploading to Vimeo',
    );
    field_create_instance($instance);
    $message .= "Created 'Browse video for uploading to Vimeo' field in '" . strtoupper($sel_con_type) . "' Content type.";
  }
  if (!in_array("field_embeddedvideo", $fields)) {
    $field_name = 'field_embeddedvideo';
    $field = array(
      'field_name' => $field_name,
      'type' => 'video_embed_field',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    );
    field_create_field($field);
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => 'node',
      'bundle' => $sel_con_type,
    );
    field_create_instance($instance);
  }
  drupal_set_message(check_plain($message), 'status');
}

Functions

Namesort descending Description
vimeo_video_uploader_admin_form Implements hook_form().
vimeo_video_uploader_admin_form_submit Implements hook_form_submit().
vimeo_video_uploader_admin_form_validate Implements hook_form_validate().