Not sure best way to handle. Here is prototype that works at least (a little ugly maybe hehe):
Anyway is it alright submit as <code>, versus a patch file as I'm working on slightly older version and thought I'd just paste each specific portion to inspect?
//webform.module
$items['node/%webform_menu/%webform_menu_submission/%/%/delete_file']= array(
'title' => t('Delete Webform File'),
'load arguments' => array(1),
'page callback' => 'drupal_get_form',
'page arguments' => array('_webform_delete_file_confirm', 1, 2, 3, 4), // node, question id, file id args (node id should be loaded automatically, calling _load)
'access callback' => 'webform_submission_access',
'access arguments' => array(1, 2, 'edit'), // make sure if going to this link that user is allowed to update the webform if he is allowed to update it
'file' => 'file.inc',
'file path' => drupal_get_path('module', 'webform') .'/components/',
'type' => MENU_CALLBACK,
);
//file.inc
/* in _webform_submission_display_file change suffix to add a delete link
$suffix_link .= ' <a href="/node/'. $form_item['#webform_component']['nid'] .'/'. $sid .'/'. $form_item['#webform_component']['cid'] .'/'. $filedata['fid'] .'/delete_file">Delete '. htmlspecialchars($filedata['filename']) .'</a>';
*/
function _webform_delete_file_confirm(&$form_state, $node, $submission, $cid, $fid) {
$nid = $node->nid;
$sid = $submission->sid;
$form['nid'] = array('#type' => 'value', '#value' => $nid);
$form['sid'] = array('#type' => 'value', '#value' => $sid);
$form['cid'] = array('#type' => 'value', '#value' => $cid);
$form['fid'] = array('#type' => 'value', '#value' => $fid);
$form['#submit'][] = '_webform_delete_file_confirm_submit';
//drupal_set_message($nid.'-'.$sid.'-'.$cid.'-'.$fid);
$result = db_query("SELECT filename,filepath FROM {files} WHERE fid = %d", $fid);
$fileobject = db_fetch_object($result);
$filename = $fileobject->filename;
return confirm_form($form,
t('Are you sure you want to delete the file') . (!empty($filename) ? (' '. $filename ) : '' ) .'?',
('node/'. $nid .'/'. intval($sid)),
t('This action cannot be undone.'),
t('Delete'), t('Cancel'));
}
/**
* Deletes the file from system/webform
*/
function _webform_delete_file_confirm_submit($form, &$form_state) { //since changed _webform_delete_file to use fid, maybe just call that instead?
$nid = $form_state['values']['nid'];
$sid = $form_state['values']['sid'];
$cid = $form_state['values']['cid'];
$fid = $form_state['values']['fid'];
$result = db_query("SELECT filename,filepath FROM {files} WHERE fid = %d", $fid);
// delete the file
if ($result && ($fileobject = db_fetch_object($result)) ) {
// delete file from drupal database and file itself
db_query("DELETE FROM {files} WHERE fid = %d", $fid);
$filename = $fileobject->filename;
$filepath = $fileobject->filepath; //relative or full filepath depending on public/private filesystem
// delete data from webform_submission_data, possible case if file already deleted or not in database, so just get rid now from form data
_webform_delete_submitted_file_data($nid, $sid, $cid, $filename );
if (isset($filepath) && is_file($filepath)) {
unlink($filepath);
}
else {
drupal_set_message("File '". $filename ."' could not be deleted since it was not found. Contact Administrator", "error");
watchdog(WATCHDOG_WARNING, "File '". $filename ."' could not be deleted since it was not found. Contact Administrator");
}
drupal_goto('node/'. $nid .'/submission/'. intval($sid) .'/edit' ); // probably want to redirect to the edit submission page
}
else { //Edge case: if file still there just not in database for some reason
_webform_delete_submitted_file_data($nid, $sid, $cid, $filename );
// unlink() would be nice to delete the file also but... Don't unlink as, if uploaded a file called file.doc, and someone deleted off server. someone else (or same user for different question) could have uploaded resume.doc and hence it will have same filename as the one you want to delete
drupal_set_message("File database information could not be deleted for file id: '". $fid .".", "error");
watchdog(WATCHDOG_WARNING, "File database information not found for file id: ". $fid .".");
drupal_goto('node/'. $nid .'/submission/'. intval($sid) .'/edit' );
}
}
Comments
Comment #1
quicksketchThis sounds like a good feature request to me, though I think the approach is a little awkward. Could you make it possible to remove a file by placing a checkbox next to currently uploaded files? Then the user wouldn't have to leave the page while editing a submission to remove the file.
Comment #2
armyofda12mnkeys commentedYeh, I can look into it (I was using above approach cause I wanted standalone delete confirmation just to be sure user wants to delete that specific file, as I've seen both methods used in Drupal modules for deleting).
Wonder if an Ajax-popup delete could also be good so user doesnt leave page, (but I am not familiar with AJAX in drupal yet, something for me to research).
Do you prefer checkbox versus ajax popup?
Comment #3
quicksketchThis feature will probably never come to the D6 version of Webform. Let's focus on #289919: Provide AJAX-based file uploads/Progress Bar for uploads, which would add a remove button through D7's managed_file element.