1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
|
<?php
/**
* Blog Calendar
*
* b2evolution - {@link http://b2evolution.net/}
* Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
* @copyright (c)2003-2005 by Francois PLANQUE - {@link http://fplanque.net/}
*
* @package evocore
*/
if( !defined('DB_USER') ) die( 'Please, do not access this page directly.' );
/**
* Calendar
*
* @package evocore
*/
class Calendar
{
var $blog;
var $year, $month;
var $specific; // WE ASKED FOR A SPECIFIC MONTH
var $mode; // 'month' or 'year'
var $where;
var $request; // SQL query string
var $result; // Result set
var $result_num_rows; // Number of rows in result set
var $displaycaption;
var $monthformat;
var $monthstart;
var $monthend;
var $linktomontharchive;
/**
* Where to do the navigation
*
* 'caption' or 'tfoot';
*
* @var string
*/
var $navigation = 'caption';
var $tablestart;
var $tableend;
var $rowstart;
var $rowend;
var $headerdisplay;
var $headerrowstart;
var $headerrowend;
var $headercellstart;
var $headercellend;
var $cellstart;
var $cellend;
var $emptycellstart;
var $emptycellend;
var $emptycellcontent;
var $searchframe;
var $browseyears;
/*
* Calendar::Calendar(-)
*
* Constructor
*/
function Calendar(
$blog = 1,
$m = '',
$show_statuses = array(),
$timestamp_min = '', // Do not show posts before this timestamp
$timestamp_max = 'now' ) // Do not show posts after this timestamp
{
global $Settings;
$this->blog = $blog;
// Find out which month to display:
if( empty($m) )
{
$this->year = date('Y');
$this->month = date('m');
$this->mode = 'month';
}
else
{
$this->specific = true;
$this->year = substr($m, 0, 4);
if (strlen($m) < 6)
{ // no month provided
$this->mode = 'year';
if( $this->year == date('Y') )
{ // we display current year, month gets current
$this->month = date('m');
}
else
{ // highlight no month, when not current year
$this->month = '';
}
}
else
{
$this->month = substr($m, 4, 2);
$this->mode = 'month';
}
}
// CONSTRUCT THE WHERE CLAUSE:
/*
* ----------------------------------------------------
* Restrict to the statuses we want to show:
* ----------------------------------------------------
*/
$where = ' AND '.statuses_where_clause( $show_statuses );
$where_link = ' AND ';
// Restrict to timestamp limits:
if( $timestamp_min == 'now' ) $timestamp_min = time();
if( !empty($timestamp_min) )
{ // Hide posts before
$date_min = date('Y-m-d H:i:s', $timestamp_min + ($Settings->get('time_difference') * 3600) );
$where .= $where_link.' post_issue_date >= \''.$date_min.'\'';
$where_link = ' AND ';
}
if( $timestamp_max == 'now' ) $timestamp_max = time();
if( !empty($timestamp_max) )
{ // Hide posts after
$date_max = date('Y-m-d H:i:s', $timestamp_max + ($Settings->get('time_difference') * 3600) );
$where .= $where_link.' post_issue_date <= \''.$date_max.'\'';
$where_link = ' AND ';
}
// Do we need to restrict categories:
if( $blog > 1 )
{ // Blog #1 aggregates all
$where .= $where_link.' cat_blog_ID = '.$blog;
$where_link = ' AND ';
}
$this->where = $where;
// Default styling:
$this->displaycaption = 1; // set this to 0 if you don't want to display the month name
$this->monthformat = 'F Y';
$this->linktomontharchive = true; // month displayed as link to month' archive
$this->tablestart = '<table class="bCalendarTable" cellspacing="0" summary="Monthly calendar with links to each day\'s posts">'."\n";
$this->tableend = '</table>';
$this->monthstart = '<caption class="bCalendarCaption">';
$this->monthend = "</caption>\n";
$this->rowstart = '<tr class="bCalendarRow">' . "\n";
$this->rowend = "</tr>\n";
$this->headerdisplay = 'D'; // D => 'Fri'; e => 'F', l (lowercase l) => 'Friday'
// These codes are twisted because they're the same as for date formats.
// set this to 0 or '' if you don't want to display the "Mon Tue Wed..." header
$this->headerrowstart = '<thead><tr class="bCalendarRow">' . "\n";
$this->headerrowend = "</tr></thead>\n";
$this->headercellstart = '<th class="bCalendarHeaderCell" abbr="[abbr]" scope="col" title="[abbr]">'; // please leave [abbr] there !
$this->headercellend = "</th>\n";
$this->cellstart = '<td class="bCalendarCell">';
$this->cellend = "</td>\n";
$this->emptycellstart = '<td class="bCalendarEmptyCell">';
$this->emptycellend = "</td>\n";
$this->emptycellcontent = ' ';
$this->linkpostcellstart = '<td class="bCalendarLinkPost">';
$this->linkposttodaycellstart = '<td class="bCalendarLinkPostToday">';
$this->todaycellstart = '<td id="bCalendarToday">';
$this->todaycellstartpost = '<td id="bCalendarToday" class="bCalendarLinkPost">';
$this->searchframe = 12; // How many month will we search back for a post before we give up
$this->browseyears = ($this->mode == 'year'); // browsing years from Calendar's caption
/**#@+
* Display number of posts with days/months
*
* - set to '' (empty) to disable
* - %d gets replaced with the number of posts on that day/month
*/
$this->postcount_month_cell = ''; // in table cell (behind day)
$this->postcount_month_cell_one = ''; // -- " -- [for single post]
$this->postcount_month_atitle = T_('%d posts'); // in archive links title tag
$this->postcount_month_atitle_one = T_('1 post'); // -- " -- [for single post]
#$this->postcount_year_cell = ' (%d)'; // in table cell (behind abbr of month)
$this->postcount_year_cell = '';
$this->postcount_year_cell_one = '';
$this->postcount_year_atitle = T_('%d posts'); // in archive links title tag
$this->postcount_year_atitle_one = T_('1 post'); // in archive links title tag
/**#@-*/
}
/*
* Calendar->set(-)
*
* set a variable
*/
function set( $var, $value )
{
$this->$var = $value;
}
/**
* Calendar->display(-)
*
* display the calendar.
*
* @param string
* @param string
*/
function display( $file = '', $params = '' ) // Page to use for links
{
global $DB;
global $tableposts, $tablepostcats, $tablecategories;
global $weekday, $weekday_abbrev, $weekday_letter, $month, $month_abbrev;
global $start_of_week;
global $Settings;
if( $this->mode == 'month' )
{
$end_of_week = (($start_of_week + 7) % 7);
// Find a month with posts
$searchmonth = $this->month;
$searchyear = $this->year;
for( $i = 0; $i < $this->searchframe; $i++ )
{
$arc_sql = "SELECT COUNT(DISTINCT ID), YEAR(post_issue_date), MONTH(post_issue_date), DAYOFMONTH(post_issue_date) AS myday".
" FROM ($tableposts INNER JOIN $tablepostcats ON ID = postcat_post_ID)".
" INNER JOIN $tablecategories ON postcat_cat_ID = cat_ID".
" WHERE MONTH(post_issue_date) = '$searchmonth' AND YEAR(post_issue_date) = '$searchyear' ".$this->where.
" GROUP BY myday".
" ORDER BY post_issue_date DESC";
$arc_result = $DB->get_results( $arc_sql, ARRAY_A );
if( $DB->num_rows > 0 )
{ // OK we have a month with posts!
foreach( $arc_result as $arc_row )
{
$daysinmonthwithposts[ $arc_row['myday'] ] = $arc_row['COUNT(DISTINCT ID)'];
}
$this->month = $searchmonth;
$this->year = $searchyear;
break; // Don't search any further!
}
elseif ($this->specific)
{ // No post, but we asked for a specific month to be displayed
break; // Don't search any further!
}
else
{ // No, post, let's search in previous month!
$searchmonth = zeroise(intval($searchmonth)-1,2);
if ($searchmonth == '00') {
$searchmonth = '12';
$searchyear = ''.(intval($searchyear)-1);
}
}
}
// echo $this->month,'.',$this->year;
$daysinmonth = intval(date('t', mktime(0, 0, 0, $this->month, 1, $this->year)));
// echo 'days in month=', $daysinmonth;
$datestartofmonth = $this->year.'-'.$this->month.'-01';
$dateendofmonth = $this->year.'-'.$this->month.'-'.$daysinmonth;
// caution: offset bug inside
$calendarblah = get_weekstartend($datestartofmonth, $start_of_week);
if (mysql2date('w', $datestartofmonth) == $start_of_week) {
$calendarfirst = $calendarblah['start'] + 1 + 3600; // adjust for daylight savings time
} else {
$calendarfirst = $calendarblah['end'] - 604799 + 3600; // adjust for daylight savings time
}
//echo 'calendarfirst=', $calendarfirst;
$calendarblah = get_weekstartend($dateendofmonth, $end_of_week);
if (mysql2date('w', $dateendofmonth) == $end_of_week) {
$calendarlast = $calendarblah['start'] + 1;
} else {
$calendarlast = $calendarblah['end'] + 10000;
}
//echo 'calendarlast=', $calendarlast;
// here the offset bug is corrected
if ((intval(date('d', $calendarfirst)) > 1) && (intval(date('m', $calendarfirst)) == intval($this->month))) {
$calendarfirst = $calendarfirst - 604800;
}
// Create links to previous/next month
$previous_month_link = '<a href="'.
archive_link( ($this->month > 1) ? $this->year : ($this->year - 1), ($this->month > 1) ? ($this->month - 1) : 12, '', '', false, $file, $params )
.'" title="'.T_('previous month').'"><</a>';
$next_month_link = '<a href="'.
archive_link( ($this->month < 12) ? $this->year : ($this->year + 1), ($this->month < 12) ? ($this->month + 1) : 1, '', '', false, $file, $params )
.'" title="'.T_('next month').'">></a>';
}
else
{ // mode is 'year'
// Find months with posts
$arc_sql = "SELECT COUNT(DISTINCT ID), MONTH(post_issue_date) AS mymonth ".
"FROM ($tableposts INNER JOIN $tablepostcats ON ID = postcat_post_ID) ".
"INNER JOIN $tablecategories ON postcat_cat_ID = cat_ID ".
"WHERE YEAR(post_issue_date) = '$this->year' ".$this->where.
" GROUP BY mymonth".
" ORDER BY post_issue_date DESC";
$arc_result = $DB->get_results( $arc_sql, ARRAY_A );
if( $DB->num_rows > 0 )
{ // OK we have a month with posts!
foreach( $arc_result as $arc_row )
{
$monthswithposts[ $arc_row['mymonth'] ] = $arc_row['COUNT(DISTINCT ID)'];
}
}
}
if( $this->browseyears )
{ // create links to previous/next year
$previous_year_link = '<a href="'.
archive_link( $this->year - 1, ($this->mode == 'month') ? $this->month : '', '', '', false, $file, $params )
.'" title="'.T_('previous year').'"><<</a> ';
$next_year_link = ' <a href="'.
archive_link( $this->year + 1, ($this->mode == 'month') ? $this->month : '', '', '', false, $file, $params )
.'" title="'.T_('next year').'">>></a>';
}
// ** display everything **
echo $this->tablestart;
// CAPTION :
if( $this->displaycaption )
{ // caption:
echo $this->monthstart;
if( $this->navigation == 'caption' )
{ // Link to previous year:
echo isset( $previous_year_link ) ? $previous_year_link : '';
}
if( $this->mode == 'month' )
{ // MONTH CAPTION:
if( $this->navigation == 'caption' )
{ // Link to previous month:
echo $previous_month_link.' ';
}
if( $this->linktomontharchive )
{ // chosen month with link to archives
echo '<a href="'.archive_link( $this->year, $this->month, '', '', false, $file, $params ).'" title="'.T_('go to month\'s archive').'">';
}
echo date_i18n($this->monthformat, mktime(0, 0, 0, $this->month, 1, $this->year));
if( $this->linktomontharchive )
{ // close link to month archive
echo '</a>';
}
if( $this->navigation == 'caption' )
{ // Link to next month:
echo ' '.$next_month_link;
}
}
else
{ // YEAR CAPTION:
echo date_i18n('Y', mktime(0, 0, 0, 1, 1, $this->year)); // display year
}
if( $this->navigation == 'caption' )
{ // Link to next year:
echo isset( $next_year_link ) ? $next_year_link : '';
}
echo $this->monthend;
}
// HEADER :
if( !empty($this->headerdisplay) && ($this->mode == 'month') )
{ // Weekdays:
echo $this->headerrowstart;
for ($i = $start_of_week; $i < ($start_of_week + 7); $i = $i + 1)
{
echo str_replace('[abbr]', T_($weekday[($i % 7)]), $this->headercellstart);
switch( $this->headerdisplay )
{
case 'e':
// e => 'F'
echo T_($weekday_letter[($i % 7)]);
break;
case 'l':
// l (lowercase l) => 'Friday'
echo T_($weekday[($i % 7)]);
break;
default: // Backward compatibility: any non emty value will display this
// D => 'Fri'
echo T_($weekday_abbrev[($i % 7)]);
}
echo $this->headercellend;
}
echo $this->headerrowend;
}
if( $this->navigation == 'tfoot' )
{ // We want to display navigation in the table footer:
// TODO: YEAR MODE support
echo "<tfoot>\n";
echo "<tr>\n";
echo '<td colspan="'.(($this->mode == 'month') ? '3' : '2' ).'" id="prev">';
echo isset( $previous_year_link ) ? $previous_year_link : '';
echo isset( $previous_month_link ) ? $previous_month_link : '';
echo "</td>\n";
if( $this->mode == 'month' ) echo '<td class="pad"> </td>'."\n";
echo '<td colspan="'.(($this->mode == 'month') ? '3' : '2' ).'" id="next">';
echo isset( $next_month_link ) ? $next_month_link : '';
echo isset( $next_year_link ) ? $next_year_link : '';
echo "</td>\n";
echo "</tr>\n";
echo "</tfoot>\n";
}
// REAL TABLE DATA :
echo $this->rowstart;
if( $this->mode == 'year' )
{
for ($i = 1; $i < 13; $i = $i + 1)
{
if( isset($monthswithposts[ $i ]) )
{
if( $this->month == $i )
{
echo $this->todaycellstartpost;
}
else
{
echo $this->linkpostcellstart;
}
echo '<a href="';
archive_link( $this->year, $i, '', '', true, $file, $params );
echo '"';
if( $monthswithposts[ $i ] > 1 && !empty($this->postcount_year_atitle) )
{ // display postcount
echo ' title="'.sprintf($this->postcount_year_atitle, $monthswithposts[ $i ]).'"';
}
elseif( !empty($this->postcount_year_atitle_one) )
{ // display postcount for one post
echo ' title="'.sprintf($this->postcount_year_atitle_one, 1).'"';
}
echo '>';
}
elseif( $this->month == $i )
{ // current month
echo $this->todaycellstart;
}
else
{
echo $this->cellstart;
}
echo T_($month_abbrev[ zeroise($i, 2) ]);
if( isset($monthswithposts[ $i ]) )
{ // close anchor and show how many posts we have for this month
if( $monthswithposts[ $i ] > 1 && !empty($this->postcount_year_cell) )
{ // display postcount
printf($this->postcount_year_cell, $monthswithposts[ $i ]);
}
elseif( !empty($this->postcount_year_cell_one) )
{ // display postcount for one post
printf($this->postcount_year_cell_one, 1);
}
echo '</a>';
}
echo $this->cellend;
if( $i == 4 || $i == 8 )
{ // new row
echo $this->rowend.$this->rowstart;
}
}
}
else // mode == 'month'
{
$newrow = 0;
$j = 0;
$k = 1;
for($i = $calendarfirst; $i < ($calendarlast + 86400); $i = $i + 86400)
{ // loop day by day (86400 seconds = 24 hours)
if ($newrow == 1)
{ // We need to start a new row:
if( $k > $daysinmonth )
{ // Last day already displayed!
break;
}
echo $this->rowend;
echo $this->rowstart;
$newrow = 0;
}
if (date('m', $i) != $this->month)
{ // empty cell
echo $this->emptycellstart;
echo $this->emptycellcontent;
echo $this->emptycellend;
}
else
{ // This day is in this month
$k = $k + 1;
$calendartoday = (date('Ymd',$i) == date('Ymd', (time() + ($Settings->get('time_difference') * 3600))));
if( isset($daysinmonthwithposts[ date('j', $i) ]) )
{
if( $calendartoday )
{
echo $this->todaycellstartpost;
}
else
{
echo $this->linkpostcellstart;
}
echo '<a href="';
archive_link( $this->year, $this->month, date('d',$i), '', true, $file, $params );
echo '"';
if( $daysinmonthwithposts[ date('j', $i) ] > 1 && !empty($this->postcount_month_atitle) )
{ // display postcount
echo ' title="'.sprintf($this->postcount_month_atitle, $daysinmonthwithposts[ date('j', $i) ]).'"';
}
elseif( !empty($this->postcount_month_atitle_one) )
{ // display postcount for one post
echo ' title="'.sprintf($this->postcount_month_atitle_one, 1).'"';
}
echo '>';
}
elseif ($calendartoday)
{
echo $this->todaycellstart;
}
else
{
echo $this->cellstart;
}
echo date('j',$i);
if( isset($daysinmonthwithposts[ date('j', $i) ]) )
{
if( $daysinmonthwithposts[ date('j', $i) ] > 1 && !empty($this->postcount_month_cell) )
{ // display postcount
printf($this->postcount_month_cell, $daysinmonthwithposts[ date('j', $i) ]);
}
elseif( !empty($this->postcount_month_cell_one) )
{ // display postcount for one post
printf($this->postcount_month_cell_one, 1);
}
echo '</a>';
}
echo $this->cellend;
}
$j = $j + 1;
if ($j == 7)
{ // This was the last day of week, we need to start a new row:
$j = 0;
$newrow = 1;
}
} // loop day by day
} // mode == 'month'
echo $this->rowend;
echo $this->tableend;
} // display(-)
}
?>
|