Jump to content

User:Eric Bouliane/MWCalendar

From Wikipedia, the free encyclopedia

MediaWiki Calendar

[edit]

Installation

[edit]
  • Save MWCalendar.php to ./extensions folder.
  • Open LocalSettings.php and append :
require_once("extensions/MWCalendar.php");

Usage

[edit]

Hook

[edit]
  • <MWCalendar ...Arguments... > ...Data... </MWCalendar>

Arguments

[edit]
  • Display: Integer. This will show X number of months. (Default 1)
  • MonthSkip: Integer. Skip X number of months. (Default 1)
  • Year: Integer. Which Year to start with. (Default Current)
  • TitleYear: Boolean. Show year title or not. (Default True)
  • Month: Integer. Which Month to start with. (Default Current)
  • TitleMonth: Boolean. Show month title or not. (Default True)
  • MonthNames: String. Comma delimited values for all 12 months. (Default January,February,March,April,May,June,July,August,September,October,November,December)
  • DaysOfWeek: String. Comma delimited values for all 7 days of the week. (Default Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday)
  • StartDay: Integer. Value between 0 and 6. 0=Sunday..6=Saturday for start of week. (Default 0)
  • Classes: All Strings. Value for given item. (Default Blank)
    • ClassTable: Entire Table
    • ClassTableRow: All Rows
    • ClassTableCell: All Cells
    • ClassTableCellToday: Today's Cell
    • ClassTableDOW: Days of Week row.
    • ClassTableMonth: Month row.
    • ClassTableYear: Year row.
  • Styles: All Strings. Value for given item. (Default Blank)
    • StyleTable: Entire Table
    • StyleTableRow: All Rows
    • StyleTableCell: All Cells
    • StyleTableCellToday: Today's Cell (Default Red Text)
    • StyleTableDOW: Days of Week row.
    • StyleTableMonth: Month row.
    • StyleTableYear: Year row.
  • TableBorder: Integer. Size of border. (Default 1)
  • TableCellSpacing: Integer. Spacing between cells. (Default 0)
  • TableCellPadding: Integer. Spacing within cell. (Default 1)
  • DayUnderline: Boolean. Underline day in calender. (Default true)

Data

[edit]
  • YYYY-MM-DD ::: Data: For One Day
  • YYYY-MM-DD ::: YYYY-MM-DD ::: Data: For Range Of Days
  • %YYYY-MM-DD ::: Data: To omit one day of data.
  • YYYY-MM-DD E xD ::: YYYY-MM-DD ::: Data: Every x days within range.
  • YYYY-MM-DD E xW ::: YYYY-MM-DD ::: Data: Every x week(s) within range.
  • YYYY-MM-DD E xM ::: YYYY-MM-DD ::: Data: Every x month(s) within range.

Examples

[edit]

Examples only show images as the extension is not installed here. The output will be HTML, and linking and data can include html, it just cannot show up here.

Example 1

[edit]
<MWCalendar Display="1" Month="8" Year="1978">
   1978-08-13:::Eric's Birthday!
</MWCalendar>

Example 2

[edit]
<MWCalendar
  Display="2"
  StyleTable="width:80%;"
  StyleTableCellToday="background:#009;color:#FFF;"
  StyleTableDOW="font-weight:bold;height:50px;"
  StyleTableMonth="background:#C06;color:#FFF;"
  StyleTableCell="height:50px;width:14%;">

Holidays
    2016-06-03 ::: <font color = cadetblue>Sorin</font>
   

</MWCalendar>

Example 3

[edit]
<MWCalendar
  Display="1" Month="7" Year="2007">
2007-07-19 E 2D ::: 2007-07-25 ::: <font color=red>In Oshawa</font>
%2007-07-21 ::: <font color=red>In Oshawa</font>
2007-07-21 ::: Marcia's Wedding
</MWCalendar>

MWCalendar.php

[edit]
<?php
$wgExtensionFunctions[] = "MWCalendar";

