By Prometheus6 on
This module creates a weekly archive page and a sidebar block with the links to the pages. As written the block shows up on only node, blog and story pages. Also, on the blog pages it generates links to archives that are limited to those written by the blogger whose page you are viewing.
function week_help($section) {
switch($section) {
case "admin/system/modules/week":
$output = "";
break;
case "admin/system/modules#description":
$output = "Create block containing a list of weekly archives.";
break;
default:
$output = "";
break;
}
return $output;
}
function week_perm(){
return array("administer week");
}
function _archive_param(){
// node report or blog/book/whatever?
$page_type = arg(0);
switch ($page_type){
case 'week':
$node_type = arg(1);
if (arg(3)){
$user_no = arg(2);
}
break;
case 'blog':
$node_type = $page_type;
if(arg(1)) {
if (arg(1) !== 'add')
$user_no = arg(1);
}
case 'node':
case 'story':
$node_type = $page_type;
if (arg(2)){
$start_node = arg(2);
} elseif(arg(1)) {
if (arg(1) !== 'add')
$user_no = arg(1);
}
break;
default:
return;
break;
}
$request_params= array("page_type" => $page_type, "node_type" => $node_type, "user_no" => $user_no);
return $request_params;
}
function _week_block_params($param_array){
$output = $param_array["node_type"] ? $param_array["node_type"] != "node" ? 'type = "'.$param_array["node_type"].'"':"":"";
$output .= !empty($output)&&!empty($param_array["user_no"])? ' AND ':'';
$output .= $param_array["user_no"] ? 'uid = '.$param_array["user_no"] :"";
return $output;
}
function _week_path_builder($param_array){
$output = 'week/';
$output .= $param_array["node_type"].'/';
$output .= !empty($param_array["user_no"]) ? $param_array["user_no"].'/':'';
return $output;
}
function week_block($op='list', $delta=0) {
// listing of blocks, such as on the admin/system/block page
if ($op == "list") {
$block[0]["info"] = t("Weekly Archives");
return $block;
} else {
// our block content
$block_content = '';
$block['subject'] = t("Weekly Archives");
// first post for node type and user
$sql_params = _archive_param();
if (!empty($sql_params["node_type"])){
$sql_selector = _week_block_params($sql_params);
$query = 'SELECT MIN(created) AS created, MAX(created) AS max_created from {node}';
$query .= empty($sql_selector) ? '':' WHERE '.$sql_selector;
$query_result = db_fetch_object(db_query($query));
$first_post_date = $query_result->created;
$last_post_date = $query_result->max_created;
$start_day = variable_get('default_firstday', 0);
$date_data = getdate($first_post_date);
$start_date = mktime(0,0,0, $date_data["mon"], $date_data["mday"] - ($date_data["wday"] - $start_day), $date_data["year"]);
$date_data = getdate($last_post_date);
$end_date = mktime(0,0,0, $date_data["mon"], $date_data["mday"] + (6 -($date_data["wday"] - $start_day)), $date_data["year"]);
$arc_path = _week_path_builder($sql_params);
while ($start_date < $end_date) {
// make anchor
$block_content[] = l(t(date('m/d/y',$start_date).' - '.date('m/d/y',$start_date+ 518400)),
$arc_path.$start_date).'<br />';
// decrement $start_date by a week
$start_date += (604800); // a week of seconds
}
// set up the block
$block_content = array_reverse($block_content);
$block['content'] = theme_links($block_content, '');
}
return $block;
}
}
function _weekly_contentQuery($query_date, $param_array){
$date_data = getdate($query_date);
$start_date = $query_date;
$end_date = mktime(23, 59, 59, $date_data['mon'],($date_data['mday'] + 6), $date_data['year']);
$sql_selector = _week_block_params($param_array);
// create the SQL for the query
$query_info[0] = 'SELECT n.nid from node n where (created >= '.$start_date.') and (created <= '.$end_date.') AND (status = 1)';
$query_info[0] .= $sql_selector ? ' AND '.$sql_selector:'';
$query_info[0] .= ' ORDER BY n.created DESC';
$query_info[1] = $start_date;
$query_info[2] = $end_date;
return $query_info;
}
function _weekly_page() {
global $base_url;
$paginate_output = variable_get('archive_lister_paginate', 1);
if (arg(3)){
$arcdate = arg(3);
} else {
$arcdate = arg(2);
}
// create the appropriate query
$param_array = _archive_param();
$query = _weekly_contentQuery($arcdate, $param_array);
if ($paginate_output){
$query_result = pager_query($query[0], variable_get("default_nodes_main", 10));
} else{
$query_result = db_query($query[0]);
}
// for each node returned by the query, theme the node and append it to the output string
while ($outnode = db_fetch_object($query_result)){
$output .= node_view(node_load(array("nid" => $outnode->nid)), 1);
}
$output .= theme('pager', NULL, variable_get("default_nodes_main", 20));
// if there's no output, don't let them stare at a blank page
$output = $output? $output:t("Sorry, there are no entries for this week.");
// return the output string
print theme('page', $output, t('Week of '.date('F d, Y', $query[1]).' to '.date('F d, Y', $query[2])));
}
function week_link($type, $node=0) {
if (($type == "system")) {
menu('week', t("Weekly Archive List"), "_weekly_page", NULL, 1);
}
}
Comments
Thanks for the formatting assist
I thought a BBCode-like [php] would do it, but...
Now with a settings page
I have absolutely given up trying to colorize this code.
This module now has two options in the settings page:
function week_help($section) {
switch($section) {
case "admin/system/modules/week":
$output = "";
break;
case "admin/system/modules#description":
$output = "Create block containing a list of weekly archives.";
break;
default:
$output = "";
break;
}
return $output;
}
function week_perm(){
return array("administer week");
}
function week_settings(){
if (!user_access("administer week")){
return message_access();
}
$op = $_POST["op"];
$config_values = $_POST["edit"];
if ($op){
switch ($op) {
case "Save configuration":
foreach($config_values as $key => $value){
variable_set($key, $value);
}
break;
case "Reset to defaults":
variable_set("rev_chronsort", 0);
variable_set("paginate", 1);
break;
}
}
$output .= form_checkbox (t("Sort block in chronological order"), "rev_chronsort", 1, variable_get("rev_chronsort", 0),
t("If checked the list of archive links will be sorted from oldest to newest. ".
"Otherwise it will be sorted from newest to oldest"));
$output .= form_checkbox (t("Paginate archive page"), "paginate", 1, variable_get("paginate", 1),
t("If checked the output will be split into pages. ".
"The number of posts will respect the global nodes per page setting."));
$output = form_group(t("Configure weekly archives"), $output);
return $output;
}
function _archive_param(){
// node report or blog/book/whatever?
$page_type = arg(0);
switch ($page_type){
case 'week':
$node_type = arg(1);
if (arg(3)){
$user_no = arg(2);
}
break;
case 'blog':
$node_type = $page_type;
if(arg(1)) {
if (arg(1) !== 'add')
$user_no = arg(1);
}
case 'node':
case 'story':
$node_type = $page_type;
if (arg(2)){
$start_node = arg(2);
} elseif(arg(1)) {
if (arg(1) !== 'add')
$user_no = arg(1);
}
break;
default:
return;
break;
}
$request_params= array("page_type" => $page_type, "node_type" => $node_type, "user_no" => $user_no);
return $request_params;
}
function _week_block_params($param_array){
$output = $param_array["node_type"] ? $param_array["node_type"] != "node" ? 'type = "'.$param_array["node_type"].'"':"":"";
$output .= !empty($output)&&!empty($param_array["user_no"])? ' AND ':'';
$output .= $param_array["user_no"] ? 'uid = '.$param_array["user_no"] :"";
return $output;
}
function _week_path_builder($param_array){
$output = 'week/';
$output .= $param_array["node_type"].'/';
$output .= !empty($param_array["user_no"]) ? $param_array["user_no"].'/':'';
return $output;
}
function week_block($op='list', $delta=0) {
// listing of blocks, such as on the admin/system/block page
if ($op == "list") {
$block[0]["info"] = t("Weekly Archives");
return $block;
} else {
// our block content
$block_content = '';
$block['subject'] = t("Weekly Archives");
// first post for node type (and user?)
$sql_params = _archive_param();
if (!empty($sql_params["node_type"])){
$sql_selector = _week_block_params($sql_params);
$query = 'SELECT MIN(created) AS created, MAX(created) AS max_created from {node}';
$query .= empty($sql_selector) ? '':' WHERE '.$sql_selector;
$query_result = db_fetch_object(db_query($query));
$first_post_date = $query_result->created;
$last_post_date = $query_result->max_created;
$start_day = variable_get('default_firstday', 0);
$date_data = getdate($first_post_date);
$start_date = mktime(0,0,0, $date_data["mon"], $date_data["mday"] - ($date_data["wday"] - $start_day), $date_data["year"]);
$date_data = getdate($last_post_date);
$end_date = mktime(0,0,0, $date_data["mon"], $date_data["mday"] + (6 -($date_data["wday"] - $start_day)), $date_data["year"]);
$arc_path = _week_path_builder($sql_params);
while ($start_date < $end_date) {
// make anchor
$block_content[] = l(t(date('m/d/y',$start_date).' - '.date('m/d/y',$start_date+ 518400)),
$arc_path.$start_date).'
';
// decrement $start_date by a week
$start_date += (604800); // a week of seconds
}
// set up the block
$rev_chronsort = variable_get("rev_chronsort", 0);
if (!$rev_chronsort){
$block_content = array_reverse($block_content);
}
$block['content'] = theme_links($block_content, '');
}
return $block;
}
}
function _weekly_contentQuery($query_date, $param_array){
$date_data = getdate($query_date);
$start_date = mktime(0,0,0,$date_data['mon'], $date_data['mday'], $date_data['year']);
$end_date = mktime(23, 59, 59, $date_data['mon'],($date_data['mday'] + 6), $date_data['year']);
$sql_selector = _week_block_params($param_array);
// create the SQL for the query
$query_info[0] = 'SELECT n.nid from node n where (created >= '.$start_date.') and (created <= '.$end_date.') AND (status = 1)';
$query_info[0] .= $sql_selector ? ' AND '.$sql_selector:'';
$query_info[0] .= ' ORDER BY n.created DESC';
$query_info[1] = $start_date;
$query_info[2] = $end_date;
return $query_info;
}
function _weekly_page() {
global $base_url;
$paginate = variable_get("paginate", 1);
if (arg(3)){
$arcdate = arg(3);
} else {
$arcdate = arg(2);
}
// create the appropriate query
$param_array = _archive_param();
$query = _weekly_contentQuery($arcdate, $param_array);
if ($paginate){
$query_result = pager_query($query[0], variable_get("default_nodes_main", 10));
} else{
$query_result = db_query($query[0]);
}
// for each node returned by the query, theme the node and append it to the output string
while ($outnode = db_fetch_object($query_result)){
$output .= node_view(node_load(array("nid" => $outnode->nid)), 1);
}
if ($paginate){
$output .= theme('pager', NULL, variable_get("default_nodes_main", 20));
}
// if there's no output, don't let them stare at a blank page
$output = $output? $output:t("Sorry, there are no entries for this week.");
// return the output string
print theme('page', $output, t('Week of '.date('F d, Y', $query[1]).' to '.date('F d, Y', $query[2])));
}
function week_link($type, $node=0) {
if (($type == "system")) {
menu('week', t("Weekly Archive List"), "_weekly_page", NULL, 1);
}
}
functional?
I don't know if this code and/or discussion has continued elsewhere. If so, please provide a link. I'm working on this code a bit as I need something similar.
It's a little changed but functional
The conversation has NOT continued, but the module did. Since you want to build on it, here's a download link to the version I'm using on my site.
http://www.prometheus6.org/files/drupal-dev/week.zip
I haven't actually released it for general use because it doesn't really get along with the pagination code and I didn't get the filtering right. On the other hand, there's a couple of other changes, like the way the URL works (the week listing will actually will work with any given date, not just your defined first day of the week).
Anyway, feel free to grab it and work it over. And I turned on my contact page for no reason...I don't think you need my input but if you do give me a shout.
Thanks
Thanks very much for the zip. I see you've made a few changes. I was still working on updating it to work with 4.5 instead of 4.4 including some comments and such. I'll let you know when I get a chance to dig into it.
I've checked into CVS a fairly simple blogroll module. I haven't formally created a Drupal project yet since I wanted to add the UI and necessary features to use a weight system similar to the way blocks are configured. I also was busy getting a patch accepted by Dries and Steven for the filter.module. Blogroll simply provides a standard blog feature on a user-blog basis, not site wide like most Drupal modules seem to be.
I want to use your code as a basis for a user blog based monthly archives module. This is another sorely missing blogging feature that shouldn't be too hard, especially after I finish learning and understanding how you have done your week.module. I'll keep you updated. Thanks for enabling your contact page, I may contact you that way when I find the time to implement my plans.
It's not that hard. On a
It's not that hard. On a node page get the nid from the query string, get the uid from the node, stick the uid in a slightly modified query. On a blog page get the uid from the query string, stick the uid in a slightly modified query. I didn't think of it because I'm the only one writing at my site. Plus I still need to get the pager counting query right.
Small bug
Thanks for the tips. I think the hardest part of finishing my blogroll module and doing my new module will be finding the time. FYI, I also found that when I installed it on my site, the link in my user navigation block tried to get the content for Week of December 31, 1969 to December 06, 1999 instead of the current week.
Forgot curly braces around the table name
Thanks.
Interested in week.module
I am excited to know that someone is working on a function that allows searching archives by week - looking forward to it being available...
thanks,
eva
Try this
http://drupal.org/node/15804
where do the curly braces go? around what?
i downloaded the module and all seems to working dandy, except that i get the same error as the above person with the 1969 date - where do the curly braces go - around what specifically? thanks.
ps - the newest version includes the fix - yay! great module - thank you
This is all a nice first
This is all a nice first step - but the blocks generated by this module aren't really visitor friendly. I have the same gripe with the archive module and it's "next/last 1..2..3..4..." link scheme.
What about something a little more readable like this?
Few issues...
Hi,
this is a nice module and i was looking for exactly the same functionality. I installed it, but have a few issues. I am using drupal 4.5.0 at www.chintha.com
Issues:
01. When a link on the archive side block is clicked, the whole page including the left sidebar is loaded on the right content area. Anyone else saw similar issues? You can see the problem here : http://chintha.com/week
02. On the http://chintha.com/week page, the links are not correct too...
03. Another issue is that, it doesn't obey my html filters, textile tags etc. So if i have textile tags in the content, they are printed as it is. No formatting is done on the output.
Requests:
01. Is it possible to exclude links that doesn't have any posts in that week?
02. Is it possible to make links than span two weeks?
Paul
Take a look at
Take a look at week.module.
http://drupal.org/node/15804
If that doesn't work properly for you, leave me a bug report there.
Archives for per-user
Hello,
I'm looking for an archive module which display's the individual user's archive instead of the entire's community archive. noticed that the above code allows for this, since it "generates links to archives that are limited to those written by the blogger whose page you are viewing." but the latest archive module doesnt do as such. is there a patch for this somewhere? thanks for helping! :)