function _archive_post_count in Archive 7
Same name and namespace in other branches
- 5 archive.module \_archive_post_count()
- 6 archive.module \_archive_post_count()
- 7.2 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 392 - Implements a block and page showing an archive of all site content.
Code
function _archive_post_count($type, $date) {
$final_types = _archive_types_sql_array($type);
$node_query = db_select('node', 'n');
$node_query
->fields('n', array(
'uid',
'created',
));
$node_query
->condition('n.status', 1);
$node_query
->condition('n.type', $final_types, 'IN');
$node_query
->addTag('node_access');
$result = $node_query
->execute();
$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;
foreach ($result as $o) {
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;
}