| 1 |
<?php |
<?php |
| 2 |
/* $Id: week.module,v 1.19.2.1.2.2 2009/08/30 23:15:27 prometheus6 Exp $ */ |
/* $Id: week.module,v 1.19.2.1.2.3 2009/08/30 23:49:20 prometheus6 Exp $ */ |
| 3 |
define('ARCHIVEROOTBEFORETYPE', 1); |
define('ARCHIVEROOTBEFORETYPE', 1); |
| 4 |
define('TYPEBEFOREARCHIVEROOT', 2); |
define('TYPEBEFOREARCHIVEROOT', 2); |
| 5 |
|
|
| 6 |
function _week_week_data(&$link_data) { |
/** |
| 7 |
// calculate the start and end of the week |
* Calculate beginning and end dates for a given year and week |
| 8 |
|
* @param $date_data must have at least $date_data['year'] and $date_data['week'] populated |
| 9 |
|
*/ |
| 10 |
|
function _week_week_data(&$date_data) { |
| 11 |
|
// This is a mess because there's no function that gives you the first day of the week for a given year and week combination. |
| 12 |
|
|
| 13 |
|
// Drupal allows you to set any day as the first day of the week. However, the ISO8601 standard specifies Monday as the first day of the week |
| 14 |
|
// and the USofA standard specifies Sunday. So we are going to make Sunday the first day of the week if specified; otherwise it will be Monday. |
| 15 |
$date_first_day = (variable_get('date_first_day', 1) == 0) ? 0 : 1; |
$date_first_day = (variable_get('date_first_day', 1) == 0) ? 0 : 1; |
| 16 |
$wk_ts = strtotime('+' . ($link_data['week'] - $date_first_day) . ' weeks', strtotime($link_data['year'] .'-01-01')); |
|
| 17 |
|
// First we need to create a date variable holding SOME day in the given week (the first day of the year, plus a number of weeks). |
| 18 |
|
// the $date_first_day adjustment reflects the practical observation that starting the week with Monday gets you a lower week number |
| 19 |
|
// for a given date. |
| 20 |
|
$wk_ts = strtotime('+' . ($date_data['week'] - $date_first_day) . ' weeks', strtotime($date_data['year'] .'-01-01')); |
| 21 |
|
|
| 22 |
|
// Now that we have a time stamp that falls somewhere in the week specified by the week number, we want a timestamp for the first day |
| 23 |
|
// of the week. We get the day number of the date in this timestamp (0 = Sunday), subtract that number of days from the timestamp if |
| 24 |
|
// the week starts with Sunday, or one less than the day number if the week starts with Monday |
| 25 |
$start_week_ts = strtotime('-' . (date('w', $wk_ts) - $date_first_day) . ' days', $wk_ts); |
$start_week_ts = strtotime('-' . (date('w', $wk_ts) - $date_first_day) . ' days', $wk_ts); |
| 26 |
|
|
| 27 |
|
// The end of the week is six days later |
| 28 |
$end_week_ts = strtotime('+6 days', $start_week_ts); |
$end_week_ts = strtotime('+6 days', $start_week_ts); |
| 29 |
|
|
| 30 |
$link_data['month'] = date('F', $start_week_ts); |
$date_data['month'] = date('F', $start_week_ts); |
| 31 |
$link_data['mon'] = date('M', $start_week_ts); |
$date_data['mon'] = date('M', $start_week_ts); |
| 32 |
$link_data['monthno'] = date('m', $start_week_ts); |
$date_data['monthno'] = date('m', $start_week_ts); |
| 33 |
$link_data['day'] = date('d', $start_week_ts); |
$date_data['day'] = date('d', $start_week_ts); |
| 34 |
$link_data['dayname'] = date('l', $start_week_ts); |
$date_data['dayname'] = date('l', $start_week_ts); |
| 35 |
$link_data['sdayname'] = date('D', $start_week_ts); |
$date_data['sdayname'] = date('D', $start_week_ts); |
| 36 |
|
|
| 37 |
$link_data['end_year'] = date('Y', $end_week_ts); |
$date_data['end_year'] = date('Y', $end_week_ts); |
| 38 |
$link_data['end_month'] = date('F', $end_week_ts); |
$date_data['end_month'] = date('F', $end_week_ts); |
| 39 |
$link_data['end_mon'] = date('M', $end_week_ts); |
$date_data['end_mon'] = date('M', $end_week_ts); |
| 40 |
$link_data['end_monthno'] = date('m', $end_week_ts); |
$date_data['end_monthno'] = date('m', $end_week_ts); |
| 41 |
$link_data['end_day'] = date('d', $end_week_ts); |
$date_data['end_day'] = date('d', $end_week_ts); |
| 42 |
$link_data['end_dayname'] = date('l', $end_week_ts); |
$date_data['end_dayname'] = date('l', $end_week_ts); |
| 43 |
$link_data['end_sdayname'] = date('D', $end_week_ts); |
$date_data['end_sdayname'] = date('D', $end_week_ts); |
|
$link_data['end_year'] = date('Y', $end_week_ts); |
|
| 44 |
} |
} |
| 45 |
|
|
| 46 |
|
/** |
| 47 |
|
* |
| 48 |
|
* @param $archive_type |
| 49 |
|
* @return boolean |
| 50 |
|
*/ |
| 51 |
function week_access_page($archive_type) { |
function week_access_page($archive_type) { |
| 52 |
if (is_string($archive_type)) { |
if (is_string($archive_type)) { |
| 53 |
$archive_types = variable_get('week_block_node_type', array()); |
$archive_types = variable_get('week_block_node_type', array()); |
| 54 |
return user_access('access content') and ($archive_types[$archive_type]); |
return user_access('access content') and ($archive_types[$archive_type]); |
| 55 |
} |
} |
| 56 |
elseif (is_null($archive_type)) {return TRUE;} |
elseif (is_null($archive_type)) {return user_access('access content');} |
| 57 |
} |
} |
| 58 |
|
|
| 59 |
|
/** |
| 60 |
|
* |
| 61 |
|
* hook_block() implementation |
| 62 |
|
*/ |
| 63 |
function week_block($op = 'list', $delta = 0, $edit = array()) { |
function week_block($op = 'list', $delta = 0, $edit = array()) { |
| 64 |
switch ($op) { |
switch ($op) { |
| 65 |
// listing of blocks, such as on the admin/system/block page |
// listing of blocks, such as on the admin/system/block page |
| 100 |
} |
} |
| 101 |
if ($week_include_node_type) { |
if ($week_include_node_type) { |
| 102 |
$week_rev_chronsort = variable_get('week_rev_chronsort', 0); |
$week_rev_chronsort = variable_get('week_rev_chronsort', 0); |
| 103 |
$query = week_block_links_query($week_include_node_type, $week_rev_chronsort); |
$query = week_links_query($week_include_node_type, $week_rev_chronsort); |
| 104 |
// plus one is the easiest way to find out if you need a "more" link |
// plus one is the easiest way to find out if you need a "more" link |
| 105 |
$weeklist = db_query_range(db_rewrite_sql($query, 'n', 'nid'), $week_max_block_links + 1); |
$weeklist = db_query_range(db_rewrite_sql($query, 'n', 'nid'), $week_max_block_links + 1); |
| 106 |
|
|
| 130 |
} |
} |
| 131 |
} |
} |
| 132 |
|
|
| 133 |
|
/** |
| 134 |
|
* |
| 135 |
|
* (Really weak) hook_help() implementation |
| 136 |
|
*/ |
| 137 |
function week_help($section) { |
function week_help($section) { |
| 138 |
switch ($section) { |
switch ($section) { |
| 139 |
case 'admin/settings/week' : |
case 'admin/settings/week' : |
| 150 |
} |
} |
| 151 |
|
|
| 152 |
/** |
/** |
| 153 |
* Create SQL for links query |
* The archive link query builder |
| 154 |
* |
* |
| 155 |
* @param array $node_types |
* @param array $node_types |
| 156 |
* @return string |
* @return string |
| 157 |
*/ |
*/ |
| 158 |
function week_block_links_query($node_types) { |
function week_links_query($node_types) { |
| 159 |
foreach ($node_types as $key => $value) { |
foreach ($node_types as $key => $value) { |
| 160 |
$node_types[$key] = "'" . db_escape_string($value) . "'"; |
$node_types[$key] = "'" . db_escape_string($value) . "'"; |
| 161 |
} |
} |
| 173 |
$query .= ' AND status = 1 '; |
$query .= ' AND status = 1 '; |
| 174 |
$query .= ' GROUP BY yearweek ORDER BY yearweek '; |
$query .= ' GROUP BY yearweek ORDER BY yearweek '; |
| 175 |
$query .= $week_rev_linksort ? ' ASC' : ' DESC '; |
$query .= $week_rev_linksort ? ' ASC' : ' DESC '; |
|
// drupal_set_message($query); |
|
| 176 |
return $query; |
return $query; |
| 177 |
} |
} |
| 178 |
|
|
| 188 |
else { |
else { |
| 189 |
$week_include_node_type = variable_get('week_include_node_type', array_keys(node_get_types('names'))); |
$week_include_node_type = variable_get('week_include_node_type', array_keys(node_get_types('names'))); |
| 190 |
} |
} |
| 191 |
$query = week_block_links_query($week_include_node_type); |
$query = week_links_query($week_include_node_type); |
| 192 |
$weeklist = db_query(db_rewrite_sql($query, 'n', 'nid')); |
$weeklist = db_query(db_rewrite_sql($query, 'n', 'nid')); |
| 193 |
$block_content = array(); |
$block_content = array(); |
| 194 |
$listed_links = 0; |
$listed_links = 0; |
| 210 |
return theme('item_list', $block_content); |
return theme('item_list', $block_content); |
| 211 |
} |
} |
| 212 |
|
|
| 213 |
|
/** |
| 214 |
|
* hook_menu() implementation |
| 215 |
|
*/ |
| 216 |
function week_menu() { |
function week_menu() { |
| 217 |
$archive_types = array_flip(variable_get('week_block_node_type', array())); |
$archive_types = array_flip(variable_get('week_block_node_type', array())); |
| 218 |
$archive_types['all'] = NULL; |
$archive_types['all'] = NULL; |
| 293 |
return $items; |
return $items; |
| 294 |
} |
} |
| 295 |
|
|
| 296 |
|
/** |
| 297 |
|
* hook_perm() implementation |
| 298 |
|
*/ |
| 299 |
function week_perm() { |
function week_perm() { |
| 300 |
return array( |
return array( |
| 301 |
|
|
| 303 |
); |
); |
| 304 |
} |
} |
| 305 |
|
|
| 306 |
|
/** |
| 307 |
|
* Generate the archive page |
| 308 |
|
* @param $year |
| 309 |
|
* @param $week |
| 310 |
|
* @param $include_node_types |
| 311 |
|
* @return unknown_type |
| 312 |
|
*/ |
| 313 |
function week_post_page($year, $week, $include_node_types = NULL) { |
function week_post_page($year, $week, $include_node_types = NULL) { |
| 314 |
if (is_numeric($year) and is_numeric($week)) { |
if (is_numeric($year) and is_numeric($week)) { |
| 315 |
$default_nodes_main = variable_get('default_nodes_main', 20); |
$default_nodes_main = variable_get('default_nodes_main', 20); |
| 321 |
|
|
| 322 |
$node_query_str = week_post_query($week_include_node_types, $year, $week); |
$node_query_str = week_post_query($week_include_node_types, $year, $week); |
| 323 |
|
|
|
// $node_query_str = db_rewrite_sql(montharchive_post_query($montharchive_include_node_types, $year, $month), 'n', 'node'); |
|
| 324 |
$node_query = $week_paginate ? pager_query($node_query_str, $default_nodes_main, 0) : db_query($node_query_str); |
$node_query = $week_paginate ? pager_query($node_query_str, $default_nodes_main, 0) : db_query($node_query_str); |
| 325 |
while ($nid = db_fetch_array($node_query)) { |
while ($nid = db_fetch_array($node_query)) { |
| 326 |
$node = node_load($nid['nid']); |
$node = node_load($nid['nid']); |
| 346 |
return $page ? $page : ''; |
return $page ? $page : ''; |
| 347 |
} |
} |
| 348 |
|
|
| 349 |
|
/** |
| 350 |
|
* the archive page query builder |
| 351 |
|
* @param $week_include_node_types |
| 352 |
|
* @param $year |
| 353 |
|
* @param $week |
| 354 |
|
* @return unknown_type |
| 355 |
|
*/ |
| 356 |
function week_post_query($week_include_node_types, $year, $week) { |
function week_post_query($week_include_node_types, $year, $week) { |
| 357 |
if (is_string($week_include_node_types)) { |
if (is_string($week_include_node_types)) { |
| 358 |
$week_include_node_types[] = $week_include_node_types; |
$week_include_node_types[] = $week_include_node_types; |
| 373 |
return $query; |
return $query; |
| 374 |
} |
} |
| 375 |
|
|
| 376 |
|
/** |
| 377 |
|
* Creates the form to enable node type blocks |
| 378 |
|
* @return array |
| 379 |
|
*/ |
| 380 |
function week_settings_form() { |
function week_settings_form() { |
| 381 |
$output['week_all'] = array( |
$output['week_all'] = array( |
| 382 |
|
|
| 643 |
return $output; |
return $output; |
| 644 |
} |
} |
| 645 |
|
|
| 646 |
|
/** |
| 647 |
|
* week_settings_type_path_form reset function |
| 648 |
|
*/ |
| 649 |
function week_settings_type_path_form_reset() { |
function week_settings_type_path_form_reset() { |
| 650 |
variable_del('week_link_text'); |
variable_del('week_link_text'); |
| 651 |
variable_del('week_path'); |
variable_del('week_path'); |
| 661 |
cache_clear_all(); |
cache_clear_all(); |
| 662 |
} |
} |
| 663 |
|
|
| 664 |
|
/** |
| 665 |
|
* week_settings_type_path_form submit function |
| 666 |
|
* @param $form |
| 667 |
|
* @param $form_state |
| 668 |
|
*/ |
| 669 |
function week_settings_type_path_submit($form, &$form_state) { |
function week_settings_type_path_submit($form, &$form_state) { |
| 670 |
if ($form_state['clicked_button']['#value'] == 'Reset to defaults') { |
if ($form_state['clicked_button']['#value'] == 'Reset to defaults') { |
| 671 |
week_settings_type_path_form_reset(); |
week_settings_type_path_form_reset(); |
| 686 |
menu_rebuild(); |
menu_rebuild(); |
| 687 |
} |
} |
| 688 |
|
|
| 689 |
|
/** |
| 690 |
|
* week_settings_type_path_form validate function |
| 691 |
|
* @param $form |
| 692 |
|
* @param $form_state |
| 693 |
|
*/ |
| 694 |
function week_settings_type_path_validate($form, &$form_state) { |
function week_settings_type_path_validate($form, &$form_state) { |
| 695 |
$found = preg_match('%^[a-z|0-9|_]+$%', $form_state['values']['week_path']); |
$found = preg_match('%^[a-z|0-9|_]+$%', $form_state['values']['week_path']); |
| 696 |
if (!$found) { |
if (!$found) { |