diff -u -N filefield.inc filefield.inc --- filefield.inc 1970-01-01 01:00:00.000000000 +0100 +++ filefield.inc 2007-04-11 09:46:28.000000000 +0200 @@ -0,0 +1,178 @@ +nid, $file['filename'], $file['filepath'], + $file['filemime'], $file['filesize']); + db_query('INSERT into {file_revisions} (fid, vid, description, list) + VALUES (%d, %d, "%s", %d)', + $file['fid'], $node->vid, $file['filename'], 0); + module_invoke_all('filefield', 'file_save', $node, $field, $file); + return (array)$file; + } + else { + // Include file name in upload error. + form_set_error(NULL, t('File upload was unsuccessful.')); + return FALSE; + } +} + +/** + * update the file record if necessary + * @param $node + * @param $file + * @param $field + */ +function filefield_file_update($node, $field, &$file) { + $file = (array)$file; + if ($file['remove'] == TRUE) { + module_invoke_all('filefield', 'file_delete', $node, $field, $file); + filefield_file_delete($node, $field, $file); + // should I return an array here instead as imagefield does, or is that a bug in + // in imagefield. I remember I was working on a content.module patch that would + // delete multivalue fields whose value was NULL. Maybe a leftover. + return NULL; + } + if ($file['fid'] == 'upload') { + return filefield_file_insert($node, $field, $file); + } + else { + // if fid is not numeric here we should complain. + // else we update the file table. + } + $result = db_result(db_query('SELECT COUNT(*) FROM {file_revisions} WHERE fid = %d', $fid)); + return $file; +} + +function filefield_file_delete($node, $field, $file) { + if (is_numeric($file['fid'])) { + db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']); + db_query('DELETE FROM {file_revisions} WHERE fid = %d', $file['fid']); + } + else { + unset($_SESSION['filefield'][$field['field_name']][$file['sessionid']]); + } + module_invoke_all('filefield', 'delete', $node, $field, $file); + return file_delete($file['filepath']); +} + +function filefield_file_load($fid = NULL) { + if (isset($fid)) { + if (is_numeric($fid)) { + $result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid); + $file = db_fetch_array($result); + if ($file) { + // let modules load extended attributes. + $file += module_invoke_all('filefield', 'file_load', $node, $field, $file); + return $file; + } + } + } + // return an empty array if nothing was found. + return array(); +} + +if (!function_exists('upload_file_download')) { + function filefield_file_download($file) { + $file = file_create_path($file); + $result = db_query("SELECT * FROM {files} WHERE filepath = '%s'", $file); + if ($file = db_fetch_object($result)) { + if (user_access('view uploaded files')) { + $node = node_load($file->nid); + if (node_access('view', $node)) { + $name = mime_header_encode($file->filename); + $type = mime_header_encode($file->filemime); + // Serve images and text inline for the browser to display rather than download. + $disposition = ereg('^(text/|image/)', $file->filemime) ? 'inline' : 'attachment'; + return array( + 'Content-Type: '. $type .'; name='. $name, + 'Content-Length: '. $file->filesize, + 'Content-Disposition: '. $disposition .'; filename='. $name, + 'Cache-Control: private', + ); + } + else { + return -1; + } + } + else { + return -1; + } + } + } +} + + + +/** + * Call this function from hook_form_alter() in order to keep the upload module + * from showing uploaded files in its own "Uploaded files" fieldset. + * + * @param $form_id The form id, as passed to hook_form_alter(). + * @param $form The form array, as passed to hook_form_alter(). + * @param $field_type + * The type of the CCK field, as specified in hook_field_info() + * (e.g. 'file', 'image', 'audio' or 'video'). + */ +function filefield_hide_upload_files($form_id, &$form, $field_type) { + if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) { + $node = $form['#node']; + + if (!empty($node->files)) { + $types = content_types($node->type); + if (!empty($types['fields'])) { + foreach ($types['fields'] as $field_info) { + if ($field_info['type'] !== $field_type) { + continue; + } + if (!empty($node->$field_info['field_name'])) { + foreach ($node->$field_info['field_name'] as $field) { + unset($form['attachments']['wrapper']['files'][$field['fid']]); + } + } + } + + $files_left = FALSE; + if (!empty($form['attachments']['wrapper']['files'])) { + foreach ($form['attachments']['wrapper']['files'] as $key => $value) { + if (is_string($key) && $key[0] == '#') { + continue; + } + $files_left = TRUE; + break; + } + } + if (!$files_left) { + unset($form['attachments']['wrapper']['files']); + $form['attachments']['#collapsed'] = TRUE; + } + } + } + } +} diff -u -N filefield.module filefield.module --- filefield.module 2007-03-30 05:20:24.000000000 +0200 +++ filefield.module 2007-04-11 09:47:57.000000000 +0200 @@ -8,6 +8,7 @@ * uses content.module to store the fid, and the drupal files * table to store the actual file data. */ +include_once(drupal_get_path('module', 'filefield') .'/filefield.inc'); function filefield_menu($maycache) { @@ -83,70 +84,6 @@ } } -/** - * insert a file into the database. - * @param $node - * node object file will be associated with. - * @param $file - * file to be inserted, passed by reference since fid should be attached. - * - */ -function filefield_file_insert($node, $field, &$file) { - $fieldname = $field['field_name']; - - // allow tokenized paths. - if (function_exists('token_replace')) { - global $user; - $widget_file_path = token_replace($field['widget']['file_path'], 'user', $user); - } - else { - $widget_file_path = $field['widget']['file_path']; - } - - $filepath = file_create_path($widget_file_path) . '/' . $file['filename']; - - if (filefield_check_directory($widget_file_path) && $file = file_save_upload((object)$file, $filepath)) { - $file = (array)$file; - $file['fid'] = db_next_id('{files}_fid'); - db_query('INSERT into {files} (fid, nid, filename, filepath, filemime, filesize) - VALUES (%d, %d, "%s","%s","%s",%d)', - $file['fid'], $node->nid, $file['filename'], $file['filepath'], $file['filemime'], $file['filesize']); - module_invoke_all('filefield', 'file_save', $node, $field, $file); - return (array)$file; - } - else { - // Include file name in upload error. - form_set_error(NULL, t('file upload was unsuccessful.')); - return FALSE; - } -} - - -/** - * update the file record if necessary - * @param $node - * @param $file - * @param $field - */ -function filefield_file_update($node, $field, &$file) { - $file = (array)$file; - if ($file['remove'] == TRUE) { - module_invoke_all('filefield', 'file_delete', $node, $field, $file); - _filefield_file_delete($node, $field, $file); - // should I return an array here instead as imagefield does, or is that a bug in - // in imagefield. I remember I was working on a content.module patch that would - // delete multivalue fields whose value was NULL. Maybe a leftover. - return NULL; - } - if ($file['fid'] == 'upload') { - return filefield_file_insert($node, $field, $file); - } - else { - // if fid is not numeric here we should complain. - // else we update the file table. - } - return $file; -} /** * Implementation of hook_field(). @@ -160,7 +97,7 @@ $values = array(); foreach ($node_field as $delta => $file) { if (!empty($file)) { - $node_field[$delta] += _filefield_file_load($file['fid']); + $node_field[$delta] += filefield_file_load($file['fid']); } $output = array($fieldname => $node_field); } @@ -183,7 +120,7 @@ case 'delete': foreach ($node_field as $delta => $item) { - _filefield_file_delete($node, $field, $item); + filefield_file_delete($node, $field, $item); } break; } @@ -258,17 +195,6 @@ } } -function _filefield_file_delete($node, $field, $file) { - if (is_numeric($file['fid'])) { - db_query('DELETE FROM {files} WHERE fid = %d', $file['fid']); - } - else { - unset($_SESSION['filefield'][$field['field_name']][$file['sessionid']]); - } - module_invoke_all('filefield', 'delete', $node, $field, $file); - return file_delete($file['filepath']); -} - /** * Implementation of hook_widget(). */ @@ -479,15 +405,24 @@ /** + * Implementation of hook_form_alter(): + * Remove the file entries from the upload module's "Uploaded files" fieldset. + */ +function filefield_form_alter($form_id, &$form) { + filefield_hide_upload_files($form_id, $form, 'file'); +} + + +/** * Implementation of hook_field formatter. * @todo: finish transformer.module and integrate like imagecache with imagefield. */ function filefield_field_formatter_info() { $formatters = array( - 'default' => array( - 'label' => t('Default'), - 'field types' => array('file'), - ), + 'default' => array( + 'label' => t('Default'), + 'field types' => array('file'), + ), ); return $formatters; } @@ -496,26 +431,10 @@ if(!isset($item['fid'])) { return ''; } - $file = _filefield_file_load($item['fid']); + $file = filefield_file_load($item['fid']); return theme('filefield', $file, $item); } -function _filefield_file_load($fid = NULL) { - if (isset($fid)) { - if (is_numeric($fid)) { - $result = db_query('SELECT * FROM {files} WHERE fid = %d', $fid); - $file = db_fetch_array($result); - if ($file) { - // let modules load extended attributes. - $file += module_invoke_all('filefield', 'file_load', $node, $field, $file); - return $file; - } - } - } - // return an empty array if nothing was found. - return array(); -} - function theme_filefield_icon($file) { $ext = array_pop(explode('.',$file['filename'])); $known_extensions = array('0','ace','aif','ai','ani','asf','asp','avi','bak','bat','bin','bmp','bz2','bz','cab','cdr','cfg','com','conf','cpt','css','cur','dat','db','dcr','dic','diff','dir','dll','dmg','doc','dwg','edir','eml','eps','exe','fla','flv','fon','gif','gz','hqx','html','htm','ico','inc','ini','iso','jpeg','jpg','js','lnk','log','m3u','mdb','midi','mid','mov','mp3','mpeg','mpg','nfo','odb','odc','odf','odg','odm','odp','ods','odt','ogg','otg','oth','otp','ots','ott','patch','pdf','php3','php','phtml','pl','png','pps','ppt','psd','pwl','qt','ram','ra','rar','reg','rpm','rtf','sh','shtml','sit','sql','svg','swf','sxc','sxi','sxw','sys','tar','tgz','tiff','tif','tmp','tpl','ttf','txt','wav','wma','wmv','wp','xls','xml','zip'); @@ -548,37 +467,6 @@ } } -if (!function_exists('upload_file_download')) { - function filefield_file_download($file) { - $file = file_create_path($file); - $result = db_query("SELECT f.* FROM {files} f WHERE filepath = '%s'", $file); - if ($file = db_fetch_object($result)) { - if (user_access('view uploaded files')) { - $node = node_load($file->nid); - if (node_access('view', $node)) { - $name = mime_header_encode($file->filename); - $type = mime_header_encode($file->filemime); - // Serve images and text inline for the browser to display rather than download. - $disposition = ereg('^(text/|image/)', $file->filemime) ? 'inline' : 'attachment'; - return array( - 'Content-Type: '. $type .'; name='. $name, - 'Content-Length: '. $file->filesize, - 'Content-Disposition: '. $disposition .'; filename='. $name, - 'Cache-Control: private' - ); - } - else { - return -1; - } - } - else { - return -1; - } - } - } - -} - /** * Wrapper function for filefield_check_directory that accepts a form element * to validate - if user specified one. Won't allow form submit unless the