array( 'label' => t('Delete unused'), 'callback' => '', 'callback arguments' => array(), ), 'delete_hard' => array( 'label' => t('Delete ignoring usage'), 'callback' => '', 'callback arguments' => array(), ), ); return $operations; } /** * Title callback. */ function file_page_title($file) { return $file->filename; } /** * Menu callback; view a single file. */ function file_page_view($file) { drupal_set_title($file->filename); // Figure out who's using the file. $references = array_keys(module_invoke_all('file_references', $file)); $rows = array( array( array('data' => t('File Id'), 'header' => TRUE), t($file->fid), ), array( array('data' => t('User'), 'header' => TRUE), theme('username', user_load(array('uid' => $file->uid))), ), array( array('data' => t('Path'), 'header' => TRUE), l($file->filepath, file_create_url($file->filepath)), ), array( array('data' => t('MIME Type'), 'header' => TRUE), check_plain($file->filemime), ), array( array('data' => t('Status'), 'header' => TRUE), ($file->status & FILE_STATUS_PERMANENT == FILE_STATUS_PERMANENT) ? t('Permanent') : t('Temporary'), ), array( array('data' => t('Size'), 'header' => TRUE), format_size($file->filesize), ), array( array('data' => t('Last Modified'), 'header' => TRUE), format_date($file->timestamp, 'large'), ), array( array('data' => t('Modules'), 'header' => TRUE), count($references) ? implode(', ', $references) : t('Unused'), ), ); $attributes = array('class' => 'file-view'); return theme('table', array(), $rows, $attributes); } /** * Menu callback : Page where admins manage uploaded files. */ function file_admin_form($form_state) { $form = array(); // Special case for deletion. if (isset($form_state['storage'])) { switch ($form_state['storage']['operation']) { case 'delete': return file_admin_multiple_delete_confirm($form_state, $form_state['storage']['files']); } return; } $options = array(); foreach (module_invoke_all('file_operations') as $operation => $array) { $options[$operation] = $array['label']; } $form['options'] = array( '#type' => 'fieldset', '#title' => t('Update options'), '#prefix' => '
', '#suffix' => '
', ); $form['options']['operation'] = array( '#type' => 'select', '#options' => $options, ); $form['options']['op'] = array( '#value' => t('Update'), '#type' => 'submit', ); $headers = array( theme('table_select_header_cell'), array('data' => t('File name'), 'field' => 'f.filename'), array('data' => t('Type'), 'field' => 'f.filemime', 'sort' => 'asc'), array('data' => t('Owner'), 'field' => 'u.name', 'sort' => 'asc'), array('data' => t('Modified'), 'field' => 'f.timestamp', 'sort' => 'asc'), array('data' => t('Size'), 'field' => 'f.filesize'), ); $form['header'] = array( '#type' => 'value', '#value' => $headers, ); $sql = 'SELECT f.*, u.name FROM {files} f INNER JOIN {users} u ON f.uid = u.uid ' . tablesort_sql($headers); $result = pager_query($sql, variable_get('files_per_page', 50)); $files = array(); foreach ($result as $file) { $files[$file->fid] = $file; } $form['operations'] = array( '#type' => 'value', '#value' => $operations, ); $form['files'] = array( '#type' => 'value', '#value' => $files, ); $form['checks'] = array( '#type' => 'checkboxes', '#options' => array_combine(array_keys($files), array_keys($files)), ); return $form; } function theme_file_admin_form($form) { $output = ''; $rows = array(); foreach ($form['files']['#value'] as $fid => $file) { unset($form['checks'][$fid]['#title']); $rows[] = array( drupal_render($form['checks'][$fid]), l($file->filename, 'admin/content/file/'. $file->fid), check_plain($file->filemime), theme('username', $file), format_date($file->timestamp), format_size($file->filesize), ); } if (empty($rows)) { $output .= '

' . t('No files available.') . '

'; } else { $output .= drupal_render($form['options']); $output .= theme('table', $form['header']['#value'], $rows); $output .= theme('pager', NULL, variable_get('files_per_page', 50)); } $output .= drupal_render($form); return $output; } function file_admin_form_submit($form, &$form_state) { dvm($form_state); $operations = module_invoke_all('file_operations'); $operation = $operations[$form_state['values']['operation']]; // Build an array of the selected files. $files = array(); foreach (array_filter($form_state['values']['checks']) as $fid) { $files[$fid] = $form_state['values']['files'][$fid]; } if ($function = $operation['callback']) { // Add in callback arguments if present. if (isset($operation['callback arguments'])) { $args = array_merge(array($files), $operation['callback arguments']); } else { $args = array($files); } call_user_func_array($function, $args); cache_clear_all(); } else { // We need to rebuild the form to go to a second step. For example, to // show the confirmation form for the deletion of nodes. $form_state['rebuild'] = TRUE; $form_state['storage']['operation'] = $form_state['values']['operation']; $form_state['storage']['files'] = $files; } } function file_admin_multiple_delete_confirm(&$form_state, $files) { $form['#submit'][] = 'file_admin_multiple_delete_confirm_submit'; $filenames = array(); foreach ($files as $file) { $filenames[] = $file->filename; } $form['files'] = array('#type' => 'value', '#value' => $files); $form['filenames'] = array('#markup' => theme('item_list', $filenames)); return confirm_form($form, t('Are you sure you wish to delete these files?'), 'admin/content/file', NULL, t('Delete')); } function file_admin_multiple_delete_confirm_submit($form, &$form_state) { if ($form_state['values']['confirm']) { dvm($form_state['values']['files']); foreach ($form_state['values']['files'] as $fid => $file) { file_delete($file, TRUE); } drupal_set_message(t('The items have been deleted.')); } $form_state['redirect'] = 'admin/content/file'; return; } /** * Menu callback : Page listing disk space usage for each user. */ function file_admin_usage_page() { $header = array( array('data' => t('Username'), 'field' => 'name'), array('data' => t('Size'), 'field' => 'total_filesize', 'sort' => 'desc'), ); $files_per_page = variable_get('files_per_page', 50); $sql = 'SELECT u.uid, u.name, SUM(f.filesize) AS total_filesize FROM {users} u INNER JOIN {files} f ON f.uid = u.uid GROUP BY f.uid'; $sql .= tablesort_sql($header); $result = pager_query($sql, $files_per_page); $rows = array(); foreach ($result as $row) { $rows[] = array( theme('username', $row), format_size($row->total_filesize), ); } if (!$rows) { $rows[] = array(array('data' => t('No users have uploaded files.'), 'colspan' => '2')); } // Get statistics about files. $total_size = db_query('SELECT SUM(f.filesize) FROM {files} f INNER JOIN {upload} u ON f.fid = u.fid')->fetchField(); $output = '

' . t('Total disk space used: %size', array('%size' => format_size($total_size))) . '

'; $output .= theme('table', $header, $rows) . theme('pager', NULL, $files_per_page); return $output; }