You are here

function path_redirect_list_redirects in Path redirect 6

@file Administrative page callbacks for the path_redirect module.

2 calls to path_redirect_list_redirects()
path_redirect_admin_redirects in ./path_redirect.admin.inc
Render a list of redirects for the main admin page.
path_redirect_form_alter in ./path_redirect.module
Implements hook_form_alter().

File

./path_redirect.admin.inc, line 8
Administrative page callbacks for the path_redirect module.

Code

function path_redirect_list_redirects($query = array(), $conditions = array(), $tableselect = FALSE) {

  // Initialize the query array.
  $query += array(
    'conditions' => array(),
    'args' => array(),
    'limit' => 0,
  );

  // Check if this will be a tableselect element.
  $tableselect &= user_access('administer redirects') && module_exists('elements');

  // Set up the header.
  $header = array(
    'source' => array(
      'data' => t('From'),
      'field' => 'source',
      'sort' => 'asc',
    ),
    'redirect' => array(
      'data' => t('To'),
      'field' => 'redirect',
    ),
    'type' => array(
      'data' => t('Type'),
      'field' => 'type',
    ),
    'language' => array(
      'data' => t('Language'),
      'field' => 'language',
    ),
    'last_used' => array(
      'data' => t('Last used'),
      'field' => 'last_used',
    ),
    'operations' => array(
      'data' => t('Operations'),
    ),
  );

  // Do not include the language column if locale is disabled.
  if (!module_exists('locale')) {
    unset($header['language']);
  }

  // Remove any columns that are present in conditions.
  _path_redirect_build_conditions($query, NULL, $conditions);
  foreach ($conditions as $field => $value) {
    unset($header[$field]);
  }

  // Build the SQL query.
  $sql = 'SELECT rid FROM {path_redirect}';
  if ($query['conditions']) {
    $sql .= ' WHERE (' . implode(') AND (', $query['conditions']) . ')';
  }
  $sql .= tablesort_sql($header);
  if ($query['limit']) {
    $query = pager_query($sql, $query['limit'], 0, NULL, $query['args']);
  }
  else {
    $query = db_query($sql, $query['args']);
  }

  // Load the redirects.
  $rids = array();
  while ($rid = db_result($query)) {
    $rids[] = $rid;
  }
  $redirects = path_redirect_load_multiple($rids);
  $destination = drupal_get_destination();
  $rows = array();
  $weight = 0;
  foreach ($rids as $rid) {
    $redirect = $redirects[$rid];
    $row = array();
    if (isset($header['source'])) {
      $source_url = path_redirect_build_url($redirect['source'], $redirect['source_query']);
      $row['source'] = l($source_url, $redirect['source'], array(
        'query' => $redirect['source_query'],
        'alias' => TRUE,
      ));
    }
    if (isset($header['redirect'])) {
      $redirect_url = path_redirect_build_url($redirect['redirect'], $redirect['query'], $redirect['fragment'], TRUE);
      $row['redirect'] = l($redirect_url, $redirect['redirect'], array(
        'query' => $redirect['query'],
        'fragment' => $redirect['fragment'],
      ));
    }
    if (isset($header['type'])) {
      $row['type'] = $redirect['type'];
    }
    if (isset($header['language'])) {
      $row['language'] = module_invoke('locale', 'language_name', $redirect['language']);
    }
    if (isset($header['last_used'])) {
      $row['last_used'] = format_date($redirect['last_used'], 'short');
    }
    if (isset($header['operations'])) {
      $operations = array();
      $operations['edit'] = array(
        'title' => t('Edit'),
        'href' => 'admin/build/path-redirect/edit/' . $rid,
        'query' => $destination,
      );
      $operations['delete'] = array(
        'title' => t('Delete'),
        'href' => 'admin/build/path-redirect/delete/' . $rid,
        'query' => $destination,
      );
      $row['operations'] = theme('links', $operations, array(
        'class' => 'links inline nowrap',
      ));
    }
    $rows[(string) $rid] = $row;
  }
  if ($tableselect) {
    return array(
      '#type' => 'tableselect',
      '#header' => $header,
      '#options' => $rows,
      '#empty' => t('No URL redirects available.'),
    );
  }
  else {
    if (empty($rows)) {
      $rows[] = array(
        array(
          'data' => t('No URL redirects available.'),
          'colspan' => count($header),
          'class' => 'empty',
        ),
      );
    }
    return array(
      '#type' => 'markup',
      '#value' => theme('table', $header, $rows),
    );
  }
}