Index: trunk/extensions/SemanticResultFormats/Calendar/SRF_Calendar.php |
— | — | @@ -8,7 +8,7 @@ |
9 | 9 | * @file SRF_Calendar.php |
10 | 10 | * @ingroup SemanticResultFormats |
11 | 11 | * |
12 | | - * @author ... |
| 12 | + * @author Yaron Koren |
13 | 13 | */ |
14 | 14 | class SRFCalendar extends SMWResultPrinter { |
15 | 15 | |
— | — | @@ -227,6 +227,7 @@ |
228 | 228 | |
229 | 229 | function displayCalendar( $events ) { |
230 | 230 | global $wgOut, $srfgScriptPath, $wgParser, $wgRequest; |
| 231 | + global $srfgFirstDayOfWeek; |
231 | 232 | |
232 | 233 | $wgParser->disableCache(); |
233 | 234 | |
— | — | @@ -277,7 +278,43 @@ |
278 | 279 | } else { |
279 | 280 | $skin = $wgParser->getOptions()->getSkin(); |
280 | 281 | } |
281 | | - // get all the date-based values we need - the current month |
| 282 | + |
| 283 | + // Set days of the week. |
| 284 | + $week_day_names = array( |
| 285 | + 1 => wfMsg( 'sunday' ), |
| 286 | + 2 => wfMsg( 'monday' ), |
| 287 | + 3 => wfMsg( 'tuesday' ), |
| 288 | + 4 => wfMsg( 'wednesday' ), |
| 289 | + 5 => wfMsg( 'thursday' ), |
| 290 | + 6 => wfMsg( 'friday' ), |
| 291 | + 7 => wfMsg( 'saturday' ) |
| 292 | + ); |
| 293 | + if ( empty( $srfgFirstDayOfWeek ) ) { |
| 294 | + $firstDayOfWeek = 1; |
| 295 | + $lastDayOfWeek = 7; |
| 296 | + } else { |
| 297 | + $firstDayOfWeek = array_search( $srfgFirstDayOfWeek, $week_day_names ); |
| 298 | + if ( $firstDayOfWeek === false ) { |
| 299 | + // Bad value for $srfgFirstDayOfWeek! |
| 300 | + print 'Warning: Bad value for $srfgFirstDayOfWeek ("' . $srfgFirstDayOfWeek . '")'; |
| 301 | + $firstDayOfWeek = 1; |
| 302 | + } |
| 303 | + if ( $firstDayOfWeek == 1 ) { |
| 304 | + $lastDayOfWeek = 7; |
| 305 | + } else { |
| 306 | + $lastDayOfWeek = $firstDayOfWeek - 1; |
| 307 | + } |
| 308 | + } |
| 309 | + |
| 310 | + // Now create the actual array of days of the week, based on |
| 311 | + // the start day |
| 312 | + $week_days = array(); |
| 313 | + for ( $i = 1; $i <= 7; $i++ ) { |
| 314 | + $curDay = ( ( $firstDayOfWeek + $i - 2 ) % 7 ) + 1; |
| 315 | + $week_days[$i] = $week_day_names[$curDay]; |
| 316 | + } |
| 317 | + |
| 318 | + // Get all the date-based values we need - the current month |
282 | 319 | // and year (i.e., the one the user is looking at - not |
283 | 320 | // necessarily the "current" ones), the previous and next months |
284 | 321 | // and years (same - note that the previous or next month could |
— | — | @@ -316,7 +353,7 @@ |
317 | 354 | $next_year = $cur_year; |
318 | 355 | } |
319 | 356 | |
320 | | - // there's no year '0' - change it to '1' or '-1' |
| 357 | + // There's no year '0' - change it to '1' or '-1'. |
321 | 358 | if ( $cur_year == '0' ) { $cur_year = '1'; } |
322 | 359 | if ( $next_year == '0' ) { $next_year = '1'; } |
323 | 360 | if ( $prev_year == '0' ) { $prev_year = '-1'; } |
— | — | @@ -330,18 +367,19 @@ |
331 | 368 | $next_month_text = wfMsg( 'srfc_nextmonth' ); |
332 | 369 | $go_to_month_text = wfMsg( 'srfc_gotomonth' ); |
333 | 370 | |
334 | | - // get day of the week that the first of this month falls on |
| 371 | + // Get day of the week that the first of this month falls on. |
335 | 372 | $first_day = new SRFCHistoricalDate(); |
336 | 373 | $first_day->create( $cur_year, $cur_month_num, 1 ); |
337 | 374 | $day_of_week_of_1 = $first_day->getDayOfWeek(); |
338 | | - $start_day = 1 - $day_of_week_of_1; |
| 375 | + $start_day = $firstDayOfWeek - $day_of_week_of_1; |
| 376 | + if ( $start_day > 0 ) { $start_day -= 7; } |
339 | 377 | $days_in_prev_month = SRFCHistoricalDate::daysInMonth( $prev_year, $prev_month_num ); |
340 | 378 | $days_in_cur_month = SRFCHistoricalDate::daysInMonth( $cur_year, $cur_month_num ); |
341 | 379 | $today_string = date( 'Y n j', time() ); |
342 | 380 | $url_year = $wgRequest->getVal( 'year' ); |
343 | 381 | $page_name = $page_title->getPrefixedDbKey(); |
344 | 382 | |
345 | | - // create table for holding title and navigation information |
| 383 | + // Create table for holding title and navigation information. |
346 | 384 | $text = <<<END |
347 | 385 | <table class="navigation_table"> |
348 | 386 | <tr> |
— | — | @@ -378,21 +416,22 @@ |
379 | 417 | <tr class="weekdays"> |
380 | 418 | |
381 | 419 | END; |
382 | | - // first row of the main table holds the days of the week |
383 | | - $week_days = array( wfMsg( 'sunday' ), wfMsg( 'monday' ), wfMsg( 'tuesday' ), wfMsg( 'wednesday' ), wfMsg( 'thursday' ), wfMsg( 'friday' ), wfMsg( 'saturday' ) ); |
| 420 | + // First row of the main table holds the days of the week |
384 | 421 | foreach ( $week_days as $week_day ) { |
385 | 422 | $text .= "<td>$week_day</td>"; |
386 | 423 | } |
387 | 424 | $text .= "</tr>\n"; |
388 | 425 | |
389 | | - // now, create the calendar itself - |
390 | | - // loop through a set of weeks, from a Sunday (which might be |
391 | | - // before the beginning of the month) to a Saturday (which might |
392 | | - // be after the end of the month) |
393 | | - $day_of_the_week = 1; |
| 426 | + // Now, create the calendar itself - |
| 427 | + // loop through a set of weeks, from a "Sunday" (which might be |
| 428 | + // before the beginning of the month) to a "Saturday" (which |
| 429 | + // might be after the end of the month). |
| 430 | + // "Sunday" and "Saturday" are in quotes because the actual |
| 431 | + // start and end days of the week can be set by the admin. |
| 432 | + $day_of_the_week = $firstDayOfWeek; |
394 | 433 | $is_last_week = false; |
395 | | - for ( $day = $start_day; ( ! $is_last_week || $day_of_the_week != 1 ); $day++ ) { |
396 | | - if ( $day_of_the_week == 1 ) { |
| 434 | + for ( $day = $start_day; ( ! $is_last_week || $day_of_the_week != $firstDayOfWeek ); $day++ ) { |
| 435 | + if ( $day_of_the_week == $firstDayOfWeek ) { |
397 | 436 | $text .= "<tr>\n"; |
398 | 437 | } |
399 | 438 | if ( "$cur_year $cur_month_num $day" == $today_string ) { |
— | — | @@ -403,9 +442,9 @@ |
404 | 443 | $text .= "<td>\n"; |
405 | 444 | } |
406 | 445 | if ( $day == $days_in_cur_month || $day > 50 ) { $is_last_week = true; } |
407 | | - // if this day is before or after the current month, set a |
408 | | - // "display day" to show on the calendar, and use a different |
409 | | - // CSS style for it |
| 446 | + // If this day is before or after the current month, |
| 447 | + // set a "display day" to show on the calendar, and |
| 448 | + // use a different CSS style for it. |
410 | 449 | if ( $day > $days_in_cur_month || $day < 1 ) { |
411 | 450 | if ( $day < 1 ) { |
412 | 451 | $display_day = $day + $days_in_prev_month; |
— | — | @@ -420,13 +459,13 @@ |
421 | 460 | $date_str = $cur_year . '-' . $cur_month_num . '-' . $day; |
422 | 461 | $text .= "<div class=\"day\">$day</div>\n"; |
423 | 462 | } |
424 | | - // finally, the most important step - get the events that |
425 | | - // match this date, and the given set of criteria, and |
426 | | - // display them in this date's box |
| 463 | + // Finally, the most important step - get the events |
| 464 | + // that match this date, and the given set of criteria, |
| 465 | + // and display them in this date's box. |
427 | 466 | $text .= "<div class=\"main\">\n"; |
428 | | - // avoid errors if array is null |
429 | | - if ( $events == null ) |
| 467 | + if ( $events == null ) { |
430 | 468 | $events = array(); |
| 469 | + } |
431 | 470 | foreach ( $events as $event ) { |
432 | 471 | list( $event_title, $other_text, $event_date, $color ) = $event; |
433 | 472 | if ( $event_date == $date_str ) { |
— | — | @@ -450,8 +489,10 @@ |
451 | 490 | </td> |
452 | 491 | |
453 | 492 | END; |
| 493 | + if ( $day_of_the_week == $lastDayOfWeek ) { |
| 494 | + $text .= "</tr>\n"; |
| 495 | + } |
454 | 496 | if ( $day_of_the_week == 7 ) { |
455 | | - $text .= "</tr>\n"; |
456 | 497 | $day_of_the_week = 1; |
457 | 498 | } else { |
458 | 499 | $day_of_the_week++; |