You are here

function filedepot_native_submit in filedepot 7

1 string reference to 'filedepot_native_submit'
filedepot_form_alter in ./filedepot.module
Implementation of hook_form_alter().

File

./filedepot.module, line 879
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_native_submit($form, &$form_state) {
  global $user;

  // Retrieve the upload location - private folder
  $node = node_load($form_state['values']['nid']);
  $filedepot = filedepot_filedepot();
  module_load_include('php', 'filedepot', 'lib-common');
  $private_destination = 'private://filedepot/' . $node->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_folder_file'][LANGUAGE_NONE])) {

    /* Interate through all the files attached to the folder but only interested
     * in the files that have not yet been moved to the private file system area
     */
    foreach ($form_state['values']['filedepot_folder_file'][LANGUAGE_NONE] as $id => $filefield) {
      if (isset($filefield['fid']) and $filefield['fid'] > 0) {
        $file = file_load($filefield['fid']);
        list($scheme, $target) = explode('://', $file->uri, 2);

        // If the file is still public then 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);
          $ret = file_move($file, $private_uri, FILE_EXISTS_RENAME);
          drupal_set_message(t('Moving file to: @path', array(
            '@path' => "{$private_uri} - {$ret->uri}",
          )));

          //"moving file to: {$private_uri} - {$ret->uri}");
          $unapprovedfile = FALSE;
          $filedepot_fid = db_query("SELECT fid FROM {filedepot_files} WHERE drupal_fid=:fid", array(
            ':fid' => $file->fid,
          ))
            ->fetchField();
          if ($filedepot_fid === FALSE) {

            // If not, check if there is a record in the submissions table that has just not been approved
            $id = db_query("SELECT id FROM {filedepot_filesubmissions} WHERE drupal_fid=:fid", array(
              ':fid' => $file->fid,
            ))
              ->fetchField();
            if ($id > 0) {
              $unapprovedfile = TRUE;

              // Found a record for this file in the submission queue
            }
          }
          if ($unapprovedfile === FALSE and $filedepot_fid === FALSE) {

            // A new file was attached via the Drupal UI or external API - need to process
            $ext_parts = explode(".", $file->filename);
            $ext = end($ext_parts);
            file_usage_add($file, 'filedepot', 'node', $node->nid);

            // Create filedepot record for file and set status of file to 1 - online
            $query = db_insert('filedepot_files');
            $query
              ->fields(array(
              'cid',
              'fname',
              'title',
              'description',
              'version',
              'drupal_fid',
              'size',
              'mimetype',
              'extension',
              'submitter',
              'status',
              'date',
            ));
            $query
              ->values(array(
              'cid' => $node->folder,
              'fname' => $file->filename,
              'title' => $file->filename,
              'description' => 'TODO',
              'version' => 1,
              'drupal_fid' => $file->fid,
              'size' => $file->filesize,
              'mimetype' => $file->filemime,
              'extension' => $ext,
              'submitter' => $user->uid,
              'status' => 1,
              'date' => time(),
            ));
            $newfid = $query
              ->execute();
            db_query("INSERT INTO {filedepot_fileversions} (fid,fname,version,notes,size,date,uid,status,drupal_fid)\n            VALUES (:fid,:fname,'1','',:size,:time,:uid,1,:drupal_fid)", array(
              ':fid' => $newfid,
              ':fname' => $file->filename,
              ':size' => $file->filesize,
              ':time' => time(),
              ':uid' => $user->uid,
              ':drupal_fid' => $file->fid,
            ));

            // Update related folders last_modified_date
            $workspaceParentFolder = filedepot_getTopLevelParent($node->folder);
            filedepot_updateFolderLastModified($workspaceParentFolder);
          }
        }

        // Need to clear the cache as the node will still have the original file name
        field_cache_clear();
      }
    }
  }
}