r82416 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82415‎ | r82416 | r82417 >
Date:21:34, 18 February 2011
Author:yaron
Status:deferred
Tags:
Comment:
Added Ankit Garg's patch to handle new $srfgFirstDayOfWeek setting; also made some improvements to the comments
Modified paths:
  • /trunk/extensions/SemanticResultFormats/Calendar/SRF_Calendar.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/Calendar/SRF_Calendar.php
@@ -8,7 +8,7 @@
99 * @file SRF_Calendar.php
1010 * @ingroup SemanticResultFormats
1111 *
12 - * @author ...
 12+ * @author Yaron Koren
1313 */
1414 class SRFCalendar extends SMWResultPrinter {
1515
@@ -227,6 +227,7 @@
228228
229229 function displayCalendar( $events ) {
230230 global $wgOut, $srfgScriptPath, $wgParser, $wgRequest;
 231+ global $srfgFirstDayOfWeek;
231232
232233 $wgParser->disableCache();
233234
@@ -277,7 +278,43 @@
278279 } else {
279280 $skin = $wgParser->getOptions()->getSkin();
280281 }
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
282319 // and year (i.e., the one the user is looking at - not
283320 // necessarily the "current" ones), the previous and next months
284321 // and years (same - note that the previous or next month could
@@ -316,7 +353,7 @@
317354 $next_year = $cur_year;
318355 }
319356
320 - // there's no year '0' - change it to '1' or '-1'
 357+ // There's no year '0' - change it to '1' or '-1'.
321358 if ( $cur_year == '0' ) { $cur_year = '1'; }
322359 if ( $next_year == '0' ) { $next_year = '1'; }
323360 if ( $prev_year == '0' ) { $prev_year = '-1'; }
@@ -330,18 +367,19 @@
331368 $next_month_text = wfMsg( 'srfc_nextmonth' );
332369 $go_to_month_text = wfMsg( 'srfc_gotomonth' );
333370
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.
335372 $first_day = new SRFCHistoricalDate();
336373 $first_day->create( $cur_year, $cur_month_num, 1 );
337374 $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; }
339377 $days_in_prev_month = SRFCHistoricalDate::daysInMonth( $prev_year, $prev_month_num );
340378 $days_in_cur_month = SRFCHistoricalDate::daysInMonth( $cur_year, $cur_month_num );
341379 $today_string = date( 'Y n j', time() );
342380 $url_year = $wgRequest->getVal( 'year' );
343381 $page_name = $page_title->getPrefixedDbKey();
344382
345 - // create table for holding title and navigation information
 383+ // Create table for holding title and navigation information.
346384 $text = <<<END
347385 <table class="navigation_table">
348386 <tr>
@@ -378,21 +416,22 @@
379417 <tr class="weekdays">
380418
381419 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
384421 foreach ( $week_days as $week_day ) {
385422 $text .= "<td>$week_day</td>";
386423 }
387424 $text .= "</tr>\n";
388425
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;
394433 $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 ) {
397436 $text .= "<tr>\n";
398437 }
399438 if ( "$cur_year $cur_month_num $day" == $today_string ) {
@@ -403,9 +442,9 @@
404443 $text .= "<td>\n";
405444 }
406445 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.
410449 if ( $day > $days_in_cur_month || $day < 1 ) {
411450 if ( $day < 1 ) {
412451 $display_day = $day + $days_in_prev_month;
@@ -420,13 +459,13 @@
421460 $date_str = $cur_year . '-' . $cur_month_num . '-' . $day;
422461 $text .= "<div class=\"day\">$day</div>\n";
423462 }
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.
427466 $text .= "<div class=\"main\">\n";
428 - // avoid errors if array is null
429 - if ( $events == null )
 467+ if ( $events == null ) {
430468 $events = array();
 469+ }
431470 foreach ( $events as $event ) {
432471 list( $event_title, $other_text, $event_date, $color ) = $event;
433472 if ( $event_date == $date_str ) {
@@ -450,8 +489,10 @@
451490 </td>
452491
453492 END;
 493+ if ( $day_of_the_week == $lastDayOfWeek ) {
 494+ $text .= "</tr>\n";
 495+ }
454496 if ( $day_of_the_week == 7 ) {
455 - $text .= "</tr>\n";
456497 $day_of_the_week = 1;
457498 } else {
458499 $day_of_the_week++;

Status & tagging log