You are here

function linkchecker_form_alter in Link checker 5.2

Same name and namespace in other branches
  1. 6.2 linkchecker.module \linkchecker_form_alter()
  2. 7 linkchecker.module \linkchecker_form_alter()

File

./linkchecker.module, line 959
This module periodically check links in given node types, blocks, cck fields, etc.

Code

function linkchecker_form_alter($form_id, &$form) {
  switch ($form_id) {

    // Catch the block add/configure form and add custom submit handler.
    case 'block_box_form':

      // Add custom submit handler to block add form.
      $form['#submit']['linkchecker_block_add_form_submit'] = array();
      break;
    case 'block_admin_configure':

      // When displaying the form, show the broken links warning.
      if (empty($form['#post']) && is_numeric(arg(5))) {

        // Show a message on block edit page if a link check failed once or more.
        $ignore_response_codes = preg_split('/(\\r\\n?|\\n)/', variable_get('linkchecker_ignore_response_codes', "200\n302\n304\n401\n403"));
        $placeholders = implode(',', array_fill(0, count($ignore_response_codes), '%d'));
        $links = db_query("SELECT url, code, fail_count FROM {linkchecker_boxes} lb INNER JOIN {linkchecker_links} ll ON lb.lid = ll.lid WHERE lb.bid = %d AND ll.fail_count > %d AND ll.status = %d AND ll.code NOT IN (" . $placeholders . ")", array_merge(array(
          arg(5),
          0,
          1,
        ), $ignore_response_codes));
        while ($link = db_fetch_object($links)) {
          drupal_set_message(strtr(format_plural($link->fail_count, 'Link check of <a href="@url">@url</a> failed once (status code: @code).', 'Link check of <a href="@url">@url</a> failed @count times (status code: @code).'), array(
            '@url' => check_plain($link->url),
            '@code' => $link->code,
          )), 'warning');
        }
      }

      // Add custom submit handler to block configuration form.
      $form['#submit']['linkchecker_block_configure_form_submit'] = array();
      break;
    case 'block_box_delete':

      // Add custom submit handler to block delete form.
      $form['#submit']['linkchecker_block_box_delete_form_submit'] = array();
      break;
    case 'comment_form':

      // When displaying the form as 'view' or 'preview', show the broken links warning.
      if ((empty($form['#post']) || isset($form['#post']['op']) && $form['#post']['op'] == t('Preview comment')) && arg(0) == 'comment' && arg(1) == 'edit' && is_numeric(arg(2))) {

        // Show a message on comment edit page if a link check failed once or more.
        $ignore_response_codes = preg_split('/(\\r\\n?|\\n)/', variable_get('linkchecker_ignore_response_codes', "200\n302\n304\n401\n403"));
        $placeholders = implode(',', array_fill(0, count($ignore_response_codes), '%d'));
        $links = db_query("SELECT url, code, fail_count FROM {linkchecker_comments} lc INNER JOIN {linkchecker_links} ll ON lc.lid = ll.lid WHERE lc.cid = %d AND ll.fail_count > %d AND ll.status = %d AND ll.code NOT IN (" . $placeholders . ")", array_merge(array(
          arg(2),
          0,
          1,
        ), $ignore_response_codes));
        while ($link = db_fetch_object($links)) {
          drupal_set_message(strtr(format_plural($link->fail_count, 'Link check of <a href="@url">@url</a> failed once (status code: @code).', 'Link check of <a href="@url">@url</a> failed @count times (status code: @code).'), array(
            '@url' => check_plain($link->url),
            '@code' => $link->code,
          )), 'warning');
        }
      }
      break;
  }
}