You are here

function finder::find in Finder 7.2

Finder find.

Get a list of choices for form or results.

Return value

An array of choices/results.

1 call to finder::find()
finder::results in includes/finder.inc
Finder results.

File

includes/finder.inc, line 457
finder.inc

Class

finder
An object to contain all of the data to generate a finder, plus the member functions to build the finder, and render the output.

Code

function find() {
  static $finder_find_cache = array();
  $this
    ->build();
  $keywords =& $this->find['keywords'];
  $mode =& $this->find['mode'];
  $matches =& $this->find['matches'];
  $pager =& $this->find['pager'];
  $element =& $this->find['element'];
  $reset =& $this->find['reset'];

  //    $field_logic = &$this->find['field_logic'];
  //    $value_logic = &$this->find['value_logic'];
  //    $nesting_order = &$this->find['nesting_order'];
  // Some defaults.
  $mode = $mode ? $mode : 'choices';
  $pager = $pager ? $pager : 0;
  $element = $element ? $element : NULL;
  $reset = $reset ? $reset : FALSE;

  // Handle reduce setting.
  if ($mode == 'choices' && $this
    ->esetting($element, 'choices_style', 'used_values') == 'used_values' && $this
    ->esetting($element, 'reduce')) {
    $reduce = $this
      ->esetting($element, 'reduce');
    foreach ($reduce as $reduce_element_id) {
      if (isset($this->form_state['values'][$reduce_element_id]) && !isset($keywords[$reduce_element_id])) {
        $reduce_element_keys = is_array($this->form_state['values'][$reduce_element_id]) ? $this->form_state['values'][$reduce_element_id] : array(
          $this->form_state['values'][$reduce_element_id],
        );
        foreach ($reduce_element_keys as $reduce_element_key) {
          $keywords[$reduce_element_id][] = $reduce_element_key;
        }
      }
    }
  }

  // Build $find_data for cache ids, and populate the $matches variable.
  $find_data = $keywords;
  foreach (array_keys($keywords) as $eid) {
    if (empty($matches[$eid])) {
      $matches[$eid]['match'] = $this
        ->esetting($this->elements[$eid], 'match', 'e');
    }
    if (empty($matches[$eid]['match_x'])) {
      $matches[$eid]['match_x'] = array(
        'operator' => $this
          ->esetting($this->elements[$eid], 'match_custom_operator', '='),
        'value_prefix' => $this
          ->esetting($this->elements[$eid], 'match_custom_prefix'),
        'value_suffix' => $this
          ->esetting($this->elements[$eid], 'match_custom_suffix'),
      );
    }
    if ($matches[$eid]['match'] != 'e') {
      if ($matches[$eid]['match'] == 'x') {
        $find_data[$eid] = implode(',', $matches[$eid]['match_x']) . '=' . implode(';', $find_data[$eid]);
      }
      else {
        $find_data[$eid] = $matches[$eid]['match'] . '=' . implode(';', $find_data[$eid]);
      }
    }
  }

  // Create an ID using the function params so we can cache the return value.
  $id = ($mode == 'choices' ? 'e' . $element->id : 'f' . $this->id) . '|';
  $id_length = strlen($id);
  $cache_id = ($pager ? $pager . (isset($_GET['page']) ? '.' . $_GET['page'] : '') : '') . json_encode($find_data);
  $cache_id_length = strlen($cache_id);
  if ($cache_id_length + $id_length > 255) {

    // For ID's that (will) exceed 255 we will try to represent them a unique way and pray for the best :/
    $cache_id = md5($cache_id) . $cache_id_length . substr($cache_id, 0, 223 - strlen($cache_id_length) - $id_length);

    // 223 = 255 - 32
  }
  $cache_id = $id . $cache_id;
  if (isset($finder_find_cache[$cache_id]) && !$reset) {

    // Use the static data.
    $this->find = $finder_find_cache[$cache_id];
  }
  elseif ($this
    ->setting('cache') && !$reset && ($cache = cache_get($cache_id, 'cache_finder_find')) && !empty($cache->data)) {

    // Use the cached data.
    $this->find = $cache->data;
  }
  else {

    // Allow other modules to react to and alter the finder.
    module_invoke_all('finder_find', $this);
    if ($mode == 'choices' && $this
      ->esetting($element, 'choices_style', 'used_values') != 'used_values') {

      // Calculate the values from the finder_choice table.
      $this
        ->choice_find();
    }
    else {

      // Calculate the values from the original tables..
      $this
        ->views_find();
    }
    if ($mode == 'choices' && $this
      ->esetting($element, 'sort')) {
      natcasesort($this->find['results']);
    }

    // Allow other modules to react to and alter the finder.
    module_invoke_all('finder_find_alter', $this);

    // Add the resulting $finder_find to the drupal cache.
    if ($this
      ->setting('cache')) {
      cache_set($cache_id, $this->find, 'cache_finder_find', REQUEST_TIME + 60 * $this
        ->setting('cache'));
    }

    // Add the resulting $finder_find to the static internal load cache.
    $finder_find_cache[$cache_id] = $this->find;
  }
}