You are here

function finder_form_state in Finder 6

Same name and namespace in other branches
  1. 7.2 includes/form.inc \finder_form_state()
  2. 7 includes/finder.form.inc \finder_form_state()

Statically 'get' or 'set' the FAPI form state in a per-finder cache.

When used to 'set' the form state it will also check to see if a redirect is required to go to the results path with arguments. When used to 'get' the form state it will check the static cache for a stored form state, then it will check the session for a form state carried over from another page, and finally it will attempt to build a form state out of the path arguments.

Parameters

$finder_id: The finder's ID.

$form_state: The Forms API form state (if supplied, will 'set' the form state).

Return value

A copy of the Forms API form state.

3 calls to finder_form_state()
finder_form in includes/finder.form.inc
FAPI definition for the finder form.
finder_form_submit in includes/finder.form.inc
Submit function for finder form.
finder_results in ./finder.module
Create finder results output.

File

includes/finder.form.inc, line 206
The finder form.

Code

function finder_form_state($finder_id, $form_state = NULL) {
  static $finder_form_state = NULL;
  if ($form_state) {

    // we are setting the form_state in a submit.
    // last chance for modules to intefere before potential redirect.
    drupal_alter('finder_form_state', $form_state, $finder_id);
    $finder_form_state[$finder_id] = $form_state;

    // handle URL stuff.
    $finder = finder_load($finder_id);
    if (!$finder->settings['advanced']['hide_args']) {
      $sep =& $finder->settings['advanced']['arg_sep'];
      $empty_symbol = !empty($finder->settings['advanced']['empty_symbol']) ? $finder->settings['advanced']['empty_symbol'] : ' ';
      $query = array();
      foreach ($finder->elements as $element) {
        $keyword = (array) $form_state['values'][$element->finder_element_id];
        foreach ($keyword as $k => $v) {

          // Handle forward slashes in input.
          $v = str_replace("/", "%2f%2f", $v);

          // Handle new lines in input.
          $v = str_replace("\r\n", "\n", $v);
          $v = str_replace("\n", urlencode("***BREAK***"), $v);
          if (strpos($v, $sep) !== FALSE) {
            $v = '"' . $v . '"';
          }
          $keyword[$k] = $v ? trim($v) : $empty_symbol;
        }
        $keywords[$element->finder_element_id] = implode(',', $keyword);
      }
      if (!$form_state['storage']['finished']) {
        $query['finished'] = '0';
      }
      if ($form_state['clicked_button']['#name'] == 'go' && $form_state['storage']['finished']) {
        $query['go'] = '1';
      }
      finder_form_goto($sep, $finder->path . '/' . implode('/', $keywords), $query);
    }
  }
  elseif (!isset($finder_form_state[$finder_id])) {
    $finder = finder_load($finder_id);
    if ($finder->settings['advanced']['hide_args'] && isset($_GET['finder'])) {

      // check the session
      $finder_form_state[$finder_id] = $_SESSION['finder'][$_GET['finder']];
    }
    elseif (!isset($_GET['finder']) && strlen($finder->path) < strlen($_GET['q']) && stripos($_GET['q'], $finder->path) === 0) {

      // check the URL
      // Get the seperator for element values - this is usually a comma.
      $sep =& $finder->settings['advanced']['arg_sep'];

      // Get the finder arguments.
      $args = str_replace($finder->path . '/', '', $_GET['q']);

      // Handle new lines from input.
      $args = str_replace("***BREAK***", "\n", $args);

      // Forward slashes were encoded as double forward slashes.  We must temporarily replace those here to prevent the explode() affecting this.
      // Rawurlencode() doesn't work but can be fixed using Apache's AllowEncodedSlashes Directive, but how do you tell people to switch that on?
      $args = str_replace('//', '[-finder-forward-slash-]', $args);

      // Double seperators break the $csv_regex below, and I'm not clever enough to fix the regex.
      $args = str_replace($sep . $sep, '[-finder-double-sep-]', $args);

      // Break arguments apart into a string for each element.
      $args = explode('/', $args);
      $form_state['storage']['finished'] = TRUE;
      $empty_symbol = !empty($finder->settings['advanced']['empty_symbol']) ? $finder->settings['advanced']['empty_symbol'] : ' ';
      $csv_regex = "/" . $sep . "(?!(?:[^\\\"" . $sep . "]|[^\\\"]" . $sep . "[^\\\"])+\\\")/";
      foreach ($finder->elements as $key => $element) {
        $keywords = preg_split($csv_regex, $args[$key]);
        foreach ($keywords as $k => $v) {
          $v = str_replace('[-finder-double-sep-]', $sep . $sep, $v);
          $v = str_replace('[-finder-forward-slash-]', '/', $v);
          $v = str_replace(urlencode($sep), $sep, trim($v));
          if (trim($v) == trim($empty_symbol)) {
            $v = NULL;
          }
          if (strpos($v, $sep) !== FALSE && $v[0] == '"' && $v[strlen($v) - 1] == '"') {
            $v = substr($v, 1, strlen($v) - 2);
          }
          unset($keywords[$k]);
          if ($v) {
            $keywords[$v] = $v;
          }
        }
        if (count($keywords) === 1) {
          $keywords = current($keywords);
        }
        elseif (!count($keywords)) {
          $keywords = NULL;
        }
        $form_state['values'][$element->finder_element_id] = $keywords;
      }
      $finder_form_state[$finder_id] = $form_state;
    }
  }
  return $finder_form_state[$finder_id];
}