00001 <?php
00002
00003 $wgAutoloadClasses['SRFCHistoricalDate'] = dirname( __FILE__ ) . '/SRFC_HistoricalDate.php';
00004
00013 class SRFCalendar extends SMWResultPrinter {
00014
00015 protected $mTemplate;
00016 protected $mUserParam;
00017 protected $mRealUserLang = null;
00018
00019 protected function setColors( $colorsText ) {
00020 $colors = array();
00021 $colorElements = explode( ',', $colorsText );
00022 foreach ( $colorElements as $colorElem ) {
00023 $propAndColor = explode( '=>', $colorElem );
00024 if ( count( $propAndColor ) == 2 ) {
00025 $colors[$propAndColor[0]] = $propAndColor[1];
00026 }
00027 }
00028 $this->mColors = $colors;
00029 }
00030
00031 protected function handleParameters( array $params, $outputmode ) {
00032 parent::handleParameters( $params, $outputmode );
00033
00034 $this->mTemplate = trim( $params['template'] );
00035 $this->mUserParam = trim( $params['userparam'] );
00036
00037 if ( $params['lang'] !== false ) {
00038 global $wgLang;
00039
00040
00041 $this->mRealUserLang = clone ( $wgLang );
00042 $wgLang = Language::factory( trim( $params['lang'] ) );
00043 }
00044
00045 $this->setColors( $params['colors'] );
00046 }
00047
00048 public function getName() {
00049 return wfMsg( 'srf_printername_calendar' );
00050 }
00051
00052 public function getResult( SMWQueryResult $results, array $params, $outputmode ) {
00053 $this->isHTML = false;
00054 $this->hasTemplates = false;
00055
00056
00057 $this->handleParameters( $params, $outputmode );
00058 return $this->getResultText( $results, SMW_OUTPUT_HTML );
00059 }
00060
00067 protected function getResultText( SMWQueryResult $res, $outputmode ) {
00068 $events = array();
00069
00070
00071 while ( $row = $res->getNext() ) {
00072 $dates = array();
00073 $title = $text = $color = '';
00074
00075 if ( $this->mTemplate != '' ) {
00076
00077 $this->hasTemplates = true;
00078
00079 if ( $this->mUserParam ) {
00080 $text = "|userparam=$this->mUserParam";
00081 }
00082
00083 foreach ( $row as $i => $field ) {
00084 $pr = $field->getPrintRequest();
00085 $text .= '|' . ( $i + 1 ) . '=';
00086
00087 while ( ( $object = $field->getNextDataValue() ) !== false ) {
00088 if ( $object->getTypeID() == '_dat' ) {
00089 $text .= $object->getLongWikiText();
00090 } elseif ( $object->getTypeID() == '_wpg' ) {
00091
00092 if ( $this->mLinkOthers ) {
00093 $text .= $object->getLongText( $outputmode, null );
00094 } else {
00095 $text .= $object->getWikiValue();
00096 }
00097 } else {
00098 $text .= $object->getShortText( $outputmode, null );
00099 }
00100
00101 if ( $pr->getMode() == SMWPrintRequest::PRINT_PROP && $pr->getTypeID() == '_dat' ) {
00102 $dates[] = $this->formatDateStr( $object );
00103 }
00104 }
00105 }
00106 } else {
00107
00108 $numNonDateProperties = 0;
00109
00110
00111
00112 foreach ( $row as $i => $field ) {
00113 $pr = $field->getPrintRequest();
00114
00115
00116
00117 $textForProperty = '';
00118
00119 while ( ( $object = $field->getNextDataValue() ) !== false ) {
00120 if ( $object->getTypeID() == '_dat' ) {
00121
00122 } elseif ( $object->getTypeID() == '_wpg' ) {
00123 if ( $i == 0 ) {
00124 $title = Title::newFromText( $object->getShortWikiText( false ) );
00125 } else {
00126 $numNonDateProperties++;
00127
00128
00129 if ( $this->mShowHeaders == SMW_HEADERS_SHOW ) {
00130 $textForProperty .= $pr->getHTMLText( smwfGetLinker() ) . ' ';
00131 } elseif ( $this->mShowHeaders == SMW_HEADERS_PLAIN ) {
00132 $textForProperty .= $pr->getLabel() . ' ';
00133 }
00134
00135
00136
00137 if ( $this->mLinkOthers ) {
00138 $textForProperty .= $object->getLongText( $outputmode, smwfGetLinker() );
00139 } else {
00140 $textForProperty .= $object->getWikiValue();
00141 }
00142 }
00143 } else {
00144 $numNonDateProperties++;
00145 $textForProperty .= $pr->getHTMLText( smwfGetLinker() ) . ' ' . $object->getShortText( $outputmode, smwfGetLinker() );
00146 }
00147 if ( $pr->getMode() == SMWPrintRequest::PRINT_PROP && $pr->getTypeID() == '_dat' ) {
00148 $datePropLabel = $pr->getLabel();
00149 if ( !array_key_exists( $datePropLabel, $dates ) ) {
00150 $dates[$datePropLabel] = array();
00151 }
00152 $dates[$datePropLabel][] = $this->formatDateStr( $object );
00153 }
00154 }
00155
00156
00157
00158
00159 if ( $numNonDateProperties == 1 ) {
00160 $text .= ' (';
00161 } elseif ( $numNonDateProperties > 1 ) {
00162 $text .= ', ';
00163 }
00164 $text .= $textForProperty;
00165 }
00166 if ( $numNonDateProperties > 0 ) {
00167 $text .= ')';
00168 }
00169 }
00170
00171 if ( count( $dates ) > 0 ) {
00172
00173
00174 $res_subject = $field->getResultSubject();
00175 if ( isset( $res_subject->display_options )
00176 && is_array( $res_subject->display_options ) ) {
00177 if ( array_key_exists( 'color', $res_subject->display_options ) ) {
00178 $color = $res_subject->display_options['color'];
00179 }
00180 if ( array_key_exists( 'colors', $res_subject->display_options ) ) {
00181 $this->setColors( $res_subject->display_options['colors'] );
00182 }
00183 }
00184
00185 foreach ( $dates as $label => $datesForLabel ) {
00186 foreach ( $datesForLabel as $date ) {
00187 $curText = $text;
00188
00189
00190
00191
00192
00193 if ( count( $dates ) > 1 ) {
00194 $curText = "($label) " . $curText;
00195 }
00196 $curColor = $color;
00197 if ( array_key_exists( $label, $this->mColors ) ) {
00198 $curColor = $this->mColors[$label];
00199 }
00200 $events[] = array( $title, $curText, $date, $curColor );
00201 }
00202 }
00203 }
00204 }
00205
00206 $result = $this->displayCalendar( $events );
00207
00208
00209
00210 if ( ! is_null( $this->mRealUserLang ) ) {
00211 global $wgLang;
00212 $wgLang = $this->mRealUserLang;
00213 }
00214
00215 global $wgParser;
00216
00217 if ( is_null( $wgParser->getTitle() ) ) {
00218 return $result;
00219 } else {
00220 return array( $result, 'noparse' => 'true', 'isHTML' => 'true' );
00221 }
00222 }
00223
00224 protected static function intToMonth( $int ) {
00225 $months = array(
00226 '1' => 'january',
00227 '2' => 'february',
00228 '3' => 'march',
00229 '4' => 'april',
00230 '5' => 'may_long',
00231 '6' => 'june',
00232 '7' => 'july',
00233 '8' => 'august',
00234 '9' => 'september',
00235 '10' => 'october',
00236 '11' => 'november',
00237 '12' => 'december',
00238 );
00239
00240 return wfMsgForContent( array_key_exists( $int, $months ) ? $months[$int] : 'january' );
00241 }
00242
00243 function formatDateStr( $object ) {
00244
00245
00246 return $object->getYear() . '-' . (int)$object->getMonth() . '-' . (int)$object->getDay();
00247 }
00248
00249 function displayCalendar( $events ) {
00250 global $wgOut, $srfgScriptPath, $wgParser, $wgRequest;
00251 global $srfgFirstDayOfWeek;
00252
00253 $wgParser->disableCache();
00254
00255 $wgOut->addLink( array(
00256 'rel' => 'stylesheet',
00257 'type' => 'text/css',
00258 'media' => 'screen, print',
00259 'href' => $srfgScriptPath . '/Calendar/skins/SRFC_main.css'
00260 ) );
00261
00262
00263
00264
00265
00266 $page_title = $wgParser->getTitle();
00267 $additional_query_string = '';
00268 $hidden_inputs = '';
00269 $in_special_page = is_null( $page_title ) || $page_title->isSpecialPage();
00270
00271 if ( $in_special_page ) {
00272 global $wgTitle;
00273 $page_title = $wgTitle;
00274 global $wgUser;
00275 $skin = $wgUser->getSkin();
00276 $request_values = $wgRequest->getValues();
00277
00278
00279
00280
00281 foreach ( $_REQUEST as $key => $value ) {
00282 if ( is_array( $value ) ) {
00283 foreach ($value as $k2 => $v2 ) {
00284 $new_key = $key . '[' . $k2 . ']';
00285 $request_values[$new_key] = $v2;
00286 }
00287 }
00288 }
00289
00290 foreach ( $request_values as $key => $value ) {
00291 if ( $key != 'month' && $key != 'year'
00292
00293 && $key != 'query' && $key != 'free_text'
00294 ) {
00295 $additional_query_string .= "&$key=$value";
00296 $hidden_inputs .= "<input type=\"hidden\" name=\"$key\" value=\"$value\" />";
00297 }
00298 }
00299 } else {
00300 $skin = $wgParser->getOptions()->getSkin();
00301 }
00302
00303
00304 $week_day_names = array(
00305 1 => wfMsg( 'sunday' ),
00306 2 => wfMsg( 'monday' ),
00307 3 => wfMsg( 'tuesday' ),
00308 4 => wfMsg( 'wednesday' ),
00309 5 => wfMsg( 'thursday' ),
00310 6 => wfMsg( 'friday' ),
00311 7 => wfMsg( 'saturday' )
00312 );
00313 if ( empty( $srfgFirstDayOfWeek ) ) {
00314 $firstDayOfWeek = 1;
00315 $lastDayOfWeek = 7;
00316 } else {
00317 $firstDayOfWeek = array_search( $srfgFirstDayOfWeek, $week_day_names );
00318 if ( $firstDayOfWeek === false ) {
00319
00320 print 'Warning: Bad value for $srfgFirstDayOfWeek ("' . $srfgFirstDayOfWeek . '")';
00321 $firstDayOfWeek = 1;
00322 }
00323 if ( $firstDayOfWeek == 1 ) {
00324 $lastDayOfWeek = 7;
00325 } else {
00326 $lastDayOfWeek = $firstDayOfWeek - 1;
00327 }
00328 }
00329
00330
00331
00332 $week_days = array();
00333 for ( $i = 1; $i <= 7; $i++ ) {
00334 $curDay = ( ( $firstDayOfWeek + $i - 2 ) % 7 ) + 1;
00335 $week_days[$i] = $week_day_names[$curDay];
00336 }
00337
00338
00339
00340
00341
00342
00343
00344 $cur_month_num = date( 'n', time() );
00345 if ( $wgRequest->getCheck( 'month' ) ) {
00346 $query_month = $wgRequest->getVal( 'month' );
00347 if ( is_numeric( $query_month ) && ( intval( $query_month ) == $query_month ) && $query_month >= 1 && $query_month <= 12 ) {
00348 $cur_month_num = $wgRequest->getVal( 'month' );
00349 }
00350 }
00351
00352 $cur_month = self::intToMonth( $cur_month_num );
00353 $cur_year = date( 'Y', time() );
00354 if ( $wgRequest->getCheck( 'year' ) ) {
00355 $query_year = $wgRequest->getVal( 'year' );
00356 if ( is_numeric( $query_year ) && intval( $query_year ) == $query_year ) {
00357 $cur_year = $wgRequest->getVal( 'year' );
00358 }
00359 }
00360
00361 if ( $cur_month_num == '1' ) {
00362 $prev_month_num = '12';
00363 $prev_year = $cur_year - 1;
00364 } else {
00365 $prev_month_num = $cur_month_num - 1;
00366 $prev_year = $cur_year;
00367 }
00368
00369 if ( $cur_month_num == '12' ) {
00370 $next_month_num = '1';
00371 $next_year = $cur_year + 1;
00372 } else {
00373 $next_month_num = $cur_month_num + 1;
00374 $next_year = $cur_year;
00375 }
00376
00377
00378 if ( $cur_year == '0' ) { $cur_year = '1'; }
00379 if ( $next_year == '0' ) { $next_year = '1'; }
00380 if ( $prev_year == '0' ) { $prev_year = '-1'; }
00381
00382 $prev_month_url = $page_title->getLocalURL( "month=$prev_month_num&year=$prev_year" . $additional_query_string );
00383 $next_month_url = $page_title->getLocalURL( "month=$next_month_num&year=$next_year" . $additional_query_string );
00384 $today_url = $page_title->getLocalURL( $additional_query_string );
00385
00386 $today_text = wfMsg( 'srfc_today' );
00387 $prev_month_text = wfMsg( 'srfc_previousmonth' );
00388 $next_month_text = wfMsg( 'srfc_nextmonth' );
00389 $go_to_month_text = wfMsg( 'srfc_gotomonth' );
00390
00391
00392 $first_day = new SRFCHistoricalDate();
00393 $first_day->create( $cur_year, $cur_month_num, 1 );
00394 $day_of_week_of_1 = $first_day->getDayOfWeek();
00395 $start_day = $firstDayOfWeek - $day_of_week_of_1;
00396 if ( $start_day > 0 ) { $start_day -= 7; }
00397 $days_in_prev_month = SRFCHistoricalDate::daysInMonth( $prev_year, $prev_month_num );
00398 $days_in_cur_month = SRFCHistoricalDate::daysInMonth( $cur_year, $cur_month_num );
00399 $today_string = date( 'Y n j', time() );
00400 $page_name = $page_title->getPrefixedDbKey();
00401
00402
00403 $text = <<<END
00404 <table class="navigation_table">
00405 <tr>
00406 <td class="month_name">$cur_month $cur_year</td>
00407 <td class="nav_links">
00408 <a href="$prev_month_url" title="$prev_month_text"><img src="$srfgScriptPath/Calendar/skins/left-arrow.png" border="0" /></a>
00409  
00410 <a href="$today_url">$today_text</a>
00411  
00412 <a href="$next_month_url" title="$next_month_text"><img src="$srfgScriptPath/Calendar/skins/right-arrow.png" border="0" /></a>
00413 </td>
00414 <td class="nav_form">
00415 <form>
00416 <input type="hidden" name="title" value="$page_name">
00417 <select name="month">
00418
00419 END;
00420 for ( $i = 1; $i <= 12; $i++ ) {
00421 $month_name = self::intToMonth( $i );
00422 $selected_str = ( $i == $cur_month_num ) ? "selected" : "";
00423 $text .= "<option value=\"$i\" $selected_str>$month_name</option>\n";
00424 }
00425 $text .= <<<END
00426 </select>
00427 <input name="year" type="text" value="$cur_year" size="4">
00428 $hidden_inputs
00429 <input type="submit" value="$go_to_month_text">
00430 </form>
00431 </td>
00432 </tr>
00433 </table>
00434
00435 <table class="month_calendar">
00436 <tr class="weekdays">
00437
00438 END;
00439
00440 foreach ( $week_days as $week_day ) {
00441 $text .= "<td>$week_day</td>";
00442 }
00443 $text .= "</tr>\n";
00444
00445
00446
00447
00448
00449
00450
00451 $day_of_the_week = $firstDayOfWeek;
00452 $is_last_week = false;
00453 for ( $day = $start_day; ( ! $is_last_week || $day_of_the_week != $firstDayOfWeek ); $day++ ) {
00454 if ( $day_of_the_week == $firstDayOfWeek ) {
00455 $text .= "<tr>\n";
00456 }
00457 if ( "$cur_year $cur_month_num $day" == $today_string ) {
00458 $text .= "<td class=\"today\">\n";
00459 } elseif ( $day_of_the_week == 1 || $day_of_the_week == 7 ) {
00460 $text .= "<td class=\"weekend_day\">\n";
00461 } else {
00462 $text .= "<td>\n";
00463 }
00464 if ( $day == $days_in_cur_month || $day > 50 ) { $is_last_week = true; }
00465
00466
00467
00468 if ( $day > $days_in_cur_month || $day < 1 ) {
00469 if ( $day < 1 ) {
00470 $display_day = $day + $days_in_prev_month;
00471 $date_str = $prev_year . '-' . $prev_month_num . '-' . $display_day;
00472 }
00473 if ( $day > $days_in_cur_month ) {
00474 $display_day = $day - $days_in_cur_month;
00475 $date_str = $next_year . '-' . $next_month_num . '-' . $display_day;
00476 }
00477 $text .= "<div class=\"day day_other_month\">$display_day</div>\n";
00478 } else {
00479 $date_str = $cur_year . '-' . $cur_month_num . '-' . $day;
00480 $text .= "<div class=\"day\">$day</div>\n";
00481 }
00482
00483
00484
00485 $text .= "<div class=\"main\">\n";
00486 if ( $events == null ) {
00487 $events = array();
00488 }
00489 foreach ( $events as $event ) {
00490 list( $event_title, $other_text, $event_date, $color ) = $event;
00491 if ( $event_date == $date_str ) {
00492 if ( $this->mTemplate != '' ) {
00493 $templatetext = '{{' . $this->mTemplate . $other_text . '|thisdate=' . $date_str . '}}';
00494 $templatetext = $wgParser->replaceVariables( $templatetext );
00495 $templatetext = $wgParser->recursiveTagParse( $templatetext );
00496 $text .= $templatetext;
00497 } else {
00498 $event_str = $skin->makeLinkObj( $event_title );
00499 if ( $color != '' ) {
00500 $text .= "<div class=\"colored-entry\"><p style=\"border-left: 7px $color solid;\">$event_str $other_text</p></div>\n";
00501 } else {
00502 $text .= "$event_str $other_text\n\n";
00503 }
00504 }
00505 }
00506 }
00507 $text .= <<<END
00508 </div>
00509 </td>
00510
00511 END;
00512 if ( $day_of_the_week == $lastDayOfWeek ) {
00513 $text .= "</tr>\n";
00514 }
00515 if ( $day_of_the_week == 7 ) {
00516 $day_of_the_week = 1;
00517 } else {
00518 $day_of_the_week++;
00519 }
00520 }
00521 $text .= "</table>\n";
00522
00523 return $text;
00524 }
00525
00530 public function getParameters() {
00531 $params = parent::getParameters();
00532
00533 $params['lang'] = new Parameter( 'lang' );
00534 $params['lang']->setMessage( 'srf_paramdesc_calendarlang' );
00535 $params['lang']->setDefault( false, false );
00536
00537 $params['template'] = new Parameter( 'template' );
00538 $params['template']->setDefault( '' );
00539
00540 $params['userparam'] = new Parameter( 'userparam' );
00541 $params['userparam']->setDefault( '' );
00542
00543 $params['color'] = new Parameter( 'color' );
00544 $params['color']->setDefault( '' );
00545
00546 $params['colors'] = new Parameter( 'colors' );
00547 $params['colors']->setMessage( 'srf_paramdesc_calendarcolors' );
00548 $params['colors']->setDefault( '' );
00549
00550 return $params;
00551 }
00552
00553 }