function MWCalendar() {
	global $wgParser;
	# register the extension with the WikiText parser
	$wgParser->setHook( "MWCalendar", "renderMWCalendar" );
}

# The callback function for converting the input text to HTML output
function renderMWCalendar( $input, $argv ) {
	global $wgAuth;
	$retval = ""; # Default Output
	$proceed = true;
	$development = false;

	if($development)
		$retval .= "<H5 STYLE=\"width:60%;text-align:center;color:#C00;border-top:1px #C00 solid;border-bottom:1px #C00 solid;\">Unstable Version of <MWCalendar> Extension<br>Currently Testing</H5>";

	$argv['display'] = isset($argv['display']) ? $argv['display'] : 1; // Number Of Months
	$argv['month'] = isset($argv['month']) ? $argv['month'] : date('m'); // Default Month
	$argv['year'] = isset($argv['year']) ? $argv['year'] : date('Y'); // Default Year
	$argv['titleyear'] = isset($argv['titleyear']) ? $argv['titleyear'] : true;
	$argv['titlemonth'] = isset($argv['titlemonth']) ? $argv['titlemonth'] : true;
	$argv['monthskip'] = isset($argv['monthskip']) ? $argv['monthskip'] : 1;

	$argv['monthnames'] = isset($argv['monthnames']) ? $argv['monthnames'] : "January,February,March,April,May,June,July,August,September,October,November,December";
	$argv['daysofweek'] = isset($argv['daysofweek']) ? $argv['daysofweek'] : "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday";
	$argv['startday'] = isset($argv['startday']) ? $argv['startday'] : 0; // 0= Sunday .... 6=Saturday

	$argv['classtable'] = isset($argv['classtable']) ? $argv['classtable'] : "";
	$argv['classtablerow'] = isset($argv['classtablerow']) ? $argv['classtablerow'] : "";
	$argv['classtablecell'] = isset($argv['classtablecell']) ? $argv['classtablecell'] : "";
	$argv['classtablecelltoday'] = isset($argv['classtablecelltoday']) ? $argv['classtablecelltoday'] : "";
	$argv['classtabledow'] = isset($argv['classtabledow']) ? $argv['classtabledow'] : "";
	$argv['classtablemonth'] = isset($argv['classtablemonth']) ? $argv['classtablemonth'] : "";
	$argv['classtableyear'] = isset($argv['classtableyear']) ? $argv['classtableyear'] : "";

	$argv['styletable'] = isset($argv['styletable']) ? $argv['styletable'] : "";
	$argv['styletablerow'] = isset($argv['styletablerow']) ? $argv['styletablerow'] : "";
	$argv['styletablecell'] = isset($argv['styletablecell']) ? $argv['styletablecell'] : "";
	$argv['styletablecelltoday'] = isset($argv['styletablecelltoday']) ? $argv['styletablecelltoday'] : "font-weight:bold;text-color:#cc000;";
	$argv['styletabledow'] = isset($argv['styletabledow']) ? $argv['styletabledow'] : "";
	$argv['styletablemonth'] = isset($argv['styletablemonth']) ? $argv['styletablemonth'] : "";
	$argv['styletableyear'] = isset($argv['styletableyear']) ? $argv['styletableyear'] : "";

	$argv['tableborder'] = isset($argv['tableborder']) ? $argv['tableborder'] : 1;
	$argv['tablecellspacing'] = isset($argv['tablecellspacing']) ? $argv['tablecellspacing'] : 0;
	$argv['tablecellpadding'] = isset($argv['tablecellpadding']) ? $argv['tablecellpadding'] : 1;

	$argv['dayunderline'] = isset($argv['dayunderline']) ? $argv['dayunderline'] : true;

/*
******************************************************
Perform Value Checks
******************************************************
*/
	// Check If Value Passed Is Numeric
	if(!is_numeric($argv['display'])) { $retval = "<B>Display</B> is Non-Numeric. Please double-check value.<br><br>display = <I>".$argv['display']."</I>"; $proceed = false; }
	elseif($argv['display']<=0) { $retval = "<B>Display</B> is set below 0. Please double-check value.<br><br>display = <I>".$argv['display']."</I>"; $proceed = false; }
	if(!is_numeric($argv['monthskip'])) { $retval = "<B>Month Skip</B> is Non-Numeric. Please double-check value.<br><br>monthskip = <I>".$argv['monthskip']."</I>"; $proceed = false; }
	elseif($argv['monthskip']<=0) { $retval = "<B>Month Skip</B> is set below 0. Please double-check value.<br><br>monthskip = <I>".$argv['monthskip']."</I>"; $proceed = false; }
	if(!is_numeric($argv['startday'])) { $retval = "<B>Start Day</B> is Non-Numeric. Please double-check value.<br><br>startday = <I>".$argv['startday']."</I>"; $proceed = false; }
	elseif($argv['startday']<0) { $retval = "<B>Start Day</B> is is below 0. Please double-check value.<br><br>startday = <I>".$argv['startday']."</I>"; $proceed = false; }
	if(!is_numeric($argv['year'])) { $retval = "<B>Year</B> is Non-Numeric. Please double-check value.<br><br>year = <I>".$argv['year']."</I>"; $proceed = false; }
	elseif($argv['year']<=0) { $retval = "<B>Year</B> is is below 0. Please double-check value.<br><br>year = <I>".$argv['year']."</I>"; $proceed = false; }
	if(!is_numeric($argv['month'])) { $retval = "<B>Month</B> is Non-Numeric. Please double-check value.<br><br>month = <I>".$argv['month']."</I>"; $proceed = false; }
	elseif($argv['month']==0) { $retval = "<B>Month</B> is set to 0. Please double-check value.<br><br>month = <I>".$argv['month']."</I>"; $proceed = false; }
	elseif($argv['month']<0) {
		// If specified month is in negative, substract from current month and selected year.
		$argv['month'] = date('m',mktime(0,0,0,date('m')+$argv['month'],1,$argv['year']));
	}
	if($proceed) {
		$data = MWCalendar_ConvertData($input);
		$tmpYear = $argv['year'];
		$retval .= "<TABLE CELLSPACING=\"".$argv['tablecellspacing']."\" CELLPADDING=\"".$argv['tablecellpadding']."\" BORDER=\"".$argv['tableborder']."\"".((strlen($argv['classtable'])>0) ? " CLASS=\"".$argv['classtable']."\"" : "")."".((strlen($argv['styletable'])>0) ? " STYLE=\"".$argv['styletable']."\"" : "").">";
		for($x=1; $x <= $argv['display'] ; $x++) {

			// Create Calendar and Assign Default Values
			$cal = new MWCalendar;
			foreach($data as $value) $cal->setIncludeData($value['day'],$value['month'],$value['year'],$value['data']);
			$cal->setHTMLClasses($argv['classtable'],$argv['classtablerow'],$argv['classtablecell'],$argv['classtablecelltoday'],$argv['classtabledow'],$argv['classtableyear'],$argv['classtablemonth']);
			$cal->setHTMLStyles($argv['styletable'],$argv['styletablerow'],$argv['styletablecell'],$argv['styletablecelltoday'],$argv['styletabledow'],$argv['styletableyear'],$argv['styletablemonth']);
			$cal->setDayNames(explode(",",$argv['daysofweek']));
			$cal->setDayUnderline($argv['dayunderline']);
			$cal->setMonthNames(explode(",",$argv['monthnames']));
			$cal->setStartDay($argv['startday']);
			$cal->showMonth = $argv['titlemonth'];
			if($x!=1 || $argv['titleyear']) $cal->showYear = false;
			if($tmpYear!=$argv['year'] && $argv['titleyear']) { $cal->showYear = true; $tmpYear = $argv['year']; }

			// Draw Calendar
			$retval .= $cal->getMonthHTML($argv['month'],$argv['year']);

			// Generate Next Month Values
			$newDate = mktime(0,0,0,$argv['month']+$argv['monthskip'],1,$argv['year']);
			$argv['month'] = date('m',$newDate);
			$argv['year'] = date('Y',$newDate);

			unset($newDate);
			unset($cal);
		}
		$retval .= "<tr ".$argv['classtablerow']." ".$argv['styletablerow'].">\n".
			"<td align='center' valign='top' colspan='7'><A HREF='http://en.wikipedia.org/wiki/User:Eric_Bouliane/MWCalendar'>MWCalendar</A></td>\n".
			"</tr>\n";
		$retval .= "</TABLE>";
	}

	return $retval;
}

