You are here

function media_admin_list in D7 Media 7

Form builder: Builds the media list administration overview.

1 string reference to 'media_admin_list'
media_media_display_types in ./media.media.inc
Implements hook_media_display_types().

File

includes/media.admin.inc, line 98
This file contains the admin functions for the Media module.

Code

function media_admin_list(&$parent_form) {

  // @todo Change to media_variable_get('admin_pager_limit') for consistency
  //   with browser_pager_limit?
  $limit = variable_get('media_admin_limit', 50);

  // 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')
    ->extend('PagerDefault')
    ->extend('TableSort');
  $query
    ->join('users', 'u', 'f.uid = u.uid');
  $query
    ->fields('f', array(
    'fid',
  ));
  $query
    ->fields('u', array(
    'uid',
  ));
  $query
    ->condition('f.status', FILE_STATUS_PERMANENT);
  $query
    ->limit($limit);
  $query
    ->orderByHeader($header);
  foreach (array_keys(media_get_hidden_stream_wrappers()) as $name) {
    $query
      ->condition('f.uri', $name . '%', 'NOT LIKE');
  }

  // Result array keys are file IDs, values are the file owner's UIDs.
  $result = $query
    ->execute()
    ->fetchAllKeyed();

  // Hide the operations form if there are no files to operate on.
  $parent_form['options']['#access'] &= !empty($result);

  // Load all the file entities.
  $files = $form['#files'] = file_load_multiple(array_keys($result));

  // Load all the file owner user entities to display usernames.
  $accounts = $form['#accounts'] = user_load_multiple(array_unique($result));
  $destination = drupal_get_destination();
  $options = array();
  foreach ($files as $file) {
    $options[$file->fid] = array(
      'title' => theme('media_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'] = l(t('Edit'), 'media/' . $file->fid . '/edit', array(
      'query' => $destination,
    ));
  }
  $form['files'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No media available.'),
    '#attributes' => array(
      'class' => array(
        'media-display-table',
        'media-clear',
      ),
    ),
  );
  $form['pager'] = array(
    '#markup' => theme('pager', array(
      'tags' => NULL,
    )),
  );
  return $form;
}