<?php
/**
* @file
* A block module that displays recent blog and forum posts.
*/
/**
* Implements hook_help().
*
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
*/
function current_posts_help($path, $arg) {
switch ($path) {
case "admin/help#current_posts":
return '<p>' . t("Displays links to nodes created on this date") . '</p>';
break;
}
}
/**
* Implements hook_block_info().
*/
function current_posts_block_info() {
$blocks['current_posts'] = array(
// The name that will appear in the block list.
'info' => t('Current posts'),
// Default setting.
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Custom content function.
*
* Set beginning and end dates, retrieve posts from database
* saved in that time period.
*
* @return
* A result set of the targeted posts.
*/
function current_posts_contents(){
//Get today's date.
$today = getdate();
//Calculate the date a week ago.
$start_time = mktime(0, 0, 0,$today['mon'],($today['mday'] - 7), $today['year']);
//Get all posts from one week ago to the present.
$end_time = time();
//Use Database API to retrieve current posts.
$query = db_select('node', 'n')
->fields('n', array('nid', 'title', 'created'))
->condition('status', 1) //Published.
->condition('created', array($start_time, $end_time), 'BETWEEN')
->orderBy('created', 'DESC') //Most recent first.
->execute();
return $query;
}
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function current_posts_block_view($delta = '') {
switch ($delta) {
case 'current_posts':
$block['subject'] = t('Current posts');
if (user_access('access content')) {
// Use our custom function to retrieve data.
$result = current_posts_contents();
// Array to contain items for the block to render.
$items = array();
// Iterate over the resultset and format as links.
foreach ($result as $node) {
$items[] = array(
'data' => l($node->title, 'node/' . $node->nid),
);
}
// No content in the last week.
if (empty($items)) {
$block['content'] = t('No posts available.');
}
else {
// Pass data through theme function.
$block['content'] = theme('item_list', array(
'items' => $items));
}
}
return $block;
}
}
/**
* Implements hook_menu().
*/
function current_posts_menu() {
$items = array();
$items['admin/config/content/current_posts'] = array(
'title' => 'Current posts',
'description' => 'Configuration for Current posts module',
'page callback' => 'drupal_get_form',
'page arguments' => array('current_posts_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
------------------------------------------
current_posts.info
name = Current Posts
description = A block module that lists links to recent posts.
core = 7.x
configure = admin/config/content/current_posts
No comments:
Post a Comment