You are here

function _linkchecker_report_page in Link checker 7

Same name and namespace in other branches
  1. 6.2 includes/linkchecker.pages.inc \_linkchecker_report_page()

Builds the HTML report page table with pager.

Parameters

SelectQueryInterface $query: The pager query for the report page. Can be per user report or global.

object|null $account: The user account object.

Return value

string Themed report page.

2 calls to _linkchecker_report_page()
linkchecker_admin_report_page in ./linkchecker.pages.inc
Menu callback for general reporting.
linkchecker_user_report_page in ./linkchecker.pages.inc
Menu callback for author specific reporting.

File

./linkchecker.pages.inc, line 118
User page callbacks for the linkchecker module.

Code

function _linkchecker_report_page($query, $account = NULL) {
  $links_unchecked = db_query('SELECT COUNT(1) FROM {linkchecker_link} WHERE last_checked = :last_checked AND status = :status', array(
    ':last_checked' => 0,
    ':status' => 1,
  ))
    ->fetchField();
  if ($links_unchecked > 0) {
    $links_all = db_query('SELECT COUNT(1) FROM {linkchecker_link} WHERE status = :status', array(
      ':status' => 1,
    ))
      ->fetchField();
    drupal_set_message(format_plural($links_unchecked, 'There is 1 unchecked link of about @links_all links in the database. Please be patient until all links have been checked via cron.', 'There are @count unchecked links of about @links_all links in the database. Please be patient until all links have been checked via cron.', array(
      '@links_all' => $links_all,
    )), 'warning');
  }
  $header = array(
    array(
      'data' => t('URL'),
      'field' => 'url',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Response'),
      'field' => 'code',
      'sort' => 'desc',
    ),
    array(
      'data' => t('Error'),
      'field' => 'error',
    ),
    array(
      'data' => t('Operations'),
    ),
  );
  $result = $query
    ->limit(50)
    ->orderByHeader($header)
    ->execute();

  // Evaluate permission once for performance reasons.
  $access_edit_link_settings = user_access('edit link settings');
  $access_administer_blocks = user_access('administer blocks');
  $access_administer_redirects = user_access('administer redirects');
  $rows = array();
  foreach ($result as $link) {

    // Get the node, block and comment IDs that refer to this broken link and
    // that the current user has access to.
    $nids = _linkchecker_link_node_ids($link, $account);
    $cids = _linkchecker_link_comment_ids($link, $account);
    $bids = _linkchecker_link_block_ids($link);

    // If the user does not have access to see this link anywhere, do not
    // display it, for reasons explained in _linkchecker_link_access(). We
    // still need to fill the table row, though, so as not to throw off the
    // number of items in the pager.
    if (empty($nids) && empty($cids) && empty($bids)) {
      $rows[] = array(
        array(
          'data' => t('Permission restrictions deny you access to this broken link.'),
          'colspan' => count($header),
        ),
      );
      continue;
    }
    $links = array();

    // Show links to link settings.
    if ($access_edit_link_settings) {
      $links[] = l(t('Edit link settings'), 'linkchecker/' . $link->lid . '/edit', array(
        'query' => drupal_get_destination(),
      ));
    }

    // Show link to nodes having this broken link.
    foreach ($nids as $nid) {
      $links[] = l(t('Edit node @node', array(
        '@node' => $nid,
      )), 'node/' . $nid . '/edit', array(
        'query' => drupal_get_destination(),
      ));
    }

    // Show link to comments having this broken link.
    $comment_types = linkchecker_scan_comment_types();
    if (module_exists('comment') && !empty($comment_types)) {
      foreach ($cids as $cid) {
        $links[] = l(t('Edit comment @comment', array(
          '@comment' => $cid,
        )), 'comment/' . $cid . '/edit', array(
          'query' => drupal_get_destination(),
        ));
      }
    }

    // Show link to blocks having this broken link.
    if ($access_administer_blocks) {
      foreach ($bids as $bid) {
        $links[] = l(t('Edit block @block', array(
          '@block' => $bid,
        )), 'admin/structure/block/manage/block/' . $bid . '/configure', array(
          'query' => drupal_get_destination(),
        ));
      }
    }

    // Show link to redirect this broken internal link.
    if (module_exists('redirect') && $access_administer_redirects && _linkchecker_is_internal_url($link)) {
      $links[] = l(t('Create redirection'), 'admin/config/search/redirect/add', array(
        'query' => array(
          'source' => $link->internal,
          drupal_get_destination(),
        ),
      ));
    }

    // Create table data for output.
    $rows[] = array(
      'data' => array(
        l(_filter_url_trim($link->url, 40), $link->url),
        $link->code,
        check_plain($link->error),
        theme('item_list', array(
          'items' => $links,
        )),
      ),
    );
  }
  $build['linkchecker_table'] = array(
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => t('No broken links have been found.'),
  );
  $build['linkchecker_pager'] = array(
    '#theme' => 'pager',
  );
  return $build;
}