You are here

public function filedepot::approveFileSubmission in filedepot 7

Same name and namespace in other branches
  1. 6 filedepot.class.php \filedepot::approveFileSubmission()

File

./filedepot.class.php, line 1365
filedepot.class.php Main class for the Filedepot module

Class

filedepot
@file filedepot.class.php Main class for the Filedepot module

Code

public function approveFileSubmission($id) {
  $nexcloud = filedepot_nexcloud();
  $query = db_query("SELECT * FROM {filedepot_filesubmissions} WHERE id=:fid", array(
    'fid' => $id,
  ));
  $rec = $query
    ->fetchObject();
  $newfid = 0;

  // @TODO: Check if there have been multiple submission requests for the same file and thus have same new version #
  if ($rec->version == 1) {
    $private_destination = "private://filedepot/{$rec->cid}/";

    // Best to call file_prepare_directory() - even if you believe directory exists
    file_prepare_directory($private_destination, FILE_CREATE_DIRECTORY);
    $file = file_load($rec->drupal_fid);
    $private_uri = $private_destination . $rec->fname;
    $file = file_move($file, $private_uri, FILE_EXISTS_RENAME);

    // Get name of new file in case it was renamed after the file_move()
    list($scheme, $target) = explode('://', $file->uri, 2);
    $filename = str_replace("filedepot/{$rec->cid}/", '', $target);
    if (isset($rec->title) and !empty($rec->title)) {
      $filetitle = $rec->title;
    }
    else {
      $filetitle = $rec->fname;
    }

    // Load the node for the folder and then update the file usage table
    $nid = db_query("SELECT nid FROM {filedepot_categories} WHERE cid=:cid", array(
      ':cid' => $rec->cid,
    ))
      ->fetchField();
    $node = node_load($nid);
    file_usage_add($file, 'filedepot', 'node', $node->nid);

    // Remove the record for the core file module from the file usage table
    file_usage_delete($file, 'file');
    $query = db_insert('filedepot_files');
    $query
      ->fields(array(
      'cid',
      'fname',
      'title',
      'description',
      'version',
      'drupal_fid',
      'size',
      'mimetype',
      'submitter',
      'status',
      'date',
      'version_ctl',
      'extension',
    ));
    $query
      ->values(array(
      'cid' => $rec->cid,
      'fname' => $filename,
      'title' => $filetitle,
      'description' => $rec->description,
      'version' => $rec->version,
      'drupal_fid' => $file->fid,
      'size' => $file->filesize,
      'mimetype' => $file->filemime,
      'submitter' => $rec->submitter,
      'status' => 1,
      'date' => $rec->date,
      'version_ctl' => $rec->version_ctl,
      'extension' => $rec->extension,
    ));
    $query
      ->execute();

    // Get fileid for the new file record
    $newfid = db_query_range("SELECT fid FROM {filedepot_files} WHERE cid=:cid AND submitter=:uid ORDER BY fid DESC", 0, 1, array(
      ':cid' => $rec->cid,
      ':uid' => $rec->submitter,
    ))
      ->fetchField();
    $query = db_insert('filedepot_fileversions');
    $query
      ->fields(array(
      'fid',
      'fname',
      'drupal_fid',
      'version',
      'notes',
      'size',
      'date',
      'uid',
      'status',
    ));
    $query
      ->values(array(
      'fid' => $newfid,
      'fname' => $filename,
      'drupal_fid' => $file->fid,
      'version' => 1,
      'notes' => $rec->version_note,
      'size' => $file->filesize,
      'date' => time(),
      'uid' => $rec->submitter,
      'status' => 1,
    ));
    $query
      ->execute();
    if (!empty($rec->tags) and $this
      ->checkPermission($rec->cid, 'view', 0, FALSE)) {
      $nexcloud
        ->update_tags($newfid, $rec->tags);
    }
  }
  if ($newfid > 0) {
    if ($rec->notify == 1) {
      filedepot_sendNotification($newfid, FILEDEPOT_NOTIFY_APPROVED);
    }
    db_delete('filedepot_filesubmissions')
      ->condition('id', $id)
      ->execute();

    // Send out notifications of update to all subscribed users
    filedepot_sendNotification($newfid, FILEDEPOT_NOTIFY_NEWFILE);

    // Update related folders last_modified_date
    $workspaceParentFolder = filedepot_getTopLevelParent($rec->cid);
    filedepot_updateFolderLastModified($workspaceParentFolder);
    return TRUE;
  }
  else {
    return FALSE;
  }
}