function MWCalendar_ConvertData($input) {
// $day,$month,$year,$data
	settype($retval,"Array");
	// Convert Each New Line Into Array
	$lines = explode("\n",$input);
	foreach($lines as $line) {
		$range = false; // Range Variable
		$line = ltrim($line); // Trim Whitespace
		// Verify If First Character Is Numeric
		if( is_numeric( $line[0] )) {
			$data = explode(":::",$line); //Explode Line
			foreach($data as $key=>$value) $data[$key] = trim($value); //Trim WhiteSpace
			if(is_numeric(substr($data[1],0,1)) && isset($data[2])) $range = true;

			//Format Date Variables
			$dataInterval = strtoupper( trim( substr($data[0],11) ) );
			$data[0] = date('Y-m-d',strtotime(substr($data[0],0,10)));
			$data[1] = (sizeof($data)==3) ? date('Y-m-d',strtotime($data[1])) : $data[1];

			// Set Temporary Date Variables
			$date1 = strtotime($data[0]); //Set Date 1
			$date2 = ($range) ? strtotime($data[1]) : strtotime($data[0]); //Set Date 2 with Range If Not Default Date 1
			$calData = ($range) ? $data[2] : $data[1];

			//Loop Date Range
			$interval=0;
			for( $x=$date1 ; $x<=$date2 ; $x=mktime(0,0,0,date('m',$date1),date('d',$date1)+$interval,date('Y',$date1)) ) {
				$retval[] = Array("day"=>date('d',$x),"month"=>date('m',$x),"year"=>date('Y',$x),"data"=>$calData);
				$interval = MWCalendar_SetInterval($interval,$dataInterval);

			}
		}
		// Verify If First Character Is % (To Ommit Range)
		elseif( $line[0] == "%" ) {
			$data = explode(":::",$line);
			foreach($data as $key=>$value) $data[$key]=trim($value); //Trim WhiteSpace
			if(sizeof($data)==2) {
				$data[0] = date('Y-m-d',strtotime(substr($data[0],1))); //Format Date Variables
				foreach($retval as $key=>$value) {
					if($value['year']."-".$value['month']."-".$value['day'] == $data[0] && $value['data']==$data[1])
						unset($retval[$key]);
				}
			}
		}
		unset($range);
	}
	return $retval;
}

