You are here

function support_search_execute in Support Ticketing System 7

File

./support.module, line 2421
support.module

Code

function support_search_execute($keys = NULL, $conditions = NULL) {
  global $user;

  // Build matching conditions
  $query = db_select('search_index', 'i', array(
    'target' => 'slave',
  ))
    ->extend('SearchQuery')
    ->extend('PagerDefault');
  $query
    ->join('node', 'n', 'n.nid = i.sid');
  $query
    ->condition('n.status', 1)
    ->condition('n.type', 'support_ticket')
    ->addTag('node_access')
    ->addTag('support_search')
    ->searchExpression($keys, 'node');
  $query
    ->leftJoin('support_ticket', 'st', 'st.nid = n.nid');
  $query
    ->setOption('client', 'st.client');
  $query
    ->setOption('state', 'st.state');
  $query
    ->setOption('priority', 'st.priority');
  $clients = support_search_available_clients();
  if (!empty($clients)) {
    $query
      ->condition('st.client', support_search_available_clients());
  }
  else {

    // User can not access any tickets
    $query
      ->condition('n.type', 'support_ticket', '<>');
  }
  if (!user_access('view other users tickets') && !user_access('administer support') && !user_access('edit any support_ticket content') && !user_access('delete any support_ticket content')) {
    $query
      ->condition('n.uid', $user->uid);
  }

  // Only continue if the first pass query matches.
  if (!$query
    ->executeFirstPass()) {
    return array();
  }

  // Add the ranking expressions.
  _node_rankings($query);

  // Load results.
  $find = $query
    ->limit(10)
    ->execute();
  $results = array();
  foreach ($find as $item) {

    // Build the node body.
    $node = node_load($item->sid);
    $build = node_view($node, 'search_result');
    unset($build['#theme']);
    $node->rendered = drupal_render($build);

    // Fetch comments for snippet.
    $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node);
    $extra = module_invoke_all('node_search_result', $node);
    $uri = entity_uri('node', $node);
    if (sizeof($clients) > 1) {
      $title = check_plain(_support_client($node->client)) . ': ' . $node->title;
    }
    else {
      $title = $node->title;
    }

    // change 'comments' to 'follow ups' for support tickets
    foreach ($extra as $key => $value) {
      $trans = array(
        ' comments' => ' follow ups',
      );
      $extra[$key] = strtr($value, $trans);
    }
    $extra[] = check_plain(_support_state($node->state));
    $extra[] = check_plain(_support_priorities($node->priority));
    $clients = support_search_available_clients();
    $results[] = array(
      'link' => url($uri['path'], array_merge($uri['options'], array(
        'absolute' => TRUE,
      ))),
      'type' => check_plain(node_type_get_name($node)),
      'title' => $title,
      'user' => theme('username', array(
        'account' => $node,
      )),
      'date' => $node->changed,
      'node' => $node,
      'extra' => $extra,
      'score' => $item->calculated_score,
      'snippet' => search_excerpt($keys, $node->rendered),
      'language' => $node->language,
    );
  }
  return $results;
}