You are here

function finder_find_choices in Finder 7

Same name and namespace in other branches
  1. 6 finder.module \finder_find_choices()

Postprocessing for returned finder_find options when mode is choices.

TO DO: some of the added properties here could probably just be "selected" in the query.

1 call to finder_find_choices()
finder_find in ./finder.module
Get a list of choices for form or results.

File

./finder.module, line 1490
The finder module.

Code

function finder_find_choices($finder, $finder_element_id, $options, $keywords, $match) {
  if ($options) {

    // If there are options, fetch some info about the field names.
    $element =& finder_element($finder, $finder_element_id);
    $fields =& $element->settings['choices']['field'];
    foreach ($fields as $key => $field) {
      $field_info[$key] = finder_split_field($field);
      $field_names[$key] = finder_field_alias($finder_element_id, $field_info[$key]['table'], $field_info[$key]['field']);
    }

    // Create a new array where we will put the options to return.
    $new_options = array();

    // Iterate through the options.
    foreach ($options as $option) {

      // Simply case - there is only one field.
      if (count($fields) === 1) {

        // Add the field name to the option.
        $option->field_name = end($field_names);

        // Add the field name to the display_field property of the option too.
        $option->display_field = $option->field_name;

        // If doing "per_result" then switch the field name to the base field.
        if (isset($element->settings['choices']['per_result']) && $element->settings['choices']['per_result']) {
          $option->field_name = $option->base_field;
        }

        // Append this option to the array.
        $new_options[] = $option;
      }
      elseif (count($fields) > 1) {
        if (is_null($keywords)) {
          $matching_names = $field_names;
        }
        else {
          $operator = str_replace('%%', '%', str_replace("'", "", finder_condition_args($match)));
          $matching_names = array();
          foreach ($field_names as $field_name) {
            $expression = str_replace('%s', $keywords, $operator);
            if (strpos($expression, 'LIKE') !== FALSE) {
              $expression = str_replace(' LIKE ', '', $expression);

              // Added strtolower() because some people were reporting
              // case-sensitivity even with the i modifier.
              $like_matches = preg_grep("/^" . str_replace('%', '(.*?)', preg_quote(drupal_strtolower($expression))) . "\$/si", array(
                drupal_strtolower($option->{$field_name}),
              ));
              if (!empty($like_matches)) {
                $matching_names[] = $field_name;
              }
            }
            elseif (eval('return ' . $option->{$field_name} . $expression . ';')) {
              $matching_names[] = $field_name;
            }
          }
        }
        if (count($matching_names) === 1) {
          $option->field_name = end($matching_names);
          $option->display_field = $option->field_name;
          $new_options[] = $option;
        }
        elseif (!empty($matching_names)) {
          foreach ($matching_names as $matching_name) {
            $new_option = clone $option;
            $new_option->field_name = $matching_name;
            $new_option->display_field = $new_option->field_name;
            $new_options[] = $new_option;
          }
        }
      }
    }
    return $new_options;
  }
  return $options;
}