/[drupal]/contributions/modules/archive/archive.pages.inc
ViewVC logotype

Diff of /contributions/modules/archive/archive.pages.inc

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

revision 1.2 by susurrus, Fri Aug 24 16:03:17 2007 UTC revision 1.3 by susurrus, Sat Aug 25 05:49:12 2007 UTC
# Line 8  Line 8 
8   *   Number of year   *   Number of year
9   *   * @param $month   *   * @param $month
10   *   Number of month   *   Number of month
11   *   * @param $month   *   * @param $month
12   *   Number of day   *   Number of day
13   * @return   * @return
14   *   A string with the themed page   *   A string with the themed page
15   */   */
16  function _archive_page($type = 'all', $year = NULL, $month = NULL, $day = NULL) {  function _archive_page($type = 'all', $year = NULL, $month = NULL, $day = NULL) {
17      // Check that there's nodes we can
18      $nodes = db_result(db_query(db_rewrite_sql('SELECT COUNT(1) FROM {node} n WHERE n.status = 1 AND n.type IN (\''. implode('\',\'', variable_get('archive_type_filters', array())) .'\')')));
19      if (!$nodes) {
20        return t('No content');
21      }
22    
23    drupal_add_css(drupal_get_path('module', 'archive') .'/archive.css');    drupal_add_css(drupal_get_path('module', 'archive') .'/archive.css');
24    
25    $date = _archive_date($type, $year, $month, $day);    $date = _archive_date($type, $year, $month, $day);
# Line 23  function _archive_page($type = 'all', $y Line 29  function _archive_page($type = 'all', $y
29    $query = _archive_query($type, $date);    $query = _archive_query($type, $date);
30    $result = pager_query(db_rewrite_sql(array_shift($query)), $nodes, 0, NULL, $query);    $result = pager_query(db_rewrite_sql(array_shift($query)), $nodes, 0, NULL, $query);
31    
32    if (db_num_rows($result)) {    $found_rows = FALSE;
33      while ($node = db_fetch_object($result)) {    while ($node = db_fetch_object($result)) {
34        $output .= node_view(node_load($node->nid), TRUE);      $output .= node_view(node_load($node->nid), TRUE);
35      }      $found_rows = TRUE;
36      }
37      if ($found_rows) {
38      $output .= theme('pager', NULL, $nodes);      $output .= theme('pager', NULL, $nodes);
39    }    }
40    elseif ($date->days[$date->day] > 0) {    elseif ($date->days[$date->day] > 0) {
# Line 62  function _archive_date($type, $year = NU Line 70  function _archive_date($type, $year = NU
70      return $date;      return $date;
71    }    }
72    $date->tz = _archive_get_timezone();    $date->tz = _archive_get_timezone();
   $date->now = time();  
73    
74    $date->year  = 0;    $date->year  = 0;
75    $date->month = 0;    $date->month = 0;
76    $date->day   = 0;    $date->day   = 0;
   
77    if (_archive_validate_year($year)) {    if (_archive_validate_year($year)) {
78      $date->year = $year;      $date->year = $year;
79      if (_archive_validate_month($month)) {      if (_archive_validate_month($month)) {
# Line 78  function _archive_date($type, $year = NU Line 84  function _archive_date($type, $year = NU
84      }      }
85    }    }
86    else {    else {
87      $date->year = gmdate('Y', $date->now + $date->tz);      $date->year = gmdate('Y', time() + $date->tz);
88    }    }
89    
90    $date->start_of_month = gmmktime(0, 0, 0, $date->month, 1, $date->year);    $date->years  = _archive_post_count('year', $type, $date);
91    $date->month_last_day = gmdate('t', $date->start_of_month);    $date->months = _archive_post_count('month', $type, $date);
92    $date->end_of_month   = gmmktime(23, 59, 59, $date->month, $date->month_last_day, $date->year);    $date->days   = _archive_post_count('day', $type, $date);
   
   $date->years  = _archive_years($type, $date);  
   $date->months = _archive_months($type, $date);  
   $date->days   = _archive_days($type, $date);  
93    
94    return $date;    return $date;
95  }  }
# Line 145  function _archive_types_sql_string($type Line 147  function _archive_types_sql_string($type
147            unset($types[$key]);            unset($types[$key]);
148          }          }
149        }        }
150        $final_types = join(array_keys($types), '", "');        $final_types = implode('", "', array_keys($types));
151      }      }
152    }    }
153    if (strlen($final_types) > 0) {    if (strlen($final_types) > 0) {
# Line 164  function _archive_types_sql_string($type Line 166  function _archive_types_sql_string($type
166   * @return   * @return
167   *    An array of the (first year with posts, last year with posts).   *    An array of the (first year with posts, last year with posts).
168   */   */
169  function _archive_years($type, $date) {  function _archive_post_count($timeframe, $type, $date) {
   $final_types = _archive_types_sql_string($type);  
   $result = db_query(db_rewrite_sql('SELECT MIN(created) AS min_date, MAX(created) AS max_date FROM {node} n WHERE n.status = 1 ' . $final_types));  
   $min_max = db_fetch_object($result);  
   return array(gmdate('Y', $min_max->min_date + $date->tz), gmdate('Y', $min_max->max_date + $date->tz));  
 }  
   
 /**  
  * Returns the months of a given year which have nodes.  
  * Note: FROM_UNIXTIME could be used, but it is MySQL only and has timezone issues.  
  *  
  * @param $type  
  *    The current type we're filtering  
  * @param $date  
  *    A date object obtained from _archive_date()  
  * @return  
  *    An array of months with number of posts for each.  
  */  
 function _archive_months($type, $date) {  
   $final_types = _archive_types_sql_string($type);  
   $months_with_posts = array();  
   foreach (range(1, 12) as $month) {  
     $start  = gmmktime(0, 0, 0, $month, 1, $date->year) - $date->tz;  
     $end    = gmmktime(0, 0, 0, $month + 1, 1, $date->year) - $date->tz;  
     $result = db_query(db_rewrite_sql('SELECT COUNT(*) AS count FROM {node} n WHERE n.status = 1 ' . $final_types . 'AND n.created >= %d AND n.created < %d'), $start, $end);  
     $row = db_fetch_object($result);  
     $months_with_posts[$month] = $row->count;  
   }  
   return $months_with_posts;  
 }  
   
 /**  
  * Returns the days of a given month which have nodes.  
  *  
  * @param $type  
  *    The current node type we're filtering  
  * @param $date  
  *    A date object obtained from _archive_date()  
  * @return  
  *    An array of days having posts with number of posts for each.  
  */  
 function _archive_days($type, $date) {  
170    $final_types = _archive_types_sql_string($type);    $final_types = _archive_types_sql_string($type);
171      $result = db_fetch_object(db_query(db_rewrite_sql('SELECT MIN(n.created) AS min_date, MAX(n.created) AS max_date FROM {node} n WHERE n.status = 1 ' . $final_types)));
172    $result = db_query(db_rewrite_sql('SELECT n.nid, n.created FROM {node} n WHERE n.status = 1 ' . $final_types . 'AND n.created BETWEEN %d AND %d ORDER BY n.created'), $date->start_of_month - $date->tz, $date->end_of_month - $date->tz);    $result->min_date += $date->tz;
173      $result->max_date += $date->tz;
174    $days_with_posts = array();    $first_date = new DateTime("@{$result->min_date}");
175    while ($day_with_post = db_fetch_object($result)) {    $last_date = new DateTime("@{$result->max_date}");
176      $daynum = gmdate('j', $day_with_post->created + $date->tz);    $mod = '';
177      if (isset($days_with_posts[$daynum])) {    switch ($timeframe) {
178        $days_with_posts[$daynum]++;      case 'year':
179      }        $mod = 'Y';
180      else {        $first_date->setDate($first_date->format('Y'), 1, 1);
181        $days_with_posts[$daynum] = 1;        $first_date->setTime(0, 0, 0);
182          $last_date->setDate($last_date->format('Y'), 1, 1);
183          $last_date->setTime(0, 0, 0);
184          break;
185        case 'month':
186          $mod = 'n';
187          $first_date->setDate($first_date->format('Y'), $first_date->format('n'), 1);
188          $first_date->setTime(0, 0, 0);
189          $last_date->setDate($last_date->format('Y'), $last_date->format('n'), 1);
190          $last_date->setTime(0, 0, 0);
191          break;
192        case 'day':
193          $mod = 'j';
194          $first_date->setDate($first_date->format('Y'), $first_date->format('n'), $first_date->format('j'));
195          $first_date->setTime(0, 0, 0);
196          $last_date->setDate($last_date->format('Y'), $last_date->format('n'), $last_date->format('j'));
197          $last_date->setTime(0, 0, 0);
198          break;
199      }
200      $start_timeframe = $first_date;
201      $with_posts = array();
202      while ($start_timeframe->format($mod) <= $last_date->format($mod)){
203        $end_timeframe = clone $start_timeframe;
204        $end_timeframe->modify('+1 '. $timeframe);
205        $sql = db_rewrite_sql('SELECT COUNT(*) AS count FROM {node} n WHERE n.status = 1 ' . $final_types . 'AND n.created >= %d AND n.created < %d');
206        $result = db_result(db_query($sql, $start_timeframe->format('U') - $date->tz, $end_timeframe->format('U') - $date->tz));
207        if ($result) {
208          $with_posts[$start_timeframe->format($mod)] = (int)$result;
209      }      }
210        $start_timeframe = $end_timeframe;
211    }    }
212      return $with_posts;
   return $days_with_posts;  
213  }  }
214    
215  /**  /**
# Line 235  function _archive_days($type, $date) { Line 223  function _archive_days($type, $date) {
223  function _archive_node_types($date) {  function _archive_node_types($date) {
224    
225    $types = variable_get('archive_type_filters', array());    $types = variable_get('archive_type_filters', array());
226    if ($types[0] == '1') {  
     return array();  
   }  
227    foreach ($types as $key => $value) {    foreach ($types as $key => $value) {
228      if (!$value) {      if (!$value) {
229        unset($types[$key]);        unset($types[$key]);
# Line 443  function theme_archive_navigation($type, Line 429  function theme_archive_navigation($type,
429   * Theme the list of years for the archive navigation.   * Theme the list of years for the archive navigation.
430   * @ingroup themeable   * @ingroup themeable
431   */   */
432  function theme_archive_navigation_years($type, $date) {  function theme_archive_navigation_years($type, $date) {
433    $num_to_display = 3;    $all_years = array_keys($date->years);
   $right_offset = min(gmdate('Y', $date->now + $date->tz) - $date->year, 2);  
   $left_offset  = $num_to_display - $right_offset;  
   
434    $output = "<ul id=\"archive-years\">\n";    $output = "<ul id=\"archive-years\">\n";
435    for ($year = ($date->year - $left_offset); $year <= $date->year + $right_offset; $year++) {    for ($year = min($all_years); $year <= max($all_years); $year++) {
     $class = '';  
436    
437      if ($year > gmdate('Y', $date->now + $date->tz)) {      if ($year == $date->year) {
438        $class = ' class="future"';        $output .= '<li class="selected">'. l($year, _archive_url($type, $date, $year)). "</li>\n";
     }  
     elseif ($year == $date->year) {  
       $class = ' class="selected"';  
     }  
   
     $name = gmdate('Y', gmmktime(0, 0, 0, 1, 1, $year));  
     if (min($date->years) <= $year && $year <= max($date->years)) {  
         $output .= "<li$class>". l($name, _archive_url($type, $date, $year)). "</li>\n";  
439      }      }
440      else {      else {
441        $output .= "<li$class>$name</li>\n";        $output .= "<li>$name</li>\n";
442      }      }
443    }    }
444    $output .= "</ul>\n";    $output .= "</ul>\n";
# Line 498  function theme_archive_navigation_days($ Line 472  function theme_archive_navigation_days($
472    $output = "<ul id=\"archive-days\">\n";    $output = "<ul id=\"archive-days\">\n";
473    
474    for ($day = 1; $day <= $day_stop; $day++) {    for ($day = 1; $day <= $day_stop; $day++) {
475      $posts = !empty($date->days[$day]) ? $date->days[$day] : 0;      $posts = array_key_exists($day, $date->days) ? $date->days[$day] : 0;
476      $class = ($day == $date->day) ? ' class="selected"' : '';      $class = ($day == $date->day) ? ' class="selected"' : '';
477      $name = gmdate('d', gmmktime(0, 0, 0, $date->month, $day, $date->year));      $output .= "<li$class>". ($posts ? l($day, _archive_url($type, $date, 0, 0, $day), array('attributes' => array("title" => format_plural($posts, "1 post", "@count posts")))) : $day) ."</li>\n";
     $output .= "<li$class>". ($posts ? l($name, _archive_url($type, $date, 0, 0, $day), array('attributes' => array("title" => format_plural($posts, "1 post", "@count posts")))) : $name) ."</li>\n";  
478    }    }
479    $output .= "</ul>\n";    $output .= "</ul>\n";
480    return $output;    return $output;

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.3