You are here

function _sna_blocks_resultset in Simple Node Archive Blocks 6

Same name and namespace in other branches
  1. 7.2 theme/sna_blocks.theme.inc \_sna_blocks_resultset()
  2. 7 sna_blocks.module \_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 204
Provide a simple node archive block

Code

function _sna_blocks_resultset($value, $type) {
  $archive = array();
  $query = '';
  if ($type == 'node') {
    $query = "SELECT n.nid, n.type, n.title, n.created FROM {node} n WHERE n.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 .= ' AND n.type IN (' . implode(', ', $condition) . ')';
    }
    elseif ($value != 'all') {
      $query .= " AND n.type='" . $value . "'";
    }
    $query .= ' ORDER BY n.created DESC';
    $result = db_query(db_rewrite_sql($query));
    while ($row = db_fetch_object($result)) {
      $archive[date('Y', $row->created)][date('F', $row->created)][$row->nid] = $row->title;
    }
  }
  elseif ($type == 'terms') {

    // Build query for taxonomy type archive.
    $result = db_query(db_rewrite_sql('SELECT n.nid, n.type, n.title, n.created FROM {node} n INNER JOIN {term_node} term_node ON n.vid = term_node.vid WHERE term_node.tid IN (' . implode(', ', $value) . ') AND n.status = 1 ORDER BY n.created DESC'));
    while ($row = db_fetch_object($result)) {
      $archive[date('Y', $row->created)][date('F', $row->created)][$row->nid] = $row->title;
    }
  }
  else {
    return $archive;
  }

  // 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;
}