You are here

function _archive_post_count in Archive 6

Same name and namespace in other branches
  1. 5 archive.module \_archive_post_count()
  2. 7.2 archive.module \_archive_post_count()
  3. 7 archive.module \_archive_post_count()

Returns the range of dates with nodes. Returns an array with keys of 'years', 'months', and 'days' which map their respective timeframe to the number of posts in that timeframe.

Parameters

$type: The object that is being filtered.

$date: A date object returned from _archive_date().

Return value

An array of the (first year with posts, last year with posts).

1 call to _archive_post_count()
_archive_date in ./archive.module
Parses the current URL and populates an archive date object.

File

./archive.module, line 460

Code

function _archive_post_count($type, $date) {
  $final_types = _archive_types_sql_string($type);
  $node_query = db_query(db_rewrite_sql('SELECT n.uid, n.created FROM {node} n WHERE n.status = 1 ' . $final_types));
  $with_posts = array(
    'years' => array(),
    'months' => array(),
    'days' => array(),
    'next_month_days' => array(),
    'prev_month_days' => array(),
  );
  $next_month = $date->month == 12 ? 1 : $date->month + 1;
  $next_year = $date->month == 12 ? $date->year + 1 : $date->year;
  $prev_month = $date->month == 1 ? 12 : $date->month - 1;
  $prev_year = $date->month == 1 ? $date->year - 1 : $date->year;
  while ($o = db_fetch_object($node_query)) {
    list($year, $month, $day) = explode(' ', format_date($o->created, 'custom', 'Y n j'));

    // Check for current month.
    $with_posts['years'][$year] = array_key_exists($year, $with_posts['years']) ? $with_posts['years'][$year] + 1 : 1;
    if ($date->year && $year == $date->year) {
      $with_posts['months'][$month] = array_key_exists($month, $with_posts['months']) ? $with_posts['months'][$month] + 1 : 1;
      if ($date->month && $month == $date->month) {
        $with_posts['days'][$day] = array_key_exists($day, $with_posts['days']) ? $with_posts['days'][$day] + 1 : 1;
      }
    }

    // Check for previous month.
    if ($year == $next_year && $month == $next_month) {
      if (isset($with_posts['next_month_days'][$day])) {
        $with_posts['next_month_days'][$day]++;
      }
      else {
        $with_posts['next_month_days'][$day] = 1;
      }
    }

    // Check for next month.
    if ($year == $prev_year && $month == $prev_month) {
      if (isset($with_posts['prev_month_days'][$day])) {
        $with_posts['prev_month_days'][$day]++;
      }
      else {
        $with_posts['prev_month_days'][$day] = 1;
      }
    }
  }
  return $with_posts;
}