You are here

function scanner_view in Search and Replace Scanner 7

Same name and namespace in other branches
  1. 5.2 scanner.module \scanner_view()
  2. 6 scanner.module \scanner_view()

Menu callback; presents the scan form and results.

1 string reference to 'scanner_view'
scanner_menu in ./scanner.module
Implements hook_menu().

File

./scanner.module, line 119
Search and Replace Scanner - works on all nodes text content.

Code

function scanner_view() {
  $output = '';

  // Using set_html_head because it seems unecessary to load a separate css
  // file for just two simple declarations.
  drupal_add_css('
      #scanner-form .form-submit { margin-top:0; }
      #scanner-form .form-item { margin-bottom:0; }
    ', array(
    'type' => 'inline',
  ));

  // Javascript checks to make sure user has entered some search text.
  drupal_add_js("\n    jQuery(document).ready(function() {\n      var searchfield = jQuery('#edit-search');\n      jQuery('input[type=submit][value=" . str_replace('\'', '\\\'', t('Search')) . "]').click(function() {\n        var chars = searchfield.val().length;\n        if (chars == 0) {\n          alert('" . str_replace('\'', '\\\'', t('Please provide some search text and try again.')) . "');\n          searchfield.addClass('error');\n          searchfield[0].focus();\n          return false;\n        } else if (chars < 3) {\n          return confirm('" . str_replace('\'', '\\\'', t('Searching for a keyword that has fewer than three characters could take a long time. Are you sure you want to continue?')) . "');\n        }\n        return true;\n      });\n      searchfield.keyup(function() {\n        searchfield.removeClass('error');\n      });\n    });\n  ", array(
    'type' => 'inline',
    'group' => JS_DEFAULT,
  ));
  if (isset($_SESSION['scanner_search'])) {
    $search = $_SESSION['scanner_search'];
  }
  else {
    $search = NULL;
  }
  if (isset($_SESSION['scanner_status'])) {
    $status = $_SESSION['scanner_status'];
  }
  else {
    $status = NULL;
  }
  if (!is_null($search) && $status >= SCANNER_STATUS_GO_SEARCH) {
    if ($status == SCANNER_STATUS_GO_CONFIRM) {
      drupal_goto('admin/content/scanner/scan/confirm');
    }
    elseif ($status == SCANNER_STATUS_GO_REPLACE) {
      $resulttxt = '<a name="results"></a>' . t('Replacement Results');
      $results = scanner_execute('replace');
    }
    else {
      $resulttxt = t('Search Results');
      $results = scanner_execute('search');
    }

    // @todo Please change this theme call to use an associative array for
    // the $variables parameter.
    if ($results) {
      $results = '<a name="results"></a><div><h2>' . $resulttxt . '</h2>' . $results;
    }
    else {
      $results = t('Your search yielded no results.');
    }
    $scanner_form = drupal_get_form('scanner_form');
    $output = drupal_render($scanner_form);
    $output .= $results;

    // Clear any old search form input.
    unset($_SESSION['scanner_search']);
    unset($_SESSION['scanner_replace']);
    unset($_SESSION['scanner_preceded']);
    unset($_SESSION['scanner_followed']);
    unset($_SESSION['scanner_mode']);
    unset($_SESSION['scanner_wholeword']);
    unset($_SESSION['scanner_published']);
    unset($_SESSION['scanner_pathauto']);
    unset($_SESSION['scanner_regex']);
    unset($_SESSION['scanner_terms']);

    // Clear old status.
    unset($_SESSION['scanner_status']);
    return $output;
  }
  $scanner_form = drupal_get_form('scanner_form');
  $output = drupal_render($scanner_form);
  return $output;
}