You are here

function photos_search_execute in Album Photos 7.3

Implements hook_search_execute().

File

./photos.module, line 2732
Implementation of photos.module.

Code

function photos_search_execute($keys = NULL, $conditions = NULL) {
  $query = db_select('search_index', 'i', array(
    'target' => 'slave',
  ))
    ->extend('SearchQuery')
    ->extend('PagerDefault')
    ->limit(10);

  // only want photos, not nodes of any kind
  $query
    ->condition('i.type', 'photos');
  $query
    ->leftJoin('search_dataset', 'sd', 'i.sid = sd.sid');
  $query
    ->condition('sd.reindex', '=0');
  $query
    ->searchExpression($keys, 'photos');
  if (!$query
    ->executeFirstPass()) {

    // Only continue if the first pass query matches.
    return array();
  }

  // if we do NOT get here: problem with query?
  $query
    ->addTag('node_access');
  $query
    ->fields('i', array(
    'word',
    'type',
    'score',
  ));
  $query
    ->leftJoin('photos_image', 'pi', 'pi.fid = i.sid');
  $query
    ->fields('pi', array(
    'fid',
    'pid',
    'des',
    'title',
    'wid',
    'count',
  ));
  $query
    ->orderBy('pi.wid', 'ASC');
  $query
    ->leftJoin('photos_album', 'pa', 'pa.pid = pi.pid');
  $query
    ->leftJoin('node', 'n', 'n.nid = pa.pid');
  $query
    ->fields('n', array(
    'title',
    'uid',
    'language',
  ));
  $query
    ->join('file_managed', 'fm', 'fm.fid = pi.fid');
  $query
    ->fields('fm', array(
    'filesize',
    'timestamp',
    'uid',
    'filename',
    'uri',
  ));

  // Load results.
  $found = $query
    ->execute();
  global $user;
  $results = array();

  // process each photo found:
  foreach ($found as $item) {
    $node = node_load($item->pid);

    // If user has "bypass node access" then they can view anything, else
    // test if photos_access() is enabled and any permissions required:
    if (!user_access('bypass node access', $user)) {

      // test for photos_access enabled
      if (module_exists('photos_access')) {

        // if photo is *not* owned by current user, test for access permissions:
        if (!_photos_access('album', $node)) {

          // next photo...
          continue;
        }
      }
    }
    $thumbnail_src = image_style_url('thumbnail', $item->uri);
    if ($item->filesize > 1024 * 1000) {
      $file_size_sfx = 'MB';
      $file_size = number_format($item->filesize / (1024 * 1000), 1);
    }
    elseif ($item->filesize > 1024) {
      $file_size_sfx = 'KB';
      $file_size = number_format($item->filesize / 1024);
    }
    else {
      $file_size_sfx = 'Bytes';
      $file_size = number_format($item->filesize);
    }

    // Iterate through found photos:
    $results[] = array(
      'link' => url('photos/image/' . $item->sid, array(
        'absolute' => TRUE,
      )),
      'type' => $item->type,
      // should always be 'photos',
      'title' => $item->title,
      // User and date appear BEFORE extras (thumbnail) - it's ugly-ish.
      // They've been moved into the extras, after the thumbnail.
      'user' => '',
      'date' => '',
      'extra' => array(
        'extra1' => '<p><a href="' . url('photos/image/' . $item->sid, array(
          'absolute' => TRUE,
        )) . '"><img src="' . $thumbnail_src . '" alt="' . $item->title . '" title="' . check_plain($item->des) . '"  class="photos_search_result"></a> ',
        'extra2' => 'By ' . theme('username', array(
          'account' => $node,
        )) . ' - ' . format_date($item->timestamp, 'short') . ' - ' . $file_size . ' ' . $file_size_sfx . ' ',
        'extra3' => 'Album: <a href="' . url('photos/album/' . $node->nid, array(
          'absolute' => TRUE,
        )) . '" title="Jump to Album">' . check_plain($node->title) . '</a>',
      ),
      'score' => $item->calculated_score,
      'snippet' => search_excerpt($keys, ' ' . $item->des . ' '),
      'language' => $item->language == 'und' ? 'en' : $item->language,
    );
  }
  return $results;
}