Team:Leicester/extensions/calendar extension.php

From 2012.igem.org

(Difference between revisions)
Ladybird136 (Talk | contribs)
(Created page with "<?php # Calendar Extension # Creates a calendar of the month and year. # # Example Code # # <calendar>05 2006</calendar> # $wgExtensionFunctions[] = "wfCalendarExtension"; $wgEx...")
Newer edit →

Revision as of 09:38, 16 July 2012

<?php

  1. Calendar Extension
  2. Creates a calendar of the month and year.
  3. Example Code
  4. <calendar>05 2006</calendar>

$wgExtensionFunctions[] = "wfCalendarExtension"; $wgExtensionCredits['parserhook'][] = array(

       'name' => 'Calendar',
       'author' => 'Shane',
       'description' => 'adds <calender> tag, for calender creation',
       'url' => 'http://meta.wikimedia.org/wiki/Calendar_extension'

);

/* DO NOT EDIT BEYOND THIS LINE */

function wfCalendarExtension() {

   global $wgParser;    
   $wgParser->setHook( "calendar", "createmwCalendar" );

}

  1. The callback function for converting the input text to HTML output

function createmwCalendar($input) {

       global $wgOut;
       $input = $wgOut->parse($input, false);
       $array = explode(' ', $input);

       $month = $array[0];
       $year = $array[1];

       $mwCalendar = new mwCalendar();         

       $mwCalendar->dateNow($month, $year);
       return $mwCalendar->showThisMonth();

}

class mwCalendar {

       var $cal = "CAL_GREGORIAN";
       var $format = "%Y%m%d";
       var $today;
       var $day;
       var $month;
       var $year;
       var $pmonth;
       var $pyear;
       var $nmonth;
       var $nyear;
       var $wday_names = array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
       var $wmonth_names = array("January","Febuary","March","April","May","June","July","August","September","October","November","December");

       function mwCalendar()
       {
               $this->day = "1";
               $today = "";
               $month = "";
               $year = "";
               $pmonth = "";
               $pyear = "";
               $nmonth = "";
               $nyear = "";
       }


       function dateNow($month,$year)
       {
               $this->month = $month;
               $this->year = $year;
               $this->today = strftime("%d",time());           
               $this->pmonth = $this->month - 1;
               $this->pyear = $this->year - 1;
               $this->nmonth = $this->month + 1;
               $this->nyear = $this->year + 1; 
       }

       function daysInMonth($month,$year)
       {
               if (empty($year))
               {
                       $year = mwCalendar::dateNow("%Y");
               }
               if (empty($month))
               {
                       $month = mwCalendar::dateNow("%m");
               }
               if($month == "2")
               {
                       if(mwCalendar::isLeapYear($year))
                       {
                               return 29;
                       }
                       else
                       {
                               return 28;
                       }
               }
               else if ($month == "4" || $month == "6" || $month == "9" || $month == "11")
               {
                       return 30;                
               }
               else
               {
                       return 31;
               }                       
       }

       function isLeapYear($year)
       {
     return (($year % 4 == "0" && $year % 100 != "0") || $year % 400 == "0"); 
       }

       function dayOfWeek($month,$year) 
       { 
               if ($month > 2) {
                               $month -= 2; 
               }
               else 
               { 
                               $month += 10; 
                               $year--; 
               } 

               $day =  ( floor((13 * $month - 1) / 5) + 
                                               $this->day + ($year % 100) + 
                                               floor(($year % 100) / 4) + 
                                               floor(($year / 100) / 4) - 2 * 
                                               floor($year / 100) + 77); 

               $weekday_number = (($day - 7 * floor($day / 7))); 

               return $weekday_number; 
 }

       function getWeekDay()
       {
               $week_day = mwCalendar::dayOfWeek($this->month,$this->year);
               //return $this->wday_names[$week_day];
               return $week_Day;
       }

       function showThisMonth()
       {
               $output = ;   //      reset var
$output = ''; $output .= ''; $output .= ''; for($i=0;$i<7;$i++) $output .= ''; $output .= ''; $wday = mwCalendar::dayOfWeek($this->month,$this->year); $no_days = mwCalendar::daysInMonth($this->month,$this->year); $count = 1; $output .= ''; for($i=1;$i<=$wday;$i++) { $output .= '';
                       $count++;
               }
               for($i=1;$i<=$no_days;$i++)
               {
                               if($count > 6)
                               {
                                       if($i == $this->today)
                                       {
$output .= '';
                                       }
                                       else
                                       {
$output .= '</tr>';
                                       }
                                       $count = 0;
                               }
                               else
                               {
                                       if($i == $this->today)
                                       {
$output .= '';
                                       }
                                       else
                                       {
$output .= '';
                                       }
                               }
                               $count++;
               }
$output .= '</tr>
'. $this->wmonth_names[$this->pmonth] . " " .$this->year .'
'. $this->wday_names[$i]. '
 ' . $i . '
' . $i . '' . $i . '' . $i . '
';
               return $output;
       } 

}

?>