You are here

function _sna_blocks_resultset in Simple Node Archive Blocks 7

Same name and namespace in other branches
  1. 6 sna_blocks.module \_sna_blocks_resultset()
  2. 7.2 theme/sna_blocks.theme.inc \_sna_blocks_resultset()

Fectch the records from database and format to an associate array.

Parameters

string|array $value: String contain node machine name for node type archive block. Array contain term ids for taxonomy archive block.

string $type: Type of archive block 'node' or 'terms'.

Return value

array An associate arrry.

2 calls to _sna_blocks_resultset()
theme_sna_blocks_node in ./sna_blocks.module
Returns a chronological archive block for node type.
theme_sna_blocks_taxonomy in ./sna_blocks.module
Returns a chronological archive block for node Vocabulary or Term.

File

./sna_blocks.module, line 214
Provide a simple node archive block

Code

function _sna_blocks_resultset($value, $type) {
  global $language;
  $archive = array();

  // Select current user's language.
  if (module_exists('locale')) {
    $lang = $language->language;
  }
  else {
    $lang = 'und';
  }
  if ($type == 'node') {

    // Build the query for Node type archive.
    $query = db_select('node');
    $query
      ->fields('node', array(
      'nid',
      'type',
      'title',
      'created',
    ));
    $query
      ->condition('status', 1);
    if ($value == 'custom') {
      $custom_selection = variable_get('sna_blocks_custom_selection', array(
        'page',
      ));
      foreach ($custom_selection as $k => $v) {
        if ($v != '0') {
          $condition[] = $v;
        }
      }
      $query
        ->condition('type', $condition, 'IN');
    }
    elseif ($value != 'all') {
      $query
        ->condition('type', $value);
    }
    $query
      ->condition('language', $lang);
    $query
      ->orderBy('created', 'DESC');
    $query
      ->addTag('node_access');
    $results = $query
      ->execute()
      ->fetchAll();
  }
  elseif ($type == 'terms') {

    // Build query for taxonomy type archive.
    $subquery = db_select('taxonomy_index', 'tn');
    $subquery
      ->addField('tn', 'nid');
    $subquery
      ->condition('tid', $value, 'IN');
    $query = db_select('node');
    $query
      ->fields('node', array(
      'nid',
      'type',
      'title',
      'created',
    ));
    $query
      ->condition('status', 1);
    $query
      ->condition('nid', $subquery, 'IN');
    $query
      ->condition('language', $lang);
    $query
      ->orderBy('created', 'DESC');
    $query
      ->addTag('node_access');
    $query
      ->addTag('term_access');
    $results = $query
      ->execute()
      ->fetchAll();
  }
  else {
    return $archive;
  }

  // Adding year and month field.
  foreach ($results as $row) {
    $archive[date('Y', $row->created)][date('F', $row->created)][$row->nid] = $row->title;
  }

  // Adding count field.
  if (!empty($archive)) {
    foreach ($archive as $year => $month) {
      $year_count = 0;
      foreach ($month as $key => $title) {
        $count = count($title, COUNT_RECURSIVE);
        $archive[$year][$key . ' (' . $count . ')'] = $archive[$year][$key];
        unset($archive[$year][$key]);
        $year_count += $count;
      }
      $archive[$year . ' (' . $year_count . ')'] = $archive[$year];
      unset($archive[$year]);
    }
  }
  return $archive;
}