You are here

function _archive_post_count in Archive 5

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

Returns the count of nodes per day/month/year

Parameters

$type: A string representing the node-type currently being displayed

$date: A date object obtained from _archive_date()

Return value

An array, indexed by date and with post counts as values.

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

File

./archive.module, line 252

Code

function _archive_post_count($type, $date) {
  $final_types = _archive_types_sql_string($type);
  $node_query = db_query(db_rewrite_sql('SELECT 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 next 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 previous 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;
}