/[drupal]/contributions/modules/week/week.module
ViewVC logotype

Diff of /contributions/modules/week/week.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.19.2.1.2.6, Tue Sep 1 18:52:37 2009 UTC revision 1.19.2.1.2.7, Tue Sep 1 19:08:25 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  /* $Id: week.module,v 1.19.2.1.2.5 2009/08/31 17:16:19 prometheus6 Exp $ */  /* $Id: week.module,v 1.19.2.1.2.6 2009/09/01 18:52:37 prometheus6 Exp $ */
3  define('ARCHIVEROOTBEFORETYPE', 1);  define('ARCHIVEROOTBEFORETYPE', 1);
4  define('TYPEBEFOREARCHIVEROOT', 2);  define('TYPEBEFOREARCHIVEROOT', 2);
5    
# Line 10  define('TYPEBEFOREARCHIVEROOT', 2); Line 10  define('TYPEBEFOREARCHIVEROOT', 2);
10  function _week_week_data(&$date_data) {  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.    //  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    
14    //  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    //  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
15    //  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.    //  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.
16    $date_first_day = (variable_get('date_first_day', 1) == 0) ? 0 : 1;    $date_first_day = (variable_get('date_first_day', 1) == 0) ? 0 : 1;
17    
18    // 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).    // 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).
19    // the $date_first_day adjustment reflects the practical observation that starting the week with Monday gets you a lower week number    // the $date_first_day adjustment reflects the practical observation that starting the week with Monday gets you a lower week number
20    // for a given date.    // for a given date.
21    $wk_ts = strtotime('+' . ($date_data['week'] - $date_first_day) . ' weeks', strtotime($date_data['year'] .'-01-01'));    $wk_ts = strtotime('+'. ($date_data['week'] - $date_first_day) .' weeks', strtotime($date_data['year'] .'-01-01'));
22    
23    // 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    // 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
24    // 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    // 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
25    // the week starts with Sunday, or one less than the day number if the week starts with Monday    // the week starts with Sunday, or one less than the day number if the week starts with Monday
26    $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);
27    
28    // The end of the week is six days later    // The end of the week is six days later
29    $end_week_ts = strtotime('+6 days', $start_week_ts);    $end_week_ts = strtotime('+6 days', $start_week_ts);
30    
31    $month_names = date_month_names(1);    $month_names = date_month_names(1);
32    $month_names_abbr = date_month_names_abbr(1);    $month_names_abbr = date_month_names_abbr(1);
33    $day_names = date_week_days(1);    $day_names = date_week_days(1);
# Line 85  function week_block($op = 'list', $delta Line 86  function week_block($op = 'list', $delta
86          if ($type) {          if ($type) {
87            $nodetypes = node_get_types('names');            $nodetypes = node_get_types('names');
88            // I have no idea why the inserted string is wrapped in <em> tags...delete the word 'strip_tags' calls if you don't believe me.            // I have no idea why the inserted string is wrapped in <em> tags...delete the word 'strip_tags' calls if you don't believe me.
89            $block['week_' . $type]['info'] = strip_tags(t('Weekly %type archives', array(            $block['week_'. $type]['info'] = strip_tags(t('Weekly %type archives', array(
90    
91              '%type' => $nodetypes[$type]              '%type' => $nodetypes[$type]
92            )));            )));
# Line 104  function week_block($op = 'list', $delta Line 105  function week_block($op = 'list', $delta
105        }        }
106        else {        else {
107          $requested_type = substr($delta, strlen('week_'));          $requested_type = substr($delta, strlen('week_'));
108          $block['subject'] = t('Weekly %type archives', array(          $block['subject'] = t('Weekly %type archives', array(
109            '%type' => node_get_types('name', $requested_type)            '%type' => node_get_types('name', $requested_type)
110          ));          ));
111          $week_include_node_type = array(          $week_include_node_type = array(
112            $requested_type            $requested_type
113          );          );
114        }        }
115        if ($week_include_node_type) {        if ($week_include_node_type) {
116          $week_rev_chronsort = variable_get('week_rev_chronsort', 0);          $week_rev_chronsort = variable_get('week_rev_chronsort', 0);
117          $query = week_links_query($week_include_node_type, $week_rev_chronsort);          $query = week_links_query($week_include_node_type, $week_rev_chronsort);
118          // 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
119          $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);
# Line 129  function week_block($op = 'list', $delta Line 130  function week_block($op = 'list', $delta
130          if (db_fetch_object($weeklist)) {          if (db_fetch_object($weeklist)) {
131            $week_path = variable_get('week_path', 'archive');            $week_path = variable_get('week_path', 'archive');
132            $path_order = variable_get('week_path_order', ARCHIVEROOTBEFORETYPE);            $path_order = variable_get('week_path_order', ARCHIVEROOTBEFORETYPE);
133            $node_path = variable_get('week_' . $week['type'] . '_path', $week['type']);            $node_path = variable_get('week_'. $week['type'] .'_path', $week['type']);
134            $path = $requested_type ? ($path_order == TYPEBEFOREARCHIVEROOT ? $node_path . '/' . $week_path : $week_path . '/' . $node_path) : $week_path;            $path = $requested_type ? ($path_order == TYPEBEFOREARCHIVEROOT ? $node_path .'/'. $week_path : $week_path .'/'. $node_path) : $week_path;
135            $morelink = l(t('more...'), $path);            $morelink = l(t('more...'), $path);
136          }          }
137          if (count($block_content)) {          if (count($block_content)) {
# Line 170  function week_help($section) { Line 171  function week_help($section) {
171   */   */
172  function week_links_query($node_types) {  function week_links_query($node_types) {
173    foreach ($node_types as $key => $value) {    foreach ($node_types as $key => $value) {
174      $node_types[$key] = "'" . db_escape_string($value) . "'";      $node_types[$key] = "'". db_escape_string($value) ."'";
175    }    }
176    $week_rev_linksort = variable_get('week_rev_linksort', 1);    $week_rev_linksort = variable_get('week_rev_linksort', 1);
177    // WEEK(date, 3) gets precisely the ISO-8601 week number that PHP uses so I can calculate the first and last day of the week in the token module hooks    // WEEK(date, 3) gets precisely the ISO-8601 week number that PHP uses so I can calculate the first and last day of the week in the token module hooks
# Line 182  function week_links_query($node_types) { Line 183  function week_links_query($node_types) {
183      DATE_FORMAT(FROM_UNIXTIME(created), '$week_option') AS week,      DATE_FORMAT(FROM_UNIXTIME(created), '$week_option') AS week,
184      MONTH(FROM_UNIXTIME(created)) AS month      MONTH(FROM_UNIXTIME(created)) AS month
185      FROM {node} n ";      FROM {node} n ";
186    $query .= 'WHERE n.type IN (' . implode(',', $node_types) . ') ';    $query .= 'WHERE n.type IN ('. implode(',', $node_types) .') ';
187    $query .= ' AND status = 1 ';    $query .= ' AND status = 1 ';
188    $query .= ' GROUP BY yearweek ORDER BY yearweek ';    $query .= ' GROUP BY yearweek ORDER BY yearweek ';
189    $query .= $week_rev_linksort ? ' ASC' : ' DESC ';    $query .= $week_rev_linksort ? ' ASC' : ' DESC ';
# Line 233  function week_menu() { Line 234  function week_menu() {
234    $archive_root = variable_get('week_path', 'archive');    $archive_root = variable_get('week_path', 'archive');
235    $path_order = variable_get('week_path_order', ARCHIVEROOTBEFORETYPE);    $path_order = variable_get('week_path_order', ARCHIVEROOTBEFORETYPE);
236    foreach ($archive_types as $archive_type) {    foreach ($archive_types as $archive_type) {
237      $node_path = variable_get('week_' . $archive_type . '_path', $archive_type);      $node_path = variable_get('week_'. $archive_type .'_path', $archive_type);
238      $archive_index = $archive_type ? ($path_order == TYPEBEFOREARCHIVEROOT ? $node_path . '/' . $archive_root : $archive_root . '/' . $node_path) : $archive_root;      $archive_index = $archive_type ? ($path_order == TYPEBEFOREARCHIVEROOT ? $node_path .'/'. $archive_root : $archive_root .'/'. $node_path) : $archive_root;
239      $items[$archive_index] = array(      $items[$archive_index] = array(
240        'title' => $archive_type ? "Weekly $archive_type archives links" : 'Weekly archives links',        'title' => $archive_type ? "Weekly $archive_type archives links" : 'Weekly archives links',
241        'page callback' => 'week_link_page',        'page callback' => 'week_link_page',
# Line 247  function week_menu() { Line 248  function week_menu() {
248          $archive_type          $archive_type
249        )        )
250      );      );
251      $items[$archive_index . '/%/%'] = array(      $items[$archive_index .'/%/%'] = array(
252        'title' => $archive_type ? variable_get('week_' . $archive_type . '_title', $archive_type) : variable_get('week_title', 'Weekly archive [month] [yyyy]'),        'title' => $archive_type ? variable_get('week_'. $archive_type .'_title', $archive_type) : variable_get('week_title', 'Weekly archive [month] [yyyy]'),
253        'page callback' => 'week_post_page',        'page callback' => 'week_post_page',
254        'page arguments' => $archive_type ? array(        'page arguments' => $archive_type ? array(
255          2,          2,
# Line 268  function week_menu() { Line 269  function week_menu() {
269    
270    $items['admin/settings/week'] = array(    $items['admin/settings/week'] = array(
271    
272      'title' => t('Weekly archive settings'),      'title' => 'Weekly archive settings',
273      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
274      'page arguments' => array(      'page arguments' => array(
275    
# Line 282  function week_menu() { Line 283  function week_menu() {
283    );    );
284    $items['admin/settings/week/'] = array(    $items['admin/settings/week/'] = array(
285    
286      'title' => t('Weekly archive settings'),      'title' => 'Weekly archive settings',
287      'description' => 'Configure node types for which blocks will be generated.',      'description' => 'Configure node types for which blocks will be generated.',
288      'type' => MENU_DEFAULT_LOCAL_TASK,      'type' => MENU_DEFAULT_LOCAL_TASK,
289      'weight' => 5      'weight' => 5
290    );    );
291    $items['admin/settings/week/type_path'] = array(    $items['admin/settings/week/type_path'] = array(
292    
293      'title' => t('Links and titles'),      'title' => 'Links and titles',
294      'description' => t('Configure path names for node types for which blocks will be generated and date format of the link text.'),      'description' => 'Configure path names for node types for which blocks will be generated and date format of the link text.',
295      'page callback' => 'drupal_get_form',      'page callback' => 'drupal_get_form',
296      'page arguments' => array(      'page arguments' => array(
297    
# Line 349  function week_post_page($year, $week, $i Line 350  function week_post_page($year, $week, $i
350      if (is_string($include_node_types)) {      if (is_string($include_node_types)) {
351        $link_data['type'] = $include_node_types;        $link_data['type'] = $include_node_types;
352        $link_data['type_name'] = node_get_types('name', $include_node_types);        $link_data['type_name'] = node_get_types('name', $include_node_types);
353        $page_title = variable_get('week_' . $include_node_types . '_title', '[type_name] [month] [d], [yyyy] thru [end_month] [end_d], [end_yyyy]');        $page_title = variable_get('week_'. $include_node_types .'_title', '[type_name] [month] [d], [yyyy] thru [end_month] [end_d], [end_yyyy]');
354      }      }
355      else {      else {
356        $page_title = variable_get('week_title', 'Week of [month] [d], [yyyy] thru [end_month] [end_d], [end_yyyy]');        $page_title = variable_get('week_title', 'Week of [month] [d], [yyyy] thru [end_month] [end_d], [end_yyyy]');
# Line 371  function week_post_query($week_include_n Line 372  function week_post_query($week_include_n
372      $week_include_node_types[] = $week_include_node_types;      $week_include_node_types[] = $week_include_node_types;
373    }    }
374    foreach ($week_include_node_types as $key => $value) {    foreach ($week_include_node_types as $key => $value) {
375      $node_types[$key] = "'" . db_escape_string($value) . "'";      $node_types[$key] = "'". db_escape_string($value) ."'";
376    }    }
377    $week_rev_pagesort = variable_get('week_rev_pagesort', 1);    $week_rev_pagesort = variable_get('week_rev_pagesort', 1);
378    $date_first_day = variable_get('date_first_day', 1);    $date_first_day = variable_get('date_first_day', 1);
# Line 379  function week_post_query($week_include_n Line 380  function week_post_query($week_include_n
380    $query = "SELECT n.nid FROM {node} n    $query = "SELECT n.nid FROM {node} n
381      WHERE YEAR(FROM_UNIXTIME(n.created)) = '{$year}'      WHERE YEAR(FROM_UNIXTIME(n.created)) = '{$year}'
382      AND WEEK(FROM_UNIXTIME(n.created), $week_option) = '{$week}'";      AND WEEK(FROM_UNIXTIME(n.created), $week_option) = '{$week}'";
383    $query .= ' AND n.type IN (' . implode(',', $node_types) . ') ';    $query .= ' AND n.type IN ('. implode(',', $node_types) .') ';
384    $query .= ' AND status = 1 ';    $query .= ' AND status = 1 ';
385    $query .= ' ORDER BY n.created';    $query .= ' ORDER BY n.created';
386    $query .= $week_rev_pagesort ? '' : ' DESC ';    $query .= $week_rev_pagesort ? '' : ' DESC ';
# Line 596  function week_settings_type_path_form() Line 597  function week_settings_type_path_form()
597      '#title' => t('Node type archive path'),      '#title' => t('Node type archive path'),
598      '#options' => array(      '#options' => array(
599    
600        ARCHIVEROOTBEFORETYPE => $archive_path . '/node_type',        ARCHIVEROOTBEFORETYPE => $archive_path .'/node_type',
601        TYPEBEFOREARCHIVEROOT => 'node_type/' . $archive_path        TYPEBEFOREARCHIVEROOT => 'node_type/'. $archive_path
602      ),      ),
603      '#default_value' => variable_get('week_path_order', ARCHIVEROOTBEFORETYPE),      '#default_value' => variable_get('week_path_order', ARCHIVEROOTBEFORETYPE),
604      '#description' => 'Select the order in which the parts of node specific archive appear in the path to the archive page. <br />The node type archive paths will be      '#description' => 'Select the order in which the parts of node specific archive appear in the path to the archive page. <br />The node type archive paths will be
# Line 620  function week_settings_type_path_form() Line 621  function week_settings_type_path_form()
621    
622              '#type' => 'textfield',              '#type' => 'textfield',
623              '#title' => node_get_types('name', $node_type) . t(' archive title'),              '#title' => node_get_types('name', $node_type) . t(' archive title'),
624              '#default_value' => variable_get('week_' . $node_type . '_title', '[type_name] [month] [d], [yyyy]')              '#default_value' => variable_get('week_'. $node_type .'_title', '[type_name] [month] [d], [yyyy]')
625            ),            ),
626            'path' => array(            'path' => array(
627    
628              '#type' => 'textfield',              '#type' => 'textfield',
629              '#title' => node_get_types('name', $node_type) . t(' archive path'),              '#title' => node_get_types('name', $node_type) . t(' archive path'),
630              '#default_value' => variable_get('week_' . $node_type . '_path', $node_type)              '#default_value' => variable_get('week_'. $node_type .'_path', $node_type)
631            )            )
632          );          );
633        }        }
# Line 667  function week_settings_type_path_form_re Line 668  function week_settings_type_path_form_re
668    $node_types = node_get_types('names');    $node_types = node_get_types('names');
669    foreach ($node_types as $key => $node_type) {    foreach ($node_types as $key => $node_type) {
670      if ($node_type) {      if ($node_type) {
671        variable_del('week_' . $key . '_path');        variable_del('week_'. $key .'_path');
672        variable_del('week_' . $key . '_title');        variable_del('week_'. $key .'_title');
673      }      }
674    }    }
675    cache_clear_all();    cache_clear_all();
# Line 692  function week_settings_type_path_submit( Line 693  function week_settings_type_path_submit(
693    
694      $edit = $form_state['values']['node_type'];      $edit = $form_state['values']['node_type'];
695      foreach ($edit as $key => $value) {      foreach ($edit as $key => $value) {
696        variable_set('week_' . $key . '_path', $value['path']);        variable_set('week_'. $key .'_path', $value['path']);
697        variable_set('week_' . $key . '_title', $value['title']);        variable_set('week_'. $key .'_title', $value['title']);
698      }      }
699    }    }
700    menu_rebuild();    menu_rebuild();
# Line 848  function _week_linktext($link_data) { Line 849  function _week_linktext($link_data) {
849   */   */
850  function _week_linkpath($link_data, $week_path) {  function _week_linkpath($link_data, $week_path) {
851    $path_order = variable_get('week_path_order', ARCHIVEROOTBEFORETYPE);    $path_order = variable_get('week_path_order', ARCHIVEROOTBEFORETYPE);
852    $node_path = variable_get('week_' . $link_data['type'] . '_path', $link_data['type']);    $node_path = variable_get('week_'. $link_data['type'] .'_path', $link_data['type']);
853    $path = $link_data['type'] ? ($path_order == TYPEBEFOREARCHIVEROOT ? $node_path . '/' . $week_path : $week_path . '/' . $node_path) : $week_path;    $path = $link_data['type'] ? ($path_order == TYPEBEFOREARCHIVEROOT ? $node_path .'/'. $week_path : $week_path .'/'. $node_path) : $week_path;
854    return $path . "/{$link_data['year']}/{$link_data['week']}";    return $path ."/{$link_data['year']}/{$link_data['week']}";
855  }  }
856    
857  /**  /**

Legend:
Removed from v.1.19.2.1.2.6  
changed lines
  Added in v.1.19.2.1.2.7

  ViewVC Help
Powered by ViewVC 1.1.2