function MWCalendar_SetInterval($current, $dataInterval) {
	$newInterval = $current + 1;

	$id = substr($dataInterval,0,1);
	$modifier = substr($dataInterval,-1,1);
	$count = substr($dataInterval, 2, -1);

	if( $id == "E" ) {
		switch( $modifier ) {
			case "M": if(is_numeric($count)) $newInterval = $current + 30 * $count; break;
			case "W": if(is_numeric($count)) $newInterval = $current +  7 * $count; break;
			case "D": if(is_numeric($count)) $newInterval = $current +  1 * $count; break;
		}
	}

	return $newInterval;
}

class MWCalendar {
	#####################################
	# Public Methods
	#####################################
	public function setDayNames($names) { $this->dayNames = $names; }
	public function setHTMLClasses($table="", $table_row="", $table_cell="", $table_cell_today="", $table_dow="", $table_year="", $table_month="") {
		$this->class_table = (strlen($table)>0) ? "class=\"".$table."\"" : "";
		$this->class_table_row = (strlen($table_row)>0) ? "class=\"".$table_row."\"" : "";
		$this->class_table_cell = (strlen($table_cell)>0) ? "class=\"".$table_cell."\"" : "";
		$this->class_table_cell_today = (strlen($table_cell_today)>0) ? "class=\"".$table_cell_today."\"" : "";
		$this->class_table_dow = (strlen($table_dow)>0) ? "class=\"".$table_dow."\"" : "";
		$this->class_table_month = (strlen($table_month)>0) ? "class=\"".$table_month."\"" : "";
		$this->class_table_year = (strlen($table_year)>0) ? "class=\"".$table_year."\"" : "";
	}
	public function setHTMLStyles($table="", $table_row="", $table_cell="", $table_cell_today="", $table_dow="", $table_year="", $table_month="") {
		$this->style_table = (strlen($table)>0) ? "style=\"".$table."\"" : "";
		$this->style_table_row = (strlen($table_row)>0) ? "style=\"".$table_row."\"" : "";
		$this->style_table_cell = (strlen($table_cell)>0) ? "style=\"".$table_cell."\"" : "";
		$this->style_table_cell_today = (strlen($table_cell_today)>0) ? "style=\"".$table_cell_today."\"" : "";
		$this->style_table_dow = (strlen($table_dow)>0) ? "style=\"".$table_dow."\"" : "";
		$this->style_table_month = (strlen($table_month)>0) ? "style=\"".$table_month."\"" : "";
		$this->style_table_year = (strlen($table_year)>0) ? "style=\"".$table_year."\"" : "";
	}
	public function setDayUnderline($value) { $this->dayUnderline = ($value==1) ? true : false; }
	public function setMonthNames($names) { $this->monthNames = $names; }
	public function setStartDay($day) { $this->startDay = $day; }
	public function setStartMonth($month) { $this->startMonth=$month; }
	public function setIncludeData($day,$month,$year,$data) {
		$this->includeData[$this->includeDataCount]['day'] = $day;
		$this->includeData[$this->includeDataCount]['month'] = $month;
		$this->includeData[$this->includeDataCount]['year'] = $year;
		$this->includeData[$this->includeDataCount]['data'] = $data;
		$this->includeDataCount++;
	}

