You are here

function document_search_table in Document 8.x

Same name and namespace in other branches
  1. 6 document.inc \document_search_table()
  2. 7 document.inc \document_search_table()
2 calls to document_search_table()
document_perform_search in ./document.inc
document_search_form in ./document.search.inc

File

./document.inc, line 140

Code

function document_search_table($conditions) {

  //http://www.rahulsingla.com/projects/drupal-document-module#comment-194

  //During Ajax search, the path is document/search, and hence clicking table sort headers after a search

  //take you to a blank page with only the document table.

  //So, need to change the query to the document page.
  $q = $_GET['q'];
  $_GET['q'] = 'document';
  $headers = array(
    array(
      'data' => t('Type'),
      'field' => 'd.type',
      'class' => array(
        'search-result-header col-type',
      ),
    ),
    array(
      'data' => t('Title'),
      'field' => 'title',
      'sort' => 'asc',
      'class' => array(
        'search-result-header col-title',
      ),
    ),
    array(
      'data' => t('Author'),
      'field' => 'author',
      'class' => array(
        'search-result-header col-author',
      ),
    ),
    array(
      'data' => t('Year of Publication'),
      'field' => 'publish_year',
      'class' => array(
        'search-result-header col-publish-year',
      ),
    ),
    array(
      'data' => t('Keywords'),
      'class' => array(
        'search-result-header col-keywords',
      ),
    ),
    array(
      'data' => '',
      'class' => array(
        'search-result-header col-download',
      ),
    ),
  );
  $query = db_select('node', 'n');
  $query
    ->join('document', 'd', 'n.vid = d.vid');
  $query
    ->condition($conditions)
    ->extend('PagerDefault')
    ->limit(10)
    ->extend('TableSort')
    ->orderByHeader($headers)
    ->fields('n')
    ->fields('d');
  $results = $query
    ->execute();
  $moderate = user_access('moderate document');
  if ($moderate) {
    array_unshift($headers, '');
  }
  $imgUnpublish = theme_image(array(
    'path' => document_image_url('spacer.gif'),
    'alt' => t('Unpublish'),
    'title' => t('Unpublish'),
    'attributes' => array(
      'onclick' => 'doc.changeDocStatus(this, %1$d, \'icon-unpublish\', false);',
      'class' => array(
        'icon-unpublish',
      ),
      'width' => 16,
      'height' => 16,
    ),
  ));
  $imgDelete = theme_image(array(
    'path' => document_image_url('spacer.gif'),
    'alt' => t('Delete'),
    'title' => t('Delete'),
    'attributes' => array(
      'onclick' => 'doc.deleteDoc(this, %1$d, \'icon-delete\');',
      'class' => array(
        'icon-delete',
      ),
      'width' => 16,
      'height' => 16,
    ),
  ));
  $rows = array();
  foreach ($results as $doc) {
    $fileurl = explode(":", $doc->url, 2);
    if ($fileurl[0] == 'http' or $fileurl[0] == 'https') {
      $fileurl = explode("files/", $doc->url);
      $uri = 'public://' . $fileurl[1];
    }
    else {
      $uri = 'public://' . $doc->url;
    }
    $path = file_create_url($uri);
    $download_access = user_access('download document');
    if ($download_access) {
      $url = l(t('Download'), $path, array(
        'attributes' => array(
          'target' => '_blank',
        ),
      ));
    }
    else {
      $path = explode("/sites", $path);
      $path = $path[0] . '/document';
      $url = l(t('Download'), $path, array(
        'attributes' => array(
          'onclick' => 'return alert("You must login to download")',
        ),
      ));
    }
    $row = array(
      check_plain($doc->type),
      l($doc->title, 'node/' . $doc->nid),
      check_plain($doc->author),
      $doc->publish_year,
      check_plain($doc->keywords),
      $url,
    );
    if ($moderate) {
      array_unshift($row, sprintf($imgUnpublish . '   ' . $imgDelete, $doc->nid));
    }
    $rows[] = $row;
  }
  $table = theme('table', array(
    'header' => $headers,
    'rows' => $rows,
  ));

  //Restore the original query.
  $_GET['q'] = $q;
  return $table;
}