function filedepot_nodeapi in filedepot 6
Implements hook_nodeapi().
File
- ./
filedepot.module, line 816 - filedepot.module Filedepot: File Management Module developed by Nextide www.nextide.ca Full featured document managment module with a desktop application feel. Integrated role and user permissions to secure folders, automated notifications, Tag Cloud…
Code
function filedepot_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
global $conf, $user;
$filedepot = filedepot_filedepot();
module_load_include('php', 'filedepot', 'lib-common');
if ($op == 'insert' || $op == 'update') {
if (is_object($node)) {
$newfiles = array();
$updatefiles = array();
$depotfiles = array();
/* Determine how many files Filedepot has record for in this folder
* Build an array so that we can determine if anyone removes a file via the Drupal UI or external to filedepot
*/
$cid = db_result(db_query("SELECT cid FROM {filedepot_categories} WHERE nid=%d", $node->nid));
if ($cid > 0) {
$nexquery = db_query("SELECT fid FROM {filedepot_files} WHERE cid=%d", $cid);
while ($A = db_fetch_array($nexquery)) {
$depotfiles[$A['fid']] = 1;
// Array of existing folder files
}
$system_directory_path = $conf['file_directory_path'];
$real_system_directory_path = realpath($system_directory_path);
$filedepot_private_directory_path = $filedepot->root_storage_path . $cid;
// The $file['realname'] element is added in the filedepot class when saving a new file before calling the node_save API.
if (is_array($node->field_filedepot_file) and count($node->field_filedepot_file) > 0) {
foreach ($node->field_filedepot_file as $file) {
if (file_exists($file['filepath']) and isset($file['realname'])) {
$file['realpath'] = realpath($file['filepath']);
// If within the private path, ignore the file
if (strpos($file['realpath'], $filedepot_private_directory_path) !== 0) {
if ($file['moderated'] and !empty($file['moderated_tmpname'])) {
$file['dest'] = rtrim($filedepot_private_directory_path, '\\/') . '/submissions/' . $file['moderated_tmpname'];
}
elseif ($file['incoming'] and !empty($file['incoming_tmpname'])) {
$file['dest'] = rtrim($filedepot_private_directory_path, '\\/') . '/' . $file['realname'];
}
else {
$file['dest'] = rtrim($filedepot_private_directory_path, '\\/') . '/' . $file['realname'];
}
$newfiles[$file['fid']] = $file;
}
}
elseif (is_array($file)) {
/* file['realname'] would only be set for new files (attachments) being added to the node_save ('update')
* Someone is editing the folder and or files via the drupal interface
* Need to process as the file description may have changed
*/
// Check for allowable file type.
if (!$filedepot
->checkFilter($file['filename'], $file['filemime'])) {
// Error - TODO Find a better way
$delete = file_delete($file['filepath']);
db_query("DELETE FROM {files} WHERE filepath = '%s'", $file['filepath']);
$message = t('The file %name could not be uploaded. Mimetype %mimetype or extension not permitted.', array(
'%name' => $file['filename'],
'%mimetype' => $file['filemime'],
));
drupal_set_message($message, 'error');
watchdog('filedepot', 'The file %name could not be uploaded. Mimetype %mimetype or extension not permitted.', array(
'%name' => $file['filename'],
'%mimetype' => $file['filemime'],
));
return TRUE;
}
$unapprovedfile = FALSE;
$filedepot_fid = db_result(db_query("SELECT fid FROM {filedepot_files} WHERE cckfid=%d", $file['fid']));
if ($filedepot_fid === FALSE) {
// If not, check if there is a record in the submissions table that has just not been approved
$id = db_result(db_query("SELECT id FROM {filedepot_filesubmissions} WHERE cckfid=%d", $file['fid']));
if ($id > 0) {
$unapprovedfile = TRUE;
}
// Found a record for this file in the submission queue
}
if ($unapprovedfile === FALSE and $filedepot_fid === FALSE and is_array($file)) {
// A new file was attached via the Drupal UI or external API - need to process
$file['dest'] = rtrim($filedepot_private_directory_path, '\\/') . '/' . $file['filename'];
$newfiles[$file['fid']] = $file;
$ext = end(explode(".", $file['filename']));
// Create filedepot record for file and set status of file to 1 - online
$sql = "INSERT INTO {filedepot_files} (cid,fname,title,description,version,cckfid,size,mimetype,extension,submitter,status,date) " . "VALUES (%d,'%s','%s','%s',1,%d,%d,'%s','%s',%d,1,%d)";
db_query($sql, $cid, $file['filename'], $file['filename'], $file['data']['description'], $file['fid'], $file['filesize'], $file['filemime'], $ext, $user->uid, time());
// Get fileid for the new file record
$args = array(
$cid,
$user->uid,
);
$fid = db_result(db_query_range("SELECT fid FROM {filedepot_files} WHERE cid=%d AND submitter=%d ORDER BY fid DESC", $args, 0, 1));
$newfiles[$file['fid']]['nexfid'] = $fid;
db_query("INSERT INTO {filedepot_fileversions} (fid,fname,version,notes,size,date,uid,status)\n VALUES (%d,'%s','1','',%d,%d,%d,1)", $fid, $file['filename'], $file['filesize'], time(), $user->uid);
// Update related folders last_modified_date
$workspaceParentFolder = filedepot_getTopLevelParent($cid);
filedepot_updateFolderLastModified($workspaceParentFolder);
}
elseif ($filedepot_fid > 0 and $unapprovedfile === FALSE) {
$updatefiles[$filedepot_fid] = $file;
}
}
}
}
foreach ($newfiles as $file) {
$conf['file_directory_path'] = $filedepot_private_directory_path;
$src = $file['filepath'];
// After a successful file_move, $src will be the set to the new filename including path
// In case of a duplicate file in the destination directory,
// the variable $src will be updated with the resulting appended incremental number
// Refer to the drupal file_move API
if (file_move($src, $file['dest'], FILE_EXISTS_RENAME)) {
// update db with the filename and full name including directory after the successful move
db_query("UPDATE {files} SET filename = '%s', filepath = '%s' WHERE fid = %d", basename($src), $src, $file['fid']);
// Only need to do this if file is being added from the Drupal UI
if (isset($file['nexfid']) and $file['nexfid'] > 0) {
db_query("UPDATE {filedepot_files} SET fname = '%s' WHERE fid = %d", basename($src), $file['nexfid']);
}
}
else {
// Error - TODO Find a better way
$delete = file_delete($file['filepath']);
db_query("DELETE FROM {files} WHERE filepath = '%s'", $file['filepath']);
if ($delete) {
drupal_set_message(t('Error moving the file from the public to private file directories. The file was deleted as a pre-caution from the public error. The node file details should be updated by removing the file reference.'), 'error');
}
else {
drupal_set_message(t('Error moving the file from the public to private file directories. This file could not be deleted from the public file area and should be removed manually. Please report this to your system administrator. Filepath: "%filepath".', array(
'%filepath' => $file['filepath'],
)), 'error');
}
}
$conf['file_directory_path'] = $system_directory_path;
}
foreach ($updatefiles as $id => $file) {
/*
* May 11/2010 (Blaine): Commented this update out as there are reports of files having descriptions cleared.
* Not sure right now what condition this update was required for as it appears to work just fine without it.
*/
//db_query("UPDATE {filedepot_files} SET description = '%s' WHERE fid=%d", $file['data']['description'], $id);
unset($depotfiles[$id]);
}
// Remove any files that filedepot still has on record but where not in the updated files
// A file was removed using the Drupal UI or external API
foreach ($depotfiles as $id => $remove) {
$filedepot
->deleteFile($id);
}
}
}
}
elseif ($op == 'delete') {
// Called when folder node is deleted
$filedepot
->deleteFolder($node->nid);
}
}