You are here

tft_multiple_upload.module in Taxonomy File Tree 7.2

Hook implementations for TFT Opigno.

File

modules/tft_multiple_upload/tft_multiple_upload.module
View source
<?php

/**
 * @file
 * Hook implementations for TFT Opigno.
 */

/**
 * Implementation of hook_menu().
 */
function tft_multiple_upload_menu() {
  $menu = array(
    'node/multipleupload' => array(
      'title' => "Multiple Upload",
      'page callback' => 'tft_multiple_upload_get_form',
      'access arguments' => array(
        TFT_PERM__ACCESS_FULL_TREE,
      ),
      'type' => MENU_CALLBACK,
    ),
  );
  return $menu;
}
function tft_multiple_upload_get_form() {
  return drupal_get_form('tft_multiple_upload_tft_form');
}

/**
 * Implements tft_get_add_content_links_alter().
 */
function tft_multiple_upload_tft_get_add_content_links_alter(&$links, $tid) {
  $add_multiple_file_query = array(
    'destination' => $_SESSION['tft']['q'] . "#tft/{$tid}",
  );
  $setting = tft_get_file_setting();

  // Can the user create files ?
  if (user_access('create ' . $setting['type'] . ' content') && tft_term_access($tid, NULL, 'add-file')) {
    $link['add_multiple_files'] = array(
      'title' => t("Add Multiple files"),
      'href' => 'node/multipleupload/' . str_replace('_', '-', $setting['type']),
      'attributes' => array(
        'id' => 'add-multiple-child-files',
      ),
      'query' => array_reverse($add_multiple_file_query),
    );
    $links = array_merge($link, $links);
  }
}

/*
 * Multiple file upload form
 */
function tft_multiple_upload_tft_form($form, $form_state) {
  $file_field_instance = field_info_instance('node', 'tft_file', 'tft_file');
  $form['multiple_upload'] = array(
    '#type' => 'plupload',
    '#title' => t('Upload files'),
    '#description' => t('This multi-upload widget uses Plupload library.'),
    '#autoupload' => FALSE,
    '#autosubmit' => FALSE,
    '#submit_element' => '#edit-submit',
    '#upload_validators' => array(
      'file_validate_extensions' => array(
        $file_field_instance['settings']['file_extensions'],
      ),
    ),
    '#plupload_settings' => array(
      'runtimes' => 'html5',
      'chunk_size' => '1mb',
    ),
    '#event_callbacks' => array(),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Finish'),
  );
  $form['#submit'][] = 'tft_multiple_upload_files_submit';
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'tft') . '/css/tft.css',
  );
  return $form;
}

/*
 * Custom submit function
 */
function tft_multiple_upload_files_submit($form, &$form_state) {
  $file_field_instance = field_info_instance('node', 'tft_file', 'tft_file');
  $file_field = field_info_field('tft_file');
  $path = file_field_widget_uri($file_field, $file_field_instance, $data = array());
  $saved_files = array();

  //Array to capture file objects
  foreach ($form_state['values']['multiple_upload'] as $uploaded_file) {
    if ($uploaded_file['status'] == 'done') {
      $source = $uploaded_file['tmppath'];
      $destination = file_stream_wrapper_uri_normalize($path . $uploaded_file['name']);
      $destination = file_unmanaged_move($source, $destination, FILE_EXISTS_RENAME);
      $file = plupload_file_uri_to_object($destination);
      file_save($file);
      $saved_files[] = $file;
    }
    else {
      form_set_error('uploadfiles', "Upload of {$uploaded_file['name']} failed");
    }
  }
  $destination = drupal_get_query_parameters();
  $destination = $destination['destination'];
  $parts = explode("/", $destination);
  $termid = $parts[count($parts) - 1];
  foreach ($saved_files as $index => $saved_file) {
    tft_multiple_upload_create_node($saved_file, $termid);
  }
}

/*
 *  Tft file node creation
 */
function tft_multiple_upload_create_node($saved_file, $tid) {
  global $user;
  $node = new stdClass();
  $node->title = $saved_file->filename;
  $node->type = "tft_file";
  node_object_prepare($node);
  $node->language = LANGUAGE_NONE;
  $node->uid = $user->uid;
  $node->status = 1;
  $node->promote = 0;
  $node->comment = 0;
  if ($tid !== 0) {
    $node->tft_folder[LANGUAGE_NONE][]['tid'] = $tid;
  }
  $node->tft_file[LANGUAGE_NONE][] = array(
    'fid' => $saved_file->fid,
    'display' => 1,
    'description' => '',
  );
  node_save($node);
}