	#####################################
	# Private Methods
	#####################################
	private function getDayNames() { return $this->dayNames; }
	private function getMonthNames() { return $this->monthNames; }
	private function getStartDay() { return $this->startDay; }
	private function getStartMonth() { return $this->startMonth; }
	private function getCurrentMonthView() { $d = getdate(time()); return $this->getMonthView($d["mon"], $d["year"]); }
	private function getMonthView($month, $year) { return $this->getMonthHTML($month, $year); }
	public function Calendar() {
		$this->setStartMonth(date("m"));
	}

	private function getDaysInMonth($month, $year) {
		if ($month < 1 || $month > 12) return 0;
		$d = $this->daysInMonth[$month - 1];
		if ($month == 2) $d = checkdate($month, 29, $year) ? 29 : 28;
		return $d;
	}

	private function adjustDate($month, $year) {
		$a = array();  
		$a[0] = $month;
		$a[1] = $year;
		while ($a[0] > 12) {
			$a[0] -= 12;
			$a[1]++;
		}
		while ($a[0] <= 0) {
			$a[0] += 12;
			$a[1]--;
		}
		return $a;
	}

	public function getMonthHTML($m, $y) {
		$s = "";
		$a = $this->adjustDate($m, $y);
		$month = $a[0];
		$year = $a[1];
		$daysInMonth = $this->getDaysInMonth($month, $year);
		$date = getdate(mktime(12, 0, 0, $month, 1, $year));
		$first = $date["wday"];
		$monthName = $this->monthNames[$month - 1];

#		$s .= "<table ".$this->class_table." ".$this->style_table.">\n";

		if($this->showYear)
			$s .= "<tr ".$this->class_table_row." ".$this->style_table_row.">\n".
				"<th align='center' valign='top' ".$this->class_table_year." ".$this->style_table_year." colspan='7'>".$year."</td>\n".
				"</tr>\n";

		if($this->showMonth)
			$s .= "<tr ".$this->class_table_row." ".$this->style_table_row.">\n".
				"<th align='center' valign='top' ".$this->class_table_month." ".$this->style_table_month." colspan='7'>".$monthName."</td>\n".
				"</tr>\n";

		if($this->showDaysOfWeek)
			$s .= "<tr ".$this->class_table_row." ".$this->style_table_row.">\n".
				"<td align='center' valign='top' ".$this->class_table_dow." ".$this->style_table_dow.">".$this->dayNames[($this->startDay)%7]."</td>\n".
				"<td align='center' valign='top' ".$this->class_table_dow." ".$this->style_table_dow.">".$this->dayNames[($this->startDay+1)%7]."</td>\n".
				"<td align='center' valign='top' ".$this->class_table_dow." ".$this->style_table_dow.">".$this->dayNames[($this->startDay+2)%7]."</td>\n".
				"<td align='center' valign='top' ".$this->class_table_dow." ".$this->style_table_dow.">".$this->dayNames[($this->startDay+3)%7]."</td>\n".
				"<td align='center' valign='top' ".$this->class_table_dow." ".$this->style_table_dow.">".$this->dayNames[($this->startDay+4)%7]."</td>\n".
				"<td align='center' valign='top' ".$this->class_table_dow." ".$this->style_table_dow.">".$this->dayNames[($this->startDay+5)%7]."</td>\n".
				"<td align='center' valign='top' ".$this->class_table_dow." ".$this->style_table_dow.">".$this->dayNames[($this->startDay+6)%7]."</td>\n".
				"</tr>\n";

		// We need to work out what date to start at so that the first appears in the correct column
		$d = $this->startDay + 1 - $first;
		while ($d > 1) { $d -= 7; }

		// Make sure we know when today is, so that we can use a different CSS style
		$today = getdate(time());

		while ($d <= $daysInMonth) {
			$s .= "<tr ".$this->class_table_row." ".$this->style_table_row.">\n";
			for ($i = 0; $i < 7; $i++) {
				$class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? $this->class_table_cell_today : $this->class_table_cell;
				$style = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? $this->style_table_cell_today : $this->style_table_cell;
				$s .= "<td ".$class." ".$style." valign='top'>";		 
				if ($d > 0 && $d <= $daysInMonth) {
					$s .= "<DIV ALIGN='right'>".(($this->dayUnderline) ? "<U>" : "").$d.(($this->dayUnderline) ? "</U>" : "")."</DIV>";
					if(sizeof($this->includeData)>0) {
						foreach($this->includeData as $dataValue) {
							if($d==$dataValue['day'] && $month==$dataValue['month'] && $year==$dataValue['year']) $s .= "<DIV>".$dataValue['data']."</DIV>";
						}
					}
				} else {
					$s .= "";
				}
				$s .= "</td>\n";
				$d++;
			}
			$s .= "</tr>\n";	 
		}
#		$s .= "</table>\n";
		return $s;  	
	}

	var $startDay = 0;
	var $startMonth;
	var $dayNames = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var $monthNames = array("January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December");
	var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	var $showYear = true;
	var $showMonth = true;
	var $showDaysOfWeek = true;
	var $includeData = Array();
	var $includeDataCount = 0;
	var $class_table, $class_table_row, $class_table_cell, $class_table_dow, $class_table_month, $class_table_year, $class_table_cell_today;
	var $style_table, $style_table_row, $style_table_cell, $style_table_dow, $style_table_month, $style_table_year, $style_table_cell_today;
	var $dayUnderline = true;

}

$wgExtensionCredits['parserhook'][] = array(
		'name' => 'MWCalendar',
		'version' => '1.0.7',
		'author' => 'Eric Bouliane (ebouliane@gmail.com)',
		'url' => 'http://www.cosil.ca',
		'description' => 'Mediawiki Calendar Extension'
);

?>