You are here

function finder_views_finder_find in Finder 6

Same name and namespace in other branches
  1. 7 modules/finder_views/finder_views.module \finder_views_finder_find()

Implementation of hook_finder_find().

See also

hook_finder_find()

File

modules/finder_views/finder_views.module, line 352
The finder views module.

Code

function finder_views_finder_find($finder, $finder_element_id, $keywords, $mode, $match, $pager) {
  $options = array();
  $view_name =& $finder->settings['views']['view'];
  if ($view = views_get_view($view_name)) {
    $view_args = array();

    // We add a display, and let it derive from the 'default' display.
    $display = $view
      ->add_display('finder_views');

    // Change the display options based on 'query display' setting.
    if ($mode == 'results' && $finder->settings['views']['displays']['query']) {
      $custom_display =& $finder->settings['views']['displays']['query'];
    }
    elseif ($mode == 'choices' && ($fe =& finder_element($finder, $finder_element_id)) && $fe->settings['choices']['displays']['query']) {
      $custom_display =& $fe->settings['choices']['displays']['query'];
    }
    if (isset($custom_display) && $custom_display != 'default') {
      $view->display[$display]->display_options = $view->display[$custom_display]->display_options;
    }

    // Activate the display
    $view
      ->set_display($display);

    // For choices, remove all the currently added fields.
    // This prevents problems with the 'group by' clauses.

    /*
    if ($mode == 'choices') {
      // Get a list of currently added fields.
      $fields = $view->get_items('field', $display);
      foreach ($fields as $key => $field) {
        $view->set_item($display, 'field', $field['id'], NULL);
        unset($fields[$key]);
      }
    }
    */
    $view->display_handler
      ->set_option('style_plugin', 'finder_views_php_array_finder');
    $view->display_handler
      ->set_option('row_plugin', 'fields');
    $field_info = array();
    foreach ($keywords as $feid => $keyword_array) {
      if ($mode == 'choices' && $feid == $finder_element_id) {
        $element =& finder_element($finder, $feid);

        // Select the 'choices' field.
        $element_fields =& $element->settings['choices']['field'];
        foreach ($element_fields as $key => $field) {
          $field_info[$feid][$key] = finder_split_field($field);
          $field_alias = finder_field_alias($feid, $field_info[$feid][$key]['table'], $field_info[$feid][$key]['field']);
          $view
            ->add_item($display, 'field', $field_info[$feid][$key]['table'], $field_info[$feid][$key]['field'], array(), $field_alias);

          //$groups[] = $field_alias;
        }

        // Add element args for 'choices'.
        $view_args = finder_views_get_element_args($element);
      }

      // Temporary hack - rather than doing this, the functions calling finder_find() should set this up?
      // or maybe this func should handle the whole thing?  i.e. creating the dummy keywords for choices.
      // See #1270150.
      if (is_null($keywords[$feid])) {
        $keywords[$feid][] = NULL;
      }

      //This code is removed because of #1270150.

      /*
            // Remove empty keyword strings.
            $keywords[$feid] = array_filter((array)$keywords[$feid], 'finder_empty_keyword');
            // Remove blank keyword array.
            if (empty($keywords[$feid])) {
              unset($keywords[$feid]);
            }
      */
    }
    $options = array(
      'finder' => $finder,
      'finder_element_id' => $finder_element_id,
      'keywords' => $keywords,
      'mode' => $mode,
      'match' => finder_match_operator($match),
      'groups' => isset($groups) ? $groups : array(),
    );
    $view->display_handler
      ->set_option('finder_views_options', $options);

    // Save $field_info into the view, so we can use it in the display and style plugins.
    $view->display_handler
      ->set_option('finder_views_field_info', $field_info);

    // Limit result set size.
    if (isset($pager)) {
      $view->display_handler
        ->set_option('use_pager', TRUE);
      $view->display_handler
        ->set_option('items_per_page', $pager);
      $pager_element = $mode == 'results' ? 0 : 'finder_choices_' . $finder_element_id;
      $view->display_handler
        ->set_option('pager_element', $pager_element);
    }
    if ($mode == 'results') {
      $view_args = finder_views_get_finder_args($finder);

      // Limit result for redirect.
      if (isset($finder->go) && $finder->go) {
        $view->display_handler
          ->set_option('use_pager', TRUE);
        $view->display_handler
          ->set_option('items_per_page', 1);
        $view->display_handler
          ->set_option('pager_element', 0);
      }
    }

    // Make sure the query is not cached
    $view->is_cacheable = FALSE;

    // Removed because of issue #1345930.

    //$view->build($display);
    $result = $view
      ->execute_display($display, $view_args);
  }
  else {
    $result = FALSE;
  }
  return $result;
}