You are here

function filedepot_newversion_submit in filedepot 7

1 string reference to 'filedepot_newversion_submit'
filedepot_newversion_form in ./filedepot.module

File

./filedepot.module, line 2703
filedepot.module Filedepot: File Management Module developed by Nextide www.nextide.ca Full featured document managment module with a desktop application feel. Integrated Organic Group, Role and User permissions to secure folders, automated…

Code

function filedepot_newversion_submit($form, &$form_state) {
  global $user;
  module_load_include('php', 'filedepot', 'lib-common');
  if ($form_state['values']['filedepot_fid'] > 0) {
    $fid = $form_state['values']['filedepot_fid'];
    $filetags = $form_state['values']['filedepot_filetags'];

    // Retrieve the current file and folder details
    $query = db_select('filedepot_files', 'a');
    $query
      ->join('filedepot_categories', 'b', 'b.cid = a.cid');
    $query
      ->fields('a', array(
      'cid',
      'drupal_fid',
      'version',
      'fname',
    ));
    $query
      ->fields('b', array(
      'nid',
    ));
    $query
      ->condition('a.fid', $fid, '=');
    list($cid, $dfid, $current_version, $fname, $folder_nid) = array_values($query
      ->execute()
      ->fetchAssoc());
    $foldernode = node_load($folder_nid);
    $filedepot = filedepot_filedepot();
    $private_destination = 'private://filedepot/' . $foldernode->folder . '/';

    // Best to call file_prepare_directory() - even if you believe directory exists
    file_prepare_directory($private_destination, FILE_CREATE_DIRECTORY);
    if (isset($form_state['values']['filedepot_file']) and $form_state['values']['filedepot_file'] > 0) {
      $file = file_load($form_state['values']['filedepot_file']);
      list($scheme, $target) = explode('://', $file->uri, 2);

      // The new file should still be in the temporary location so we need to move it
      if ($scheme == 'temporary') {

        // Remove erroneous leading or trailing, forward-slashes and backslashes.
        $target = trim($target, '\\/');
        $private_uri = $private_destination . $file->filename;
        file_prepare_directory($private_destination, FILE_CREATE_DIRECTORY);
        $file = file_move($file, $private_uri, FILE_EXISTS_RENAME);

        //Set the status of the uploaded file.
        $file->status = FILE_STATUS_PERMANENT;
        file_save($file);

        // Get name of new file in case it was renamed
        list($scheme, $target) = explode('://', $file->uri, 2);
        $filename = str_replace("filedepot/{$foldernode->folder}/", '', $target);

        // Update the Drupal managed files table to point to the new version of this file
        // Need to cycle thru the attachments to get the right one
        foreach ($foldernode->filedepot_folder_file[LANGUAGE_NONE] as $delta => $original_file) {
          if ($original_file['fid'] == $dfid) {
            $foldernode->filedepot_folder_file[LANGUAGE_NONE][$delta]['fid'] = $file->fid;
            $foldernode->filedepot_folder_file[LANGUAGE_NONE][$delta]['uri'] = $file->uri;
            $foldernode->filedepot_folder_file[LANGUAGE_NONE][$delta]['filename'] = $file->filename;
            $foldernode->filedepot_folder_file[LANGUAGE_NONE][$delta]['filemime'] = $file->filemime;
            $foldernode->filedepot_folder_file[LANGUAGE_NONE][$delta]['filesize'] = $file->filesize;
            node_save($foldernode);
            break;
          }
        }

        // Update the file usage table
        file_usage_add($file, 'filedepot', 'node', $foldernode->nid);

        // Need to clear the cache as the node will still have the original file name
        field_cache_clear();
        if ($current_version < 1) {
          $current_version = 1;
        }
        $new_version = $current_version + 1;
        $sql = "INSERT INTO {filedepot_fileversions} (fid, drupal_fid, fname, version, notes, size, date, uid, status) " . "VALUES (:fid,:dfid,:fname,:version,:note,:size,:time,:uid,1)";
        db_query($sql, array(
          'fid' => $fid,
          'dfid' => $file->fid,
          'fname' => $filename,
          'version' => $new_version,
          'note' => check_plain($form_state['values']['filedepot_version_note']),
          'size' => $file->filesize,
          'time' => time(),
          'uid' => $user->uid,
        ));
        $sql = "UPDATE {filedepot_files} SET fname=:fname,version=:version,size=:size,date=:date,drupal_fid=:dfid WHERE fid=:fid";
        db_query($sql, array(
          'fname' => $filename,
          'version' => $new_version,
          'size' => $file->filesize,
          'date' => time(),
          'dfid' => $file->fid,
          'fid' => $fid,
        ));

        // Update tags for this file
        if (!empty($filetags) and $filedepot
          ->checkPermission($cid, 'view', 0, FALSE)) {
          $nexcloud = filedepot_nexcloud();
          $nexcloud
            ->update_tags($fid, $filetags);
        }

        // Send out email notifications of new file added to all users subscribed
        if ($form_state['values']['filedepot_email_notification']['yes'] === 'yes') {
          filedepot_sendNotification($fid);
        }
      }
      $form_state['redirect'] = array(
        "filedepot/folder/{$cid}",
      );
    }
  }
  else {
    drupal_set_message(t('Invalid upload folder selected'), 'error');
  }
}