You are here

function file_entity_admin_files in File Entity (fieldable files) 7

Same name and namespace in other branches
  1. 7.3 file_entity.admin.inc \file_entity_admin_files()
  2. 7.2 file_entity.admin.inc \file_entity_admin_files()

Administrative form for browsing files and performing operations on them.

1 string reference to 'file_entity_admin_files'
file_entity_menu in ./file_entity.module
Implements hook_menu().

File

./file_entity.admin.inc, line 13
Administrative interface for file type configuration.

Code

function file_entity_admin_files($form, &$form_state) {
  if (!empty($form_state['values']['operation'])) {

    // The form is being rebuilt because an operation requiring confirmation
    // was selected.  Hand off to the callback of the operation at this point
    // which should return a confirm form.
    $operation = $form_state['values']['operation'];
    $files = array_keys(array_filter($form_state['values']['files']));
    return call_user_func_array($operation['callback'], array(
      $files,
    ));
  }
  $limit = variable_get('file_entity_admin_files_limit', 50);

  // Build the 'Update options' form.
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Update options'),
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
  );
  $options = array();
  $form['#operations'] = module_invoke_all('file_operations_info');
  drupal_alter('file_entity_file_operations', $form['#operations']);
  foreach ($form['#operations'] as $operation => $array) {
    $options[$operation] = $array['label'];
  }
  $form['options']['operation'] = array(
    '#type' => 'select',
    '#title' => t('Operation'),
    '#title_display' => 'invisible',
    '#options' => $options,
  );
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
  );

  // Build the sortable table header.
  $header = array(
    'title' => array(
      'data' => t('Title'),
      'field' => 'f.filename',
    ),
    'type' => array(
      'data' => t('Type'),
      'field' => 'f.filemime',
    ),
    'size' => array(
      'data' => t('Size'),
      'field' => 'f.filesize',
    ),
    'author' => array(
      'data' => t('Author'),
      'field' => 'u.name',
    ),
    'timestamp' => array(
      'data' => t('Updated'),
      'field' => 'f.timestamp',
      'sort' => 'desc',
    ),
    'operations' => array(
      'data' => t('Operations'),
    ),
  );
  $query = db_select('file_managed', 'f');
  $query
    ->join('users', 'u', 'f.uid = u.uid');
  $query
    ->extend('PagerDefault')
    ->extend('TableSort')
    ->fields('f', array(
    'fid',
  ))
    ->fields('u', array(
    'uid',
  ))
    ->condition('f.status', FILE_STATUS_PERMANENT)
    ->limit($limit)
    ->orderByHeader($header);
  foreach (array_keys(file_entity_get_hidden_stream_wrappers()) as $name) {
    $query
      ->condition('f.uri', $name . '%', 'NOT LIKE');
  }
  $result = $query
    ->execute()
    ->fetchAllKeyed();
  $files = file_load_multiple(array_keys($result));
  $form['#files'] = $files;

  // This is used to add usernames to the table below.
  $accounts = user_load_multiple(array_values($result));
  $destination = drupal_get_destination();
  $options = array();
  foreach ($files as $file) {
    $options[$file->fid] = array(
      'title' => theme('file_entity_file_link', array(
        'file' => $file,
      )),
      'type' => check_plain($file->filemime),
      'size' => format_size($file->filesize),
      'author' => theme('username', array(
        'account' => $accounts[$file->uid],
      )),
      'timestamp' => format_date($file->timestamp, 'short'),
    );
    $options[$file->fid]['operations'] = array(
      'data' => array(
        '#theme' => 'links__file_operations',
        '#links' => array(),
        '#attributes' => array(
          'class' => array(
            'links',
            'inline',
          ),
        ),
      ),
    );
    $options[$file->fid]['operations']['data']['#links']['edit'] = array(
      'title' => t('edit'),
      'href' => 'file/' . $file->fid . '/edit',
      'query' => $destination,
    );
    $options[$file->fid]['operations']['data']['#links']['delete'] = array(
      'title' => t('delete'),
      'href' => 'file/' . $file->fid . '/delete',
      'query' => $destination,
    );
  }
  $form['files'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No files available.'),
    '#attributes' => array(
      'class' => array(
        'file-display-table',
        'clear',
      ),
    ),
  );
  $form['pager'] = array(
    '#markup' => theme('pager', array(
      'tags' => NULL,
    )),
  );
  return $form;
}