You are here

function file_entity_admin_files in File Entity (fieldable files) 7.2

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

Form builder: Builds the file administration overview.

1 call to file_entity_admin_files()
file_entity_admin_file in ./file_entity.admin.inc
Menu callback: file administration.

File

./file_entity.admin.inc, line 290

Code

function file_entity_admin_files() {
  $admin_access = user_access('administer files');

  // Build the 'Update options' form.
  $form['options'] = array(
    '#type' => 'fieldset',
    '#title' => t('Update options'),
    '#attributes' => array(
      'class' => array(
        'container-inline',
      ),
    ),
    '#access' => $admin_access,
  );
  $options = array();
  foreach (module_invoke_all('file_operations') as $operation => $array) {
    $options[$operation] = $array['label'];
  }
  $form['options']['operation'] = array(
    '#type' => 'select',
    '#title' => t('Operation'),
    '#title_display' => 'invisible',
    '#options' => $options,
    '#default_value' => 'approve',
  );
  $form['options']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update'),
    '#validate' => array(
      'file_entity_admin_files_validate',
    ),
    '#submit' => array(
      'file_entity_admin_files_submit',
    ),
  );

  // Build the sortable table header.
  $header = array(
    'title' => array(
      'data' => t('Title'),
      'field' => 'fm.filename',
    ),
    'type' => array(
      'data' => t('Type'),
      'field' => 'fm.type',
    ),
    'size' => array(
      'data' => t('Size'),
      'field' => 'fm.filesize',
    ),
    'author' => t('Author'),
    'timestamp' => array(
      'data' => t('Updated'),
      'field' => 'fm.timestamp',
      'sort' => 'desc',
    ),
    'usage' => array(
      'data' => t('Used in'),
      'field' => 'total_count',
    ),
    'operations' => array(
      'data' => t('Operations'),
    ),
  );
  if (variable_get('file_entity_total_count_optimization', FALSE)) {

    // If the total_count is being retrieved by subqueries,
    // the table is not sortable by this column.
    unset($header['usage']['field']);
  }
  $query = db_select('file_managed', 'fm')
    ->extend('PagerDefault')
    ->extend('TableSort');
  if (!variable_get('file_entity_total_count_optimization', FALSE)) {
    $query
      ->leftJoin('file_usage', 'fu', 'fm.fid = fu.fid');
    $query
      ->groupBy('fm.fid');
    $query
      ->groupBy('fm.uid');
    $query
      ->groupBy('fm.timestamp');
    $query
      ->addExpression('SUM(fu.count)', 'total_count');
  }
  file_entity_build_filter_query($query);
  $result = $query
    ->fields('fm', array(
    'fid',
    'uid',
  ))
    ->limit(50)
    ->orderByHeader($header)
    ->addTag('file_access')
    ->execute()
    ->fetchAllAssoc('fid');
  if (variable_get('file_entity_total_count_optimization', FALSE)) {

    // Get the total_count in separate queries, otherwise the main
    // query will take too long.
    // This setting can be configured under /admin/config/development/performance.
    foreach ($result as &$file_result) {
      $count_query = db_select('file_usage', 'fu')
        ->fields('fu', array(
        'fid',
        'count',
      ))
        ->condition('fu.fid', $file_result->fid, '=');
      $count_query
        ->addExpression('fu.count', 'total_count');
      $count_result = $count_query
        ->execute()
        ->fetchAll();
      if (!empty($count_result[0]->total_count)) {
        $file_result->total_count = $count_result[0]->total_count;
      }
    }
  }
  $files = file_load_multiple(array_keys($result));
  $uids = array();
  foreach ($files as $file) {
    $uids[] = $file->uid;
  }
  $accounts = !empty($uids) ? user_load_multiple(array_unique($uids)) : array();

  // Prepare the list of files.
  $destination = drupal_get_destination();
  $options = array();
  foreach ($files as $file) {
    $file_type = file_type_load($file->type);
    $account = isset($accounts[$file->uid]) ? $accounts[$file->uid] : NULL;
    $total_count = (int) isset($result[$file->fid]->total_count) ? $result[$file->fid]->total_count : 0;
    $options[$file->fid] = array(
      'title' => array(
        'data' => array(
          '#type' => 'link',
          '#title' => $file->filename,
          '#href' => 'file/' . $file->fid,
        ),
      ),
      'type' => $file_type ? check_plain($file_type->label) : FILE_TYPE_NONE,
      'size' => format_size($file->filesize),
      'author' => theme('username', array(
        'account' => $account,
      )),
      'timestamp' => format_date($file->timestamp, 'short'),
      'usage' => format_plural($total_count, '1 place', '@count places'),
    );

    // Show a warning for files that do not exist.
    if (@(!is_file($file->uri))) {
      $options[$file->fid]['#attributes']['class'][] = 'error';
      if (!file_stream_wrapper_get_instance_by_uri($file->uri)) {
        $options[$file->fid]['#attributes']['title'] = t('The stream wrapper for @scheme files is missing.', array(
          '@scheme' => file_uri_scheme($file->uri),
        ));
      }
      else {
        $options[$file->fid]['#attributes']['title'] = t('The file does not exist.');
      }
    }

    // Build a list of all the accessible operations for the current file.
    $operations = array();
    if (file_entity_access('update', $file)) {

      // Convert the usage count to a link.
      $options[$file->fid]['usage'] = l($options[$file->fid]['usage'], 'file/' . $file->fid . '/usage');
      $operations['edit'] = array(
        'title' => t('Edit'),
        'href' => 'file/' . $file->fid . '/edit',
        'query' => $destination,
      );
    }
    if (file_entity_access('delete', $file)) {
      $operations['delete'] = array(
        'title' => t('Delete'),
        'href' => 'file/' . $file->fid . '/delete',
        'query' => $destination,
      );
    }
    $options[$file->fid]['operations'] = array();
    if (count($operations) > 1) {

      // Render an unordered list of operations links.
      $options[$file->fid]['operations'] = array(
        'data' => array(
          '#theme' => 'links__file_entity_operations',
          '#links' => $operations,
          '#attributes' => array(
            'class' => array(
              'links',
              'inline',
            ),
          ),
        ),
      );
    }
    elseif (!empty($operations)) {

      // Render the first and only operation as a link.
      $link = reset($operations);
      $options[$file->fid]['operations'] = array(
        'data' => array(
          '#type' => 'link',
          '#title' => $link['title'],
          '#href' => $link['href'],
          '#options' => array(
            'query' => $link['query'],
          ),
        ),
      );
    }
  }

  // Only use a tableselect when the current user is able to perform any
  // operations.
  if ($admin_access) {
    $form['files'] = array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $options,
      '#empty' => t('No files available.'),
    );
  }
  else {
    $form['files'] = array(
      '#theme' => 'table',
      '#header' => $header,
      '#rows' => $options,
      '#empty' => t('No files available.'),
    );
  }
  $form['pager'] = array(
    '#markup' => theme('pager'),
  );
  return $form;
}